Advertisement
ambitiousnobody

Laravel external login custom implementation

Dec 1st, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | Source Code | 0 0
  1. <?php
  2. /*
  3. custom implementation of users where users login on different site,
  4. however, I do have access to the DB.
  5. Stores user token in session instead of DB.
  6. Not sure if this will work.
  7. */
  8.  
  9. //===========================================
  10. //  In CustomUserProvider.php
  11. //===========================================
  12.  
  13. use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;
  14. use Illuminate\Contracts\Auth\Authenticatable as Authenticatable;
  15.  
  16. use Illuminate\Support\Facades\DB;
  17.  
  18.  
  19. class CustomUserProvider implements IlluminateUserProvider
  20. {
  21.     /**
  22.      * @param  mixed  $identifier
  23.      * @return \Illuminate\Contracts\Auth\Authenticatable|null
  24.      */
  25.     public function retrieveById($identifier)
  26.     {
  27.        // Get and return a user by their unique identifier
  28.          return new CustomUser($identifier);
  29.     }
  30.  
  31.     /**
  32.      * @param  mixed   $identifier
  33.      * @param  string  $token
  34.      * @return \Illuminate\Contracts\Auth\Authenticatable|null
  35.      */
  36.     public function retrieveByToken($identifier, $token)
  37.     {
  38.        // Get and return a user by their unique identifier and "remember me" token
  39.  
  40.        if (session('custom_user_token_'.$user->acct_id) == $token)
  41.        {
  42.                 return new CustomUser($identifier);
  43.        }
  44.  
  45.      else
  46.        {
  47.              return null;
  48.        }
  49.     }
  50.  
  51.     /**
  52.      * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
  53.      * @param  string  $token
  54.      * @return void
  55.      */
  56.     function  updateRememberToken(Authenticatable $user, $token)
  57.     {
  58.         // Save the given "remember me" token for the given user
  59.          session(['custom_user_token_'.$user->acct_id => $token]);
  60.     }
  61.  
  62.     /**
  63.      * Retrieve a user by the given credentials.
  64.      *
  65.      * @param  array  $credentials
  66.      * @return \Illuminate\Contracts\Auth\Authenticatable|null
  67.      */
  68.     public function retrieveByCredentials(array $credentials)
  69.     {
  70.        // Get and return a user by looking up the given credentials
  71.  
  72.          // Return null, since user should be logging in using WAYF
  73.        return null;
  74.     }
  75.  
  76.     /**
  77.      * Validate a user against the given credentials.
  78.      *
  79.      * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
  80.      * @param  array  $credentials
  81.      * @return bool
  82.      */
  83.     public function validateCredentials(Authenticatable $user, array $credentials)
  84.     {
  85.        // Check that given credentials belong to the given user
  86.          // Return false, since user should be logging in using WAYF
  87.          return false;
  88.     }
  89.  
  90.     public function isDeferred()
  91.     {
  92.       return false;
  93.     }
  94. }
  95.  
  96. //===========================================
  97. // in CustomUser.php
  98. //===========================================
  99.  
  100.  
  101. use Illuminate\Contracts\Auth\Authenticatable;
  102.  
  103. use Illuminate\Support\Facades\DB;
  104.  
  105.  
  106. class CustomUser
  107.     implements Authenticatable
  108. {
  109.  
  110.     /*
  111.       * Construct with these fields:
  112.       * acct_id
  113.       */
  114.  
  115.     function __construct($acct_id)
  116.     {
  117.         // user should be created by ciac object, don't have to create new users here
  118.  
  119.         $results = DB::select("SELECT acct_id, username, email FROM my_user_table WHERE acct_id = :acct_id",[':acct_id' =>$acct_id]);
  120.         $userRow = (array) reset($results);
  121.  
  122.         if ($userRow == null)
  123.         {
  124.             throw new Exception("Can't find user with id {$acct_id}");
  125.         }
  126.  
  127.         foreach ($userRow as $property => $value)
  128.         {
  129.             $this->$property = $value;
  130.         }
  131.  
  132.     }
  133.  
  134.  
  135.  
  136.     /**
  137.      * @return string
  138.      */
  139.     public function getAuthIdentifierName()
  140.     {
  141.         // Return the name of unique identifier for the user (e.g. "id")
  142.          return 'acct_id';
  143.     }
  144.  
  145.     /**
  146.      * @return mixed
  147.      */
  148.     public function getAuthIdentifier()
  149.     {
  150.         // Return the unique identifier for the user (e.g. their ID, 123)
  151.          return $this->acct_id;
  152.     }
  153.  
  154.     /**
  155.      * @return string
  156.      */
  157.     public function getAuthPassword()
  158.     {
  159.        // Returns the (hashed) password for the user
  160.          // Laravel doesn't need to know the password (?)
  161.          return null;
  162.     }
  163.  
  164.     /**
  165.      * @return string
  166.      */
  167.     public function getRememberToken()
  168.     {
  169.        // Return the token used for the "remember me" functionality
  170.          // no need to store in DB, is only relevant for this session
  171.          return session('custom_user_token_obj_'.$this->acct_id);
  172.     }
  173.  
  174.     /**
  175.      * @param  string  $value
  176.      * @return void
  177.      */
  178.     public function setRememberToken($value)
  179.     {
  180.        // Store a new token user for the "remember me" functionality
  181.          // no need to store in DB, is only relevant for this session
  182.          session(['custom_user_token_obj_'.$this->acct_id => $value]);
  183.     }
  184.  
  185.     /**
  186.      * @return string
  187.      */
  188.     public function getRememberTokenName()
  189.     {
  190.         // Return the name of the column / attribute used to store the "remember me" token
  191.          return 'token';
  192.     }
  193. }
  194.  
  195.  
Tags: laravel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement