Giter VIP home page Giter VIP logo

Comments (3)

taiyeoguns avatar taiyeoguns commented on June 24, 2024 9

In case anyone needs a sample, I created a project with Laravel 5.4:

https://github.com/taiyeoguns/laravel-saml-sp-demo

and there's a demo using SSOCircle as Idp:

https://laravel-saml-sp-demo.herokuapp.com/

from laravel-saml2.

m1kl avatar m1kl commented on June 24, 2024 1

You will need to listen to the Saml2LoginEvent. From this event you have access to the Saml2User from which you can retrieve the user attributes (claims).

Here is an example of an event listener:

class Saml2LoginListener
{
    const CLAIM_UPN = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn";
    const CLAIM_EMAIL_ADDRESS = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress";
    const CLAIM_FIRST_NAME = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname";
    const CLAIM_LAST_NAME = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname";
    const CLAIM_FULL_NAME = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name";
    const CLAIM_ROLE = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    private function getClaimOrDefault($attributes, $claimKey, $default = 'N/A')
    {
        if (array_key_exists($claimKey, $attributes)) {
            return $attributes[$claimKey][0];
        }
        return $default;
    }

    /**
     * Handle the event.
     *
     * @param  PodcastWasPurchased  $event
     * @return void
     */
    public function handle(Saml2LoginEvent $event)
    {
        $user = $event->getSaml2User();
        $attributes = $user->getAttributes();
        $profile = array(
          'id' => $user->getUserId(),
          'upn' => $this->getClaimOrDefault($attributes, self::CLAIM_UPN),
          'email' => $this->getClaimOrDefault($attributes, self::CLAIM_EMAIL_ADDRESS),
          'full_name' => $this->getClaimOrDefault($attributes, self::CLAIM_FULL_NAME),
          'first_name' => $this->getClaimOrDefault($attributes, self::CLAIM_FIRST_NAME),
          'last_name' => $this->getClaimOrDefault($attributes, self::CLAIM_LAST_NAME),
          'roles' => $this->getClaimOrDefault($attributes, self::CLAIM_ROLE)
        );

        // In case you want to persist user info in database
        $laravelUser = User::find($user->getUserId());
        if (isset($laravelUser)) {
            $laravelUser->fill($profile);
        } else {
            $laravelUser = new User($profile);
        }
        $laravelUser->save();

        // Here we save the received nameId and sessionIndex needed later for the LogoutRequest
        Session::put('name_id', $user->getNameId());
        Session::put('session_index', $user->getSessionIndex());

        Auth::login($laravelUser);
    }
}

You will then be able to access the laravel user from your controllers with $request->user().

from laravel-saml2.

hemorej avatar hemorej commented on June 24, 2024

After I do Auth::login($laravelUser) my session driver creates an entry with no user_id in the database, but neither Auth::user() nor $request->user() are available elsewhere from controller actions. Am I missing something ?

from laravel-saml2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.