Skip to content

Commit

Permalink
refactor: update User creation internals
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed May 8, 2023
1 parent 13e30e6 commit 467d44a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(
): ResponseInterface|RedirectResponse {
try {
$response = $cas->logout($request);
} catch (Throwable) {
} catch (Throwable $e) {
// TODO: Should we log the error ?
// If yes, we need to inject the LoggerInterface and require it
// in composer.json. Do we want an extra dependency?
Expand Down
2 changes: 1 addition & 1 deletion src/Security/CasAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function authenticate(Request $request): Passport
);
}

$user = $this->userProvider->loadUserByResponse($response);
$user = $this->userProvider->loadUserByResponse($this->httpFoundationFactory->createResponse($response));

return new SelfValidatingPassport(
new UserBadge(
Expand Down
21 changes: 7 additions & 14 deletions src/Security/Core/User/CasUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

final class CasUser implements CasUserInterface, Stringable
{
/**
* @param mixed[] $payload
*/
public function __construct(private readonly array $payload)
{
public function __construct(
private readonly array $payload
) {
}

public function __toString(): string
Expand All @@ -37,19 +35,14 @@ public function get(string $key, mixed $default = null): mixed
return $this->payload[$key] ?? $default;
}

public function getAttribute(string $key, mixed $default = null): mixed
{
return $this->payload['attributes'][$key] ?? $default;
}

public function getAttributes(): array
public function getPassword(): ?string
{
return $this->get('attributes', []);
return null;
}

public function getPassword(): ?string
public function getPayload(): array
{
return null;
return $this->payload;
}

public function getPgt(): ?string
Expand Down
16 changes: 3 additions & 13 deletions src/Security/Core/User/CasUserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,9 @@ interface CasUserInterface extends EquatableInterface, UserInterface
{
public function __toString(): string;

/**
* @param mixed $default
*
* @return mixed|null
*/
public function get(string $key, mixed $default = null);

public function getAttribute(string $key, mixed $default = null): mixed;

/**
* @return array<array|string>
*/
public function getAttributes(): array;
public function get(string $key, mixed $default = null): mixed;

public function getPayload(): array;

public function getPgt(): ?string;
}
13 changes: 8 additions & 5 deletions src/Security/Core/User/CasUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,32 @@
use EcPhp\CasLib\Contract\Response\CasResponseBuilderInterface;
use EcPhp\CasLib\Contract\Response\Type\ServiceValidate;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Throwable;

final class CasUserProvider implements CasUserProviderInterface
{
public function __construct(private readonly CasResponseBuilderInterface $casResponseBuilder)
{
public function __construct(
private readonly CasResponseBuilderInterface $casResponseBuilder,
private readonly HttpMessageFactoryInterface $httpMessageFactory
) {
}

public function loadUserByIdentifier($identifier): UserInterface
{
throw new UnsupportedUserException('Unsupported operation.');
}

public function loadUserByResponse(ResponseInterface $response): CasUserInterface
public function loadUserByResponse(Response $response): CasUserInterface
{
try {
$casResponse = $this
->casResponseBuilder
->fromResponse($response);
->fromResponse($this->httpMessageFactory->createResponse($response));
} catch (Throwable $e) {
throw new UserNotFoundException(
sprintf('Unable to get user from response, %s', $e->getMessage()),
Expand Down
4 changes: 2 additions & 2 deletions src/Security/Core/User/CasUserProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace EcPhp\CasBundle\Security\Core\User;

use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserProviderInterface;

interface CasUserProviderInterface extends UserProviderInterface
{
public function loadUserByResponse(ResponseInterface $response): CasUserInterface;
public function loadUserByResponse(Response $response): CasUserInterface;
}

0 comments on commit 467d44a

Please sign in to comment.