> ## 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.

# Custom JWT Provider

**Note: This is an advanced feature!** We recommend sticking with the
[supported third-party authentication providers](https://docs.bijection.com/access/authentication).

A [JWT](https://en.wikipedia.org/wiki/JSON_Web_Token) is a string combining
three base64-encoded JSON objects containing claims about who a user is valid
for a limited period of time like an hour. You can create them with a library
like [jose](https://github.com/panva/jose) after receiving some evidence
(typically a cookie) of a user's identity or get them from a third party
authentication service like [Clerk](https://clerk.com). The information in a JWT
is signed (the Bijection deployment can tell the information is really from the
issuer and hasn't been modified) but generally not encrypted (you can read it by
base64-decoding the token or pasting it into [jwt.io](https://jwt.io/).

If the JWTs issued to your users by an authentication service contain the right
fields to implement the OpenID Connect (OIDC) protocol, the easiest way to
configure accepting these JWTs is adding an
[OIDC Provider](https://docs.bijection.com/access/authentication/custom-oidc.md) entry in `bijection/sign-in.ts`. If
the authentication service or library you're using to issue JWTs doesn't support
these fields (for example [OpenAuth](https://openauth.js.org/) JWTs missing an
`aud` field because they implement the OAuth 2.0 spec but not OIDC) you'll need
to configure a Custom JWT provider in the `bijection/sign-in.ts` file.

Custom JWTs are required only to have header fields `kid`, `alg` and `typ`, and
payload fields `sub`, `iss`, and `exp`. An `iat` field is also expected by
Bijection clients to implement token refreshing.

## Server-side integration

Declare the verifier with `issuer.jwt`.

```ts bijection/sign-in.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { issuer } from "@bijectionhq/bijection";

export const signIn = issuer.jwt({});
```

Place this export in the app tree in `bijection/app.ts`. After deploying, trust
the issuer's values for it:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection issuer trust signIn --issuer-url https://issuer.example.com \
  --audience your-application-id \
  --jwks https://issuer.example.com/.well-known/jwks.json --algorithm RS256
```

Trusting an issuer issues a grant that carries `attest` for that issuer and
its values. Only the installation's identity admin issues it, never code: no
deploy and no `bijection env set` changes it. Each identity that signs in
through the issuer is bound by a grant under the trust that carries `speak`
for its principal, and each session is a grant under that binding, so
revoking the trust ends every binding and session under it.

* `audience`: Bijection will verify that JWTs have this value in the `aud`
  claim. See below for important information regarding leaving this field out.
  The audience field is not required, but necessary to use with many
  authentication providers for security. Read more below before omitting it.
* `url`: The issuer URL of the JWT.
* `jwks`: The URL for fetching the JWKS (JSON Web Key Set) from the auth
  provider. If you'd like to avoid hitting an external service you may use a
  data URI, e.g. `"data:text/plain;charset=utf-8;base64,ey..."`
* `algorithm`: The algorithm used to sign the JWT. Only RS256 and ES256 are
  currently supported. See
  [RFC 7518](https://datatracker.ietf.org/doc/html/rfc7518#section-3.1) for more
  details.

The `url` property must exactly match the `iss` field of the JWT used and if
specified the `audience` property must exactly match the `aud` field. If
your JWT doesn't match, use a tool like [jwt.io](https://jwt.io/) to view an JWT
and confirm these fields match exactly.

### Warning: omitting `audience` is often insecure

Leaving out `audience` from an auth configuration means the `aud`
(audience) field of your users' JWTs will not be verified. In many cases this is
insecure because a JWT intended for another service can be used to impersonate
them in your service.

Say a user has accounts with `https://todos.com` and `https://banking.com`, two
services which use the same third-party authentication service,
`accounts.google.com`. A JWT accepted by todos.com could be reused to
authenticate with banking.com by either todos.com or an attacker that obtained
access to that JWT.

The `aud` (audience) field of the JWT prevents this: if the JWT was generated
for a specific audience of `https://todos.com` then banking.com can enforce the
`aud` field and know not to accept it.

If the JWTs issued to your users have an `iss` (issuer) URL like
`https://accounts.google.com` that is not specific to your application, it is
not secure to trust these tokens without an ApplicationID because that JWT could
have been collected by a malicious application.

If the JWTs issued to your users have a more specific `iss` field like
`https://api.3rd-party-auth.com/client_0123...` then it may be secure to use no
`aud` field if you control all the services the issuer url grants then access to
and intend for access to any one of these services to grants access to all of
them.

### Application identity

`ctx.frame.as.id` is the verified subject's `PrincipalId`, `person:` followed
by the issuer and subject joined by `|`. Keep application-specific profile fields in your own data
model and load them through an authorized query.

## Client-side integration

Give the [typed client](https://docs.bijection.com/clients/typescript.md) an auth adapter as its
`token`: `connect(url, { token: auth })`. The client asks the
adapter for the current token on every request and live connection.
`authkit()` and `clerk()` from `@bijectionhq/bijection/client` read the state
of their provider's React hook (`useClientAuth(auth, state)` in a component
inside the provider). [AuthKit](https://docs.bijection.com/access/authentication/providers/authkit/existing-app.md)
also supplies session evidence through provider callbacks. For any other
provider, implement `ClientAuth`. Have `token({ refresh })` return the
current token, return `null` when signed out, and reject if a signed-in
credential is unavailable. Use `subscribe(changed)` to call `changed`
synchronously when the principal, session, or audience changes.
Keep that generation stable across ordinary token refreshes. A server or test
that holds a fixed token passes it as a string: `connect(url, { token })`.
For sessions using Bijection Auth, use its [React integration and browser
proofs](https://docs.bijection.com/access/authentication/providers/bijection-auth#quick-start).


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