2016-09-28 04:41:39 -04:00
# GitLab as an OAuth2 provider
2015-01-12 18:30:34 -05:00
2016-08-04 09:25:37 -04:00
This document covers using the OAuth2 protocol to access GitLab.
2015-01-12 18:30:34 -05:00
2016-08-04 09:25:37 -04:00
If you want GitLab to be an OAuth authentication service provider to sign into other services please see the [Oauth2 provider documentation ](../integration/oauth_provider.md ).
2015-02-18 23:49:19 -05:00
2016-09-28 04:41:39 -04:00
OAuth2 is a protocol that enables us to authenticate a user without requiring them to give their password to a third-party.
2015-01-12 18:30:34 -05:00
2015-02-03 18:18:40 -05:00
This functionality is based on [doorkeeper gem ](https://github.com/doorkeeper-gem/doorkeeper )
2015-01-12 18:30:34 -05:00
## Web Application Flow
2016-08-04 09:25:37 -04:00
This is the most common type of flow and is used by server-side clients that wish to access GitLab on a user's behalf.
>**Note:**
This flow **should not** be used for client-side clients as you would leak your `client_secret` . Client-side clients should use the Implicit Grant (which is currently unsupported).
2015-01-12 18:30:34 -05:00
2016-08-04 09:25:37 -04:00
For more detailed information, check out the [RFC spec ](http://tools.ietf.org/html/rfc6749#section-4.1 )
In the following sections you will be introduced to the three steps needed for this flow.
2015-01-12 18:30:34 -05:00
### 1. Registering the client
2016-08-04 09:25:37 -04:00
First, you should create an application (`/profile/applications`) in your user's account.
2016-09-28 04:41:39 -04:00
Each application gets a unique App ID and App Secret parameters.
2016-08-04 09:25:37 -04:00
>**Note:**
**You should not share/leak your App ID or App Secret.**
2015-01-12 18:30:34 -05:00
### 2. Requesting authorization
2016-08-04 09:25:37 -04:00
To request the authorization code, you should redirect the user to the `/oauth/authorize` endpoint:
```
https://gitlab.example.com/oauth/authorize?client_id=APP_ID& redirect_uri=REDIRECT_URI& response_type=code& state=your_unique_state_hash
```
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:
2015-01-12 18:30:34 -05:00
```
2016-08-04 09:25:37 -04:00
http://myapp.com/oauth/redirect?code=1234567890& state=your_unique_state_hash
2015-01-12 18:30:34 -05:00
```
2016-08-04 09:25:37 -04:00
You should then use the `code` to request an access token.
>**Important:**
2016-09-28 04:41:39 -04:00
It is highly recommended that you send a `state` value with the request to `/oauth/authorize` and
validate that value is returned and matches in the redirect request.
2016-09-27 06:09:05 -04:00
This is important to prevent [CSRF attacks ](http://www.oauthsecurity.com/#user-content-authorization-code-flow ),
2016-09-28 04:41:39 -04:00
`state` really should have been a requirement in the standard!
2015-01-12 18:30:34 -05:00
### 3. Requesting the access token
2016-08-04 09:25:37 -04:00
Once you have the authorization code you can request an `access_token` using the code, to do that you can use any HTTP client. In the following example, we are using Ruby's `rest-client` :
2015-01-12 18:30:34 -05:00
```
2016-07-27 22:02:39 -04:00
parameters = 'client_id=APP_ID& client_secret=APP_SECRET& code=RETURNED_CODE& grant_type=authorization_code& redirect_uri=REDIRECT_URI'
2016-12-19 06:33:55 -05:00
RestClient.post 'http://gitlab.example.com/oauth/token', parameters
2015-01-12 18:30:34 -05:00
# The response will be
{
"access_token": "de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54",
2016-09-28 04:41:39 -04:00
"token_type": "bearer",
2015-01-12 18:30:34 -05:00
"expires_in": 7200,
"refresh_token": "8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1"
}
```
2016-08-04 09:25:37 -04:00
>**Note:**
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.
### Use the access token to access the API
The access token allows you to make requests to the API on a behalf of a user.
```
2017-03-01 12:39:40 -05:00
GET https://gitlab.example.com/api/v4/user?access_token=OAUTH-TOKEN
2015-01-12 18:30:34 -05:00
```
Or you can put the token to the Authorization header:
```
2017-03-01 12:39:40 -05:00
curl --header "Authorization: Bearer OAUTH-TOKEN" https://gitlab.example.com/api/v4/user
2015-01-12 18:30:34 -05:00
```
## Resource Owner Password Credentials
2016-06-21 01:33:50 -04:00
## Deprecation Notice
2016-08-15 18:14:22 -04:00
1. Starting in GitLab 8.11, the Resource Owner Password Credentials has been *disabled* for users with two-factor authentication turned on.
2016-06-21 01:33:50 -04:00
2. These users can access the API using [personal access tokens] instead.
---
2016-09-28 04:41:39 -04: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
The credentials should only be used when there is a high degree of trust between the resource owner and the client (e.g. the
client is part of the device operating system or a highly privileged application), and when other authorization grant types are not
available (such as an authorization code).
2016-08-04 09:25:37 -04:00
>**Important:**
Never store the users credentials and only use this grant type when your client is deployed to a trusted environment, in 99% of cases [personal access tokens] are a better choice.
2015-01-12 18:30:34 -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.
You can do POST request to `/oauth/token` with parameters:
```
{
"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
}
```
Then, you'll receive the access token back in the response:
```
{
"access_token": "1f0af717251950dbd4d73154fdf0a474a5c5119adad999683f5b450c460726aa",
"token_type": "bearer",
"expires_in": 7200
}
```
For testing you can use the oauth2 ruby gem:
```
client = OAuth2::Client.new('the_client_id', 'the_client_secret', :site => "http://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
2017-06-13 08:10:31 -04:00
[personal access tokens]: ../user/profile/personal_access_tokens.md