Skip to content

Commit

Permalink
Addded: Get user from token for introspect
Browse files Browse the repository at this point in the history
  • Loading branch information
PrasadChinwal committed Sep 12, 2023
1 parent 71938f8 commit 63bcd01
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 47 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,27 @@ Under `alias` property:

Now you can use the middleware on your protected route as such:
```php
Route::middleware(['introspect'])->get('/introspect', 'Controller@index')
->name('introspect');
use UisIts\Oidc\Http\Middleware\Introspect;

Route::middleware(['introspect'])->get('/introspect', function (Request $request) {
dump($request->bearerToken());
dd(Introspect::getUserFromToken($request->bearerToken()));
})->name('introspect');
```
Note: Getting the user's netid from token

You can get the user's netid associated with token by:
Note: Below is the response received when you get a user from token
```php
Session::get('introspect.username');
Introspect::getUserFromToken($request->bearerToken());

array:8 [▼ // routes/api.php:24
"sub" => "xyz@abc.org"
"uisedu_is_member_of" => array:42 [▶]
"uisedu_uin" => "123456789"
"preferred_username" => "xyz"
"given_name" => "John"
"preferred_display_name" => "Doe, John"
"family_name" => "Doe"
"email" => "xyz@abc.org"
];
```

#### Code Style
Expand Down
45 changes: 5 additions & 40 deletions src/Http/Middleware/Introspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Laravel\Socialite\Facades\Socialite;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -27,10 +26,6 @@ public function handle(Request $request, Closure $next, ...$scopes): Response
return new JsonResponse(['message' => 'Token not set!'], 401);
}

if ($this->checkCache($request->bearerToken())) {
return $next($request);
}

$introspectResponse = Socialite::driver('shib-oidc')
->introspect($request->bearerToken());

Expand All @@ -42,44 +37,9 @@ public function handle(Request $request, Closure $next, ...$scopes): Response
$this->checkScopes($introspectResponse['scope'], $scopes);
}

Cache::put('introspect.username', $introspectResponse['username']);
Cache::put('introspect', encrypt($request->bearerToken()));

return $next($request);
}

/**
* Check if the token is already authorized
*/
protected function checkCache($token): bool
{
// If token not in cache return
if (! Cache::has('introspect')) {
return false;
}

if ($this->isCachedTokenValid($token)) {
return true;
}

return false;
}

/**
* Check if cached token is valid
*/
protected function isCachedTokenValid(string $token): bool
{
$cachedToken = decrypt(Cache::get('introspect'));

// If token valid return
if ($cachedToken === $token) {
return true;
}

return false;
}

/**
* Check the scopes of the token
*
Expand All @@ -95,4 +55,9 @@ public function checkScopes(string $newScopes, string|array $oldScopes): void
throw new \InvalidArgumentException("Missing scopes {$missingScopes->implode(',')}");
}
}

public static function getUserFromToken(string $bearerToken): array
{
return Socialite::driver('shib-oidc')->getUserByToken($bearerToken);
}
}
2 changes: 1 addition & 1 deletion src/Oidc/ShibbolethOidcProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function getIntrospectUrl(): string
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
public function getUserByToken($token)
{
$response = $this->getHttpClient()->get($this->getUserUrl(), [
RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token],
Expand Down

0 comments on commit 63bcd01

Please sign in to comment.