2020-10-23 11:08:42 -04:00
---
type: reference, howto
stage: Manage
group: Access
2020-12-22 16:10:06 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#designated-technical-writers
2020-10-23 11:08:42 -04:00
---
2020-11-16 16:09:02 -05:00
2016-09-28 04:41:39 -04:00
# GitLab as an OAuth2 provider
2015-01-12 18:30:34 -05:00
2019-01-13 22:46:01 -05:00
This document covers using the [OAuth2 ](https://oauth.net/2/ ) protocol to allow
other services to access GitLab resources on user's behalf.
2015-01-12 18:30:34 -05:00
2019-01-13 22:46:01 -05:00
If you want GitLab to be an OAuth authentication service provider to sign into
2020-05-04 11:09:38 -04:00
other services, see the [OAuth2 authentication service provider ](../integration/oauth_provider.md )
2019-01-13 22:46:01 -05:00
documentation. This functionality is based on the
[doorkeeper Ruby gem ](https://github.com/doorkeeper-gem/doorkeeper ).
2015-02-18 23:49:19 -05:00
2019-01-13 22:46:01 -05:00
## Supported OAuth2 flows
2015-01-12 18:30:34 -05:00
2019-01-13 22:46:01 -05:00
GitLab currently supports the following authorization flows:
2015-01-12 18:30:34 -05:00
2021-01-07 16:10:18 -05:00
- **Authorization code with [Proof Key for Code Exchange (PKCE) ](https://tools.ietf.org/html/rfc7636 ):**
2020-12-22 16:10:06 -05:00
Most secure. Without PKCE, you'd have to include client secrets on mobile clients,
2021-01-22 19:08:46 -05:00
and is recommended for both client and server apps.
2021-01-07 16:10:18 -05:00
- **Authorization code:** Secure and common flow. Recommended option for secure
2020-12-22 16:10:06 -05:00
server-side apps.
2021-01-07 16:10:18 -05:00
- **Implicit grant:** Originally designed for user-agent only apps, such as
2020-12-22 16:10:06 -05:00
single page web apps running on GitLab Pages).
The [IETF ](https://tools.ietf.org/html/draft-ietf-oauth-security-topics-09#section-2.1.2 )
recommends against Implicit grant flow.
- **Resource owner password credentials:** To be used **only** for securely
hosted, first-party services. GitLab recommends against use of this flow.
The draft specification for [OAuth 2.1 ](https://oauth.net/2.1/ ) specifically omits both the
Implicit grant and Resource Owner Password Credentials flows.
it will be deprecated in the next OAuth specification version.
2015-01-12 18:30:34 -05:00
2019-01-13 22:46:01 -05:00
Refer to the [OAuth RFC ](https://tools.ietf.org/html/rfc6749 ) to find out
how all those flows work and pick the right one for your use case.
2016-08-04 09:25:37 -04:00
2020-12-22 16:10:06 -05:00
Both **authorization code** (with or without PKCE) and **implicit grant** flows require `application` to be
2019-01-13 22:46:01 -05:00
registered first via the `/profile/applications` page in your user's account.
During registration, by enabling proper scopes, you can limit the range of
resources which the `application` can access. Upon creation, you'll obtain the
`application` credentials: _Application ID_ and _Client Secret_ - **keep them secure** .
2015-01-12 18:30:34 -05:00
2020-10-23 11:08:42 -04:00
### Prevent CSRF attacks
To [protect redirect-based flows ](https://tools.ietf.org/id/draft-ietf-oauth-security-topics-13.html#rec_redirect ),
the OAuth specification recommends the use of "One-time use CSRF tokens carried in the state
parameter, which are securely bound to the user agent", with each request to the
2020-11-16 16:09:02 -05:00
`/oauth/authorize` endpoint. This can prevent
2020-10-23 11:08:42 -04:00
[CSRF attacks ](https://wiki.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF )).
### Use HTTPS in production
For production, please use HTTPS for your `redirect_uri` .
For development, GitLab allows insecure HTTP redirect URIs.
As OAuth2 bases its security entirely on the transport layer, you should not use unprotected
URIs. For more information, see the [OAuth 2.0 RFC ](https://tools.ietf.org/html/rfc6749#section-3.1.2.1 )
2020-11-16 16:09:02 -05:00
and the [OAuth 2.0 Threat Model RFC ](https://tools.ietf.org/html/rfc6819#section-4.4.2.1 ).
2020-10-23 11:08:42 -04:00
These factors are particularly important when using the
[Implicit grant flow ](#implicit-grant-flow ), where actual credentials are included in the `redirect_uri` .
2016-08-04 09:25:37 -04:00
2019-01-13 22:46:01 -05:00
In the following sections you will find detailed instructions on how to obtain
authorization with each flow.
2015-01-12 18:30:34 -05:00
2020-12-22 16:10:06 -05:00
### Authorization code with Proof Key for Code Exchange (PKCE)
The [PKCE RFC ](https://tools.ietf.org/html/rfc7636#section-1.1 ) includes a
detailed flow description, from authorization request through access token.
The following steps describe our implementation of the flow.
The Authorization code with PKCE flow, PKCE for short, makes it possible to securely perform
the OAuth exchange of client credentials for access tokens on public clients.
Before starting the flow, generate the `STATE` , the `CODE_VERIFIER` and the `CODE_CHALLENGE` .
- The `STATE` a value that can't be predicted used by the client to maintain
state between the request and callback. It should also be used as a CSRF token.
- The `CODE_VERIFIER` is a random string, between 43 and 128 characters in length,
which use the characters `A-Z` , `a-z` , `0-9` , `-` , `.` , `_` , and `~` .
- The `CODE_CHALLENGE` is an URL-safe base64-encoded string of the SHA256 hash of the
`CODE_VERIFIER`
- In Ruby, you can set that up with `Base64.urlsafe_encode64(Digest::SHA256.digest(CODE_VERIFIER))` .
1. Request authorization code. To do that, you should redirect the user to the
`/oauth/authorize` page with the following query parameters:
```plaintext
2021-01-21 22:09:04 -05:00
https://gitlab.example.com/oauth/authorize?client_id=APP_ID& redirect_uri=REDIRECT_URI& response_type=code& state=STATE& scope=REQUESTED_SCOPES& code_challenge=CODE_CHALLENGE& code_challenge_method=S256
2020-12-22 16:10:06 -05:00
```
This page asks the user to approve the request from the app to access their
account based on the scopes specified in `REQUESTED_SCOPES` . The user is then
redirected back to the specified `REDIRECT_URI` . The [scope parameter ](https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes#requesting-particular-scopes )
is a space separated list of scopes associated with the user.
For example,`scope=read_user+profile` requests the `read_user` and `profile` scopes.
The redirect includes the authorization `code` , for example:
```plaintext
2021-01-21 22:09:04 -05:00
https://example.com/oauth/redirect?code=1234567890& state=STATE
2020-12-22 16:10:06 -05:00
```
1. With the authorization `code` returned from the previous request (denoted as
`RETURNED_CODE` in the following example), you can request an `access_token` , with
any HTTP client. The following example uses Ruby's `rest-client` :
```ruby
parameters = 'client_id=APP_ID& client_secret=APP_SECRET& code=RETURNED_CODE& grant_type=authorization_code& redirect_uri=REDIRECT_URI& code_verifier=CODE_VERIFIER'
RestClient.post 'https://gitlab.example.com/oauth/token', parameters
```
Example response:
```json
{
"access_token": "de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54",
"token_type": "bearer",
"expires_in": 7200,
"refresh_token": "8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1",
"created_at": 1607635748
}
```
NOTE:
The `redirect_uri` must match the `redirect_uri` used in the original
authorization request.
You can now make requests to the API with the access token.
### Authorization code flow
2015-01-12 18:30:34 -05:00
2020-12-04 16:09:29 -05:00
NOTE:
2019-01-13 22:46:01 -05:00
Check the [RFC spec ](https://tools.ietf.org/html/rfc6749#section-4.1 ) for a
detailed flow description.
2016-08-04 09:25:37 -04:00
2020-12-22 16:10:06 -05:00
The authorization code flow is essentially the same as
[authorization code flow with PKCE ](#authorization-code-with-proof-key-for-code-exchange-pkce ),
2015-01-12 18:30:34 -05:00
2021-01-21 22:09:04 -05:00
Before starting the flow, generate the `STATE` . It is a value that can't be predicted
used by the client to maintain state between the request and callback. It should also
be used as a CSRF token.
2019-01-13 22:46:01 -05:00
1. Request authorization code. To do that, you should redirect the user to the
2021-01-21 22:09:04 -05:00
`/oauth/authorize` page with the following query parameters:
2015-01-12 18:30:34 -05:00
2020-03-05 22:08:08 -05:00
```plaintext
2020-12-22 16:10:06 -05:00
https://gitlab.example.com/oauth/authorize?client_id=APP_ID& redirect_uri=REDIRECT_URI& response_type=code& state=STATE& scope=REQUESTED_SCOPES
2019-01-13 22:46:01 -05:00
```
2016-08-04 09:25:37 -04:00
2021-01-21 22:09:04 -05:00
This page asks the user to approve the request from the app to access their
account based on the scopes specified in `REQUESTED_SCOPES` . The user is then
redirected back to the specified `REDIRECT_URI` . The [scope parameter ](https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes#requesting-particular-scopes )
is a space separated list of scopes associated with the user.
For example,`scope=read_user+profile` requests the `read_user` and `profile` scopes.
The redirect includes the authorization `code` , for example:
2016-08-04 09:25:37 -04:00
2020-03-05 22:08:08 -05:00
```plaintext
2020-12-22 16:10:06 -05:00
https://example.com/oauth/redirect?code=1234567890& state=STATE
2019-01-13 22:46:01 -05:00
```
2016-08-04 09:25:37 -04:00
2021-01-21 22:09:04 -05:00
1. With the authorization `code` returned from the previous request (shown as
`RETURNED_CODE` in the following example), you can request an `access_token` , with
any HTTP client. The following example uses Ruby's `rest-client` :
2016-08-04 09:25:37 -04:00
2019-01-13 22:46:01 -05:00
```ruby
parameters = 'client_id=APP_ID& client_secret=APP_SECRET& code=RETURNED_CODE& grant_type=authorization_code& redirect_uri=REDIRECT_URI'
2020-08-21 14:10:24 -04:00
RestClient.post 'https://gitlab.example.com/oauth/token', parameters
2019-01-13 22:46:01 -05:00
```
2015-01-12 18:30:34 -05:00
2019-01-13 22:46:01 -05:00
Example response:
2015-01-12 18:30:34 -05:00
2019-01-13 22:46:01 -05:00
```json
{
"access_token": "de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54",
"token_type": "bearer",
"expires_in": 7200,
2020-12-22 16:10:06 -05:00
"refresh_token": "8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1",
"created_at": 1607635748
2019-01-13 22:46:01 -05:00
}
```
2015-01-12 18:30:34 -05:00
2020-12-04 16:09:29 -05:00
NOTE:
2019-01-13 22:46:01 -05:00
The `redirect_uri` must match the `redirect_uri` used in the original
authorization request.
2015-01-12 18:30:34 -05:00
You can now make requests to the API with the access token returned.
2019-01-13 22:46:01 -05:00
### Implicit grant flow
2015-01-12 18:30:34 -05:00
2020-12-04 16:09:29 -05:00
NOTE:
2021-01-07 16:10:18 -05:00
For a detailed flow diagram, see the [RFC specification ](https://tools.ietf.org/html/rfc6749#section-4.2 ).
2018-11-13 01:07:16 -05:00
2020-12-04 16:09:29 -05:00
WARNING:
2020-12-22 16:10:06 -05:00
The Implicit grant flow is inherently insecure. The IETF plans to remove it in
[OAuth 2.1 ](https://oauth.net/2.1/ ).
We recommend that you use [Authorization code with PKCE ](#authorization-code-with-proof-key-for-code-exchange-pkce ) instead. If you choose to use Implicit flow, be sure to verify the
`application id` (or `client_id` ) associated with the access token before granting
access to the data, as described in [Retrieving the token information ](#retrieving-the-token-information )).
Unlike the authorization code flow, the client receives an `access token`
immediately as a result of the authorization request. The flow does not use
the client secret or the authorization code because all of the application code
and storage is easily accessible on client browsers and mobile devices.
2017-07-07 10:52:45 -04:00
2019-01-13 22:46:01 -05:00
To request the access token, you should redirect the user to the
`/oauth/authorize` endpoint using `token` response type:
2015-01-12 18:30:34 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2019-07-26 20:28:19 -04:00
https://gitlab.example.com/oauth/authorize?client_id=APP_ID& redirect_uri=REDIRECT_URI& response_type=token& state=YOUR_UNIQUE_STATE_HASH& scope=REQUESTED_SCOPES
2015-01-12 18:30:34 -05:00
```
2019-07-26 20:28:19 -04:00
This will ask the user to approve the applications access to their account
based on the scopes specified in `REQUESTED_SCOPES` and then redirect back to
the `REDIRECT_URI` you provided. The [scope parameter ](https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes#requesting-particular-scopes )
is a space separated list of scopes you want to have access to (e.g. `scope=read_user+profile`
would request `read_user` and `profile` scopes). The redirect
2019-01-13 22:46:01 -05:00
will include a fragment with `access_token` as well as token details in GET
parameters, for example:
2015-01-12 18:30:34 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2020-08-21 14:10:24 -04:00
https://example.com/oauth/redirect#access_token=ABCDExyz123& state=YOUR_UNIQUE_STATE_HASH& token_type=bearer& expires_in=3600
2015-01-12 18:30:34 -05:00
```
2019-01-13 22:46:01 -05:00
### Resource owner password credentials flow
2015-01-12 18:30:34 -05:00
2020-12-04 16:09:29 -05:00
NOTE:
2019-01-13 22:46:01 -05:00
Check the [RFC spec ](https://tools.ietf.org/html/rfc6749#section-4.3 ) for a
detailed flow description.
2016-06-21 01:33:50 -04:00
2020-12-04 16:09:29 -05:00
NOTE:
2019-01-13 22:46:01 -05:00
The Resource Owner Password Credentials is disabled for users with [two-factor
authentication](../user/profile/account/two_factor_authentication.md) turned on.
These users can access the API using [personal access tokens ](../user/profile/personal_access_tokens.md )
instead.
2016-06-21 01:33:50 -04:00
2019-01-13 22:46:01 -05:00
In this flow, a token is requested in exchange for the resource owner credentials
(username and password).
2015-01-12 18:30:34 -05:00
2019-01-13 22:46:01 -05:00
The credentials should only be used when:
2016-08-04 09:25:37 -04:00
2019-01-13 22:46:01 -05:00
- There is a high degree of trust between the resource owner and the client. For
example, the client is part of the device operating system or a highly
privileged application.
- Other authorization grant types are not available (such as an authorization code).
2017-07-07 10:52:45 -04:00
2020-12-04 16:09:29 -05:00
WARNING:
2019-01-13 22:46:01 -05:00
Never store the user's credentials and only use this grant type when your client
is deployed to a trusted environment, in 99% of cases
[personal access tokens ](../user/profile/personal_access_tokens.md ) are a better
choice.
2017-07-07 10:52:45 -04:00
2019-01-13 22:46:01 -05:00
Even though this grant type requires direct client access to the resource owner
credentials, the resource owner credentials are used for a single request and
are exchanged for an access token. This grant type can eliminate the need for
the client to store the resource owner credentials for future use, by exchanging
the credentials with a long-lived access token or refresh token.
2015-01-12 18:30:34 -05:00
2019-01-13 22:46:01 -05:00
To request an access token, you must make a POST request to `/oauth/token` with
the following parameters:
```json
2015-01-12 18:30:34 -05:00
{
"grant_type" : "password",
"username" : "user@example.com",
2016-09-28 04:41:39 -04:00
"password" : "secret"
2015-01-12 18:30:34 -05:00
}
```
2020-05-28 17:08:22 -04:00
Also you must use HTTP Basic authentication using the `client_id` and`client_secret`
values to authenticate the client that performs a request.
2019-01-13 22:46:01 -05:00
Example cURL request:
2020-01-30 10:09:15 -05:00
```shell
2019-01-13 22:46:01 -05:00
echo 'grant_type=password& username=< your_username > & password=< your_password > ' > auth.txt
2020-05-28 17:08:22 -04:00
curl --data "@auth.txt" --user client_id:client_secret --request POST "https://gitlab.example.com/oauth/token"
2019-01-13 22:46:01 -05:00
```
2015-01-12 18:30:34 -05:00
Then, you'll receive the access token back in the response:
2020-02-27 19:09:08 -05:00
```json
2015-01-12 18:30:34 -05:00
{
"access_token": "1f0af717251950dbd4d73154fdf0a474a5c5119adad999683f5b450c460726aa",
"token_type": "bearer",
"expires_in": 7200
}
```
2020-05-28 17:08:22 -04:00
By default, the scope of the access token is `api` , which provides complete read/write access.
2019-01-13 22:46:01 -05:00
For testing, you can use the `oauth2` Ruby gem:
2015-01-12 18:30:34 -05:00
2020-02-27 19:09:08 -05:00
```ruby
2020-08-21 14:10:24 -04:00
client = OAuth2::Client.new('the_client_id', 'the_client_secret', :site => "https://example.com")
2016-09-28 04:41:39 -04:00
access_token = client.password.get_token('user@example.com', 'secret')
2015-01-12 18:30:34 -05:00
puts access_token.token
2015-02-03 18:18:40 -05:00
```
2016-06-21 01:33:50 -04:00
2019-07-08 23:28:41 -04:00
## Access GitLab API with `access token`
2017-07-07 10:52:45 -04:00
2019-01-13 22:46:01 -05:00
The `access token` allows you to make requests to the API on behalf of a user.
You can pass the token either as GET parameter:
2020-02-27 19:09:08 -05:00
```plaintext
2017-07-07 10:52:45 -04:00
GET https://gitlab.example.com/api/v4/user?access_token=OAUTH-TOKEN
```
or you can put the token to the Authorization header:
2020-02-27 19:09:08 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --header "Authorization: Bearer OAUTH-TOKEN" "https://gitlab.example.com/api/v4/user"
2017-07-07 10:52:45 -04:00
```
2020-02-17 22:08:54 -05:00
2020-04-07 23:09:31 -04:00
## Retrieving the token information
2020-02-17 22:08:54 -05:00
2020-02-18 22:08:47 -05:00
To verify the details of a token, use the `token/info` endpoint provided by the Doorkeeper gem.
For more information, see [`/oauth/token/info` ](https://github.com/doorkeeper-gem/doorkeeper/wiki/API-endpoint-descriptions-and-examples#get----oauthtokeninfo ).
2020-02-17 22:08:54 -05:00
2020-02-18 22:08:47 -05:00
You must supply the access token, either:
2020-02-17 22:08:54 -05:00
2020-02-18 22:08:47 -05:00
- As a parameter:
2020-02-17 22:08:54 -05:00
2020-02-27 19:09:08 -05:00
```plaintext
2020-02-18 22:08:47 -05:00
GET https://gitlab.example.com/oauth/token/info?access_token=< OAUTH-TOKEN >
```
2020-02-17 22:08:54 -05:00
2020-02-18 22:08:47 -05:00
- In the Authorization header:
2020-02-17 22:08:54 -05:00
2020-02-18 22:08:47 -05:00
```shell
2020-05-27 20:08:37 -04:00
curl --header "Authorization: Bearer < OAUTH-TOKEN > " "https://gitlab.example.com/oauth/token/info"
2020-02-18 22:08:47 -05:00
```
The following is an example response:
2020-02-17 22:08:54 -05:00
```json
{
"resource_owner_id": 1,
"scope": ["api"],
"expires_in": null,
"application": {"uid": "1cb242f495280beb4291e64bee2a17f330902e499882fe8e1e2aa875519cab33"},
"created_at": 1575890427
}
```
2020-02-18 22:08:47 -05:00
### Deprecated fields
The fields `scopes` and `expires_in_seconds` are included in the response.
These are aliases for `scope` and `expires_in` respectively, and have been included to
prevent breaking changes introduced in [doorkeeper 5.0.2 ](https://github.com/doorkeeper-gem/doorkeeper/wiki/Migration-from-old-versions#from-4x-to-5x ).
Don't rely on these fields as they will be removed in a later release.