Skip to content

Removed opinionated exception handling from controller. #11

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
108 changes: 36 additions & 72 deletions src/Http/Controllers/IntrospectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,48 +69,42 @@ public function introspectToken(ServerRequestInterface $request)
{
try {
$this->resourceServer->validateAuthenticatedRequest($request);

if (array_get($request->getParsedBody(), 'token_type_hint', 'access_token') !== 'access_token') {
// unsupported introspection
return $this->notActiveResponse();
}

$accessToken = array_get($request->getParsedBody(), 'token');
if ($accessToken === null) {
return $this->notActiveResponse();
}

$token = $this->jwt->parse($accessToken);
if (!$this->verifyToken($token)) {
return $this->errorResponse([
'error' => [
'title' => 'Token invalid'
]
]);
}

/** @var string $userModel */
$userModel = config('auth.providers.users.model');
$user = (new $userModel)->findOrFail($token->getClaim('sub'));

return $this->jsonResponse([
'active' => true,
'scope' => trim(implode(' ', (array)$token->getClaim('scopes', []))),
'client_id' => intval($token->getClaim('aud')),
'username' => $user->email,
'token_type' => 'access_token',
'exp' => intval($token->getClaim('exp')),
'iat' => intval($token->getClaim('iat')),
'nbf' => intval($token->getClaim('nbf')),
'sub' => intval($token->getClaim('sub')),
'aud' => intval($token->getClaim('aud')),
'jti' => $token->getClaim('jti'),
]);
} catch (OAuthServerException $oAuthServerException) {
return $oAuthServerException->generateHttpResponse(new Psr7Response);
} catch (\Exception $exception) {
return $this->exceptionResponse($exception);
}
} catch (OAuthServerException $oAuthServerException) {
return $oAuthServerException->generateHttpResponse(new Psr7Response);
}

if (array_get($request->getParsedBody(), 'token_type_hint', 'access_token') !== 'access_token') {
// unsupported introspection
return $this->notActiveResponse();
}

$accessToken = array_get($request->getParsedBody(), 'token');
if ($accessToken === null) {
return $this->notActiveResponse();
}

$token = $this->jwt->parse($accessToken);
if (!$this->verifyToken($token)) {
return $this->notActiveResponse();
}

/** @var string $userModel */
$userModel = config('auth.providers.users.model');
$user = (new $userModel)->findOrFail($token->getClaim('sub'));

return $this->jsonResponse([
'active' => true,
'scope' => trim(implode(' ', (array)$token->getClaim('scopes', []))),
'client_id' => intval($token->getClaim('aud')),
'username' => $user->email,
'token_type' => 'access_token',
'exp' => intval($token->getClaim('exp')),
'iat' => intval($token->getClaim('iat')),
'nbf' => intval($token->getClaim('nbf')),
'sub' => intval($token->getClaim('sub')),
'aud' => intval($token->getClaim('aud')),
'jti' => $token->getClaim('jti'),
]);
}

/**
Expand Down Expand Up @@ -167,34 +161,4 @@ private function verifyToken(Token $token) : bool
return false;
}

/**
* @param array $data
* @param int $status
*
* @return \Illuminate\Http\JsonResponse
*/
private function errorResponse($data, $status = 400) : JsonResponse
{
return $this->jsonResponse($data, $status);
}

/**
* returns an error
*
* @param \Exception $exception
* @param int $status
*
* @return \Illuminate\Http\JsonResponse
*/
private function exceptionResponse(\Exception $exception, $status = 500) : JsonResponse
{
return $this->errorResponse([
'error' => [
'id' => str_slug(get_class($exception) . ' ' . $status),
'status' => $status,
'title' => $exception->getMessage(),
'detail' => $exception->getTraceAsString()
],
], $status);
}
}