mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.5 KiB
105 lines
4.5 KiB
6 years ago
|
---
|
||
|
date: "2019-04-19:44:00+01:00"
|
||
|
title: "OAuth2 provider"
|
||
|
slug: "oauth2-provider"
|
||
|
weight: 41
|
||
4 years ago
|
toc: false
|
||
6 years ago
|
draft: false
|
||
|
menu:
|
||
|
sidebar:
|
||
4 years ago
|
parent: "developers"
|
||
6 years ago
|
name: "OAuth2 Provider"
|
||
|
weight: 41
|
||
|
identifier: "oauth2-provider"
|
||
|
---
|
||
|
|
||
|
# OAuth2 provider
|
||
|
|
||
4 years ago
|
**Table of Contents**
|
||
6 years ago
|
|
||
4 years ago
|
{{< toc >}}
|
||
6 years ago
|
|
||
4 years ago
|
Gitea supports acting as an OAuth2 provider to allow third party applications to access its resources with the user's consent. This feature is available since release 1.8.0.
|
||
6 years ago
|
|
||
4 years ago
|
## Endpoints
|
||
6 years ago
|
|
||
3 years ago
|
| Endpoint | URL |
|
||
|
| ------------------------ | ----------------------------------- |
|
||
|
| OpenID Connect Discovery | `/.well-known/openid-configuration` |
|
||
|
| Authorization Endpoint | `/login/oauth/authorize` |
|
||
|
| Access Token Endpoint | `/login/oauth/access_token` |
|
||
|
| OpenID Connect UserInfo | `/login/oauth/userinfo` |
|
||
|
| JSON Web Key Set | `/login/oauth/keys` |
|
||
6 years ago
|
|
||
|
## Supported OAuth2 Grants
|
||
|
|
||
4 years ago
|
At the moment Gitea only supports the [**Authorization Code Grant**](https://tools.ietf.org/html/rfc6749#section-1.3.1) standard with additional support of the following extensions:
|
||
2 years ago
|
|
||
4 years ago
|
- [Proof Key for Code Exchange (PKCE)](https://tools.ietf.org/html/rfc7636)
|
||
|
- [OpenID Connect (OIDC)](https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth)
|
||
4 years ago
|
|
||
6 years ago
|
To use the Authorization Code Grant as a third party application it is required to register a new application via the "Settings" (`/user/settings/applications`) section of the settings.
|
||
|
|
||
|
## Scopes
|
||
|
|
||
2 years ago
|
Currently Gitea does not support scopes (see [#4300](https://github.com/go-gitea/gitea/issues/4300)) and all third party applications will be granted access to all resources of the user and their organizations.
|
||
6 years ago
|
|
||
2 years ago
|
## Client types
|
||
|
|
||
|
Gitea supports both confidential and public client types, [as defined by RFC 6749](https://datatracker.ietf.org/doc/html/rfc6749#section-2.1).
|
||
|
|
||
|
For public clients, a redirect URI of a loopback IP address such as `http://127.0.0.1/` allows any port. Avoid using `localhost`, [as recommended by RFC 8252](https://datatracker.ietf.org/doc/html/rfc8252#section-8.3).
|
||
|
|
||
6 years ago
|
## Example
|
||
|
|
||
|
**Note:** This example does not use PKCE.
|
||
|
|
||
2 years ago
|
1. Redirect to user to the authorization endpoint in order to get their consent for accessing the resources:
|
||
6 years ago
|
|
||
4 years ago
|
```curl
|
||
|
https://[YOUR-GITEA-URL]/login/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI& response_type=code&state=STATE
|
||
|
```
|
||
6 years ago
|
|
||
4 years ago
|
The `CLIENT_ID` can be obtained by registering an application in the settings. The `STATE` is a random string that will be send back to your application after the user authorizes. The `state` parameter is optional but should be used to prevent CSRF attacks.
|
||
6 years ago
|
|
||
4 years ago
|
![Authorization Page](/authorize.png)
|
||
6 years ago
|
|
||
4 years ago
|
The user will now be asked to authorize your application. If they authorize it, the user will be redirected to the `REDIRECT_URL`, for example:
|
||
6 years ago
|
|
||
4 years ago
|
```curl
|
||
|
https://[REDIRECT_URI]?code=RETURNED_CODE&state=STATE
|
||
|
```
|
||
6 years ago
|
|
||
4 years ago
|
2. Using the provided `code` from the redirect, you can request a new application and refresh token. The access token endpoints accepts POST requests with `application/json` and `application/x-www-form-urlencoded` body, for example:
|
||
6 years ago
|
|
||
4 years ago
|
```curl
|
||
|
POST https://[YOUR-GITEA-URL]/login/oauth/access_token
|
||
|
```
|
||
6 years ago
|
|
||
4 years ago
|
```json
|
||
|
{
|
||
|
"client_id": "YOUR_CLIENT_ID",
|
||
|
"client_secret": "YOUR_CLIENT_SECRET",
|
||
|
"code": "RETURNED_CODE",
|
||
|
"grant_type": "authorization_code",
|
||
|
"redirect_uri": "REDIRECT_URI"
|
||
|
}
|
||
|
```
|
||
6 years ago
|
|
||
4 years ago
|
Response:
|
||
6 years ago
|
|
||
4 years ago
|
```json
|
||
|
{
|
||
|
"access_token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJnbnQiOjIsInR0IjowLCJleHAiOjE1NTUxNzk5MTIsImlhdCI6MTU1NTE3NjMxMn0.0-iFsAwBtxuckA0sNZ6QpBQmywVPz129u75vOM7wPJecw5wqGyBkmstfJHAjEOqrAf_V5Z-1QYeCh_Cz4RiKug",
|
||
|
"token_type": "bearer",
|
||
|
"expires_in": 3600,
|
||
|
"refresh_token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJnbnQiOjIsInR0IjoxLCJjbnQiOjEsImV4cCI6MTU1NzgwNDMxMiwiaWF0IjoxNTU1MTc2MzEyfQ.S_HZQBy4q9r5SEzNGNIoFClT43HPNDbUdHH-GYNYYdkRfft6XptJBkUQscZsGxOW975Yk6RbgtGvq1nkEcklOw"
|
||
|
}
|
||
|
```
|
||
6 years ago
|
|
||
4 years ago
|
The `CLIENT_SECRET` is the unique secret code generated for this application. Please note that the secret will only be visible after you created/registered the application with Gitea and cannot be recovered. If you lose the secret you must regenerate the secret via the application's settings.
|
||
6 years ago
|
|
||
4 years ago
|
The `REDIRECT_URI` in the `access_token` request must match the `REDIRECT_URI` in the `authorize` request.
|
||
6 years ago
|
|
||
4 years ago
|
3. Use the `access_token` to make [API requests](https://docs.gitea.io/en-us/api-usage#oauth2) to access the user's resources.
|