Commit c690d912 authored by reminec's avatar reminec
Browse files

Refacto auth

parent 5488664f
Pipeline #343244 failed with stage
in 2 minutes and 39 seconds
......@@ -2,3 +2,4 @@ lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
user_identity_field: uuid
......@@ -27,25 +27,29 @@ security:
pattern: ^/api/docs?(?:[.](?:html|xml|json|ya?ml))?$
security: false
main:
pattern: ^/api
provider: app_user_provider
api:
pattern: ^/api/
stateless: true
security: true
# Any one can request api
anonymous: true
http_basic:
realm: "Limaju's Authentication Desk"
json_login:
check_path: /api/authentication_token
username_path: username
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
anonymous: true
provider: app_user_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
json_login:
check_path: /api/_jwt
username_path: usernameOrEmail
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
main:
anonymous: true
http_basic:
realm: "Limaju's Authentication Desk"
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication
......
authentication_token:
path: /api/authentication_token
path: /api/_jwt
methods: ['POST']
\ No newline at end of file
......@@ -56,4 +56,10 @@ services:
App\Swagger\JwtDecorator:
decorates: 'api_platform.swagger.normalizer.documentation'
arguments: ['@App\Swagger\JwtDecorator.inner']
autoconfigure: false
\ No newline at end of file
autoconfigure: false
app.event.jwt_created_listener:
class: App\EventListener\JWTCreatedListener
arguments: [ '@request_stack' ]
tags:
- { name: kernel.event_listener, event: lexik_jwt_authentication.on_jwt_created, method: onJWTCreated }
\ No newline at end of file
......@@ -100,14 +100,6 @@ class User implements UserInterface
*/
private $password;
/**
* @Groups({"User:login"})
* @Assert\NotBlank(groups={"login"})
* @Assert\Length(max=64, groups={"login"})
* @SerializedName("username")
*/
private $login;
/**
* @Groups({"User:create", "User:edit"})
* @Assert\NotBlank(groups={"register, login"})
......@@ -135,7 +127,6 @@ class User implements UserInterface
$this->uuid = Uuid::uuid4();
}
public function getId(): ?int
{
return $this->id;
......@@ -205,17 +196,6 @@ class User implements UserInterface
return $this;
}
public function getLogin(): ?string
{
return $this->login;
}
public function setLogin($login): self
{
$this->login = $login;
return this;
}
public function getPlainPassword(): string
{
return $this->plainPassword;
......
<?php
namespace App\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Symfony\Component\HttpFoundation\RequestStack;
class JWTCreatedListener
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
/**
* @param JWTCreatedEvent $event
*
* @return void
*/
public function onJWTCreated(JWTCreatedEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
$payload = $event->getData();
/** @var User $user */
$user = $event->getUser();
$payload['uuid'] = $user->getUuid();
unset($payload['username']);
$event->setData($payload);
}
}
\ No newline at end of file
......@@ -26,7 +26,7 @@ class UserRepository extends ServiceEntityRepository implements UserLoaderInterf
public function loadUserByUsername($usernameOrEmail)
{
return $this->createQueryBuilder('u')
->where('u.username = :query or u.email = :query')
->where('u.username = :query or u.email = :query or u.uuid = :query')
->setParameter('query', $usernameOrEmail)
->getQuery()
->getOneOrNullResult();
......
......@@ -38,7 +38,7 @@ final class JwtDecorator implements NormalizerInterface
$docs['components']['schemas']['Credentials'] = [
'type' => 'object',
'properties' => [
'username' => [
'usernameOrEmail' => [
'type' => 'string',
'example' => 'api',
],
......@@ -51,7 +51,7 @@ final class JwtDecorator implements NormalizerInterface
$tokenDocumentation = [
'paths' => [
'/api/authentication_token' => [
'/api/_jwt' => [
'post' => [
'tags' => ['Token'],
'operationId' => 'postCredentialsItem',
......@@ -78,7 +78,6 @@ final class JwtDecorator implements NormalizerInterface
],
],
];
dump(array_merge_recursive($docs, $tokenDocumentation));
return array_merge_recursive($docs, $tokenDocumentation);
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment