Skip to content
Snippets Groups Projects
Commit 4576a66c authored by Mateusz Charytoniuk's avatar Mateusz Charytoniuk
Browse files

chore: oauth2 session authentication hook

parent ae971568
Branches
Tags
No related merge requests found
...@@ -103,3 +103,31 @@ $ php ./bin/resonance.php generate:defuse-key > oauth2/defuse.key ...@@ -103,3 +103,31 @@ $ php ./bin/resonance.php generate:defuse-key > oauth2/defuse.key
``` ```
Then, change the CHMOD permissions for that key to `0600`. Then, change the CHMOD permissions for that key to `0600`.
## Post Session Authentication Hook
If you are using {{tutorials/session-based-authentication/index}} you need
to return `OAuth2UserSessionAuthenticated` instance
from your authentication {{docs/features/http/responders}} (see also:
{{docs/features/http/interceptors}}).
It allows OAuth2 to know that the user is authenticated and that it should
check if user is in the middle of OAuth2 flow.
```php
<?php
use Distantmagic\Resonance\OAuth2UserSessionAuthenticated;
final readonly class LoginValidation extends HttpController
{
public function createResponse(): HttpInterceptableInterface
{
// ...
// perform session authentication somehow
// ...
return new OAuth2UserSessionAuthenticated();
}
}
```
...@@ -85,6 +85,7 @@ code[class] { ...@@ -85,6 +85,7 @@ code[class] {
.fenced-code { .fenced-code {
background-color: var(--color-block-background); background-color: var(--color-block-background);
box-shadow: 8px 8px #00000033; box-shadow: 8px 8px #00000033;
margin: 20px 0;
@media screen and (min-width: 1024px) { @media screen and (min-width: 1024px) {
position: relative; position: relative;
......
...@@ -2,25 +2,32 @@ ...@@ -2,25 +2,32 @@
declare(strict_types=1); declare(strict_types=1);
namespace Distantmagic\Resonance\HttpResponder\OAuth2; namespace Distantmagic\Resonance\HttpInterceptor;
use Distantmagic\Resonance\Attribute\GrantsFeature; use Distantmagic\Resonance\Attribute\GrantsFeature;
use Distantmagic\Resonance\Attribute\Intercepts;
use Distantmagic\Resonance\Attribute\Singleton; use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\Feature; use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\HttpInterceptableInterface; use Distantmagic\Resonance\HttpInterceptableInterface;
use Distantmagic\Resonance\HttpResponder; use Distantmagic\Resonance\HttpInterceptor;
use Distantmagic\Resonance\HttpResponderInterface; use Distantmagic\Resonance\HttpResponderInterface;
use Distantmagic\Resonance\OAuth2AuthorizationCodeFlowControllerInterface; use Distantmagic\Resonance\OAuth2AuthorizationCodeFlowControllerInterface;
use Distantmagic\Resonance\OAuth2AuthorizationRequestSessionStore; use Distantmagic\Resonance\OAuth2AuthorizationRequestSessionStore;
use Distantmagic\Resonance\OAuth2AuthorizedUser; use Distantmagic\Resonance\OAuth2AuthorizedUser;
use Distantmagic\Resonance\OAuth2UserSessionAuthenticated;
use Distantmagic\Resonance\SessionAuthentication; use Distantmagic\Resonance\SessionAuthentication;
use Distantmagic\Resonance\SingletonCollection;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use RuntimeException; use RuntimeException;
/**
* @template-extends HttpInterceptor<OAuth2UserSessionAuthenticated>
*/
#[GrantsFeature(Feature::OAuth2)] #[GrantsFeature(Feature::OAuth2)]
#[Singleton] #[Intercepts(OAuth2UserSessionAuthenticated::class)]
final readonly class PostSessionAuthentication extends HttpResponder #[Singleton(collection: SingletonCollection::HttpInterceptor)]
final readonly class OAuth2UserSessionAuthenticatedInterceptor extends HttpInterceptor
{ {
public function __construct( public function __construct(
private OAuth2AuthorizationCodeFlowControllerInterface $authorizationCodeFlowController, private OAuth2AuthorizationCodeFlowControllerInterface $authorizationCodeFlowController,
...@@ -28,8 +35,11 @@ final readonly class PostSessionAuthentication extends HttpResponder ...@@ -28,8 +35,11 @@ final readonly class PostSessionAuthentication extends HttpResponder
private SessionAuthentication $sessionAuthentication, private SessionAuthentication $sessionAuthentication,
) {} ) {}
public function respond(ServerRequestInterface $request, ResponseInterface $response): HttpInterceptableInterface|HttpResponderInterface|ResponseInterface public function intercept(
{ ServerRequestInterface $request,
ResponseInterface $response,
object $intercepted,
): HttpInterceptableInterface|HttpResponderInterface|ResponseInterface {
if (!$this->authorizationRequestSessionStore->has($request)) { if (!$this->authorizationRequestSessionStore->has($request)) {
return $this->authorizationCodeFlowController->redirectToAuthenticatedPage($request, $response); return $this->authorizationCodeFlowController->redirectToAuthenticatedPage($request, $response);
} }
......
<?php
declare(strict_types=1);
namespace Distantmagic\Resonance;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final readonly class OAuth2UserSessionAuthenticated implements HttpInterceptableInterface
{
private SwooleContextRequestResponseReader $swooleContextRequestResponseReader;
/**
* @psalm-taint-source file $templatePath
*/
public function __construct(
?ServerRequestInterface $request = null,
?ResponseInterface $response = null,
) {
$this->swooleContextRequestResponseReader = new SwooleContextRequestResponseReader(
request: $request,
response: $response,
);
}
public function getResponse(): ResponseInterface
{
return $this->swooleContextRequestResponseReader->getResponse();
}
public function getServerRequest(): ServerRequestInterface
{
return $this->swooleContextRequestResponseReader->getServerRequest();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment