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

REST Authentication with JWT

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

REST Authentication with JWT

Avatar for Ignacio Anaya

Ignacio Anaya

November 15, 2017
Tweet

More Decks by Ignacio Anaya

Other Decks in Programming

Transcript

  1. ! Nacho Anaya @ianaya89 • Full Stack Developer, Tech Trainer

    & Speaker • Ambassador @Auth0 • Organizer @Vuenos_Aires { REST Authen-ca-on with JWT } - @ianaya89 2
  2. ! Why JWT? > Signed HMAC - RSA - ECDSA

    { REST Authen-ca-on with JWT } - @ianaya89 12
  3. ! Header { "alg": "HS256", "typ": "JWT" } { REST

    Authen-ca-on with JWT } - @ianaya89 17
  4. ! Payload { "id": "1234567890", "name": "John Doe", "admin": true,

    "iss": "https://api.com", "exp": 1510745797148 } { REST Authen-ca-on with JWT } - @ianaya89 18
  5. ! Payload { "id": "1234567890", "name": "John Doe", "admin": true,

    "iss": "https://api.com", "exp": 1510745797148 } { REST Authen-ca-on with JWT } - @ianaya89 19
  6. ✍ Signature const data = base64urlEncode( header ) + '.'

    + base64urlEncode( payload ) HMACSHA256(data, 'your_secret_message') { REST Authen-ca-on with JWT } - @ianaya89 20
  7. ✍ Signature const data = base64urlEncode( header ) + '.'

    + base64urlEncode( payload ) HMACSHA256(data, 'your_secret_message') { REST Authen-ca-on with JWT } - @ianaya89 21
  8. ! When to use it? > Authen)ca)on > Informa*on Exchange

    { REST Authen-ca-on with JWT } - @ianaya89 24
  9. ! Where to use it? SPA's - Mobile Serverless -

    IoT { REST Authen-ca-on with JWT } - @ianaya89 26
  10. ! How does it work with REST? 1. Sends Creden+als

    POST /login { "user": "ianaya89", "password": "dont-hack-me" } { REST Authen-ca-on with JWT } - @ianaya89 32
  11. ! How does it work with REST? 2. Creates JWT

    const jwt = require('jsonwebtoken') // POST /login function login (req, res, next) { // Validates user credentials... const payload = { user: 'ianaya89', role: 'admin' } const token = jwt.sign(payload, 'this_is_super_secret') res.status(201).send({ token: `Bearer ${token}` }) } router.post('/login', login) { REST Authen-ca-on with JWT } - @ianaya89 33
  12. ! How does it work with REST? 3. Returns JWT

    const jwt = require('jsonwebtoken') // POST /login function login (req, res, next) { // Validates user credentials... const payload = { user: 'ianaya89', role: 'admin' } const token = jwt.sign(payload, 'this_is_super_secret') res.status(201).send({ token: `Bearer ${token}` }) } router.post('/login', login) { REST Authen-ca-on with JWT } - @ianaya89 34
  13. ! How does it work with REST? 4. Gets a

    resource GET /resource Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. eyJzdWIiOiIxMjM0NTY3ODkiLCJuYW1lIjoiSm9obiBEb2UiLCJhZG1pbiI6ZmFsc2V9. b99O1RrYbHtWJ3MGZXkdADZkmiLm9HNliRccKxMPDuc { REST Authen-ca-on with JWT } - @ianaya89 35
  14. ! How does it work with REST? 5. Verifies token

    const jwt = require('jsonwebtoken') // GET /resource function getResource (req, res, next) { try { const payload = jwt.verify(token, 'this_is_super_secret') } catch (err) { return res.sendStatus(401) } } router.get('/resource', getResource) { REST Authen-ca-on with JWT } - @ianaya89 36
  15. ! How does it work with REST? 6. Sends response

    const jwt = require('jsonwebtoken') // GET /resource function getResource (req, res, next) { try { const payload = jwt.verify(token, 'this_is_super_secret') res.send(' ! ') } catch (err) { return res.sendStatus(401) } } router.get('/resource', getResource) { REST Authen-ca-on with JWT } - @ianaya89 37
  16. ! How does it work with REST? 6. Sends response

    const jwt = require('jsonwebtoken') // GET /resource function getResource (req, res, next) { try { const payload = jwt.verify(token, 'this_is_super_secret') if (payload.role !== 'admin') { return res.sendStatus(403) } res.send(' ! ') } catch (err) { return res.sendStatus(401) } } router.get('/resource', getResource) { REST Authen-ca-on with JWT } - @ianaya89 38
  17. ! Which languages are supported? > "All" of them {

    REST Authen-ca-on with JWT } - @ianaya89 40
  18. ! Is JWT secure? > Anyone can view the content

    { REST Authen-ca-on with JWT } - @ianaya89 47
  19. ! Is JWT secure? > No one can modify it

    { REST Authen-ca-on with JWT } - @ianaya89 48
  20. ! Is JWT secure? > JWT is signed not ecnrpyted

    { REST Authen-ca-on with JWT } - @ianaya89 49
  21. ! Is JWT secure? > Keep your "secret" secret {

    REST Authen-ca-on with JWT } - @ianaya89 50