Fireship · @fireship
Session vs Token Authentication in 100 Seconds
Uploaded October 29, 2020 · Length 2:18
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.
- 0:00
user authentication there are two main
- 0:02
ways to get the job done
- 0:03
sessions and tokens the traditional
- 0:05
approach on the web is cookie-based
- 0:07
server-side sessions the process begins
- 0:09
with a user filling out their username
- 0:11
and password and then submitting it to a
- 0:13
server which then validates it creates a
- 0:15
session in the database
- 0:16
then responds with a session id the
- 0:18
session id will be saved in the
- 0:19
browser's cookie jar
- 0:20
which is a place in the browser to save
- 0:22
key value pairs that will be sent back
- 0:24
to the server on each subsequent request
- 0:26
it can then respond back with content
- 0:28
designed for the currently logged end
- 0:29
user
- 0:30
in other words we have a stateful
- 0:31
session between the front end client
- 0:33
and backend server this approach works
- 0:35
great but there are some drawbacks it
- 0:37
can be vulnerable to an attack known as
- 0:39
cross-site request forgery where the
- 0:40
attacker points the user to a site
- 0:42
they're logged into to perform
- 0:44
actions they didn't intend to like
- 0:45
submitting a payment or changing their
- 0:47
password
- 0:47
although the risk is very low especially
- 0:49
if you use a modern framework to
- 0:51
implement your code the bigger problem
- 0:52
is that you'll need to store the session
- 0:54
id
- 0:54
in a database or keep it in memory on
- 0:56
the server because most of today's
- 0:58
cloud applications are scaled
- 0:59
horizontally this can be a huge
- 1:01
bottleneck in production
- 1:02
and that brings us to token-based
- 1:03
authentication which solves this problem
- 1:05
but introduces its own set of challenges
- 1:08
the process begins the same with the
- 1:09
client sending its login details to the
- 1:11
server instead of storing a session id
- 1:13
it generates a json web token the jot is
- 1:16
created with a private key on the server
- 1:18
then it's sent back to the browser where
- 1:19
it's normally kept in local storage
- 1:21
on future requests the jot will be added
- 1:23
to the authorization header
- 1:25
prefixed by bearer the server then only
- 1:28
needs to validate the signature there's
- 1:29
no need for a database lookup somewhere
- 1:31
else in the infrastructure
- 1:32
and that's way more efficient when
- 1:34
dealing with a distributed system in the
- 1:35
cloud
- 1:36
however tokens can still be hijacked by
- 1:38
an attacker and they can also be
- 1:39
difficult to invalidate
- 1:41
and they can't be used to authenticate a
- 1:42
user in the background on the server
- 1:44
here's the most important thing to
- 1:45
understand with a session the
- 1:47
authentication state is handled on the
- 1:48
server
- 1:49
while tokens are managed on the client
- 1:51
this has been user authentication in 100
- 1:53
seconds
- 1:54
if you want to master web security check
- 1:56
out web security academy
- 1:57
my friend bartos is launching a 12-week
- 1:59
coaching program


