session is dead long live jwt · jwt •authentication •on the server •create token •do not...

63
Session is Dead Long live JWT JavaCro Rovinj, Croatia – May 2017 Hrvoje Crnjak Software Developer @ Five @HrvojeCrnjak

Upload: others

Post on 14-Oct-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Session is DeadLong live JWT

JavaCro Rovinj, Croatia – May 2017

Hrvoje Crnjak

Software Developer @ Five

@HrvojeCrnjak

Page 2: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Session vs. JWT

Reference vs. Data object

Stateful vs. Stateless

Page 3: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Debunked

(JSON Web Token)

NOT Authentication Protocol

NOT Authorization Protocol

Page 4: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Debunked

(JSON Web Token)

NOT Protocol

Standard for representing claims (data)

Page 5: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

The catch is in how we use JWTs

Page 6: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Client – Server communicationSession ID + Cookie

POST /authenticate

username= &password=...

ClientServer

HTTP 200 OKSet-Cookie: session=0B3...

https://app.yourapp.com

GET /api/user

Cookie: session=0B3...

HTTP 200 OK{name: foo }

Find stored session based on Session ID

Create session and store it on server-side

Page 7: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

POST /authenticate

username= &password=...

ClientServer

HTTP 200 OK{token: ...JWT }

https://app.yourapp.com

GET /api/userAuthorization: Bearer ...JWT...

HTTP 200 OK{name: foo }

Client – Server communicationJWT + Authorization header

Validate token and extract data

Create token with necessary User data (basic info, roles, etc.)

Page 8: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Session vs. JWT analysis

Session• Authentication

• On the Server• Create Session

• Store Session

• Response• Session ID

• Client storage in Cookie

• Accessing Resource• Cookie header with Session ID

• Find Session based on ID

JWT• Authentication

• On the Server• Create Token

• DO NOT store Token

• Response• Token

• Client storage up to JS

• Accessing resource• Authorization header with JWT

• Read data from JWT itself

Page 9: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who
Page 10: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• JSON Web Token

• Pronounced “jot” /dʒɒt/

• Open standard• IETF - RFC 7519

• Proposed in May 2015

Page 11: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Standard for representing claims between parties • Transferred securely and in compact and URL-safe way

• Compact and URL-safe• Base64 encoded with URL-safe alphabet

• ( - , _ ) instead of ( + , / )

• Claim• A piece of information about a subject (user)

• Represented as name/value pair• Claim Name - always string

• Claim Value - any JSON value

Page 12: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Claim names• Registered Claim names

• Defined by the JWT standard

• Public Claim names• Defined by 3rd parties and registered at IANA

• Usually defined by standards that rely on JWT • example : OpenId Connect (given_name, family_name, nicnkname, …)

• Private Claim names• Custom names defined by JWT users

• Anyone creating and using JWTs in their App

• All Claims are optional

Page 13: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Registered Claim names

{

jti : “JWT ID”

}

Page 14: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Registered Claim names

{

jti : “JWT ID”,

iss : “JWT Issuer”

}

Page 15: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Registered Claim names

{

jti : “JWT ID”,

iss : “JWT Issuer”,

sub : “Subject of JWT”

}

Page 16: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Registered Claim names

{

jti : “JWT ID”,

iss : “JWT Issuer”,

sub : “Subject of JWT”,

aud : “Audience – intended recipients”

}

Page 17: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Registered Claim names

{

jti : “JWT ID”,

iss : “JWT Issuer”,

sub : “Subject of JWT”,

aud : “Audience – intended recipients”,

iat : “Time at which JWT was issued”

}Seconds Since the Epoch

Page 18: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

May 12, 1984

Page 19: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Registered Claim names

{

jti : “JWT ID”,

iss : “JWT Issuer”,

sub : “Subject of JWT”,

aud : “Audience – intended recipients”,

iat : “Time at which JWT was issued”

}Seconds Since the Epoch

Page 20: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Registered Claim names

{

jti : “JWT ID”,

iss : “JWT Issuer”,

sub : “Subject of JWT”,

aud : “Audience – intended recipients”,

iat : “Time at which JWT was issued”,

nbf : “Time before which JWT must not be accepted”

}

