<a id="3013160bddf2807d925ad823dba4f163"></a><a id="3013160b-ddf2-807d-925a-d823dba4f163"></a>

# Login with Learn

Use **Login with Learn** to authenticate users in your app through Learn's OAuth 2.0 and OpenID Connect provider.

This page is for external developers using Learn as their identity provider. Register your client with our team before starting.

## Provider Summary

- Base URL: [`https://learn.derose.app`](https://learn.derose.app)
- OAuth flow: Authorization Code, with refresh tokens
- OIDC support: Discovery, JWKS, `userinfo`, `id_token`
- Recommended flow: Authorization Code + PKCE (`S256`)

## 1. Register an OAuth Application

[Contact our team](/support/new) to register your application. Send:

- Your application name.
- Each callback URL, one per line.
- Your client type: confidential for a server app, public for a SPA, mobile app, or CLI.
- The scopes you need. Start with `openid email profile` for login.

Our team provides your `client_id`. Confidential clients also receive a `client_secret`; keep it on your server. Public clients do not use a secret.

The user signs in to Learn before returning to your callback. Learn can request consent for the requested permissions. An existing authorization can avoid another consent screen.

## 2. Endpoints You Will Use

### OAuth 2.0

- `GET /oauth/authorize`
- `POST /oauth/token`
- `POST /oauth/revoke`
- `POST /oauth/introspect` (requires client authentication or an authorized bearer token)

### OpenID Connect

- `GET /.well-known/openid-configuration`
- `GET /oauth/discovery/keys`
- `GET|POST /oauth/userinfo`

## 3. Required Redirect URI Rules

- Register every callback URL. Wildcards are not supported.
- Use HTTPS in production for web callbacks.
- Native apps and CLIs can use HTTP loopback callbacks, including `localhost`, `127.0.0.1`, and `[::1]`.
- For loopback IP callbacks, the runtime port can differ from the registered port. The remaining URL must match.
- For other callbacks, the URL must match exactly, including the port, path, and trailing slash.
- Unsafe schemes (`javascript:`, `data:`, `vbscript:`) are blocked.

For example, a CLI can register `http://127.0.0.1/callback` and authorize with `http://127.0.0.1:8787/callback`. Use that same runtime URL during token exchange.

## 4. Authorization Code + PKCE Flow

### Step A: Generate PKCE values

```bash
CODE_VERIFIER=$(openssl rand -base64 32 | tr -d '=' | tr '+/' '-_')
CODE_CHALLENGE=$(echo -n "$CODE_VERIFIER" | openssl dgst -sha256 -binary | base64 | tr -d '=' | tr '+/' '-_')
STATE=$(openssl rand -hex 16)
NONCE=$(openssl rand -hex 16)
```

Store the verifier, state, and nonce for this login attempt. Keep them until the callback is validated.

### Step B: Redirect the user to Learn

Build one URL with URL-encoded parameter values. The line breaks below are for readability.

```txt
https://learn.derose.app/oauth/authorize?
  client_id=YOUR_CLIENT_ID&
  redirect_uri=https://yourapp.com/callback&
  response_type=code&
  scope=openid+email+profile&
  state=YOUR_STATE&
  nonce=YOUR_NONCE&
  code_challenge=YOUR_CODE_CHALLENGE&
  code_challenge_method=S256
```

### Step C: Validate the callback and exchange the code

Handle any returned `error` before exchanging a code. Verify that the returned `state` matches your stored value.

The example below is for a confidential client. For a public client, omit `client_secret` and keep `client_id`.

Send the original verifier and the exact callback URL used in Step B.

```bash
curl -X POST https://learn.derose.app/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=authorization_code" \
  --data-urlencode "code=AUTHORIZATION_CODE" \
  --data-urlencode "redirect_uri=https://yourapp.com/callback" \
  --data-urlencode "client_id=YOUR_CLIENT_ID" \
  --data-urlencode "client_secret=YOUR_CLIENT_SECRET" \
  --data-urlencode "code_verifier=$CODE_VERIFIER"
```

Expected response includes:

- `access_token`
- `refresh_token`
- `id_token`
- `scope`
- `expires_in`

Validate the `id_token` before creating your application's session. Treat the access token as an opaque bearer credential.

### Step D: Read the user profile

Use `/oauth/userinfo` for the custom claims granted by your scopes. Do not expect these profile claims in the `id_token`.

```bash
curl https://learn.derose.app/oauth/userinfo \
  -H "Authorization: Bearer ACCESS_TOKEN"
```

### Step E: Refresh tokens

Public clients omit `client_secret`. Save the new access token and refresh token returned by each refresh.

```bash
curl -X POST https://learn.derose.app/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=refresh_token" \
  --data-urlencode "refresh_token=REFRESH_TOKEN" \
  --data-urlencode "client_id=YOUR_CLIENT_ID" \
  --data-urlencode "client_secret=YOUR_CLIENT_SECRET"
```

### Step F: Revoke a token

Public clients omit `client_secret`. Revocation prevents further use of the revoked token.

```bash
curl -X POST https://learn.derose.app/oauth/revoke \
  --data-urlencode "token=ACCESS_OR_REFRESH_TOKEN" \
  --data-urlencode "client_id=YOUR_CLIENT_ID" \
  --data-urlencode "client_secret=YOUR_CLIENT_SECRET"
```

## 5. OIDC id\_token Validation Checklist

1. Load discovery document from `/.well-known/openid-configuration`
2. Load signing keys from `jwks_uri` (usually `/oauth/discovery/keys`)
3. Verify JWT signature (`RS256`)
4. Validate claims:

- `iss` must be [`https://learn.derose.app`](https://learn.derose.app)
- `aud` must include your `client_id`
- `exp` must be valid
- `sub` must be present
- `nonce` must match the value stored for this login attempt

Use the verified issuer and `sub` together as the user's identity. Do not identify or automatically link users by email alone.

When reading `/oauth/userinfo`, check that its `sub` matches the validated ID token.

## 6. Scopes

Request only scopes registered for your client. Learn rejects invalid or unavailable scopes.

| Scope | Grants |
| --- | --- |
| `openid` | OIDC login and the stable user identifier `sub`. |
| `email` | Userinfo claims `email` and `email_verified`. |
| `profile` | Userinfo claims `name`, `given_name`, `family_name`, `picture`, `level`, and `level_name`. |
| `accounts` | Userinfo claims `enabled_accounts` and `padma_student`. |
| `bookings:read` | Read class occurrences and the user's bookings through the booking API. |
| `bookings:write` | Create and cancel bookings for the user through the booking API. |
| `learn_api` | Reserved scope. It does not replace booking permissions. |

`enabled_accounts` contains PADMA account identifiers. `padma_student` is a boolean. These claims do not replace booking eligibility checks.

For booking access, request the required scopes in addition to login scopes. `bookings:write` does not include `bookings:read`.

Send the access token as `Authorization: Bearer ACCESS_TOKEN` to these endpoints:

- `GET /api/v1/classes` and `GET /api/v1/bookings`: require `bookings:read`.
- `POST /api/v1/bookings` and `DELETE /api/v1/bookings/BOOKING_ID`: require `bookings:write`.

Booking requests act on behalf of the signed-in user and remain subject to their access and booking rules.

## 7. Token Lifetimes

- Access token: 1 hour
- Authorization code: 10 minutes
- ID token: 10 minutes
- Refresh token: no fixed expiry (revocable)

## 8. Security Expectations

- Use Authorization Code flow only (no implicit flow)
- PKCE is required for public clients and recommended for all clients
- Always send and verify `state` and `nonce`
- Use `S256` for PKCE. The provider also advertises `plain`; use `S256` for integrations
- Store secrets and refresh tokens securely. Never embed a confidential client's secret in a browser or mobile app
- Do not log `access_token`, `refresh_token`, `code`, `client_secret`, `id_token`, `code_verifier`

## 9. Common Errors

### `redirect_uri is invalid`

- Callback URL does not exactly match app configuration
- Check scheme, trailing slash, host, path, and port, allowing only the loopback IP port exception above

### `code_challenge required`

- Public client must send `code_challenge` + `code_challenge_method=S256`

### `invalid_grant`

- Authorization code expired or already used
- `code_verifier` does not match challenge
- `redirect_uri` mismatch between authorize and token exchange
- Refresh token is invalid or revoked; start a new authorization

### `invalid_scope`

- Request only scopes registered for your client
- Ask our team to enable additional scopes when needed

### `invalid_client`

- Check your `client_id`
- Confidential clients must authenticate with their current secret
- For public clients, omit `client_secret` and use PKCE

### id\_token verification failure

- JWKS is stale (reload `/oauth/discovery/keys`)
- `iss` or `aud` validation is incorrect

## 10. Ruby OmniAuth Example (OIDC)

```rb
# Gemfile
gem "omniauth"
gem "omniauth-openid-connect"
```

```rb
# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :openid_connect,
    name: :learn,
    scope: %i[openid email profile],
    response_type: :code,
    issuer: "https://learn.derose.app",
    discovery: true,
    client_options: {
      identifier: ENV.fetch("LEARN_CLIENT_ID"),
      secret: ENV.fetch("LEARN_CLIENT_SECRET"),
      redirect_uri: "https://yourapp.com/auth/learn/callback"
    },
    pkce: true
end
```

This configuration is for a confidential server app. Have our team register the exact callback URI:

[`https://yourapp.com/auth/learn/callback`](https://yourapp.com/auth/learn/callback)