Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced hard coded user model reference with UserProvider #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/Http/Controllers/IntrospectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ipunkt\Laravel\OAuthIntrospection\Http\Controllers;

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Http\JsonResponse;
use Laravel\Passport\Bridge\AccessTokenRepository;
use Laravel\Passport\ClientRepository;
Expand Down Expand Up @@ -37,25 +38,38 @@ class IntrospectionController
*/
private $clientRepository;

/**
* @var Illuminate\Contracts\Auth\UserProvider
*/
private $userProvider;

/**
* @var string
*/
protected $usernameProperty = 'email';

/**
* constructing IntrospectionController
*
* @param \Lcobucci\JWT\Parser $jwt
* @param \League\OAuth2\Server\ResourceServer $resourceServer
* @param \Laravel\Passport\Bridge\AccessTokenRepository $accessTokenRepository
* @param \Laravel\Passport\ClientRepository
* @param \Illuminate\Contracts\Auth\UserProvider $userProvider
*/
public function __construct(
Parser $jwt,
ResourceServer $resourceServer,
AccessTokenRepository $accessTokenRepository,
ClientRepository $clientRepository
ClientRepository $clientRepository,
UserProvider $userProvider
)
{
$this->jwt = $jwt;
$this->resourceServer = $resourceServer;
$this->accessTokenRepository = $accessTokenRepository;
$this->clientRepository = $clientRepository;
$this->userProvider = $userProvider;
}

/**
Expand Down Expand Up @@ -89,15 +103,17 @@ public function introspectToken(ServerRequestInterface $request)
]);
}

/** @var string $userModel */
$userModel = config('auth.providers.users.model');
$user = (new $userModel)->findOrFail($token->getClaim('sub'));
# get user by token subject ID, from the UserProvider
$user = $this->userProvider->retrieveById($token->getClaim('sub'));
if( is_null($user) ) {
return $this->notActiveResponse();
}

return $this->jsonResponse([
'active' => true,
'scope' => trim(implode(' ', (array)$token->getClaim('scopes', []))),
'client_id' => intval($token->getClaim('aud')),
'username' => $user->email,
'username' => $user->{$this->usernameProperty} ?? null,
'token_type' => 'access_token',
'exp' => intval($token->getClaim('exp')),
'iat' => intval($token->getClaim('iat')),
Expand Down
19 changes: 19 additions & 0 deletions src/Providers/OAuthIntrospectionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@

namespace Ipunkt\Laravel\OAuthIntrospection\Providers;

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Support\AggregateServiceProvider;
use Illuminate\Support\Facades\Auth;
use Ipunkt\Laravel\OAuthIntrospection\Http\Controllers\IntrospectionController;

class OAuthIntrospectionServiceProvider extends AggregateServiceProvider
{
protected $providers = [
RouteProvider::class,
];

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::register();

$this->app->when(IntrospectionController::class)
->needs(UserProvider::class)
->give(function(){
return Auth::createUserProvider();
});
}
}
2 changes: 1 addition & 1 deletion src/Providers/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RouteProvider extends RouteServiceProvider
{
protected $packagePath = __DIR__ . '/../../';

protected $routesNamespace = '\Ipunkt\Laravel\OAuthIntrospection\Http\Controllers';
protected $routesNamespace = 'Ipunkt\Laravel\OAuthIntrospection\Http\Controllers';

protected $routesMiddleware = null;

Expand Down