Page 21: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Registered Claim names

{

jti : “JWT ID”,

iss : “JWT Issuer”,

sub : “Subject of JWT”,

aud : “Audience – intended recipients”,

iat : “Time at which JWT was issued”,

nbf : “Time before which JWT must not be accepted”,

exp : “Time after which JWT must not be accepted”

}

Page 22: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Securing JWT • Signed

• Claims in “plain text”

• Guaranteed token integrity

• Defined through JWS (JSON Web Signature) standard

• Encrypted• Claims encrypted

• Guaranteed token integrity and data privacy

• Defined through JWE (JSON Web Encryption) standard

Page 23: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Securing JWT • Signed

• Claims in “plain text”

• Guaranteed token integrity

• Defined through JWS (JSON Web Signature) standard

• Encrypted• Claims encrypted

• Guaranteed token integrity and data privacy

• Defined through JWE (JSON Web Encryption) standard

Page 24: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Signing JWT• A number of supported algorithms

• Defined in JWA (JSON Web Algorithms) standard

• Most notable are• HS256 – HMAC using SHA-256

• Symmetric key alg.

• Offers integrity and authenticity

• RS256 – RSA using SHA-256

• ES256 – ECDSA using SHA-256• Asymmetric key alg.

• Offers integrity, authenticity and non-repudiation

Page 25: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Composing JWT

Page 26: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Composing JWT

• 3 parts• Header

• Holds info about algorithm used for signing• Payload

• Holds claims• Signature

• Separated by “dot” and Base64 encoded

PayloadHeader Signature

Page 27: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Composing JWT

{sub : "1234567890",name : "John Doe",admin : true

}

PayloadHeader Signature

Base64 encode data

Page 28: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Composing JWT

{alg : “HS256",typ : “JWT“

}

Base64 encode data

PayloadHeader Signature

Page 29: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Composing JWT

SIGN ( encoded_Header + “.” + encoded_Payload, secretKey)

Base64 encode data

PayloadHeader Signature

Page 30: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

• Composing JWT

PayloadHeader Signature

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ

Page 31: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

FAQ #1

Who can create JWTs?

Anyone who knows how to create them

• Pick a library and start coding!• jjwt , java-jwt , etc.

Page 32: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

FAQ #2

Who can read JWTs?

Anyone who knows how to read them

• You need • Library (again)

• A secret key if you want to verify JWTs validity• For clients (like web browsers) that’s not necessary

• For servers it should be

Page 33: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

FAQ #3

How can JWTs be transferred between parties?

The standard doesn’t specify that

• Possible scenarios• Authorization header

• Authorization : Bearer ..JWT… scheme used

• Most common, standardized

• Custom header

• Part of URL

• Part of request body

Page 34: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who
Page 35: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

JWT is Self-Contained

Page 36: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Self-contained

• All the relevant data is inside JWT• User information represented through claims

• Data required to check its validity

• Benefits• Client-side

• Data can be read directly from token

• User ID, name, email, roles, …

• Server-side• On the next slide …

Session IDJSESSIONID=8D4D409560BD4D0CCCA00105325115F3

JWT{sub : "1234567890",name : "John Doe",role : “ADMIN”

}

Page 37: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

JWT is Stateless

Page 38: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Stateless

• JWT is self-contained• No need for State

• No data stored on Server-side

GET /api/user

Cookie: session=

Session Storage

GET /api/user

Authorization: Bearer ...JWT...

Session Storage

Page 39: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

JWT is Scalable

Page 40: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Scalable

• State is evil• When we use Session…

POST /api/event

Cookie: session=

Session Storage

ReverseProxy

GET /api/event/1

Cookie: session=

Page 41: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Scalable

• State is evil• No State (Session) no problem!

• No worries about• Session replication

• Synchronization

• Inter-node communication

POST /api/event

Authorization: Bearer ...JWT...

Session Storage

ReverseProxy

GET /api/event/1

Authorization: Bearer ...JWT...

Page 42: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

JWT is more flexible

Page 43: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Flexible

• Content of JWT completely configurable

• Token creation and usage can be separated

GET /api/eventAuthorization: Bearer ...JWT...

HTTP 200 OK{name: foo }

