OAuth 2.0 is something I believe most of us can get confused with (I still do at times).
Sometimes people get confused with OAuth 2.0 and OIDC (OpenID Connect) as well.
OAuth 2.0 is a standard protocol for Authorization. But in the past, different major companies have started using OAuth 2.0 for Authentication by extending it in their own ways. Then some genius set of people thought to make it a standard, thus they came up with OIDC (OpenID Connect) specification which is to be used for Authentication.
So the key concept is that OIDC is an additional identity layer built on top of the OAuth 2.0 protocol.
Anyway, in this post, thought of writing a post about the most widely used OAuth flow: Authorization Code, and have it compared with the Implicit flow. (Implicit flow is considered legacy now and its use is recommended to replace with Authorization Code with PKCE which is a slight variation of Authorization Code)
For this post, I am considering Microsoft Identity Platform as the Identity Provider, this can be Auth0, Okta, Google, or even your own Identity Server implementation. The URLs and scopes are simplified for brevity.
Authorization Code Flow
Authorization Code Flow |
GET: https://login.microsoftonline.com/<tenant>/oauth2/v2.0/authorize? &client_id=myapplication-client &response_type=code &redirect_uri=https://myapplication.com/callback &scope=Calendars.Read &state=some-state
https://myapplication.com/callback?state=some-state&code=eyJraWQiOiIzcG...
POST: https://login.microsoftonline.com/<tenant>/oauth2/v2.0/token content type application/x-www-form-urlencoded grant_type=authorization_code client_id=myapplication-client client_secret=<client_secret>
code=<code_received_from_authorize_endpoint>
{ "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI...", "token_type": "Bearer", "not_before": 1664733775, "expires_in": 3600, "expires_on": 1664737375, "scope": "Calendars.Read" }
A couple of important things to note here. In the picture above, note that the steps from #1 to #3 is happening on the Front channel, while the steps from #4 to #6 happen on the Back channel. The reason is, in step #3, a client_secret is involved and we shouldn't be exposing that to the outside.
Note: It is now recommended to use the Authorization Code with PKCE flow to provide better security even though a client is using a client_secret.
Implicit Flow (Legacy)
Hope this helps.
Happy Coding.
No comments:
Post a Comment