0
0
Fork 0
mirror of https://codeberg.org/forgejo/docs.git synced 2024-11-21 17:36:59 -05:00
forgejo-docs/docs/user/oauth2-provider.md

148 lines
7.2 KiB
Markdown
Raw Normal View History

2023-02-28 11:40:36 -05:00
---
2023-02-28 11:43:38 -05:00
title: 'OAuth2 provider'
license: 'Apache-2.0'
origin_url: 'https://github.com/go-gitea/gitea/blob/abe8fe352711601fbcd24bf4505f7e0b81a93c5d/docs/content/development/oauth2-provider.en-us.md'
2023-02-28 11:40:36 -05:00
---
2023-02-28 11:43:38 -05:00
Forgejo supports acting as an OAuth2 provider to allow third party applications to access its resources with the user's consent.
2023-02-28 11:40:36 -05:00
## Endpoints
| 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` |
## Supported OAuth2 Grants
2023-02-28 11:43:38 -05:00
At the moment Forgejo only supports the [**Authorization Code Grant**](https://tools.ietf.org/html/rfc6749#section-1.3.1) standard with additional support of the following extensions:
2023-02-28 11:40:36 -05:00
- [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)
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. To test or debug you can use the web-tool https://oauthdebugger.com/.
2023-02-28 11:40:36 -05:00
2023-02-28 11:43:38 -05:00
## Scoped Tokens
2023-02-28 11:40:36 -05:00
refactor: use astro content collections for docs Reviewed-on: https://codeberg.org/forgejo/website/pulls/323 # Conflicts: # v1.19/admin/command-line.md # v1.19/admin/config-cheat-sheet.md # v1.19/admin/database-preparation.md # v1.19/admin/email-setup.md # v1.19/admin/incoming-email.md # v1.19/admin/index.md # v1.19/admin/logging-documentation.md # v1.19/admin/reverse-proxy.md # v1.19/admin/seek-assistance.md # v1.19/admin/upgrade.md # v1.19/developer/code-forgejo-org.md # v1.19/developer/index.md # v1.19/index.md # v1.19/license.md # v1.19/user/agit-support.md # v1.19/user/api-usage.md # v1.19/user/authentication.md # v1.19/user/email-settings.md # v1.19/user/first-repository.md # v1.19/user/index.md # v1.19/user/issue-pull-request-templates.md # v1.19/user/issue-tracking-basics.md # v1.19/user/labels.md # v1.19/user/linked-references.md # v1.19/user/merge-message-templates.md # v1.19/user/oauth2-provider.md # v1.19/user/packages/cargo.md # v1.19/user/packages/chef.md # v1.19/user/packages/composer.md # v1.19/user/packages/conan.md # v1.19/user/packages/conda.md # v1.19/user/packages/container.md # v1.19/user/packages/generic.md # v1.19/user/packages/helm.md # v1.19/user/packages/index.md # v1.19/user/packages/maven.md # v1.19/user/packages/npm.md # v1.19/user/packages/nuget.md # v1.19/user/packages/pub.md # v1.19/user/packages/pypi.md # v1.19/user/packages/rubygems.md # v1.19/user/packages/storage.md # v1.19/user/packages/vagrant.md # v1.19/user/project.md # v1.19/user/protection.md # v1.19/user/push-options.md # v1.19/user/push-to-create.md # v1.19/user/repo-permissions.md # v1.19/user/webhooks.md # v1.19/user/wiki.md # v1.20/user/semver.md
2023-07-26 18:20:30 -04:00
See the [Access Token scope](../token-scope/) section for more information.
2023-02-28 11:40:36 -05:00
## Client types
2023-02-28 11:43:38 -05:00
Forgejo supports both confidential and public client types, [as defined by RFC 6749](https://datatracker.ietf.org/doc/html/rfc6749#section-2.1).
2023-02-28 11:40:36 -05:00
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).
## Examples
### Confidential client
2023-02-28 11:40:36 -05:00
**Note:** This example does not use PKCE.
1. Redirect the user to the authorization endpoint in order to get their consent for accessing the resources:
2023-02-28 11:40:36 -05:00
```curl
https://[YOUR-FORGEJO-URL]/login/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&response_type=code&state=STATE
2023-02-28 11:40:36 -05:00
```
The `CLIENT_ID` can be obtained by registering an application in the settings. The `STATE` is a random string that will be sent back to your application after the user authorizes. The `state` parameter is optional but should be used to prevent CSRF attacks.
2023-02-28 11:40:36 -05:00
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:
```curl
https://[REDIRECT_URI]?code=RETURNED_CODE&state=STATE
```
2. Using the provided `code` from the redirect, you can request a new application and refresh token. The access token endpoint accepts POST requests with `application/json` and `application/x-www-form-urlencoded` body, for example:
2023-02-28 11:40:36 -05:00
```curl
2023-02-28 11:43:38 -05:00
POST https://[YOUR-FORGEJO-URL]/login/oauth/access_token
2023-02-28 11:40:36 -05:00
```
```json
{
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET",
"code": "RETURNED_CODE",
"grant_type": "authorization_code",
"redirect_uri": "REDIRECT_URI"
}
```
Response:
```json
{
"access_token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJnbnQiOjIsInR0IjowLCJleHAiOjE1NTUxNzk5MTIsImlhdCI6MTU1NTE3NjMxMn0.0-iFsAwBtxuckA0sNZ6QpBQmywVPz129u75vOM7wPJecw5wqGyBkmstfJHAjEOqrAf_V5Z-1QYeCh_Cz4RiKug",
"token_type": "bearer",
"expires_in": 3600,
"refresh_token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJnbnQiOjIsInR0IjoxLCJjbnQiOjEsImV4cCI6MTU1NzgwNDMxMiwiaWF0IjoxNTU1MTc2MzEyfQ.S_HZQBy4q9r5SEzNGNIoFClT43HPNDbUdHH-GYNYYdkRfft6XptJBkUQscZsGxOW975Yk6RbgtGvq1nkEcklOw"
}
```
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 Forgejo and cannot be recovered. If you lose the secret, you must regenerate the secret via the application's settings.
2023-02-28 11:40:36 -05:00
The `REDIRECT_URI` in the `access_token` request must match the `REDIRECT_URI` in the `authorize` request.
3. Use the `access_token` to make API requests to access the user's resources.
### Public client (PKCE)
PKCE (Proof Key for Code Exchange) is an extension to the OAuth flow which allows for a secure credential exchange without the requirement to provide a client secret.
**Note**: Please ensure you have registered your OAuth application as a public client.
To achieve this, you have to provide a `code_verifier` for every authorization request. A `code_verifier` has to be a random string with a minimum length of 43 characters and a maximum length of 128 characters. It can contain alphanumeric characters as well as the characters `-`, `.`, `_` and `~`.
Using this `code_verifier` string, a new one called `code_challenge` is created by using one of two methods:
- If you have the required functionality on your client, set `code_challenge` to be a URL-safe base64-encoded string of the SHA256 hash of `code_verifier`. In that case, your `code_challenge_method` becomes `S256`.
- If you are unable to do so, you can provide your `code_verifier` as a plain string to `code_challenge`. Then you have to set your `code_challenge_method` as `plain`.
After you have generated this values, you can continue with your request.
1. Redirect the user to the authorization endpoint in order to get their consent for accessing the resources:
```curl
https://[YOUR-FORGEJO-URL]/login/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&response_type=code&code_challenge_method=CODE_CHALLENGE_METHOD&code_challenge=CODE_CHALLENGE&state=STATE
```
The `CLIENT_ID` can be obtained by registering an application in the settings. The `STATE` is a random string that will be sent back to your application after the user authorizes. The `state` parameter is optional, but should be used to prevent CSRF attacks.
![Authorization Page](/authorize.png)
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:
```curl
https://[REDIRECT_URI]?code=RETURNED_CODE&state=STATE
```
2. Using the provided `code` from the redirect, you can request a new application and refresh token. The access token endpoint accepts POST requests with `application/json` and `application/x-www-form-urlencoded` body, for example:
```curl
POST https://[YOUR-FORGEJO-URL]/login/oauth/access_token
```
```json
{
"client_id": "YOUR_CLIENT_ID",
"code": "RETURNED_CODE",
"grant_type": "authorization_code",
"redirect_uri": "REDIRECT_URI",
"code_verifier": "CODE_VERIFIER"
}
```
Response:
```json
{
"access_token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJnbnQiOjIsInR0IjowLCJleHAiOjE1NTUxNzk5MTIsImlhdCI6MTU1NTE3NjMxMn0.0-iFsAwBtxuckA0sNZ6QpBQmywVPz129u75vOM7wPJecw5wqGyBkmstfJHAjEOqrAf_V5Z-1QYeCh_Cz4RiKug",
"token_type": "bearer",
"expires_in": 3600,
"refresh_token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJnbnQiOjIsInR0IjoxLCJjbnQiOjEsImV4cCI6MTU1NzgwNDMxMiwiaWF0IjoxNTU1MTc2MzEyfQ.S_HZQBy4q9r5SEzNGNIoFClT43HPNDbUdHH-GYNYYdkRfft6XptJBkUQscZsGxOW975Yk6RbgtGvq1nkEcklOw"
}
```