2016-11-03 12:29:30 +05:30
|
|
|
# GitLab as an OAuth2 provider
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
This document covers using the [OAuth2](https://oauth.net/2/) protocol to allow
|
|
|
|
other services to access GitLab resources on user's behalf.
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
If you want GitLab to be an OAuth authentication service provider to sign into
|
|
|
|
other services, see the [OAuth2 provider](../integration/oauth_provider.md)
|
|
|
|
documentation. This functionality is based on the
|
|
|
|
[doorkeeper Ruby gem](https://github.com/doorkeeper-gem/doorkeeper).
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
## Supported OAuth2 flows
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
GitLab currently supports the following authorization flows:
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
- **Web application flow:** Most secure and common type of flow, designed for
|
|
|
|
applications with secure server-side.
|
|
|
|
- **Implicit grant flow:** This flow is designed for user-agent only apps (e.g., single
|
|
|
|
page web application running on GitLab Pages).
|
|
|
|
- **Resource owner password credentials flow:** To be used **only** for securely
|
|
|
|
hosted, first-party services.
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
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-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
Both **web application** and **implicit grant** flows require `application` to be
|
|
|
|
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-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
CAUTION: **Important:**
|
|
|
|
OAuth specification advises sending the `state` parameter with each request to
|
|
|
|
`/oauth/authorize`. We highly recommended sending a unique value with each request
|
|
|
|
and validate it against the one in the redirect request. This is important in
|
|
|
|
order to prevent [CSRF attacks](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)).
|
|
|
|
The `state` parameter really should have been a requirement in the standard!
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
In the following sections you will find detailed instructions on how to obtain
|
|
|
|
authorization with each flow.
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
### Web application flow
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
NOTE: **Note:**
|
|
|
|
Check the [RFC spec](https://tools.ietf.org/html/rfc6749#section-4.1) for a
|
|
|
|
detailed flow description.
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
The web application flow is:
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
1. Request authorization code. To do that, you should redirect the user to the
|
|
|
|
`/oauth/authorize` endpoint with the following GET parameters:
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
```
|
|
|
|
https://gitlab.example.com/oauth/authorize?client_id=APP_ID&redirect_uri=REDIRECT_URI&response_type=code&state=YOUR_UNIQUE_STATE_HASH
|
|
|
|
```
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
This will ask the user to approve the applications access to their account and
|
|
|
|
then redirect back to the `REDIRECT_URI` you provided. The redirect will
|
|
|
|
include the GET `code` parameter, for example:
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
```
|
|
|
|
http://myapp.com/oauth/redirect?code=1234567890&state=YOUR_UNIQUE_STATE_HASH
|
|
|
|
```
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
You should then use `code` to request an access token.
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
1. Once you have the authorization code you can request an `access_token` using the
|
|
|
|
code. You can do that by using any HTTP client. In the following example,
|
|
|
|
we are using Ruby's `rest-client`:
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
```ruby
|
|
|
|
parameters = 'client_id=APP_ID&client_secret=APP_SECRET&code=RETURNED_CODE&grant_type=authorization_code&redirect_uri=REDIRECT_URI'
|
|
|
|
RestClient.post 'http://gitlab.example.com/oauth/token', parameters
|
|
|
|
```
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
Example response:
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
```json
|
|
|
|
{
|
|
|
|
"access_token": "de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54",
|
|
|
|
"token_type": "bearer",
|
|
|
|
"expires_in": 7200,
|
|
|
|
"refresh_token": "8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1"
|
|
|
|
}
|
|
|
|
```
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
NOTE: **Note:**
|
|
|
|
The `redirect_uri` must match the `redirect_uri` used in the original
|
|
|
|
authorization request.
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
You can now make requests to the API with the access token returned.
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
### Implicit grant flow
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
NOTE: **Note:**
|
|
|
|
Check the [RFC spec](https://tools.ietf.org/html/rfc6749#section-4.2) for a
|
|
|
|
detailed flow description.
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
CAUTION: **Important:**
|
|
|
|
Avoid using this flow for applications that store data outside of the GitLab
|
|
|
|
instance. If you do, make sure to verify `application id` associated with the
|
|
|
|
access token before granting access to the data
|
|
|
|
(see [/oauth/token/info](https://github.com/doorkeeper-gem/doorkeeper/wiki/API-endpoint-descriptions-and-examples#get----oauthtokeninfo)).
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
Unlike the web 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, therefore secrets can leak easily.
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
To request the access token, you should redirect the user to the
|
|
|
|
`/oauth/authorize` endpoint using `token` response type:
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
```
|
2017-09-10 17:25:29 +05:30
|
|
|
https://gitlab.example.com/oauth/authorize?client_id=APP_ID&redirect_uri=REDIRECT_URI&response_type=token&state=YOUR_UNIQUE_STATE_HASH
|
2015-04-26 12:48:37 +05:30
|
|
|
```
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
This will ask the user to approve the application's access to their account and
|
|
|
|
then redirect them back to the `REDIRECT_URI` you provided. The redirect
|
|
|
|
will include a fragment with `access_token` as well as token details in GET
|
|
|
|
parameters, for example:
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
```
|
2017-09-10 17:25:29 +05:30
|
|
|
http://myapp.com/oauth/redirect#access_token=ABCDExyz123&state=YOUR_UNIQUE_STATE_HASH&token_type=bearer&expires_in=3600
|
2015-04-26 12:48:37 +05:30
|
|
|
```
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
### Resource owner password credentials flow
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
NOTE: **Note:**
|
|
|
|
Check the [RFC spec](https://tools.ietf.org/html/rfc6749#section-4.3) for a
|
|
|
|
detailed flow description.
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
NOTE: **Note:**
|
|
|
|
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-08-24 12:49:21 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
In this flow, a token is requested in exchange for the resource owner credentials
|
|
|
|
(username and password).
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
The credentials should only be used when:
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
- 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-09-10 17:25:29 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
CAUTION: **Important:**
|
|
|
|
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-09-10 17:25:29 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
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-04-26 12:48:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
To request an access token, you must make a POST request to `/oauth/token` with
|
|
|
|
the following parameters:
|
|
|
|
|
|
|
|
```json
|
2015-04-26 12:48:37 +05:30
|
|
|
{
|
|
|
|
"grant_type" : "password",
|
|
|
|
"username" : "user@example.com",
|
2016-11-03 12:29:30 +05:30
|
|
|
"password" : "secret"
|
2015-04-26 12:48:37 +05:30
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
Example cURL request:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
echo 'grant_type=password&username=<your_username>&password=<your_password>' > auth.txt
|
|
|
|
curl --data "@auth.txt" --request POST https://gitlab.example.com/oauth/token
|
|
|
|
```
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
Then, you'll receive the access token back in the response:
|
|
|
|
|
|
|
|
```
|
|
|
|
{
|
|
|
|
"access_token": "1f0af717251950dbd4d73154fdf0a474a5c5119adad999683f5b450c460726aa",
|
|
|
|
"token_type": "bearer",
|
|
|
|
"expires_in": 7200
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
For testing, you can use the `oauth2` Ruby gem:
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
```
|
|
|
|
client = OAuth2::Client.new('the_client_id', 'the_client_secret', :site => "http://example.com")
|
2016-11-03 12:29:30 +05:30
|
|
|
access_token = client.password.get_token('user@example.com', 'secret')
|
2015-04-26 12:48:37 +05:30
|
|
|
puts access_token.token
|
|
|
|
```
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
## Access GitLab API with `access token`
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
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:
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
```
|
|
|
|
GET https://gitlab.example.com/api/v4/user?access_token=OAUTH-TOKEN
|
|
|
|
```
|
|
|
|
|
|
|
|
or you can put the token to the Authorization header:
|
|
|
|
|
|
|
|
```
|
|
|
|
curl --header "Authorization: Bearer OAUTH-TOKEN" https://gitlab.example.com/api/v4/user
|
|
|
|
```
|