Auth.Provider

ServiceProvider

Client

https://auth.outsorce.com

https://your.app.com

Page 44: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Flexible

• “Separation of concerns”• Benefits

• Outsource your auth. concerns

• FB, Google, Amazon, Auth0, etc.

• Disadvantages• Someone else can create valid tokens for your service

• You need to trust the Auth. Provider

• Setup• Register your App with Auth. Provider

• Define claims needed in JWT

• Agree on a shared key

Page 45: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

JWT is immune to CSRF attacks*

Page 46: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Immune to CSRF*

• “Session riding”• User tricked into making harmful requests

• User already logged to that service

• Relies on Session Cookie

• No Session Cookie• No Problem

Page 47: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Quick recap

JWT Pros

• Self-contained

• Stateless

• Scalable

• More flexible• Auth. outsourcing

• Immune to CSRF*

JWT Cons

Page 48: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Size

Page 49: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Size

• Smallest JWT larger than Session ID• Avg. Session ID + Cookie name = 45 Bytes

• Header size constraints• HTTP defines none

• But servers do• Tomcat – 8 KB

• Jetty – 6 KB

No. of Claims Size (Bytes)

0 75

1 125

3 150

5 225

10 350

15 550

Page 50: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Storing and Sending JWTs

Page 51: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Storage and transmission

• With Cookies everything is handled automatically• Server instructs Browser to store Session ID

• Browser automatically sends stored Session ID

• With JWTs• Storage

• Handled by client-side JS

• Options• Cookie (not the same as Cookies used to store Session ID)

• LocalStorage

• Transmission• Handled by client-side JS

• Options• Some frameworks have built-in support

Page 52: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

More vulnerable to XSS

Page 53: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

XSS problem

• Cross-site scripting• Like “SQL injection” – but for client-side JS

• Rogue JS• It can

• Change the data displayed to the User• Make harmful requests to the Server

• It can’t • Steal your Session ID (inside HttpOnly Cookie)

• But it can steal JWT• Handled by client-side JS

• Solution• Sanitize User inputs!

Page 54: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Signed, not encrypted*

Page 55: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Signed, not encrypted*

• Common use-case• Signed JWTs

• Readable on client-side

• “Solution”• Use encrypted JWTs

• Disadvantage• Client can’t read User info from JWT

• Client needs to contact Server to get that info

• Best practice• Don’t put anything too sensitive inside JWT

• Always use HTTPS

Page 56: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Token revocation

Page 57: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Token revocation

• How to deny access to already logged User?

• Friendly User• User pressed Logout button

• Session• Session is invalidated server-side

• JWT• Token is deleted client-side

Page 58: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Token revocation

• How to deny access to already logged User?

• Harmful User• Hijacked Session ID or JWT

• Someone is sending requests with stolen credentials

• Session• Session is invalidated server-side

• JWT• No explicit way of revoking already issued tokens

• Only trade-offs

Page 59: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Token revocation

• “Revoking” JWT token• Keep a list of revoked JWTs

• Disadvantage• Introducing state to server-side

• Make JWTs short-lived • Disadvantage

• User needs to login more frequently

• Refresh tokens can be used to mitigate frequent login issue

• Nuclear option• Change JWT signing key

• Disadvantage• Invalidate all tokens

• All users denied access

Page 60: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Quick recap

JWT Pros

• Self-contained

• Stateless

• Scalable

• More flexible• Auth. outsourcing

• Immune to CSRF*

JWT Cons

• Size

• Storage and transmission

• More vulnerable to XSS

• Signed vs. encrypted

• Token revocation

Page 61: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Quick recap

JWT Pros

• Self-contained

• Stateless

• Scalable

• More flexible• Auth. outsourcing

• Immune to CSRF*

JWT Cons

• Size

• Storage and transmission

• More vulnerable to XSS

• Signed vs. encrypted

• Token revocation

Page 62: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Thanks for your time!Q & A

Page 63: Session is Dead Long live JWT · JWT •Authentication •On the Server •Create Token •DO NOT store Token •Response ... java-jwt , etc. FAQ #2 Who can read JWTs? Anyone who

Spring Security + JWT example

Github

hcrnjak / spring-jwt-example