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
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
---
---
# GitLab as an OAuth2 provider
# GitLab as an OAuth2 provider
...
@@ -19,17 +19,26 @@ documentation. This functionality is based on the
...
@@ -19,17 +19,26 @@ documentation. This functionality is based on the
GitLab currently supports the following authorization flows:
GitLab currently supports the following authorization flows:
-**Web application flow:** Most secure and common type of flow, designed for
-**Authorization code with [Proof Key for Code Exchange (PKCE)](https://tools.ietf.org/html/rfc7636):**
applications with secure server-side.
Most secure. Without PKCE, you'd have to include client secrets on mobile clients,
-**Implicit grant flow:** This flow is designed for user-agent only apps (e.g., single
and is recommended for both client and server aoos.
page web application running on GitLab Pages).
-**Authorization code:** Secure and common flow. Recommended option for secure
-**Resource owner password credentials flow:** To be used **only** for securely
server-side apps.
hosted, first-party services.
-**Implicit grant:** Originally designed for user-agent only apps, such as
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.
Refer to the [OAuth RFC](https://tools.ietf.org/html/rfc6749) to find out
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.
how all those flows work and pick the right one for your use case.
Both **web application** and **implicit grant** flows require `application` to be
Both **authorization code** (with or without PKCE) and **implicit grant** flows require `application` to be
registered first via the `/profile/applications` page in your user's account.
registered first via the `/profile/applications` page in your user's account.
During registration, by enabling proper scopes, you can limit the range of
During registration, by enabling proper scopes, you can limit the range of
resources which the `application` can access. Upon creation, you'll obtain the
resources which the `application` can access. Upon creation, you'll obtain the
...
@@ -57,19 +66,84 @@ These factors are particularly important when using the
...
@@ -57,19 +66,84 @@ These factors are particularly important when using the
In the following sections you will find detailed instructions on how to obtain
In the following sections you will find detailed instructions on how to obtain
authorization with each flow.
authorization with each flow.
### Web application flow
### 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:
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:
@@ -114,19 +189,20 @@ You can now make requests to the API with the access token returned.
...
@@ -114,19 +189,20 @@ You can now make requests to the API with the access token returned.
### Implicit grant flow
### Implicit grant flow
NOTE:
NOTE:
Check the [RFC spec](https://tools.ietf.org/html/rfc6749#section-4.2) for a
For a detailed flow diagram, see the [RFC specification](https://tools.ietf.org/html/rfc6749#section-4.2).
detailed flow description.
WARNING:
WARNING:
Avoid using this flow for applications that store data outside of the GitLab
The Implicit grant flow is inherently insecure. The IETF plans to remove it in
instance. If you do, make sure to verify `application id` associated with the
[OAuth 2.1](https://oauth.net/2.1/).
access token before granting access to the data
(see [`/oauth/token/info`](#retrieving-the-token-information)).
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
Unlike the web flow, the client receives an `access token` immediately as a
access to the data, as described in [Retrieving the token information](#retrieving-the-token-information)).
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
Unlike the authorization code flow, the client receives an `access token`
easily accessible, therefore secrets can leak easily.
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.
To request the access token, you should redirect the user to the
To request the access token, you should redirect the user to the
`/oauth/authorize` endpoint using `token` response type:
`/oauth/authorize` endpoint using `token` response type: