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

Portable Sessions with JSON Web Tokens (RailsConf 2017)

Portable Sessions with JSON Web Tokens (RailsConf 2017)

Ever wonder why applications use sessions and APIs use tokens? Must there really be a difference? JSON Web Tokens are an emerging standard for portable secure messages. We'll talk briefly about how they're built and how they earn your trust, then dig into some practical examples you can take back and apply to your own majestic monolith or serious services.

Lance Ivy

April 25, 2017
Tweet

Other Decks in Technology

Transcript

  1. 2003 - professionally unpaid 2004 2005 2006 - paid Rails

    dev 2007 - first RailsConf 2008 - KICKSTARTER Lance Ivy @cainlevy https://cainlevy.net
  2. 2003 - professionally unpaid 2004 2005 2006 - paid Rails

    dev 2007 - first RailsConf 2008 - KICKSTARTER 2009 2010 2011 2012 2013 2014 2015 2016 2017 - Empatico, Keratin AuthN Lance Ivy @cainlevy https://cainlevy.net
  3. What cookies are, technically Response HTTP/1.0 200 OK Content-type: text/html

    Set-Cookie: <...> Request(s) GET /profile HTTP/1.1 Host: www.example.org Cookie: <...>
  4. An API token protocol Request(s) GET /profile HTTP/1.1 Host: www.example.org

    Authorization: Bearer randomstring Response HTTP/1.0 200 OK Content-type: text/json {“token”: “randomstring”}
  5. Verification Claims iss - issuer aud - audience iat -

    issued at exp - expiration Common JWT Claims Payload Claims sub - subject ??? - you decide
  6. A JSON Web Token is like an ID card The

    Internet Name: It Me Address: 123 St Here, There Expires: 2038-01-19 Born: 1970-01-01 It Me official
  7. A JSON Web Token is like an ID card The

    Internet Name: It Me Address: 123 St Here, There Expires: 2038-01-19 Born: 1970-01-01 It Me official issuer subject expiration issued at security
  8. 1. Is it from a recognized authority 2. Is it

    intended for me { “iss”: “https://issuer.example.com”, “aud”: “https://app.example.com” } Verifying a JSON Web Token
  9. { “iss”: “https://issuer.example.com”, “aud”: “https://app.example.com”, “exp”: 1492799700 } 1. Is

    it from a recognized authority 2. Is it intended for me 3. Has it expired Verifying a JSON Web Token
  10. 1. Is it from a recognized authority 2. Is it

    intended for me 3. Has it expired 4. Is it a forgery { “iss”: “https://issuer.example.com”, “aud”: “https://app.example.com”, “exp”: 1492799700 } signature Verifying a JSON Web Token
  11. Verifying a JSON Web Token 1. Is it from a

    recognized authority 2. Is it intended for me 3. Has it expired 4. Is it a forgery 5. Was it generated before or after that time we changed our secret after discovering we’d published it on GitHub ... { “iss”: “https://issuer.example.com”, “aud”: “https://app.example.com”, “exp”: 1492799700, “iat”: 1492796100 } signature
  12. Problem 1: Two Systems Cookies & Tokens • Two identities

    • Two headers • Two authentication systems JSON Web Tokens
  13. Rails Cookie Opaque Token JSON Web Token Header Cookie Authorization

    either Contents structured random string structured Security crypto query crypto Problem 1: Two Systems
  14. Problem 1: Two Systems Cookies & Tokens • Two identities

    • Two headers • Two authentication systems JSON Web Tokens • One identity • Two headers • One authentication system
  15. Problem 2: API Performance Opaque Tokens SELECT user_id FROM api_tokens

    WHERE token = ? JSON Web Tokens • Claims • Cryptography
  16. Problem 2: API Performance Opaque Tokens SELECT user_id FROM api_tokens

    WHERE token = ? Limiting factor: network JSON Web Tokens • Claims • Cryptography Limiting factor: CPU
  17. Problem 3: Shared Auth ActionDispatch::Session::CookieStore • Rails-specific solution • Optimized

    for browser cookies • Optimized for majestic monoliths JSON Web Tokens
  18. Problem 3: Shared Auth ActionDispatch::Session::CookieStore • Rails-specific solution • Optimized

    for browser cookies • Optimized for majestic monoliths JSON Web Tokens • Libraries in 20+ languages • Decoupled from cookies • Ready for distributed architectures
  19. Problem 4: Shared Secrets Managing Secrets • Config management system

    • Manual copy+paste • Extended attack surface JSON Web Tokens
  20. Problem 4: Shared Secrets Managing Secrets • Config management system

    • Manual copy+paste • Extended attack surface JSON Web Tokens • RSA signatures • Publish public keys via JSON Web Keys • Fetch, cache, and verify
  21. Problem 5: Password Resets Database-stored Nonce • Generate and save

    token • Send token in email • Query list of tokens for user_id • Detect reset and regenerate token JSON Web Tokens
  22. A Password Reset JWT { “iss”: “https://app.example.com”, “aud”: “https://app.example.com”, “exp”:

    1492799700, “iat”: 1492796100, “sub”: 82716, “scope”: “password_reset” }
  23. A Password Reset JWT { “iss”: “https://app.example.com”, “aud”: “https://app.example.com”, “exp”:

    1492799700, “iat”: 1492796100, “sub”: 82716, “scope”: “password_reset”, “lock”: 1492814165 // users.password_changed_at }
  24. Problem 5: Password Resets Database-stored Nonce • Generate and save

    token • Send token in email • Query list of tokens for user_id • Detect reset and regenerate token JSON Web Tokens • Generate token • Send token in email • Verify JWT and extract “sub” • Compare “scope” • Compare “lock” • Detect reset and record timestamp
  25. JSON Web Tokens Problem 6: Email Conversion Email to Login

    Conversion • Send email with call-to-action • Confront user with login wall • Instrument conversion rates • Watch dropoff
  26. Problem 6: Email Conversion Email to Login Conversion • Send

    email with call-to-action • Confront user with login wall • Instrument conversion rates • Watch dropoff JSON Web Tokens • Include expiring JWT in call-to-action URL • Include “scope” claim • Verify “scope” claim along with JWT • User is logged in as normal, on any device
  27. Problem 7: Application Complexity Too Many Responsibilities • Application contains

    auth and account management code. • User god model JSON Web Tokens
  28. Problem 7: Application Complexity Too Many Responsibilities • Application contains

    auth and account management code. • User god model JSON Web Tokens • Separate the issuer and audience • Separate accounts from users (1:1) • Enjoy benefits of smaller codebase
  29. Takeaways JWTs solve problems you have right now. JWTs have

    advanced features for tomorrow’s problems. Learn by doing.