|
| 1 | +--- |
| 2 | +title: "Gitea" |
| 3 | +--- |
| 4 | + |
| 5 | +# Gitea |
| 6 | + |
| 7 | +OAuth 2.0 provider for Gitea. |
| 8 | + |
| 9 | +Also see [OAuth 2.0 with PKCE](/guides/oauth2-pkce). |
| 10 | + |
| 11 | +## Initialization |
| 12 | + |
| 13 | +The `baseURL` parameter is the full URL where the Gitea instance is hosted. Use `https://gitea.com` for managed servers. Pass the client secret for confidential clients. |
| 14 | + |
| 15 | +```ts |
| 16 | +import * as arctic from "arctic"; |
| 17 | + |
| 18 | +const baseURL = "https://gitea.com"; |
| 19 | +const baseURL = "https://my-app.com/gitea"; |
| 20 | +const gitea = new arctic.gitea(baseURL, clientId, clientSecret, redirectURI); |
| 21 | +const gitea = new arctic.gitea(baseURL, clientId, null, redirectURI); |
| 22 | +``` |
| 23 | + |
| 24 | +## Create authorization URL |
| 25 | + |
| 26 | +```ts |
| 27 | +import * as arctic from "arctic"; |
| 28 | + |
| 29 | +const state = arctic.generateState(); |
| 30 | +const codeVerifier = arctic.generateCodeVerifier(); |
| 31 | +const scopes = ["read:user", "write:notification"]; |
| 32 | +const url = gitea.createAuthorizationURL(state, codeVerifier, scopes); |
| 33 | +``` |
| 34 | + |
| 35 | +## Validate authorization code |
| 36 | + |
| 37 | +`validateAuthorizationCode()` will either return an [`OAuth2Tokens`](/reference/main/OAuth2Tokens), or throw one of [`OAuth2RequestError`](/reference/main/OAuth2RequestError), [`ArcticFetchError`](/reference/main/ArcticFetchError), [`UnexpectedResponseError`](/reference/main/UnexpectedResponseError), or [`UnexpectedErrorResponseBodyError`](/reference/main/UnexpectedErrorResponseBodyError). Gitea returns an access token, the access token expiration, and a refresh token. |
| 38 | + |
| 39 | +```ts |
| 40 | +import * as arctic from "arctic"; |
| 41 | + |
| 42 | +try { |
| 43 | + const tokens = await gitea.validateAuthorizationCode(code, codeVerifier); |
| 44 | + const accessToken = tokens.accessToken(); |
| 45 | + const accessTokenExpiresAt = tokens.accessTokenExpiresAt(); |
| 46 | + const refreshToken = tokens.refreshToken(); |
| 47 | +} catch (e) { |
| 48 | + if (e instanceof arctic.OAuth2RequestError) { |
| 49 | + // Invalid authorization code, credentials, or redirect URI |
| 50 | + const code = e.code; |
| 51 | + // ... |
| 52 | + } |
| 53 | + if (e instanceof arctic.ArcticFetchError) { |
| 54 | + // Failed to call `fetch()` |
| 55 | + const cause = e.cause; |
| 56 | + // ... |
| 57 | + } |
| 58 | + // Parse error |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Refresh access tokens |
| 63 | + |
| 64 | +Use `refreshAccessToken()` to get a new access token using a refresh token. This method's behavior is identical to `validateAuthorizationCode()`. |
| 65 | + |
| 66 | +```ts |
| 67 | +import * as arctic from "arctic"; |
| 68 | + |
| 69 | +try { |
| 70 | + const tokens = await gitea.refreshAccessToken(refreshToken); |
| 71 | + const accessToken = tokens.accessToken(); |
| 72 | + const accessTokenExpiresAt = tokens.accessTokenExpiresAt(); |
| 73 | + const refreshToken = tokens.refreshToken(); |
| 74 | +} catch (e) { |
| 75 | + if (e instanceof arctic.OAuth2RequestError) { |
| 76 | + // Invalid authorization code, credentials, or redirect URI |
| 77 | + } |
| 78 | + if (e instanceof arctic.ArcticFetchError) { |
| 79 | + // Failed to call `fetch()` |
| 80 | + } |
| 81 | + // Parse error |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Get user profile |
| 86 | + |
| 87 | +Add the `read:user` scope and use the [`/user` endpoint](https://gitea.com/api/swagger#/user). |
| 88 | + |
| 89 | +```ts |
| 90 | +const scopes = ["read:user"]; |
| 91 | +const url = gitea.createAuthorizationURL(state, codeVerifier, scopes); |
| 92 | +``` |
| 93 | + |
| 94 | +```ts |
| 95 | +const response = await fetch("https://gitea.com/user", { |
| 96 | + headers: { |
| 97 | + Authorization: `Bearer ${accessToken}` |
| 98 | + } |
| 99 | +}); |
| 100 | +const user = await response.json(); |
| 101 | +``` |
0 commit comments