TL;DR;You suck! You just want the code. However, at the end of the article, you will find a questionnaire to help you choosing the flow you need. This article doesn’t want to be the final guide to OAuth 2, but an introduction to the flows that this framework is composed of. You’ll have a look at the four basic flows and some practical scenarios, to understand the involved actors and the detailed behaviors. The goal is to be able to choose a flow that best fits your needs. To make it as easy as possible, the experts will forgive me, we can say that there are four different versions. Or, more correctly, four different flows. OAuth 2 is the totality of these flows. It’s not mandatory to implement them all, but only the ones that you need. The goal remains always the same: to obtain an access_token and use it to access protected resources. The four modalities are:
Authorization Code GrantThis is the most complete and complex flow. The login process is divided into two phases, which ensure greater security. The involved actors
The flowLet’s see in detail the Authorization code grant flow. 1. The user wants to log inThe classic scenario for this flow is played in the user browser. The user will click the “Login with OAuth” button and the client will generate and send a login request to the Authorization Server. 2. The user is redirected to the Authorization ServerThe client generates a login request for the Authorization Server. The request will be sent in the form of an GET /token Location: https://the-authorization-server/token?client_id=[the_client_id]&redirect_uri=[a redirect uri]&response_type=code&scope=[list of scopes]&state=[some client parameter] The parameters are the following:
3. Request validationL’Authorization Server must validate all the request parameters:
To execute these validations the Authorization server must have previously registered all the client that will access. The onboarding and the maintenance of clients are out of the OAuth scope. 4. Login formThe Authorization server shows the login form, and the user has to insert the username and password, to make the login (step 5 in the picture). After validating the data (step 6 in the picture) the Authorization server asks the user the consents specified in the scopes. The user will decide to grant, or not, one or more of the scopes. In a contrary case, the client will act in a different manner. For example, it could limit or inhibit the use of the application. 7. Redirect to the client with the authorization_codeThe Authorization Server generates an authorization code (authorization_code) and sends it to the client, to the URI specified in the request. All these operations have happened client side, on a browser (or a mobile app) for the next steps the client must use a back-end. This, in my opinion, is one of the aspects to consider when you choose which OAuth 2 flow best fits your needs. 8 and 9. Authorization code validationNow the client has to call the Authorization Server to validate the received code. To do this operation it will pass:
The Authorization Server executes all the above validations, checking the authorization_code is intact, not altered, not expired and issued for that specific client. 10. The client receives the access_tokenThe Authorization server creates an access_token and returns it to the client. There are many types of token, they have an expiration date and they can be refreshed. Usually, along with the token, are returned some more information:
11. The client uses the access_token.Now that the client has obtained the access_token, it can use it to authenticate itself against the Resource Server. The Resource Server is a generic component and could serve many different types of resources: REST API, SOAP Services, Web pages, etc… Based on the resource type, the method to send the access_token can change. The most common way to send it against REST API is to use the HTTP header: Authorization, concatenating the token_type with access_token. Example: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ 12 and 13. Token validationWhen the Resource Server receives a request must check the token presence and integrity. The token validation, especially based on its type, can be done in many ways. In some cases the Resource Server can validate it, in some other must call the Authorization server. In some scenarios is possible that these two components are the same application, this is transparent and not important for the client. If you need to implement an OAuth server the choice on how to validate the token will vary based on your architecture and on the token type you’ll decide to use. 14 and 15. Access and display the protected resources.Once the Resource server validates the token successfully, it will return the resource to the client, and this can display it to the user. Implicit Grant FlowThis flow is very similar to the Authentication Code one, but the access_token is immediately returned to the client after the user login, in an implicit way. You use this flow in Single Page Applications and pure Javascript applications, where is not possible to keep the client_secret secret. The actors involved are the same as the previous flow. The first difference is that during the initial redirect to the Authorization server, the value of the parameter response_type is In this way, you’ll have one step fewer to execute for the client, but also less secure, because the whole flow is executed on the browser (or user-agent generally speaking), including the token. In the authorization code flow, the token comes from a server to server call, so it is more difficult to intercept or manipulate it. Client CredentialThe main difference between the flows mentioned above is that in this one there is no user interaction. The application executes the authentication request directly. This means the application cannot do any action on behalf of the user, but just on behalf of itself. This flow doesn’t take place on the browser but on a server. There are not redirect, but an
The response of this request is the access_token, that can be spent on the Resource server. Password Grant flowThe main concept, in my opinion, of this flow, is the users enter their username and password in the client application and not in the Authorization Server. A noticeable thing is the credentials “belong” to the Authorization Server, not to the client, This fact implies that the users must fully trust the client application, to insert their Authorization Server credentials. To better understand this, imagine you make an application that logs in users with Facebook, but the users insert their (Facebook) username and password on that application. Would you, as a user, do this? You must be sure the application is absolutely trusted and, for example, does not save or modify your Facebook password. if I remember correctly, Spotify has this feature, probably using this OAuth 2 flow. I remember I did a login using Facebook credentials directly on Spotify. Once the username and the user password are collected on the front-end, they are sent to the application back-end, that executes an
The Authorization server, after validating all the credentials returns the l’access_token. For this flow, you need both a front-end and a back-end. Implementation of OAuth 2To implement OAuth 2 you have to develop:
Client sideYou must remember, again, that to implement some of the OAuth flows you need to use the client password, and this must be kept in a secure place. So you may need a server. This means you cannot implement some flows if you don’t have a server. The applications that don’t need a server ( to be served) are Single Page Application and the application that runs on a user device: Mobile and Desktop app. You can bypass this problem by creating a dedicated server or back-end to do the OAuth server calls, or by using some crypt mechanisms. Server sideThere are many frameworks in many languages that implement OAuth. This makes life a little easier, for the part of exposing the needed endpoints; and especially for all the validations that are needed. But rarely I found “turnkey” solutions. One hard thing to do is the onboarding and the management platform for the clients. Just think about the many existing You also have to pay attention to the scopes and permissions, whose management is everything but banal, especially in the resource protection phase. Another point of interest is the token management. Use of a database is required, to guarantee the complete lifecycle:
Which flow should I choose?The choice of the right flow depends on many factors, answer these questions to have an idea of which best fits you. Are you able to maintain the client_secret in a safe and secret place?
Is your application a Web one?
Do your users trust you enough to insert their credentials (username and password) on your site? For example: Would users insert their Facebook password on your site?
ConclusionI hope the world of OAuth is more clear now that you read this article, my goal was to explore the basics of this protocol. If you have any comment or proposition you are welcome to leave them down here. Congratulations!! ?? You made it to the end. And if you liked ??this article, hit that clap button below ??. It means a lot to me and it helps other people see the story. |
|