Intro to Authentication - User and API Auth — Transcript
Full transcript
- 0:00Hey, what's going on? In this lesson,
- 0:01we're going to start our discussion on
- 0:03authentication for user authentication
- 0:06and API authentication. We'll talk about
- 0:08some of the differences there. Before we
- 0:09get started, I did want to mention that
- 0:10this is part of a larger series, so you
- 0:13can check out the description for the
- 0:14playlist link, as well as a link to get
- 0:16the notes for this lesson and all of my
- 0:19other videos here. So, if you want to
- 0:22follow along both video and notes, then
- 0:24I would highly recommend you check that
- 0:26link out. And finally, I wanted to
- 0:28mention that I did complete my
- 0:29fundamentals course. So, if you want to
- 0:31get software engineering fundamentals
- 0:32for beginner, intermediate, and advanced
- 0:34topics, I will have that link down
- 0:36below, as well. Check out my software
- 0:38engineering fundamentals course if
- 0:39you're looking to build a solid
- 0:41foundation for building software. This
- 0:43course will take you from the very
- 0:44beginning all the way through
- 0:46intermediate and advanced topics with a
- 0:48focus on core computer science concepts
- 0:50and applied software engineering
- 0:52principles that make you a better
- 0:53software engineer. Follow the link to
- 0:55get the first section free. So, let's
- 0:58talk about authentication.
- 1:04And I will also talk about
- 1:05authorization. So, very similar topic,
- 1:07but slightly different in concept. So,
- 1:09let's just talk about that first. So,
- 1:11authentication and authorization.
- 1:17So, let's say we have some web page or
- 1:19some resource, but this isn't available
- 1:21for just everybody. We want to know, do
- 1:23you have the permissions to view this
- 1:25content? So, what does authentication
- 1:27have to do with this? Authentication is
- 1:29you proving you are who you say you are.
- 1:32So, for example, you go to the website,
- 1:35it says, "Yo, dog, you can't visit this
- 1:37page." And it kicks you back to a login
- 1:39page. So, you log in.
- 1:42And if you successfully log in, then you
- 1:45are authenticated.
- 1:50But just because you've proved you are
- 1:52who you say you are, it doesn't
- 1:53necessarily mean you can view this.
- 1:56So, then it comes down to authorization.
- 2:04Are you authorized?
- 2:07So, we'll look at both of these
- 2:09throughout these lessons. The main thing
- 2:11you need to understand is that
- 2:12difference. Authentication is basically
- 2:14saying, "Hey, it's me. Here's proof." In
- 2:16this case, it's proof because I have the
- 2:18username and password for my account.
- 2:20Then authorization is what you are
- 2:22allowed to do. Do I have access to this
- 2:24page? Are there certain features I have
- 2:26access to and other ones I do not have
- 2:28access to? That's all related to
- 2:30authorization. Now, there are two major
- 2:32classifications for authentication that
- 2:33I'm going to talk about. We have user
- 2:36authentication
- 2:41and then we will also have API
- 2:44authentication, or you can think
- 2:46authentication for services.
- 2:49So, let's first take a very quick look
- 2:52at user authentication, and then we'll
- 2:54take a very quick look at API
- 2:56authentication. Then we'll go into more
- 2:58details, but I want you to have an
- 2:59understanding of these categories from
- 3:00the beginning. So, for user
- 3:02authentication, that is if you were a
- 3:03user of a webpage
- 3:06or some app, and you log in, and this
- 3:08allows you to access all of the hidden
- 3:11data. This is basically how login works
- 3:14for webpages. It's basically a way to
- 3:16identify the user,
- 3:19which would make sense from the name.
- 3:21One of the most common structures for
- 3:23this would be using JSON Web Tokens, and
- 3:25we'll talk about what that looks like.
- 3:26API authentication is a little bit
- 3:28different. So, let's say we're building
- 3:29an app, and we need to connect to some
- 3:32other service,
- 3:34and this service exposes an API, but
- 3:36it's not a public API. In order to use
- 3:38it, you have to use an API key.
- 3:45>> [snorts]
- 3:45>> So, this API key kind of acts like a
- 3:47password to make an API request. So,
- 3:50you're not going to be able to get that
- 3:51data back unless you provide the
- 3:53appropriate API key. And then what is
- 3:56this over here?
- 3:57Most likely this is going to be some
- 3:59back end of an app.
- 4:02And then this API is likely some
- 4:03third-party thing you're using. So maybe
- 4:05that's for maps or maybe it is for some
- 4:09AI LLM integration or really any other
- 4:12app out there that exposes an API you
- 4:15could put there. Now in many situations
- 4:17you're going to be on this side of
- 4:19things. You're going to be building
- 4:20services that use APIs, but there are
- 4:23situations when you're going to be
- 4:25building this side as well. So let's say
- 4:27you built some social network.
- 4:33And then you release an API and then
- 4:35other apps can consume your API.
- 4:40You will need to have a strong
- 4:41understanding on API authentication
- 4:43because you're going to want to make
- 4:44sure that these other services are
- 4:48authenticated and they're able to use
- 4:50your API. Otherwise you're just going to
- 4:52have a public API which you can do, but
- 4:55in many situations you don't want to do
- 4:56that. You actually want to scope things
- 4:58to just authenticated API users. It's
- 5:01also step one for controlling what they
- 5:03can and cannot access and things like
- 5:05rate limiting by user. So if you're
- 5:07wanting to make an API service layer,
- 5:09this is going to be very very important.
- 5:11And in that situation you're going to be
- 5:12on this side of the communication.
- 5:15You'll want to be familiar with both of
- 5:16those sides though. So consuming APIs
- 5:18using an API key and building APIs where
- 5:22users use an API key. This one is
- 5:24definitely more simple because you're
- 5:26most likely just going to be working
- 5:27with a front end
- 5:31that makes a request to some back end.
- 5:35And we need to understand how that front
- 5:37end back end flow works. So now that we
- 5:40understand these two categories, let's
- 5:41take a deeper look at user
- 5:43authentication.
- 5:47And let's say we have some resource that
- 5:49we can delete.
- 5:52So, we'll say delete is the method for
- 5:54the request.
- 5:56And then this will be something like
- 5:58{slash} comments.
- 6:02So, we have the full list of comments
- 6:04and the user wants to delete one of
- 6:06them. This is something where we are
- 6:08still working with an API and this is a
- 6:10very important clarification I want to
- 6:12show.
- 6:14Just because we're using the user
- 6:15authentication and not API
- 6:16authentication doesn't mean we're not
- 6:19going to be working with an API. It's
- 6:21just that the purpose of the user
- 6:23authentication is to authenticate user
- 6:26actions. So, this is basically how the
- 6:28flow is going to look. We'll have an end
- 6:30user.
- 6:32He's really happy because our app is so
- 6:35good.
- 6:37He's going to interact with the app from
- 6:39maybe his mobile phone.
- 6:43And on there there is a delete button.
- 6:46When you hit that delete button, it's
- 6:47going to make a request to the back end.
- 6:50So, that's the back end, this is the
- 6:52front end. We need to make sure that
- 6:54that request is valid based off of the
- 6:56token they provide. And if it is, the
- 6:59back end can then issue a delete to the
- 7:03database.
- 7:05But we only want to be able to do this
- 7:08if this user is logged in.
- 7:14And
- 7:16they are authorized to do that action.
- 7:18So, that's the overall flow. [snorts]
- 7:20It's dealing with what the user can do.
- 7:24And in code, basically what this is
- 7:26going to look like
- 7:30is going to be a protected route.
- 7:38So, some function that handles the
- 7:40request, but it's protected, requiring a
- 7:43correct token saying that they are able
- 7:46to do this action. So, that's the flow.
- 7:48Let's talk about what that actually
- 7:50looks like. First, the user comes to the
- 7:52page, they log in.
- 7:55That will give them a token.
- 7:59And then this token
- 8:01will then be used with any of the future
- 8:04requests, such as a delete, anything
- 8:06that requires the token.
- 8:09So, anything protected, that token will
- 8:11then say, "Hey, I'm allowed to do this
- 8:13action, so you should now do that for
- 8:15me." So, what happens if the user tries
- 8:17to do something and they haven't logged
- 8:19in? So, let's say we try to delete
- 8:21something.
- 8:23We can program this to redirect
- 8:26because the back end will give some form
- 8:29of off error, basically saying, "Hey,
- 8:31you need to log in." So, this will allow
- 8:34us to redirect on the front end
- 8:37to a login page.
- 8:42So, the user is now on the login page.
- 8:45They put in their username and password.
- 8:47This will check the back end to make
- 8:49sure that the username and password is
- 8:52correct and exists in the database.
- 8:55And in exchange, it will give back a
- 8:57token.
- 8:59And I'm going to write JWT here. We
- 9:01haven't talked about the JWT structure
- 9:03yet, but it's just a token that this
- 9:06user will keep.
- 9:09And the token basically says, "Hey, I
- 9:12logged in at some point,
- 9:18so we know that it's a valid user." And
- 9:21inside that token, we can also have any
- 9:23kind of permissions
- 9:25or authorization stuff.
- 9:30Once they have that token, they can then
- 9:32go back and retry
- 9:36that initial thing that they were not
- 9:38allowed to do. So, this is exactly what
- 9:40happens when you go to a webpage, maybe
- 9:42you step away for a couple of hours and
- 9:44you come back, the webpage is still
- 9:46loaded there. So, you do something that
- 9:49would require you to be logged in, maybe
- 9:51you delete something, but your login has
- 9:54expired, so it redirects you to a login
- 9:56page, you log in, and then it may
- 9:59re-execute that action or ask you again
- 10:01to do that action. That's the exact flow
- 10:03of what's going on behind the scenes.
- 10:05Also, that says login. I was like
- 10:08looked like a B. I couldn't figure out
- 10:10what it said. So, here's how a token
- 10:11works.
- 10:15So, I mentioned we go through a login
- 10:17page, and if we get the credentials
- 10:19correct, the server will issue a token.
- 10:22So, here we have the backend server, and
- 10:25this backend will
- 10:27sign
- 10:30the token.
- 10:32And this gets a bit into
- 10:36cryptography, but basically this digital
- 10:38signature here is what will allow us to
- 10:40verify the token in the future.
- 10:42So, when [snorts] we send the token back
- 10:45to the user,
- 10:46and then the user makes future requests,
- 10:54this gets sent to the backend, and the
- 10:56backend can verify
- 11:00the token authenticity.
- 11:07If the client were to tamper with the
- 11:09token, try to adjust their permissions
- 11:12or anything like that, it's going to
- 11:14invalidate the token because the client
- 11:16is not able to sign because to sign, you
- 11:21have to have a signing key.
- 11:25And this signing key is a secret value
- 11:27that only the server has. So, when you
- 11:29get into user auth, you often have that
- 11:32secret key. That's very important to
- 11:34protect because it is used for signing
- 11:36these tokens. The signing is important
- 11:38because it allows the backend to later
- 11:40verify the tokens that are sent from the
- 11:43client. If the client were to modify the
- 11:46token,
- 11:48this would produce a different signature
- 11:52because the client does not have the
- 11:55signing key. It never leaves the server.
- 11:57So, the server would be able to identify
- 11:59that this is a phony token. So, what is
- 12:01a token and what does it actually look
- 12:03like?
- 12:06Well, if you look at a token, it's just
- 12:07going to look like a random sequence of
- 12:09characters and numbers. So, behind that
- 12:12is encoded information. So, let's take a
- 12:14look at it in more detail. So, let's
- 12:17take a look at JWTs or JSON web tokens.
- 12:20I'm going to do a dedicated video on
- 12:22JSON web tokens. So, this is a very
- 12:24light intro, but it's basically just
- 12:25going to look like random numbers and
- 12:28letters
- 12:31separated by dots. And they're going to
- 12:34be a lot more. I just don't have much
- 12:35space. So, maybe I'll put like
- 12:38three dots here.
- 12:42And then a dot, and then another section
- 12:44of characters.
- 12:47And then another dot, and then another
- 12:49section of characters.
- 12:51So, we basically have three sections
- 12:52here. I wrote that pretty bad, but
- 12:54that's okay. This one is called the
- 12:57header.
- 12:59This one is the payload.
- 13:03And this is the signature.
- 13:07The way you can imagine the signature
- 13:09being made is something like this, where
- 13:11we
- 13:12invoke the signing,
- 13:16but we use the existing information.
- 13:22And then we use the signing key
- 13:25that only the server has. So, the fact
- 13:27that we are embedding this information
- 13:29into the signature, changing this
- 13:31information will change the actual
- 13:35signature value here.
- 13:37That's an important component if you
- 13:38want to understand how JWTs work because
- 13:40that's what allows the prevention of
- 13:42tinkering with these values on the
- 13:44client. The client shouldn't be able to
- 13:46touch these and get the server to trust
- 13:49that token. It is not going to be
- 13:51possible because if they mess with this
- 13:54data, that is part of the signature
- 13:56which requires the signing key. So, we
- 13:58don't have that signing key, we would
- 13:59have to use a different key and the
- 14:01token would be invalid. Now, because the
- 14:02server is the one creating these tokens
- 14:05and validating them, the server can put
- 14:06whatever it wants in this payload.
- 14:09And that could be even something like if
- 14:11that user is an admin or what
- 14:13authorization things they have. So, this
- 14:15payload maybe contains their user ID
- 14:20which we could then use to look up that
- 14:23user in a database.
- 14:26But, we could also just embed any other
- 14:29information that could be useful in the
- 14:31payload such as if this user is an admin
- 14:34or any other kinds of privilege
- 14:36attributes. So, in this situation, all
- 14:39of the authorization is self-contained
- 14:43in this token and we don't even have to
- 14:45make a database request. And the way
- 14:47this works is if this was changed,
- 14:51let's say the user wanted to tinker with
- 14:54this token, they changed admin to true,
- 15:04well, this would force the token to be
- 15:06invalid.
- 15:10Again, just going back to the fact that
- 15:13the client does not have the signing
- 15:15key, so
- 15:17we're not able to make any changes to
- 15:19the token that the server would then
- 15:21accept. So, those permissions are pretty
- 15:23much baked into the token. It's the full
- 15:26access pass. You don't want that token
- 15:28to be taken by a malicious user because
- 15:31they could act on behalf of another
- 15:34user. Whatever token they have, it's
- 15:37full access. It's as if the user logged
- 15:39in themselves. And it's self-contained,
- 15:41so this is stateless.
- 15:45So, that means if we had an API
- 15:47endpoint,
- 15:48we can think of that API endpoint in
- 15:50isolation. It doesn't need to access a
- 15:52database or do any extra stuff. All it
- 15:54needs is the request and a token.
- 15:59And that's enough to give permissions to
- 16:01do things.
- 16:02Now, let's take a look at API keys.
- 16:06Similar ideas, but there are some key
- 16:08differences. So, an API key will allow
- 16:12the user in the situation, which is most
- 16:14likely going to be some other service.
- 16:17So, it will allow the service to do
- 16:20things, but most of the times it's not
- 16:23stateless.
- 16:30So, it's a different structure than a
- 16:32JWT.
- 16:34And you can think of it sort of just
- 16:35like a password. You make an API
- 16:37request, you include the API key, the
- 16:40backend will take the key,
- 16:44can check against the database to see
- 16:47any kinds of permissions.
- 16:51If we're good to go, we can send the
- 16:53data back to the user.
- 16:56Or the service in this case. So, what
- 16:58this could look like is you might sign
- 16:59up for some
- 17:01API, and in here you might have multiple
- 17:04keys.
- 17:07So, you can imagine logging in to your
- 17:09account for the platform. You can go to
- 17:12your API keys, it lists them out. Every
- 17:14single one of these keys can be seen as
- 17:19separate logins effectively.
- 17:23And they might have different
- 17:25permissions,
- 17:27separate spend tracking,
- 17:31or separate rate limiting.
- 17:33These things could be set by service B,
- 17:36or you could set them potentially. So,
- 17:39when you create that API key, you could
- 17:40say, "Hey, here's the permissions I want
- 17:42this key to have." And we could
- 17:43potentially even limit that key so it
- 17:45doesn't spend more than this many API
- 17:48credits or make this many requests. So,
- 17:50in that situation, you could just use a
- 17:52single one of these API keys
- 17:54for an application,
- 17:56and you could use these other ones for
- 17:58something else.
- 18:02So, instead of thinking of the API key
- 18:05as just full access to the API, you're
- 18:08usually going to be able to limit those
- 18:10capabilities. Now, what I'm describing
- 18:11here is a pretty common structure, but
- 18:14you could see variations. For example,
- 18:16you could probably make these API keys
- 18:19be signed and not require database
- 18:22access, or maybe you get a key per user
- 18:25instead of a user being able to create
- 18:27multiple keys. There are a lot of
- 18:29variations, so what I would recommend is
- 18:31just taking a look at some of the
- 18:32different API platforms out there. Maybe
- 18:35take a look at some popular social media
- 18:37networks, look at some payment
- 18:39processors. So, maybe look at Facebook
- 18:41and Stripe and see how they run their
- 18:43APIs. Ultimately, both user auth and API
- 18:47auth are trying to achieve the same
- 18:49thing,
- 18:50just slightly different in approach. So,
- 18:52this is how I decided to group them, but
- 18:56you might see slight variations or some
- 18:58crossover in concepts. So, for example,
- 19:01with JWTs and user auth,
- 19:07you can still check the database
- 19:10if you needed to keep track of invalid
- 19:11keys or any other things related to
- 19:15specific keys, you can store that all in
- 19:17a database. So, it's not the case that
- 19:18if you're using JWTs, you never use a
- 19:20database. Now, one big difference
- 19:22between JSON web tokens and other tokens
- 19:25is that JWTs
- 19:27can be decoded,
- 19:31but other keys are typically opaque.
- 19:36So, what that means is if you have an
- 19:38API key, it's going to look like a bunch
- 19:40of random characters. Those characters,
- 19:42you're not going to be able to interpret
- 19:44those in some way unless they sometimes
- 19:47will put something at the beginning. So,
- 19:49it might have like the service
- 19:52and then like a hyphen and then all the
- 19:54random characters. But, the the main
- 19:56point here is that you're not going to
- 19:58have information in that token that you
- 20:01could see. Whereas with a JSON web
- 20:03token, you can decode this
- 20:06on the client side even, not just server
- 20:08side,
- 20:09and see the payload.
- 20:13So, you might see something like this.
- 20:18And obviously, I'm going to be an admin.
- 20:21This is just encoded, and encoding is
- 20:24different than encrypted. The
- 20:26information is still retrievable, it's
- 20:28just not immediately visible unless you
- 20:31decode that information. So, if you want
- 20:33to know more about that, you can read
- 20:35about base 64 encoding. Whereas API
- 20:37keys, you're not going to be able to
- 20:39just decode those and read the
- 20:40information.
- 20:42And it's probably more likely that the
- 20:45API platform will access the database to
- 20:49get additional information about your
- 20:51permissions for that API key
- 20:53or to check if the API key is valid.
- 20:56Since you're often given the capability
- 20:58to immediately invalidate tokens, we
- 21:01basically need a way to know if a token
- 21:03is valid. And for that, you often have a
- 21:06database for invalid API keys.
- 21:10Now, let's talk about how API keys and
- 21:12JSON Web Tokens are actually passed.
- 21:15They are typically included in the web
- 21:17request in an authorization header.
- 21:22And then the next thing here is the
- 21:23authorization scheme, which typically is
- 21:26just going to be bearer.
- 21:30And then the actual key.
- 21:33So, let's put some random numbers here.
- 21:38So, this is what that header would look
- 21:40like with that request. If you're doing
- 21:42it from an application,
- 21:45you can often include headers with your
- 21:48requests.
- 21:50So, you'll need to understand the syntax
- 21:52to include an authorization header,
- 21:55which should be pretty simple. Just look
- 21:56it up.
- 21:57Or if you're doing it from some other
- 21:59client, such as Postman,
- 22:02there's probably some way to include any
- 22:04authorization headers from the menu or
- 22:07wherever it may be. So, whatever tool
- 22:08you're using, you know, maybe it's
- 22:10Incode, maybe it's some tool, or maybe
- 22:12you're making just like curl requests or
- 22:14whatever it may be, you need to include
- 22:16this as a header. Typically, it's going
- 22:18to be
- 22:19with the bearer scheme authorization
- 22:21header.
- 22:22>> [snorts]
- 22:23>> But, you might see custom stuff, so you
- 22:24might see something like X
- 22:30API key.
- 22:32This is a custom header. So, the client
- 22:34can include arbitrary headers, and then
- 22:36the server would just need to know to
- 22:37check for that. So, you may see that as
- 22:39well, but I would say this structure is
- 22:42the most common. So, that's your
- 22:43introduction to authentication with JSON
- 22:46Web Tokens and API keys. We have a lot
- 22:48more topics for this, so stay tuned.
- 22:51First thing you should know is a better
- 22:53understanding of JSON Web Tokens,
- 22:55because that will deal with access
- 22:56tokens and refresh tokens, so you'll
- 22:58want to understand that. You should also
- 23:00understand social logins and OAuth.
- 23:02There are topics like throttling and
- 23:04rate limiting and a variety of other
- 23:06things dealing with APIs and user
- 23:09logins. So, stay tuned for the upcoming
- 23:11content. If you have any questions or
- 23:13different topics you'd like to see me
- 23:14cover, leave a comment below. Be sure to
- 23:16check out the notes, the courses, and
- 23:18anything else down in the description
- 23:20and I will see you in the next lesson.
- 23:22Thank you.
About this transcript
This page contains the full transcript of Intro to Authentication - User and API Auth by Caleb Curry, generated from the public captions YouTube serves with the video. The transcript has 3,628 words across 570 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.