Skip to content

Commit

Permalink
Handling exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Robson Tenório authored and Robson Tenório committed Jul 22, 2018
1 parent e8a849c commit 3a870aa
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Appends to the authenticated user the full decoded JWT token. Useful if you need

*Required*

Comma separated list of allowed resources accepted by API. This attribute will be confronted against `resource_access` attribute from JWT token, while authenticating.
Usually you API should handle one *resource_access*. But, if you handle multiples, just use a comma separated list of allowed resources accepted by API. This attribute will be confronted against `resource_access` attribute from JWT token, while authenticating.

## Laravel auth config

Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/KeycloakGuardException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace KeycloakGuard\Exceptions;

class KeycloakGuardException extends \UnexpectedValueException
{
public function __construct(string $message)
{
$this->message = "[Keycloack Guard] {$message}";
}
}
7 changes: 7 additions & 0 deletions src/Exceptions/ResourceAccessNotAllowedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace KeycloakGuard\Exceptions;

class ResourceAccessNotAllowedException extends KeycloakGuardException
{

}
7 changes: 7 additions & 0 deletions src/Exceptions/TokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace KeycloakGuard\Exceptions;

class TokenException extends KeycloakGuardException
{

}
7 changes: 7 additions & 0 deletions src/Exceptions/UserNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace KeycloakGuard\Exceptions;

class UserNotFoundException extends KeycloakGuardException
{

}
30 changes: 28 additions & 2 deletions src/KeycloakGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Http\Request;
use KeycloakGuard\Exceptions\TokenException;
use KeycloakGuard\Exceptions\UserNotFoundException;
use KeycloakGuard\Exceptions\ResourceAccessNotAllowedException;

class KeycloakGuard implements Guard
{
Expand All @@ -18,7 +21,25 @@ public function __construct(UserProvider $provider, Request $request)
$this->config = config('keycloak');
$this->user = null;
$this->provider = $provider;
$this->decodedToken = Token::decode($request->bearerToken(), $this->config['realm_public_key']);
$this->decodedToken = null;
$this->request = $request;

$this->authenticate();
}

/**
* Decode token, validate and authenticate user
*
* @return mixed
*/

private function authenticate()
{
try {
$this->decodedToken = Token::decode($this->request->bearerToken(), $this->config['realm_public_key']);
} catch (\Exception $e) {
throw new TokenException($e->getMessage());
}

if ($this->decodedToken) {
$this->validate([
Expand All @@ -27,6 +48,7 @@ public function __construct(UserProvider $provider, Request $request)
}
}


/**
* Determine if the current user is authenticated.
*
Expand Down Expand Up @@ -93,6 +115,10 @@ public function validate(array $credentials = [])

$user = $this->provider->retrieveByCredentials($credentials);

if (!$user) {
throw new UserNotFoundException("User not found. Credentials: " . json_encode($credentials));
}

$this->setUser($user);

return true;
Expand Down Expand Up @@ -122,7 +148,7 @@ private function validateResources()
$allowed_resources = explode(',', $this->config['allowed_resources']);

if (count(array_intersect($token_resource_access, $allowed_resources)) == 0) {
throw new ResourceNotAllowedException("The decoded JWT token has not a valid resource_access allowed by API. Allowed resources by API: " . $this->config['allowed_resources']);
throw new ResourceAccessNotAllowedException("The decoded JWT token has not a valid `resource_access` allowed by API. Allowed resources by API: " . $this->config['allowed_resources']);
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/ResourceNotAllowedException.php

This file was deleted.

0 comments on commit 3a870aa

Please sign in to comment.