Authorization code
Get a token with Authorization code
Authorization code authentication is the Oauth2 flow used primarily for users that want access to login in a web or app. Users must login through agent type clients with authorization code grant type. This authentication flow is the recommended because the client does not need to manage the resource owner’s (end user) credentials. Instead, it is the authorization server itself the one and only responsible of managing those credentials.
Authorization code Authentication flow
This graph shows the different flows involved in obtaining the access token.
Warning
To use this authentication method, an authorization code client must be created. Clients are provided by the MasStack
team after carefully auditing each use case, so please contact your MasStack
representative to obtain your client. The minimum requirements to add in a Jira ticket are:
- Title: Request to get the token in
- Environment: Create one ticket per environmnent
- Redirect_uri: Url where the response will be redirected with the “code”, it is recommended https://{{yourdomain}}/callback
- Grant-type: In this case is “authorization_code”
- If it is for custumers or for login against google
How to obtain an access_token
Step 1
Obtain the authorization code client
Authorization Code Clients can be provided by the MasStack Owners. This team must be contacted in order to get client_id and client_secret.
When a client is created, Authn will provide:
|
|
It is a good practice to store this parameters in different variables in order to have quickly access to this information because it will be needed in next steps.
Step 2
Request authorization
Name | Description |
---|---|
authn_host | is the authn host for the desired environment. |
client_id | is the id which you obtain from Authn’s team. |
redirect_uri | is the HTTP endpoint on your server that will receive the response from Authn. The value must exactly match one of the authorized redirect URIs configured for Oauth 2.0. client. If this value doesn’t match an authorized URI, the request will return a 401 response error. |
state | is the value commonly used to avoid CSRF attacks. This value should contain an anti-forgery unique session token, as well as any other information needed to recover the context when the user returns to your application. e.g. the starting URL. Commonly used to avoid CSRF attacks. |
login_hint | (Optional) (Only support Google and Azure AD provider): If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the approprieate multi-login session.Set the parameter value to an email address or sub-identifier, which is equivalent to the user’s Google ID or Azure Active Directory. |
groups_hint | (Optional)(Only support Google provider): The substring is equivalent to the groups in the provider (only support Google provider). If a groups_hint is not provided and the user is currently logged in, in the next request of token, the access_token will not contain claim groups. |
access_type | (Optional) Allowed values are online and offline . If an access token is requested, the client does not receive a refresh token unless value offline is specified. |
code_challenge | (Optional)(Recommended): Specifies an encoded code_verifier that will be used as a server-side challenge during authorization code exchange. This string helps mitigating against the threat usually through the use of Proof Key for Code Exchange (PKCE). |
code_challenge_method | (Optional)(Recommended): Specifies what method has been used to encode a code_verifier that will be used during authorization code exchange. This parameter must be used with the code_challenge parameter. The only supported values are S256 or plain . The value of code_challenge_method is plain by default if is not specified in a request that include a code_challenge. |
|
|
Step 3
Login
After authorize request Authn will redirect to the authentication provider’s default login page (p.e.:Google login page). Users will put their credentials in it and log in.
Step 4
Redirect url
If credentials are valid, Authn will redirect to the indicated redirect_uri
with the autogenerated query param code
and the state
:
https://{{redirect_uri}}?code=TgradaHhSEOIhe2pEAzS9A&state=DA05F7AF-E07E-413E-B6DA-23DE26600592
Step 5
Request access token
To get an access token a POST request with code
obtained is needed. There are two options to create this request:
Option 1: With PKCE (recommended)
Name | Description |
---|---|
grant_type | parameter defines the OAuth2 flow that is going to be used; in this case, authorization_code |
code | is the autogenerated queryparam that Authn returned in the redirect url in step 3 |
client_id | is the id which you obtain from Authn’s team. |
redirect_uri | is the HTTP endpoint on your server that will receive the response from Authn. The value must exactly match one of the authorized redirect URIs configured for Oauth 2.0. client. If this value doesn’t match an authorized URI, the request will return a 401 response error. |
code_verifier | (Optional) With the code_challenge_method=plain or blank the value is the same as the previous parameter used in code_challenge . With the code_challenge_method=S256 the value is the original crypto random string. This value is used to confirm that the client that generated it is the same as the one requesting the access_token. |
|
|
Option 2: With client_secret
Name | Description |
---|---|
grant_type | is the client grant type for token request flow. In this case, is always authorization_code |
code | is the autogenerated queryparam that Authn returned in the redirect url in step 3 |
client_id | is the id which you obtain from Authn’s team. |
redirect_uri | is the HTTP endpoint on your server that will receive the response from Authn. The value must exactly match one of the authorized redirect URIs configured for Oauth 2.0. client. If this value doesn’t match an authorized URI, the request will return a 401 response error. |
|
|
If everything worked as expected, you should get this response:
Name | Description |
---|---|
access_token | is a signed JWT used by a user to authenticate himself against Istio’s service mesh. |
refresh_token | is a signed JWT whose main goal is to obtain a new access_token without repeating customer authentication once the user has an active session. |
expires_in | is the time that the access_token will be valid. Once it expires, the access_token won’t be valid and the user won’t be able to authenticate with it. |
token_type | defines the type of the access_token that has been generated. This will normally be used in the Authorization header to indicate which kind of authentication has the request. |
|
|
Access token expired with the user in session
In this graph can be seen the sequence followed when an access token expires and the user remains in session. When this occurs is not needed that the user put its credentials again.
If a request to MasStack api fails due to access token expiration, the flow to get a new access token will start again. Here the difference with first token obtention is that when authorize request is done (Step 2), the user session is validated and Authn will redirect to redirect_uri
(Step 4). Next step will be to request the access token (Step 5).
Access token expired without the user in session
In this graph can be seen the sequence followed to the obtention of a new access token when it expires and the user is not in session. User is required to login again.
If a request to MasStack api fails due to access token expiration, the flow to get a new access token will start again. After authorize request is done (Step 2), user’s session validation fails and Authn will redirect to the provider’s login page to put credentials again (Step 3). Flow to get the access token continues as if it were obtained for first time.
Logout
For logins that have been made through the authorization-code oauth flow, logouts can be made through a call to:
https://{{authn_host}}/oauth/logout?client_id={{client_id}}&continue={{url}}&scope=all
Name | Description | Mandatory |
---|---|---|
client_id | is the identifier of the client representing the app. | yes |
continue | is the url to be redirected to after logout. | no |
scope | if the value is all, it will delete all the user’s sessions and refresh_tokens that it has. | no |