JSON Web Tokens

30
JSON WEB TOKEN

Transcript of JSON Web Tokens

Page 1: JSON Web Tokens

JSON WEB TOKEN

Page 2: JSON Web Tokens

Ivan RosolenGraduado em Sistemas de InformaçãoPós-graduado em Gerência de Projetos

Desenvolvedor a 15+ anosAutor de vários PHPT (testes para o PHP)

Entusiasta de novas tecnologias

Head of Innovation @ Arizona

CTO @ Mokation

Page 3: JSON Web Tokens

@ivanrosolen

Page 4: JSON Web Tokens

Authentication

Page 5: JSON Web Tokens

- Form Request Post/Get

- OAuth

- Key/Hash

- Credenciais em plain text

- Session Cookies

Page 6: JSON Web Tokens

- Data is stored in plain text on the server

- Filesystem read/write requests

- Distributed/clustered applications

- Redis/Sticky sessions

Page 7: JSON Web Tokens

API

Page 8: JSON Web Tokens

- Stateless authentication (simplifies horizontal scaling)

- Prevent (mitigate) Cross-Site Request Forgery (CSRF)

attacks.

- Security (https)

- Authorization: Bearer

- 401 unauthorized / 403 forbidden

Page 9: JSON Web Tokens

JOSE

Page 10: JSON Web Tokens

- JWT

- JWS

- JWA

- JWK

- JWE

JSON Object Signing and Encryption

Page 11: JSON Web Tokens

Advantages

Page 12: JSON Web Tokens

- JSON Web Tokens work across different programming languages

- JWTs are self-contained

- JWTs can be passed around easily and secure

- Better control like “one time token” to forgot password, confirm

user, request rates, access, etc.

- One token to rule them all (Stateless)

Page 13: JSON Web Tokens

Anatomy

Page 14: JSON Web Tokens

header.claims.signature

Page 15: JSON Web Tokens

Header

{

"typ": "JWT",

"alg": "HS256"

}

Page 16: JSON Web Tokens

Claims- iss: The issuer of the token

- sub: The subject of the token

- aud: The audience of the token

- exp: This will probably be the registered claim most often used. This will define the expiration

in NumericDate value. The expiration MUST be after the current date/time.

- nbf: Defines the time before which the JWT MUST NOT be accepted for processing

- iat: The time the JWT was issued. Can be used to determine the age of the JWT

- jti: Unique identifier for the JWT. Can be used to prevent the JWT from being replayed. This is

helpful for a one time use token.

http://www.slideshare.net/lcobucci/jwt-to-authentication-and-beyond

Page 17: JSON Web Tokens

Payload / Claims{

"iss": "ivanrosolen.com",

"exp": 1300819380,

"name": "Ivan Rosolen",

"admin": true

}

Page 18: JSON Web Tokens

JWT

eyJ0eXAiOiAiSldUIiwiYWxnIjogIkhTMjU2In0=.eyJpc3MiOiAiaXZhbnJvc29sZW4uY29tIiwiZXhwIjogMTMwMDgxOTM4MCwibmFtZSI6ICJJdmFuIFJvc29sZW4iLCJhZG1pbiI6IHRydWV9.

Page 19: JSON Web Tokens

JWS

- header

- claims

payload

base64(header) . base64(claims)

Page 20: JSON Web Tokens

JWA

- secret (hmac sha256, rsa256 ....)

- encrypt payload with key ‘Xuplau’

Page 21: JSON Web Tokens

Signature

var encodedString = base64UrlEncode(header) + "."

+ base64UrlEncode(payload);

HMACSHA256(encodedString, 'Xuplau');

Page 22: JSON Web Tokens

JWT

eyJ0eXAiOiAiSldUIiwiYWxnIjogIkhTMjU2In0=.eyJpc3MiOiAiaXZhbnJvc29sZW4uY29tIiwiZXhwIjogMTMwMDgxOTM4MCwibmFtZSI6ICJJdmFuIFJvc29sZW4iLCJhZG1pbiI6IHRydWV9.M2FjZTM0M2ZiNjhhMzBiOWNiYTkxN2U1Zjk4YjUxOWYzMTY3NGZlMmU4MTIzYjU1NTRkMjNlNjYzOTkyZGU2Nw==

Page 23: JSON Web Tokens

Code

Page 24: JSON Web Tokens
Page 25: JSON Web Tokens
Page 26: JSON Web Tokens

Github

- Session

- JWT

- JOSE

Page 27: JSON Web Tokens

Refs

Page 29: JSON Web Tokens

????

Page 30: JSON Web Tokens

OBRIGADO!

Visite phpsp.org.br