The OAuth 2.0 Grant Type authorization_code
The current OAuth 2.0 specification provides different grant types for different tasks. One of the most frequently used grant types is “Authorization Code.” To use the data storage service Dropbox, a new user first has to register there. Alternatively, Dropbox also offers the option of signing in with an existing Google account. If this option is used, the existing user data from the user’s own Google account is transferred to Dropbox as authorization, and the Dropbox account is activated. Because this data transfer should be especially well secured, all parties involved have to take various measures. The authorization (authorization flow) also has to follow a defined scheme so that permissions can be issued.
The Authorization Process in Detail
A closer look at the multistep authorization process shows that several important steps are necessary between the parties involved. To begin with, put simply, the OAuth2 grant “authorization_code” involves three parties in total.
- A user who wants to sign in to a service provider, an app, or a web service and use its services.
- A client (a service provider in the form of a website or web application) that wants to offer its visitors different services.
- A provider with which the user has already created an account and where the user’s complete data is stored.
A client can have a wide range of requirements for the use of its services. This has to be taken into account during sign-in and the authorization that follows. To stay with the Dropbox example, the scenario for a user, presented in simple terms for now, looks as follows.
- The user opens the Dropbox website and finds a sign-in or registration dialog there. Let us assume here that the user is not yet registered with Dropbox. To complete the login quickly and easily, our user can sign up with Dropbox through the “Sign up with Google” function.
- As soon as the user clicks the “Sign in with Google” button, a secured login window from Google as the provider opens. Here the user can sign in with their login credentials. After entering email and password, the user is asked whether they want to use and authorize the Dropbox services. This dialog shows in detail which permissions (personal data, etc.) Dropbox would like to have. If you as the user now grant Dropbox permission, you are redirected and immediately signed in to Dropbox.
Protocol and Process Steps for the Client
From the user’s perspective, this sign-in procedure leads very quickly to access to a new service provider client. It is just as convenient that a single valid user login, for example with Facebook, is enough to sign in to all other clients that support the OAuth2 procedure. For the clients, however, the procedure takes somewhat more effort in order to satisfy all security aspects. The clients go through the following process steps to register a user from a third-party provider.
- The client has to redirect the user to their provider through a login or redirect dialog. In addition, the desired permission settings have to be supplied through various parameters such as client_id, redirect_uri, state, and response_mode for sufficient authorization.
- As soon as the user has logged in to the provider with their login credentials, they are asked whether they accept the permissions the client requests.
- If the user accepts the permissions, they are redirected back to the client at the URI provided.
- With the data received, the client now sends a further request to the authorization server. Various parameters such as grant_type, client_id, client_secret, redirect_uri, and code are transmitted with it in order to obtain an unambiguous authorization. If the result is positive, the client receives an access token from the server. The user is then admitted at the client and its services are enabled.
- With the valid token, the client can now send further requests to the provider, for example to retrieve the complete user data or to exchange changes to the user account. As a rule, this is also done in order to maintain a proper user account at the client.
Security Comes First
Although OAuth 2.0 has several far-reaching permission levels, not everything is perfectly secured by default. As soon as an attacker gains possession of an access token, they can under certain circumstances access the user’s resources and undermine the authorization procedure. For this reason, the client should populate the optional “state” parameter with a CSRF token when it redirects the user to the authorization server. This token can be stored in the user session as an additional ID. The user can thus be clearly identified on return, and an attack through third-party access is prevented. Ideally, the parameter should actually be defined as “required.”