Skip to content

Commit

Permalink
Return the token data on session endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Sep 27, 2021
1 parent 8222ade commit 1c7bb76
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
4 changes: 2 additions & 2 deletions api/Config/UserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public function createUser(string $username, string $password): User
{
$this->initialize();

$password = $this->container->get(UserPasswordEncoderInterface::class)->encodePassword(
$encodedPassword = $this->container->get(UserPasswordEncoderInterface::class)->encodePassword(
new User($username, null),
$password
);

return new User($username, $password);
return new User($username, $encodedPassword);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions api/Controller/SessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Contao\ManagerApi\Config\UserConfig;
use Contao\ManagerApi\HttpKernel\ApiProblemResponse;
use Contao\ManagerApi\Security\JwtManager;
use Contao\ManagerApi\Security\TokenAuthenticator;
use Crell\ApiProblem\ApiProblem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -72,6 +73,15 @@ public function __invoke(Request $request): Response
private function getStatus(): Response
{
if ($this->security->isGranted('IS_AUTHENTICATED_FULLY')) {
$token = $this->security->getToken();

if (null !== $token
&& TokenAuthenticator::class === $token->getAttribute('authenticator')
&& null !== ($payload = $this->config->getToken($token->getAttribute('token_id')))
) {
return new JsonResponse($payload);
}

return new JsonResponse(['username' => (string) $this->security->getUser()]);
}

Expand Down
8 changes: 8 additions & 0 deletions api/Security/TokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class TokenAuthenticator extends AbstractGuardAuthenticator
*/
private $config;

/**
* @var string
*/
private $tokenId;

/**
* Constructor.
*/
Expand Down Expand Up @@ -77,6 +82,8 @@ public function getUser($credentials, UserProviderInterface $userProvider): ?Use
return null;
}

$this->tokenId = $token['id'];

return $userProvider->loadUserByUsername($token['username']);
}

Expand All @@ -93,6 +100,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): ?Response
{
$token->setAttribute('authenticator', static::class);
$token->setAttribute('token_id', $this->tokenId);

return null;
}
Expand Down
30 changes: 20 additions & 10 deletions docs/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ paths:

get:
summary: Get current session status
description: Returns information about the current session (based on authentication cookie)
description: Returns information about the current session (based on authentication)
tags: [Session]
responses:
200:
description: If the user is authenticated
content:
application/json:
schema:
$ref: '#/components/schemas/User'
oneOf:
- $ref: '#/components/schemas/User'
- $ref: '#/components/schemas/TokenResponse'
204:
description: If there are no records in the user database
401:
Expand Down Expand Up @@ -329,7 +331,8 @@ paths:
type: object
properties:
directory:
type: string|null
type: string
nullable: true
description: The directory name or null if only the `web` directory must be created.
responses:
201:
Expand Down Expand Up @@ -476,7 +479,7 @@ paths:
schema:
$ref: '#/components/schemas/TokenResponse'

/api/users/{username}/tokens/{token}:
/api/users/{username}/tokens/{id}:
get:
summary: Get Token
tags: [Users]
Expand All @@ -486,7 +489,7 @@ paths:
required: true
schema:
type: string
- name: token
- name: id
in: path
required: true
schema:
Expand All @@ -510,7 +513,7 @@ paths:
required: true
schema:
type: string
- name: token
- name: id
in: path
required: true
schema:
Expand Down Expand Up @@ -1041,14 +1044,20 @@ components:
TokenResponse:
type: object
properties:
client_id:
id:
type: string
description: OAuth client ID of the token.
token:
type: string
description: Only available if the token was freshly created.
username:
type: string
description: Username of the token.
token:
client_id:
type: string
description: Client ID of the token.
scope:
type: string
description: Permission scope of the token.

ConfigManager:
type: object
Expand Down Expand Up @@ -1098,7 +1107,8 @@ components:
properties:
version:
description: The Contao version number.
type: string|null
type: string
nullable: true
api:
type: object
properties:
Expand Down

0 comments on commit 1c7bb76

Please sign in to comment.