Fireship · @fireship

Session vs Token Authentication in 100 Seconds

Uploaded October 29, 2020 · Length 2:18

Session vs Token Authentication in 100 Seconds

Summary

session vs token authentication: pros and cons, security implications, and differences between stateful and stateless approaches.

  • session authentication
  • token authentication
  • web security
  • firebase security
  • cloud security

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    user authentication there are two main

  2. 0:02

    ways to get the job done

  3. 0:03

    sessions and tokens the traditional

  4. 0:05

    approach on the web is cookie-based

  5. 0:07

    server-side sessions the process begins

  6. 0:09

    with a user filling out their username

  7. 0:11

    and password and then submitting it to a

  8. 0:13

    server which then validates it creates a

  9. 0:15

    session in the database

  10. 0:16

    then responds with a session id the

  11. 0:18

    session id will be saved in the

  12. 0:19

    browser's cookie jar

  13. 0:20

    which is a place in the browser to save

  14. 0:22

    key value pairs that will be sent back

  15. 0:24

    to the server on each subsequent request

  16. 0:26

    it can then respond back with content

  17. 0:28

    designed for the currently logged end

  18. 0:29

    user

  19. 0:30

    in other words we have a stateful

  20. 0:31

    session between the front end client

  21. 0:33

    and backend server this approach works

  22. 0:35

    great but there are some drawbacks it

  23. 0:37

    can be vulnerable to an attack known as

  24. 0:39

    cross-site request forgery where the

  25. 0:40

    attacker points the user to a site

  26. 0:42

    they're logged into to perform

  27. 0:44

    actions they didn't intend to like

  28. 0:45

    submitting a payment or changing their

  29. 0:47

    password

  30. 0:47

    although the risk is very low especially

  31. 0:49

    if you use a modern framework to

  32. 0:51

    implement your code the bigger problem

  33. 0:52

    is that you'll need to store the session

  34. 0:54

    id

  35. 0:54

    in a database or keep it in memory on

  36. 0:56

    the server because most of today's

  37. 0:58

    cloud applications are scaled

  38. 0:59

    horizontally this can be a huge

  39. 1:01

    bottleneck in production

  40. 1:02

    and that brings us to token-based

  41. 1:03

    authentication which solves this problem

  42. 1:05

    but introduces its own set of challenges

  43. 1:08

    the process begins the same with the

  44. 1:09

    client sending its login details to the

  45. 1:11

    server instead of storing a session id

  46. 1:13

    it generates a json web token the jot is

  47. 1:16

    created with a private key on the server

  48. 1:18

    then it's sent back to the browser where

  49. 1:19

    it's normally kept in local storage

  50. 1:21

    on future requests the jot will be added

  51. 1:23

    to the authorization header

  52. 1:25

    prefixed by bearer the server then only

  53. 1:28

    needs to validate the signature there's

  54. 1:29

    no need for a database lookup somewhere

  55. 1:31

    else in the infrastructure

  56. 1:32

    and that's way more efficient when

  57. 1:34

    dealing with a distributed system in the

  58. 1:35

    cloud

  59. 1:36

    however tokens can still be hijacked by

  60. 1:38

    an attacker and they can also be

  61. 1:39

    difficult to invalidate

  62. 1:41

    and they can't be used to authenticate a

  63. 1:42

    user in the background on the server

  64. 1:44

    here's the most important thing to

  65. 1:45

    understand with a session the

  66. 1:47

    authentication state is handled on the

  67. 1:48

    server

  68. 1:49

    while tokens are managed on the client

  69. 1:51

    this has been user authentication in 100

  70. 1:53

    seconds

  71. 1:54

    if you want to master web security check

  72. 1:56

    out web security academy

  73. 1:57

    my friend bartos is launching a 12-week

  74. 1:59

    coaching program