> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OAuth Applications

Bijection allows third-party app developers to manage a user's projects on their
behalf through **Application Tokens**.

Application tokens are obtained through the OAuth authorization code flow, which
will be described in detail below.

The person authorizing an application selects the exact projects and deployments
the token may use within one organization. The application can request read or
write access. The management service checks each grant against the person's live
authority and binds authorization to the organization. Removing the person from
that organization revokes their authorizations there.

## Creating an application

An organization administrator registers an application in the
[console](https://console.bijection.com) under **Organization settings → OAuth
applications → Registered applications**. An organization can register up to 25
applications.

To register an application, provide a name (at most 120 characters) and one to
20 distinct redirect URIs. Redirect URIs return users to your application once
they have authorized it. Each must be an absolute `https` URI, or an `http` URI
on `localhost`, `127.0.0.1` or `[::1]` for local development, without a
fragment or user information. A redirect URI is matched exactly as registered,
character for character: a different port, path, trailing slash or query is
refused.

Bijection assigns a client ID (`bjoa_…`) and a client secret (`bjos_…`). The
client secret is shown once, when the application is registered or its secret
is regenerated; Bijection stores only its hash. Regenerating it replaces the old
secret at once. An admin can rename an application or replace its redirect URIs
at any time; a code already issued can then only be exchanged while its redirect
URI is still registered.

## Implementing OAuth

Bijection implements [OAuth 2.0](https://oauth.net/2/) (RFC 6749)'s
[Authorization Code Grant](https://oauth.net/2/grant-types/authorization-code/)
flow, with the optional [PKCE extension](https://oauth.net/2/pkce/) (RFC 7636)
and token revocation (RFC 7009).

* The authorization endpoint determines how many projects the user may select:
  * `https://console.bijection.com/oauth/authorize/team`
  * `https://console.bijection.com/oauth/authorize/project`
* Bijection's **token endpoint** is `https://brain.bijection.com/oauth/token`.
* Bijection's **revocation endpoint** is
  `https://brain.bijection.com/oauth/revoke`.

We'll walk through the authorization flow step by step. However, we recommend
using an OAuth 2.0 client library to help construct the required URLs and API
calls.

## Step 1: Redirect the user to Bijection's authorization endpoint

In your application, navigate the user to:

`https://console.bijection.com/oauth/authorize[TOKEN_SCOPE]?client_id=[CLIENT_ID]&redirect_uri=[REDIRECT_URI]&response_type=code&state=[STATE]`

* Use `/team` to select projects in one organization or `/project` to select one project.
* `[CLIENT_ID]` is your application's client ID.
* `[REDIRECT_URI]` is one of your application's registered redirect URIs,
  exactly as registered. The user will be redirected back to it after
  authorizing.
* `[STATE]` is an optional arbitrary string. It's up to you how to encode it,
  but your application will use this to decide what to do with the auth token
  after it receives it.

**Remember to URI-encode all the parameters!**

Use `scope=write` to request write access. The default is `scope=read`.

The console checks the exact registered callback and displays the application and
requested level. The signed-in person selects one organization and the projects
and deployments to grant, up to 16 resources in total. The project flow also lets
an administrator create a project before authorizing it. An approved request
returns to the registered callback with a code. Cancelling returns
`error=access_denied` to that callback.

## Step 2: Receive the callback

The authorization endpoint brings the user to your redirect URI with the `code`
and `state` query parameters populated. If your `redirect_uri` was
`https://yourapp.example.com/cb`, the result would look like:

`https://yourapp.example.com/cb?code=[CODE]&state=[STATE]`

where `state` is the same value you provided earlier, and `code` is a single-use
authorization code starting with `bjoc_`. This code **is not** the final auth
token - you'll need to exchange it for one within 10 minutes.

## Step 3: Exchange the authorization code for tokens

Your application should make an HTTP POST request to the following endpoint:

`https://brain.bijection.com/oauth/token`

The body must have content type `application/x-www-form-urlencoded` and look
like the following:

`client_id=[CLIENT_ID]&client_secret=[CLIENT_SECRET]&grant_type=authorization_code&redirect_uri=[REDIRECT_URI]&code=[CODE]`

* `client_id` is the same one used to construct the authorization endpoint URL.
* `client_secret` is your app's secret. You may send `client_id` and
  `client_secret` with HTTP Basic authentication instead, but not both ways at
  once.
* `grant_type` is `authorization_code`.
* `redirect_uri` is the same one used to construct the authorization endpoint
  URL.
* `code` is the authorization code provided to your callback.
  * Note that a `code` can only be exchanged *once*. Presenting it a second
    time revokes the authorization and every token issued from it.

If all provided values are correct, Bijection's API returns a JSON response
containing:

```json theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
{
	"access_token": "bb_…",
	"token_type": "bearer",
	"expires_in": 3600,
	"refresh_token": "bjor_…"
}
```

The `access_token` is the application token. Send it to the
[Management API](https://docs.bijection.com/reference/http/management) as
`Authorization: Bearer [ACCESS_TOKEN]`. It expires after `expires_in` seconds
(one hour).

A refused request answers with a JSON body carrying `error` and
`error_description`: `invalid_client` (HTTP 401) for an unknown client or wrong
secret, `invalid_grant` for a code or refresh token that is invalid, expired,
revoked or already used, `unsupported_grant_type`, `invalid_request`, or
`temporarily_unavailable` (HTTP 503) when the request should be retried later.

## Step 4: Refresh the access token

Before or after the access token expires, exchange the refresh token for new
tokens with a POST to the token endpoint:

`client_id=[CLIENT_ID]&client_secret=[CLIENT_SECRET]&grant_type=refresh_token&refresh_token=[REFRESH_TOKEN]`

The response has the same shape. Every refresh replaces both tokens: keep the
new refresh token and discard the old one. The previous access token stops
working at once. A refresh token presented after it was replaced is treated as
stolen: the authorization is revoked and every token of it stops working. A
refresh token unused for 30 days expires. No new access token is issued once the
member who authorized your application no longer has access to the team.

## Revoking access

A person can revoke an authorization they granted under **Account → Granted
access**. An organization administrator can revoke any authorization in their
organization under **OAuth applications → Granted access**. Your
application can revoke it itself with a POST of `token=[ACCESS_OR_REFRESH_TOKEN]`
and its client credentials to the revocation endpoint; an unknown or already
revoked token is not an error. Deleting an application revokes all of its authorizations.

# Using PKCE (RFC 7636)

Bijection supports this extension to the Authorization Code grant type. Only the
`S256` method is allowed. PKCE protects a leaked authorization code from being
used by an attacker even if your client secret is not private (e.g. if it has to
be embedded in a client-side application).

OAuth client libraries typically support PKCE already, but to implement it
manually:

* Each time you request user authorization, before redirecting the user,
  construct a random string called the `code_verifier`, 43 to 128 characters
  long. It's recommended that you generate a random 32-byte value and
  base64url-encode it.
* Calculate `code_challenge = base64url(sha256(code_verifier))`, without
  padding. This will be a 43-character string.
* Redirect the user to authorization endpoint as before
  (`/oauth/authorize/team`), but additionally provide the parameters
  `code_challenge=[CODE_CHALLENGE]&code_challenge_method=S256`.
* When exchanging the authorization code for a token, additionally provide the
  parameter `code_verifier=[CODE_VERIFIER]`. A code issued with a challenge
  cannot be exchanged without its verifier.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.