Share
Embed
Json Web Token
Une introduction a Json Web Token, avec un exemple d'utilisation avec Symfony.
Inal Djafar
May 10, 2016
Transcript
69 slides
-
Définition JSON Web Token (JWT) est un standard (RFC 7519)
qui permet de transférer des “claims” de manière sécurisée entre deux parties. Le JWT peut être hashé, signé numériquement et/ou chiffré en se basant sur deux autres standards JSON Web Signature et JSON Web Encryption. -
Définition JSON Web Token (JWT) est un standard (RFC 7519)
qui permet de transférer des “claims” de manière sécurisée entre deux parties. Le JWT peut être hashé, signé numériquement et/ou chiffré en se basant sur deux autres standards JSON Web Signature et JSON Web Encryption. “claims” {“key” : “value”} -
Définition JSON Web Token (JWT) est un standard (RFC 7519)
qui permet de transférer des “claims” de manière sécurisée entre deux parties. Le JWT peut être hashé, signé numériquement et/ou chiffré en se basant sur deux autres standards JSON Web Signature et JSON Web Encryption. -
Définition JSON Web Token (JWT) est un standard (RFC 7519)
qui permet de transférer des “claims” de manière sécurisée entre deux parties. Le JWT peut être hashé, signé numériquement et/ou chiffré en se basant sur deux autres standards JSON Web Signature et JSON Web Encryption. JSON Web Signature JWS : standard qui défini comment signer les objets JSON (RFC 7515) -
Définition JSON Web Token (JWT) est un standard (RFC 7519)
qui permet de transférer des “claims” de manière sécurisée entre deux parties. Le JWT peut être hashé, signé numériquement et/ou chiffré en se basant sur deux autres standards JSON Web Signature et JSON Web Encryption. -
Définition JSON Web Token (JWT) est un standard (RFC 7519)
qui permet de transférer des “claims” de manière sécurisée entre deux parties. Le JWT peut être hashé, signé numériquement et/ou chiffré en se basant sur deux autres standards JSON Web Signature et JSON Web Encryption. JSON Web Encryption JWE : standard qui défini comment chiffrer les objets JSON (RFC 7516) -
Définition JSON Web Token (JWT) est un standard (RFC 7519)
qui permet de transférer des “claims” de manière sécurisée entre deux parties. Le JWT peut être hashé, signé numériquement et/ou chiffré en se basant sur deux autres standards JSON Web Signature et JSON Web Encryption. -
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 . eyJuYW1lIjoiSmFjcXVlcyBzZWxlcmUiLCJyb 2xlcyI6WyJhZG1pbiIsInVzZXJsYW1iZGEiXS wiYmlydGhkYXkiOiIxOTg5MDgxMjExMjMiLCJ pZCI6MTI4LCJpc19wcmVtaXVtIjp0cnVlfQ . g28FdUi7RPFwyvxmwMvnLE7wb3Sux3JlvVxIZ Ew5PmY header
Payload Signature Structure du JWT { “alg”: “HS256”, “typ”: “JWT” } -
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 . eyJuYW1lIjoiSmFjcXVlcyBzZWxlcmUiLCJyb 2xlcyI6WyJhZG1pbiIsInVzZXJsYW1iZGEiXS wiYmlydGhkYXkiOiIxOTg5MDgxMjExMjMiLCJ pZCI6MTI4LCJpc19wcmVtaXVtIjp0cnVlfQ . g28FdUi7RPFwyvxmwMvnLE7wb3Sux3JlvVxIZ Ew5PmY header
Payload Signature Structure du JWT { “alg”: “HS256”, “typ”: “JWT” } { “username”: “Jacques Selere”, “roles”: [“admin”, “manager”], “exp”: 1464208788 } -
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 . eyJuYW1lIjoiSmFjcXVlcyBzZWxlcmUiLCJyb 2xlcyI6WyJhZG1pbiIsInVzZXJsYW1iZGEiXS wiYmlydGhkYXkiOiIxOTg5MDgxMjExMjMiLCJ pZCI6MTI4LCJpc19wcmVtaXVtIjp0cnVlfQ . g28FdUi7RPFwyvxmwMvnLE7wb3Sux3JlvVxIZ Ew5PmY header
Payload Signature Structure du JWT { “alg”: “HS256”, “typ”: “JWT” } { “username”: “Jacques Selere”, “roles”: [“admin”, “manager”], “exp”: 1464208788 } claims -
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 . eyJuYW1lIjoiSmFjcXVlcyBzZWxlcmUiLCJyb 2xlcyI6WyJhZG1pbiIsInVzZXJsYW1iZGEiXS wiYmlydGhkYXkiOiIxOTg5MDgxMjExMjMiLCJ pZCI6MTI4LCJpc19wcmVtaXVtIjp0cnVlfQ . g28FdUi7RPFwyvxmwMvnLE7wb3Sux3JlvVxIZ Ew5PmY header
Payload Signature Structure du JWT { “alg”: “HS256”, “typ”: “JWT” } { “username”: “Jacques Selere”, “roles”: [“admin”, “manager”], “exp”: 1464208788 } ?ouH?D?p??f???,N?ot??re?\HdL9>f claims -
header payload hash hashé header encrypted payload chiffré header payload
signature signé Types de JWT -
header payload hash hashé header encrypted payload chiffré header payload
signature signé header payload none Types de JWT -
header payload hash hashé header encrypted payload chiffré header payload
signature signé header payload none chiffré signé + Types de JWT -
header encrypted payload signature header payload hash hashé header encrypted
payload chiffré header payload signature signé header payload none chiffré signé + Types de JWT -
Mots réservés “iss” : (Issuer) l’identifiant de celui qui a
généré le JWT. “exp” : (Expiration) date d’expiration du JWT. “iat” : (Issued At) date de création. “aud” : (Audiance) à qui s’adresse le JWT? “sub” : (Subject) “nbf” : (Not Before) non autorisé avant la date indiquée. “jti” : (Identifier) identifiant unique du JWT. Payload (claims) Header (parameters) “alg” : Algorithme utilisé, (paramètre requis) “typ” : Type de token “jku” : Url qui pointe vers la clé publique pour valider la signature du token “kid” : Identifiant de clé (permet de signaler un changement de clé) -
Construire un JWT <?php function hashSignOrEncryptAlgorithm($data) { // hash, sign
or encrypt } $header = base64_encode(‘{“alg”:”HS256”, “typ”: “JWT”}’); $payload = base64_encode(‘{“foo”: “bar”}’); $signature = base64_encode(hashSignOrEncryptAlgorithm( $header.’.’.$payload )); $JWT = join(‘.’, [$header, $payload, $signature]); -
Il y a un Bundle pour ça “Il faut tout
racheter” -
LexikJWTAuthenticationBundle composer require “lexik/jwt-authentication-bundle” $bundles = [ //… new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(),
lexik_jwt_authentication: private_key_path: "%kernel.root_dir%/../../config/keys/jwt/private.pem" public_key_path: "%kernel.root_dir%/../../config/keys/jwt/public.pem" pass_phrase: jwt-presentation token_ttl: 86400 -
LexikJWTAuthenticationBundle composer require “lexik/jwt-authentication-bundle” $bundles = [ //… new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(),
lexik_jwt_authentication: private_key_path: "%kernel.root_dir%/../../config/keys/jwt/private.pem" public_key_path: "%kernel.root_dir%/../../config/keys/jwt/public.pem" pass_phrase: jwt-presentation token_ttl: 86400 firewalls: # ... login: pattern: ^/api/login stateless: true anonymous: true provider: in_memory form_login: check_path: /api/login_check success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure require_previous_session: false -
LexikJWTAuthenticationBundle composer require “lexik/jwt-authentication-bundle” $bundles = [ //… new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(),
lexik_jwt_authentication: private_key_path: "%kernel.root_dir%/../../config/keys/jwt/private.pem" public_key_path: "%kernel.root_dir%/../../config/keys/jwt/public.pem" pass_phrase: jwt-presentation token_ttl: 86400 firewalls: # ... login: pattern: ^/api/login stateless: true anonymous: true provider: in_memory form_login: check_path: /api/login_check success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure require_previous_session: false firewalls: # ... api: # ... # advanced configuration lexik_jwt: authorization_header: # check token in Authorization Header enabled: true prefix: Bearer name: Authorization cookie: # check token in a cookie enabled: false name: BEARER query_parameter: # check token in query string parameter enabled: false name: bearer throw_exceptions: false create_entry_point: true authentication_provider: lexik_jwt_authentication.security.authentication.provider authentication_listener: lexik_jwt_authentication.security.authentication.listener -
AuthenticationFailureEvent AuthenticationSuccessEvent JWTAuthenticatedEvent JWTCreatedEvent JWTDecodedEvent JWTEncodedEvent JWTInvalidEvent JWTCreatedEvent Events <?php
namespace AppBundle\EventListener; use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent; class JWTCreatedListener { public function onJWTCreated(JWTCreatedEvent $event) { if (!($request = $event->getRequest())) { return; } $payload = $event->getData(); $payload['ip'] = $request->getClientIp(); $payload['roles'] = $event->getUser()->getRoles(); $event->setData($payload); } } -
AuthenticationFailureEvent AuthenticationSuccessEvent JWTAuthenticatedEvent JWTCreatedEvent JWTDecodedEvent JWTEncodedEvent JWTInvalidEvent JWTCreatedEvent Events <?php
namespace AppBundle\EventListener; use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent; class JWTCreatedListener { public function onJWTCreated(JWTCreatedEvent $event) { if (!($request = $event->getRequest())) { return; } $payload = $event->getData(); $payload['ip'] = $request->getClientIp(); $payload['roles'] = $event->getUser()->getRoles(); $event->setData($payload); } } services: jwt_created_listener: class: AppBundle\EventListener\JWTCreatedListener tags: - { name: kernel.event_listener, event: lexik_jwt_authentication.on_jwt_created, method: onJWTCreated } -
l’idée de la démo Public /login_check Private sign JWT verify
/restricted?bearer={{JWT}} -
l’idée de la démo Public /login_check Private sign JWT verify
/restricted?bearer={{JWT}} Access Granted -
l’idée de la démo Public /login_check Private sign JWT verify
/restricted?bearer={{JWT}} /restricted ?bearer={{JWT}} Access Granted -
l’idée de la démo Public /login_check Private sign JWT verify
/restricted?bearer={{JWT}} /restricted ?bearer={{JWT}} Access Granted verify -
l’idée de la démo Public /login_check Private sign JWT verify
/restricted?bearer={{JWT}} /restricted ?bearer={{JWT}} Access Granted Access Granted verify -
l’idée de la démo Public /login_check Private sign JWT verify
/restricted?bearer={{JWT}} /restricted ?bearer={{JWT}} Access Granted Access Granted /restricted ?bearer=JWT verify -
l’idée de la démo Public /login_check Private sign JWT verify
/restricted?bearer={{JWT}} /restricted ?bearer={{JWT}} Access Granted Access Granted /restricted ?bearer=JWT verify verify -
l’idée de la démo Public /login_check Private sign JWT verify
/restricted?bearer={{JWT}} /restricted ?bearer={{JWT}} Access Granted Access Granted /restricted ?bearer=JWT Access Granted verify verify -
Interoperability Stateless Découplage de l’api authentification. Cross-Domain/CORS: indépendant du domain
Prêt pour les SPA, mobile. Avantages -
Favoriser des temps d’expiration courts. Éviter les données sensibles dans
le payload. Éviter de surcharger le payload (utiliser un /me) Attention quand même -
Comment ? Implementer un Refresh Token ? Revoker l’access à
un token ? JWTRefreshTokenBundle -
Comment ? Implementer un Refresh Token ? Revoker l’access à
un token ? JWTRefreshTokenBundle Registry d’identifiant (jti)