Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Json Web Token

Json Web Token

Une introduction a Json Web Token, avec un exemple d'utilisation avec Symfony.

Avatar for Inal Djafar

Inal Djafar

May 10, 2016
Tweet

More Decks by Inal Djafar

Other Decks in Programming

Transcript

  1. 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.
  2. 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”}
  3. 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.
  4. 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)
  5. 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.
  6. 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)
  7. 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.
  8. 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 }
  9. 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
  10. 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
  11. header payload hash hashé header encrypted payload chiffré header payload

    signature signé header payload none Types de JWT
  12. header payload hash hashé header encrypted payload chiffré header payload

    signature signé header payload none chiffré signé + Types de JWT
  13. header encrypted payload signature header payload hash hashé header encrypted

    payload chiffré header payload signature signé header payload none chiffré signé + Types de JWT
  14. 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é)
  15. 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]);
  16. 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
  17. 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
  18. 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
  19. 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); } }
  20. 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 }
  21. l’idée de la démo Public /login_check Private sign JWT verify

    /restricted?bearer={{JWT}} Access Granted
  22. l’idée de la démo Public /login_check Private sign JWT verify

    /restricted?bearer={{JWT}} /restricted ?bearer={{JWT}} Access Granted
  23. l’idée de la démo Public /login_check Private sign JWT verify

    /restricted?bearer={{JWT}} /restricted ?bearer={{JWT}} Access Granted verify
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. Comment ? Implementer un Refresh Token ? Revoker l’access à

    un token ? JWTRefreshTokenBundle Registry d’identifiant (jti)