Search code examples
securityoauth-2.0jwtmicroservicesaccess-token

How safe is an acess token?


I have been reading about OAuth 2.0 Authorization Code flow to protect APIs in microservices architectures but I dont understand how an access token issued by the Auth Server is supposed to protect an API hosted in another server.

Is that same access token also kept in the API and when the client tries to access it with the access token issued by the Auth Server, the API checks if contains it? If so, does that mean that the access token is sent both to client and the protected API in the authentication process?

I hope to have explained my problem well. Thanks in advance.


Solution

  • Access token can be understood as an passport that government issue to the citizen based on proof of identity. When you take it to another country, they look at the document and trust it because they trust the country and you because you are the holder of that document with you details. They trust the fact that passport cannot be fiddled with and allow you entry

    Now for access token, in very simple terms, authorization server verifies the user. Once verified it issues the user a JWT token (Access Token). This token is signed with private key. It has your details and is encoded along with signature. Now you can take this token to any third party who has got the public key and trust the authorization server. Now when you share the access token with this third party, it use public key to verify the token and check for expiry. If valid it allows you in. So API doesn't really need to talk to auth server or keep any details about the token. All its needs is a public key to decode the token.

    Now there are two important things. One if you ever let loose your access token, or some one who is not intended to get hold of your token gets it, he can do what ever he wants and auth server will not be able to do much. However as you see this approach reduces the chattiness of the systems specially microservices.

    So to address this we limit the expiry of access token. Like passport, it comes with expiry. Shorter you keep it,user have to go and get the token refreshed with auth server. Every time he does so, auth server gets a change to verify creds and other details. If they do not match access token will not be refreshed.