OAUTH2 Resource Server Token Validation#
How Does Resource Server Validate Token?#
- In the OAUTH2 Flow, we have discussed about main flows for applying OAUTH2 and we can know that everytime the
Client
want to access theResource Server
, it must provide the token which is received from theAuth Server
to theResource Server
. So then theResource Server
can validate and response data to theClient
. - Usually, we will have three main ways to apply for
Resource Server
to validate access token.
Direct Api Call#
- As you can see in the image above, every time the
Resource Server
receives the access token from theClient
, It has to validate that token with theAuth Server
by calling theAuth Server
api for token validation. Because these are two different servers or two different applications handling different responsibilities. Thus, theResource Server
is not aware of the access tokens issued by theAuth Server
to all the clients. So theResource Server
have to send the access token toAuth Server
for validation.
Common Database#
- From the image above, we can see the
Auth Server
and theResource Server
is connecting to the same database. So whenever theAuth Server
issues an access token, it can write into the database and theResource Server
can connect to this database to get the access token and validate it with the one received from theClient
. - In this scenarios, the
Resource Server
doesn't have to rely on the network interact with theAuth Server
.
Token Signature#
- In the Token Authentication With JWT, we know that the signature of the tokens can be validated by having some secret keys maintained by the both parties to make sure that no one is tampered.
- Following the same approach, the
Auth Server
can generate an access token by using some encryption algorithm with the secret. Then, whenever theClient
sends the same access token to theResource Server
, so theResource Server
doesn't have to make a call toAuth Server
or doesn't have to look into the database. It can simply check the signature or hash value of the token generated with the encryption algorithm that it maintains to understand the token is valid or not.
Summary#
- Every approach has it's own pros and cons. In the
Token Signature
approach which is used for validating the token signature, so you will never depend on the network to callAuth Server
or database. But if we have a clear and real time checking withAuth Server
, you can always rely on theDirect Api Call
approach. But if you don't want to overburden theAuth Server
network so you can maintain aCommon Database
which can be leveraged by bothAuth Server
andResource Server
.