Phil Nash
@philnash
http://philna.sh
philnash@twilio.com
@philnash
ARE YOU READY
FOR SOME
ABBREVIATIONS?
JWT
@philnash
JSON WEB
TOKEN
@philnash
RFC 7519
@philnash
"JOT"
@philnash
THERE'S MORE
JWS JWE
JWK JWA
@philnash
RFCS
7515 7516
7517 7518
@philnash
JOSE
@philnash
RFC 7520
@philnash
AAARGH
@philnash
ACTUALLY
@philnash
EMOJIS!
@philnash
LET'S START
AGAIN
JWT, WTF?
JWTs
•   What are they?
•   What can you use them for?
•   How do they work?
•   Pitfalls
@philnash
WHAT'S A JWT?
JWT
“JSON Web Token (JWT) is a compact, URL-safe
means of representing claims to be transferred
between two parties.”
@philnash
JWT
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiJwaGlsbmFzaEB0d2lsaW8uY29tIn0.
l9vi8Dt8Pds3QTBqNMnQGU0wDDWDv46RFIcqeOIPqDk
@philnash
JWT
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiJwaGlsbmFzaEB0d2lsaW8uY29tIn0.
l9vi8Dt8Pds3QTBqNMnQGU0wDDWDv46RFIcqeOIPqDk
@philnash
JWT
{
"alg": "HS256",
"typ": "JWT"
}
{
"sub": "philnash@twilio.com"
}
@philnash
@philnash
WHAT CAN YOU
USE THEM FOR?
STATELESS
SESSIONS
@philnash
MICROSERVICE
ARCHITECTURE
@philnash
OPENID
CONNECT
@philnash
CLIENT SIDE
AUTH FOR 3RD
PARTY
SERVICES
@philnash
HOW DO THEY
WORK?
CREATING A
JWT
@philnash
Creating a JWT
const header = {
"alg": "HS256",
"typ": "JWT"
}
const payload = {
"sub": "philnash@twilio.com"
}
@philnash
CLAIMS
@philnash
Header Claims
"typ": "JWT"
@philnash
Header Claims - Unsecured
"alg": "none"
@philnash
Header Claims - Secured
"alg": "HS256"
@philnash
Payload Claims
"iss" - issuer
"sub" - subject
"aud" - audience
"exp" - expires at
"nbf" - not before
"iat" - issued at
"jti" - JWT ID
@philnash
Payload Claims
Anything you want!
@philnash
Creating a JWT
const header = {
"alg": "HS256",
"typ": "JWT"
}
const payload = {
"sub": "philnash@twilio.com"
}
@philnash
ENCODE THE
HEADER AND
PAYLOAD
@philnash
Base64url
encodedHeader = new Buffer(JSON.stringify(header))
.toString('base64')
.replace(/=/g, "")
.replace(/+/g, "-")
.replace(///g, "_");
@philnash
Base64url
encodedPayload = new Buffer(JSON.stringify(payload))
.toString('base64')
.replace(/=/g, "")
.replace(/+/g, "-")
.replace(///g, "_");
@philnash
SIGN THE
ENCODED
HEADER AND
PAYLOAD
@philnash
HMAC SHA256
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'secret');
hmac.update(`${encodedHeader}.${encodedPayload}`);
const signature = hmac.digest('base64');
@philnash
The finished JWT
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiJwaGlsbmFzaEB0d2lsaW8uY29tIn0.
l9vi8Dt8Pds3QTBqNMnQGU0wDDWDv46RFIcqeOIPqDk
@philnash
VERIFYING A
JWT
@philnash
Verifying a JWT
[
encodedHeader,
encodedPayload,
signature
] = jwt.split('.');
@philnash
Decode the header
const decodedHeader = JSON.parse(
new Buffer(encodedHeader, 'base64').toString('ascii')
)
@philnash
Decode the payload
const decodedPayload = JSON.parse(
new Buffer(encodedPayload, 'base64').toString('ascii')
)
@philnash
HMAC SHA256
const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'secret');
hmac.update(`${encodedHeader}.${encodedPayload}`);
const generatedSignature = hmac.digest('base64');
@philnash
Compare
secureCompare(signature, generatedSignature);
@philnash
JWT Playground
https://jwt.io
@philnash
PITFALLS
DATA IS PUBLIC
@philnash
SIGNING
ALGORITHM
@philnash
JWT
{
"alg": "HS256",
"typ": "JWT"
}
{
"sub": "philnash@twilio.com"
}
@philnash
JWT
{
"alg": "none" ,
"typ": "JWT"
}
{
"sub": "philnash@twilio.com"
}
@philnash
ALWAYS VERIFY
WITH AN
EXPECTED
ALGORITHM
@philnash
PUBLIC KEYS
AND
ENCRYPTION
@philnash
WHAT CAN YOU
USE THEM FOR?
STATELESS
SESSIONS
@philnash
Stateless sessions - revocation
•   exp claim - token expiry time
•   Without state, you can't revoke individual tokens
except by expiry
•   Requires a blacklist of revoked tokens to check
against
@philnash
Stateless sessions - storage
•   Cookies
•   ensure you have CSRF protection
•   localStorage
•   vulnerable to XSS
•   requires JS to store and insert as an
Authentication header
@philnash
MICROSERVICE
ARCHITECTURE
@philnash
Microservice architecture
•   Authentication server signs tokens with private
key
•   Other servers can verify with public key
@philnash
OPENID
CONNECT
@philnash
CLIENT SIDE
AUTH FOR 3RD
PARTY
SERVICES
@philnash
JWT, WTF?
JWT, WTF?
•   https://jwt.io
•   RFC 7519
•   JWTs VS Sessions
•   Stop using JWT for sessions
•   Use JWT the Right Way
@philnash
THANKS!
Thanks!
@philnash
http://philna.sh
philnash@twilio.com
@philnash

Jwt, wtf? - Phil Nash - Codemotion Amsterdam 2017