Skip to content

Instantly share code, notes, and snippets.

@antonigiske
Last active July 18, 2019 05:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonigiske/1baf1523ea7802a82404 to your computer and use it in GitHub Desktop.
Save antonigiske/1baf1523ea7802a82404 to your computer and use it in GitHub Desktop.
Example authentication driver for Laravel 4.
<?php namespace Project\Providers;
use Illuminate\Auth\UserProviderInterface;
use Illuminate\Auth\GenericUser;
class AuthUserProvider implements UserProviderInterface {
/**
* External webservice for authentication
*/
private $webservice;
/**
* The user object.
*/
private $user;
/**
* Constructor
*
* @return void
*/
public function __construct(\Project\Webservice\Auth $webservice)
{
$this->webservice = $webservice;
$this->user = null;
}
/**
* Retrieves a user by id
*
* @param int $identifier
* @return mixed null|array
*/
public function retrieveByID($identifier)
{
$this->user = is_null($this->user) ? $this->webservice->find($identifier) : $this->user;
return $this->user;
}
/**
* Tries to find a user based on the credentials passed.
*
* @param array $crendtials username|password
* @return mixed bool|UserInterface
*/
public function retrieveByCredentials(array $credentials)
{
if(!$user = $this->webservice->find($credentials['username'])) return false;
return new GenericUser($user);
}
/**
* Validates the credentials passed to the ones in webservice.
*
* @param UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
return $this->webservice->validateCredentials($credentials['password'], $user->password);
}
}
@helmut
Copy link

helmut commented Dec 10, 2013

Should be $this->webservice in last method...

@antonigiske
Copy link
Author

@helmut: You are absolutely correct. Thanks, fixed it now.

@fowlerwill
Copy link

Thanks for the code!
Just a late edit - retrieveByCredentials should return null instead of false on line 53

@atorscho
Copy link

Thanks for you tutorial, but I still don't get what is your $webservice variable. What does it mean? Can you show an example of it, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment