-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from buggregator/feature/oauth
Add support for Auth0 user authentication
- Loading branch information
Showing
18 changed files
with
601 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\Auth; | ||
|
||
use Psr\Http\Message\UriInterface; | ||
|
||
final class AuthSettings | ||
{ | ||
public function __construct( | ||
public readonly bool $enabled, | ||
public readonly UriInterface $loginUrl, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\Auth; | ||
|
||
use Spiral\Auth\TokenInterface; | ||
use Spiral\Auth\TokenStorageInterface; | ||
use Firebase\JWT\JWT; | ||
use Firebase\JWT\Key; | ||
use Firebase\JWT\ExpiredException; | ||
|
||
final class JWTTokenStorage implements TokenStorageInterface | ||
{ | ||
/** @var callable */ | ||
private $time; | ||
|
||
public function __construct( | ||
private readonly string $secret, | ||
private readonly string $algorithm = 'HS256', | ||
private readonly string $expiresAt = '+30 days', | ||
callable $time = null | ||
) { | ||
$this->time = $time ?? static function (string $offset): \DateTimeImmutable { | ||
return new \DateTimeImmutable($offset); | ||
}; | ||
} | ||
|
||
public function load(string $id): ?TokenInterface | ||
{ | ||
try { | ||
$token = (array) JWT::decode($id, new Key($this->secret, $this->algorithm)); | ||
} catch (ExpiredException $exception) { | ||
throw $exception; | ||
} catch (\Throwable $exception) { | ||
return null; | ||
} | ||
|
||
if ( | ||
false === isset($token['data']) | ||
|| false === isset($token['iat']) | ||
|| false === isset($token['exp']) | ||
) { | ||
return null; | ||
} | ||
|
||
return new Token( | ||
$id, | ||
$token, | ||
(array) $token['data'], | ||
(new \DateTimeImmutable())->setTimestamp($token['iat']), | ||
(new \DateTimeImmutable())->setTimestamp($token['exp']) | ||
); | ||
} | ||
|
||
public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface | ||
{ | ||
$issuedAt = ($this->time)('now'); | ||
$expiresAt = $expiresAt ?? ($this->time)($this->expiresAt); | ||
$token = [ | ||
'iat' => $issuedAt->getTimestamp(), | ||
'exp' => $expiresAt->getTimestamp(), | ||
'data' => $payload, | ||
]; | ||
|
||
return new Token( | ||
JWT::encode($token,$this->secret,$this->algorithm), | ||
$token, | ||
$payload, | ||
$issuedAt, | ||
$expiresAt | ||
); | ||
} | ||
|
||
public function delete(TokenInterface $token): void | ||
{ | ||
// We don't need to do anything here since JWT tokens are self-contained. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\Auth; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use Spiral\Http\ResponseWrapper; | ||
|
||
final class SuccessRedirect | ||
{ | ||
public function __construct( | ||
private readonly ResponseWrapper $response, | ||
private readonly UriInterface $redirectUrl, | ||
) { | ||
} | ||
|
||
public function makeResponse(string $token): ResponseInterface | ||
{ | ||
return $this->response->redirect($this->redirectUrl . '?token=' . $token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\Auth; | ||
|
||
use Spiral\Auth\TokenInterface; | ||
|
||
final class Token implements TokenInterface | ||
{ | ||
public function __construct( | ||
private readonly string $id, | ||
private readonly array $token, | ||
private readonly array $payload, | ||
private readonly \DateTimeImmutable $issuedAt, | ||
private readonly \DateTimeImmutable $expiresAt, | ||
) { | ||
} | ||
|
||
public function getID(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getToken(): array | ||
{ | ||
return $this->token; | ||
} | ||
|
||
public function getPayload(): array | ||
{ | ||
return $this->payload; | ||
} | ||
|
||
public function getIssuedAt(): \DateTimeImmutable | ||
{ | ||
return $this->issuedAt; | ||
} | ||
|
||
public function getExpiresAt(): \DateTimeInterface | ||
{ | ||
return $this->expiresAt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\Bootloader; | ||
|
||
use App\Application\Auth\AuthSettings; | ||
use App\Application\Auth\JWTTokenStorage; | ||
use App\Application\Auth\SuccessRedirect; | ||
use App\Application\OAuth\ActorProvider; | ||
use App\Application\OAuth\SessionStore; | ||
use Psr\Http\Message\UriFactoryInterface; | ||
use Spiral\Boot\Bootloader\Bootloader; | ||
|
||
use Auth0\SDK\Auth0; | ||
use Auth0\SDK\Configuration\SdkConfiguration; | ||
use Spiral\Boot\EnvironmentInterface; | ||
use Spiral\Bootloader\Auth\HttpAuthBootloader; | ||
use Spiral\Core\Container\Autowire; | ||
use Spiral\Http\ResponseWrapper; | ||
use Spiral\Session\SessionScope; | ||
|
||
final class AuthBootloader extends Bootloader | ||
{ | ||
public function defineSingletons(): array | ||
{ | ||
return [ | ||
SdkConfiguration::class => static fn(EnvironmentInterface $env) => new SdkConfiguration( | ||
strategy: $env->get('AUTH_STRATEGY', SdkConfiguration::STRATEGY_REGULAR), | ||
domain: $env->get('AUTH_PROVIDER_URL'), | ||
clientId: $env->get('AUTH_CLIENT_ID'), | ||
redirectUri: $env->get('AUTH_CALLBACK_URL'), | ||
clientSecret: $env->get('AUTH_CLIENT_SECRET'), | ||
scope: \explode(',', $env->get('AUTH_SCOPES', 'openid,profile,email')), | ||
cookieSecret: $env->get('AUTH_COOKIE_SECRET', $env->get('ENCRYPTER_KEY') ?? 'secret'), | ||
), | ||
|
||
Auth0::class => static fn(SdkConfiguration $config, SessionScope $session) => new Auth0( | ||
$config->setTransientStorage(new SessionStore($session)), | ||
), | ||
|
||
AuthSettings::class => static fn( | ||
EnvironmentInterface $env, | ||
UriFactoryInterface $factory, | ||
) => new AuthSettings( | ||
enabled: $env->get('AUTH_ENABLED', false), | ||
loginUrl: $factory->createUri('/auth/sso/login'), | ||
), | ||
|
||
SuccessRedirect::class => static fn( | ||
UriFactoryInterface $factory, | ||
ResponseWrapper $response, | ||
) => new SuccessRedirect( | ||
response: $response, | ||
redirectUrl: $factory->createUri('/#/login'), | ||
), | ||
]; | ||
} | ||
|
||
public function init( | ||
HttpAuthBootloader $httpAuth, | ||
EnvironmentInterface $env, | ||
\Spiral\Bootloader\Auth\AuthBootloader $auth, | ||
): void { | ||
$auth->addActorProvider(new Autowire(ActorProvider::class)); | ||
$httpAuth->addTokenStorage( | ||
'jwt', | ||
new Autowire( | ||
JWTTokenStorage::class, | ||
[ | ||
'secret' => $env->get('AUTH_JWT_SECRET', $env->get('ENCRYPTER_KEY')), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\HTTP\Response; | ||
|
||
use App\Application\OAuth\User; | ||
|
||
final class UserResource extends JsonResource | ||
{ | ||
public function __construct( | ||
private readonly User $user, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function mapData(): array | ||
{ | ||
return [ | ||
'username' => $this->user->getUsername(), | ||
'avatar' => $this->user->getAvatar(), | ||
'email' => $this->user->getEmail(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.