diff --git a/docs/core/endpoints.md b/docs/core/endpoints.md index 89831bb..13f04cd 100644 --- a/docs/core/endpoints.md +++ b/docs/core/endpoints.md @@ -123,18 +123,41 @@ This endpoint supports following query parameters **Query String Params** -| Key | Description | Required | -| ------------- | --------------------------------------------------------------------------------------------------- | -------- | -| `redirectURL` | URL where user should be redirected after login | true | -| `state` | Unique state identifier that is used to make sure request is not interrupted | true | -| `roles` | Comma separated list of roles to login with. If not present default role(`user`) is used | false | -| `scope` | Space separated list of openID scopes. If not present default scopes `openid email profile` is used | false | +| Key | Description | Required | +| -------------- | --------------------------------------------------------------------------------------------------- | -------- | +| `redirect_uri` | URL where user should be redirected after login. See accepted values below | true | +| `redirectURL` | Deprecated alias of `redirect_uri`, still accepted | false | +| `state` | Unique state identifier that is used to make sure request is not interrupted | true | +| `roles` | Comma separated list of roles to login with. If not present default role(`user`) is used | false | +| `scope` | Space separated list of openID scopes. If not present default scopes `openid email profile` is used | false | -Sample URL: `/oauth_login/google?redirectURL=https://myapp.com&role=admin` +Sample URLs: + +``` +# Outside /authorize: return to your app (its origin is in --allowed-origins) +/oauth_login/google?redirect_uri=https://myapp.com/dashboard&state=abc123&roles=admin + +# From the login page during an /authorize request: return to /app, which resumes /authorize +/oauth_login/google?state=st-123&redirect_uri=https%3A%2F%2Fauth.example.com%2Fapp%2F%3Fclient_id%3Dc1%26response_type%3Dcode%26state%3Dst-123%26... +``` + +More cases, with the result for each, are in [What `/oauth_login` accepts](./oauth2-oidc#social-login-inside-an-authorization-request). + +**Accepted `redirect_uri` values:** + +- Any URI allowed by `--allowed-origins`. +- This server's own `/app`, `/app/` or `/app/signup` (same scheme and host:port as `--url`, exact path, no fragment), whatever `--allowed-origins` says. _Authorizer 2.4.6 and later._ The login page uses this to resume an `/authorize` request through `/authorize`, which then checks the client's registered redirect URIs. See [Social login inside an authorization request](./oauth2-oidc#social-login-inside-an-authorization-request). + +A client's registered redirect URI is **not** accepted here unless its origin is in `--allowed-origins`. To get a code to a registered client, go through `/authorize`. ## `/oauth_callback/:oauth_provider` -`GET /oauth_callback/:oauth_provider` - Endpoint that is used by oauth providers as callback after success / unsuccessful login +`GET /oauth_callback/:oauth_provider` - Endpoint that is used by oauth providers as callback after success / unsuccessful login. Register `/oauth_callback/` as the callback URL on the provider's console. + +On success it sets the session cookie and redirects to the `redirect_uri` given to `/oauth_login`: + +- For this server's own `/app` URL, it redirects to that URL unchanged and issues no authorization code. `/app` resumes the original `/authorize` request. +- For any other URI, it appends `state` and `nonce`, plus `code` when the login belongs to an `/authorize` request. That last case is deprecated: it skips the consent screen and logs a warning. ## `logout` diff --git a/docs/core/oauth2-oidc.md b/docs/core/oauth2-oidc.md index 55b6569..c1bd90c 100644 --- a/docs/core/oauth2-oidc.md +++ b/docs/core/oauth2-oidc.md @@ -527,6 +527,143 @@ Error responses (e.g. `login_required` from `prompt=none`) are delivered via the For `form_post`, Authorizer serves the auto-submitting form with a dedicated `Content-Security-Policy` (nonce-based `script-src`, relaxed `form-action`) so the browser allows the form to POST to the RP's `redirect_uri`. If a reverse proxy in front of Authorizer injects its own CSP header, it can break `form_post` — see [Common Issues](#common-issues). +#### Social login inside an authorization request + +_Authorizer 2.4.6 and later._ + +When `/authorize` needs the user to sign in, it sends the browser to the login page at `/app`. If the user picks a social provider (Google, GitHub, …), that login **resumes the original authorization request** instead of finishing on its own: + +``` +client ──► /authorize ──► /app ──► /oauth_login/github ──► GitHub + │ +client ◄── /authorize ◄── /app ◄── /oauth_callback/github ◄───┘ + (code) (checks redirect_uri, (session cookie only — + consent, PKCE) no code, nothing appended) +``` + +1. The login page calls `/oauth_login/` with `redirect_uri` set to **its own `/app` URL**, carrying the original `/authorize` parameters in the query string. +2. The provider returns to `/oauth_callback/`, which signs the user in (sets the session cookie) and redirects to that `/app` URL **unchanged**. It does not issue an authorization code and does not append `state`, `nonce` or `code`. +3. `/app` sees the session and replays the request to `/authorize`. `/authorize` then checks `redirect_uri` against the **client's registered redirect URIs**, shows the consent screen if the client requires it, binds PKCE, and redirects to the client with the code. + +This is the same path password, passkey and OTP login already take, and it is how Auth0 and other providers treat social connections: the provider always returns to the authorization server, and only `/authorize` redirects to the application. It means: + +- **A client's callback does not have to be in `--allowed-origins`.** A client registered with `https://app.example.org/callback` completes social login with `--allowed-origins` listing only your own console. Do not add client origins to `--allowed-origins` to make social login work: that list also drives CORS, the CSRF origin check and the email-link redirects, for every client. +- **Self-registered and CIMD clients always get the consent screen**, including through social login. +- **The client's `nonce` reaches the ID token**, because the callback appends nothing for `/app` to forward. + +**What `/oauth_login` accepts as `redirect_uri`:** this server's own `/app`, `/app/` or `/app/signup` (same scheme and host:port as `--url`, exact path, no fragment), or any URI allowed by `--allowed-origins`. Nothing else, and in particular not a client's registered callback. + +| `redirect_uri` sent to `/oauth_login` (server `--url=https://auth.example.com`, `--allowed-origins=https://console.example.com`) | Result | +|---|---| +| `https://auth.example.com/app/?client_id=…&response_type=code&…` | Accepted: resumes through `/authorize`. This is the URL the login page is on: `/app` 301-redirects to `/app/` | +| `https://auth.example.com/app?client_id=…&response_type=code&…` | Accepted: same | +| `https://auth.example.com/app/signup?client_id=…&…` | Accepted: same, from the signup screen | +| `https://console.example.com/dashboard` | Accepted: allowed origin, direct redirect | +| `https://app.example.org/callback` (a client's registered callback) | `400 invalid redirect uri`. Go through `/authorize` instead | +| `https://auth.example.com/app#client_id=…` | `400`: fragment | +| `https://auth.example.com/graphql`, `/app/settings` | `400`: only `/app`, `/app/` and `/app/signup` | +| `http://auth.example.com/app?…` | `400`: scheme differs from `--url` | + +##### Example: the full round trip + +A client `c1` is registered with the callback `https://app.example.org/callback`, and the server runs with `--url=https://auth.example.com --allowed-origins=https://console.example.com`. The client's callback is not in `--allowed-origins`, and doesn't need to be. These are the requests the browser makes (line breaks added; values URL-encoded in practice): + +```http +# 1. The client starts an authorization request. No session yet, so the user is sent to the login page. +GET https://auth.example.com/authorize?client_id=c1&response_type=code&response_mode=query + &redirect_uri=https://app.example.org/callback&scope=openid profile email + &state=st-123&nonce=n-456&code_challenge=E9Mel…&code_challenge_method=S256 +→ 302 Location: /app?state=st-123&scope=openid profile email&redirect_uri=https://app.example.org/callback + &response_mode=query&response_type=code&client_id=c1&nonce=n-456&code_challenge=E9Mel…&code_challenge_method=S256&… + +GET https://auth.example.com/app?state=st-123&… +→ 301 Location: /app/?state=st-123&… # the login page renders at /app/ + +# 2. The user clicks "Continue with GitHub". redirect_uri is the login page's own URL, not the client's callback. +GET https://auth.example.com/oauth_login/github?state=st-123&scope=openid profile email + &redirect_uri=https://auth.example.com/app/?state=st-123&…&client_id=c1&nonce=n-456&… +→ 307 Location: https://github.com/login/oauth/authorize?client_id=…&state= + +# 3. GitHub returns. The callback signs the user in and goes back to the login page URL, byte for byte. +GET https://auth.example.com/oauth_callback/github?code=&state= +→ 302 Location: https://auth.example.com/app/?state=st-123&…&client_id=c1&nonce=n-456&… + Set-Cookie: cookie_session=… + +# 4. /app has a session now, so it replays the original request to /authorize. +GET https://auth.example.com/authorize?client_id=c1&response_type=code&…&state=st-123&nonce=n-456&… +→ 302 Location: https://app.example.org/callback?code=&state=st-123 + (or the consent screen first, for a self-registered or CIMD client) + +# 5. The client redeems the code as usual. +POST https://auth.example.com/oauth/token + grant_type=authorization_code&code=&client_id=c1 + &redirect_uri=https://app.example.org/callback&code_verifier= +→ 200 {"access_token": "…", "id_token": "…", …} # id_token "nonce" is n-456 +``` + +Note that steps 3 and 4 never put a code in a URL that `/authorize` didn't produce, and the client only ever sees step 4's redirect. + +##### Example: a customised login page + +The hosted login page does all of this already. If you ship your own build of the login page (served by Authorizer at `/app`), its social buttons need two things. Here they are in plain JavaScript: + +```js +// The /authorize parameters this page was opened with. /authorize puts them in +// the fragment when response_mode=fragment, so read both; the query wins. +function authorizeParams() { + const params = new URLSearchParams(window.location.hash.slice(1)); + new URLSearchParams(window.location.search).forEach((v, k) => params.set(k, v)); + return params; +} + +// 1. Social buttons: come back to this page, with the parameters in the QUERY +// string (a fragment is refused, because a server redirect can't carry it). +function socialLoginURL(provider) { + const params = authorizeParams(); + const back = `${window.location.origin}${window.location.pathname}?${params}`; + const query = new URLSearchParams({ + redirect_uri: back, // never the client's callback + state: params.get('state') ?? '', + scope: params.get('scope') ?? 'openid profile email', + }); + return `/oauth_login/${provider}?${query}`; +} + +// 2. Once a session exists (after any login method), replay to /authorize. +// It checks the client's redirect_uri, runs consent and PKCE, and issues the code. +function resumeAuthorize() { + const params = authorizeParams(); + if (params.get('client_id') && params.get('response_type')) { + window.location.replace(`/authorize?${params}`); + } +} + +document.querySelector('#github').onclick = () => { + window.location.href = socialLoginURL('github'); +}; +``` + +##### Example: updating an older login page + +If the server logs `social login completed an /authorize request without resuming through /authorize`, the login page is still sending the client's callback. The change is one parameter: + +```diff +- /oauth_login/github?state=st-123&redirect_uri=https://app.example.org/callback ++ /oauth_login/github?state=st-123&redirect_uri=https://auth.example.com/app/?client_id=c1&response_type=code&redirect_uri=https://app.example.org/callback&state=st-123&nonce=n-456&… +``` + +The login page also needs to replay the request to `/authorize` once the session exists (`resumeAuthorize()` above). Password login on such a page usually does this already. + +##### Compatibility + +| You use | What to do | +|---|---| +| The hosted login page (`/app`) | Nothing. From 2.4.6 the bundled login page sends its own `/app` URL, so social login resumes through `/authorize`. The page is built into the same image as the server, so the two always match | +| Your own build of the login page, served by Authorizer at `/app` | Send `redirect_uri` = the page's own `/app` URL with the `/authorize` parameters in the **query string**, then replay to `/authorize` once the session exists ([example](#example-a-customised-login-page)). If it's built on `@authorizerdev/authorizer-react`, `AuthorizerSocialLogin` does this from 2.2.1 | +| `/oauth_login` directly, outside `/authorize` (mobile apps, a "Sign in with GitHub" link on your own site) | Nothing. Validation against `--allowed-origins` is unchanged, and the callback still appends `state` (and `code` when one exists) to your `redirect_uri` | + +**Deprecated:** completing an `/authorize` request by sending `redirect_uri` = the client's callback straight to `/oauth_login`. This was the only option before 2.4.6. It still works when that callback's origin is in `--allowed-origins`, but it skips the consent screen, and the server logs a warning (`social login completed an /authorize request without resuming through /authorize`) each time it is used. A future release will refuse it, with a note in the changelog. If you see that warning, move the login UI to the `/app` URL as described above. + ### Token Endpoint **Endpoint:** `POST /oauth/token` @@ -1020,8 +1157,14 @@ HTTPServer(('127.0.0.1', 9999), H).serve_forever() 1. Register the provider on its console (Google, GitHub, etc.), using `$AUTHORIZER_URL/oauth_callback/` as the callback 2. Configure the provider in Authorizer: `--google-client-id`, `--google-client-secret`, etc. 3. Restart Authorizer — the login page auto-shows the new button -4. Test: visit `$AUTHORIZER_URL/oauth_login/google?redirectURL=http://localhost:3000/callback&state=$STATE` +4. Test: visit `$AUTHORIZER_URL/oauth_login/google?redirect_uri=http://localhost:3000/callback&state=$STATE` 5. Verify the resulting ID token has `amr: ["fed"]` (federated authentication) +6. Test social login inside an authorization request (2.4.6+): open the authorization-code URL from [step 3](#3-authorization-code--pkce-end-to-end) in a browser, choose the provider on the login page, and check that: + - the `/oauth_login` request's `redirect_uri` is `$AUTHORIZER_URL/app?…`, not your callback (browser dev tools, Network tab); + - you land on your callback with `?code=…&state=…`, after the consent screen for a self-registered client; + - the ID token from `/oauth/token` carries the `nonce` you sent. + + See [the full round trip](#example-the-full-round-trip) for the expected requests. ### 10. Automated conformance testing @@ -1046,6 +1189,8 @@ For pre-production validation, run a full OIDC conformance suite: | Back-channel logout never fires | `--backchannel-logout-uri` not set, or receiver unreachable within 5 seconds | | Auth0 Enterprise OIDC connection can't discover | Wrong Issuer URL — must be exactly `https://your-authorizer.example` (no trailing slash, no path) | | `redirect_uri` rejected | Not in `--allowed-origins`. The debug-level log message names the exact URI that was rejected | +| Social login returns `{"error":"invalid redirect uri"}` for a client whose callback is on another origin, while password login works | Before 2.4.6, `/oauth_login` checked the client's callback against `--allowed-origins`. Upgrade to 2.4.6 or later, where social login returns to `/app` and `/authorize` checks the client's registered redirect URIs — see [Social login inside an authorization request](#social-login-inside-an-authorization-request). Don't add the client's origin to `--allowed-origins` as a workaround | +| Server logs `social login completed an /authorize request without resuming through /authorize` | A login UI is sending the client's callback straight to `/oauth_login` — a deprecated path that skips consent. Point it at its own `/app` URL instead (see the same section) | | RP reports "unexpected ID Token nonce claim value" | The `nonce` sent to `/authorize` didn't round-trip into the ID token. A custom login UI must forward `nonce` (and the PKCE params) back to `/authorize` after authentication | | `form_post` blocked: "sending form data violates Content Security Policy" | A reverse proxy in front of Authorizer is injecting its own CSP header, overriding the `form_post` CSP Authorizer sets | diff --git a/docs/sdks/authorizer-react/components.md b/docs/sdks/authorizer-react/components.md index 20cf617..f7462f7 100644 --- a/docs/sdks/authorizer-react/components.md +++ b/docs/sdks/authorizer-react/components.md @@ -214,6 +214,71 @@ const LoginPage = () => { } ``` +### Where the user lands after social login + +The button sends a `redirect_uri` to `/oauth_login`, and the user comes back there after the provider. Which one it sends depends on where the component is rendered: + +| Rendered on | `redirect_uri` sent | Then | +|---|---|---| +| Your own site (`https://myapp.example.com/login`) | `urlProps.redirect_uri` (or `redirectURL`), else `redirectURL` from `AuthorizerProvider` | The user returns to that URL. Its origin must be in the server's `--allowed-origins` | +| Authorizer's login page (`/app/` or `/app/signup`), opened by `/authorize`. _2.2.1 and later, needs Authorizer 2.4.6+_ | The page's own URL, with the `/authorize` parameters in the query string. `urlProps.redirect_uri` is ignored here, because it's the client's callback | `/app` resumes the request through `/authorize`, which checks the client's registered callback, shows consent if needed, and sends the code | +| Authorizer's login page with no `/authorize` request | `redirectURL` from `AuthorizerProvider` (`/app`) | The user is signed in on `/app` | + +The second row needs no code: the hosted login page gets it by upgrading Authorizer, since the page and the server ship in the same image. It only switches on when the page is served from the same origin as `authorizerURL` and its URL carries both `client_id` and `response_type`, so a login page on your own site never changes behaviour. See [Social login inside an authorization request](../../core/oauth2-oidc#social-login-inside-an-authorization-request) for the full flow. + +#### Example: social login on your own site + +```jsx +import { + AuthorizerProvider, + AuthorizerSocialLogin, +} from '@authorizerdev/authorizer-react' + +// Server: --allowed-origins=https://myapp.example.com +const App = () => ( + + {/* Returns to https://myapp.example.com/dashboard after GitHub/Google/… */} + + +) +``` + +#### Example: a different landing page per screen + +```jsx +// Send users who sign in from the pricing page back to checkout. + +``` + +#### Example: Authorizer's own login page during an authorization request + +Nothing to configure. When `/authorize` opens the login page as + +``` +https://auth.example.com/app/?client_id=c1&response_type=code&redirect_uri=https://app.example.org/callback&state=st-123&nonce=n-456&… +``` + +the GitHub button navigates to + +``` +https://auth.example.com/oauth_login/github?state=st-123&scope=openid%20profile%20email + &redirect_uri=https%3A%2F%2Fauth.example.com%2Fapp%2F%3Fclient_id%3Dc1%26response_type%3Dcode%26… +``` + +and the code reaches `https://app.example.org/callback` through `/authorize`, even though that origin isn't in `--allowed-origins`. With `response_mode=fragment` (`/app/#client_id=…`), the parameters move into the query string, because a server redirect can't carry a fragment. + ## `AuthorizerPasskeyLogin` Renders a "Sign in with a passkey" button that performs a passwordless, usernameless (discoverable-credential) WebAuthn login. Unlike social login, there is no backend config flag for this — it renders purely based on browser support (`isWebauthnSupported()`), and renders nothing when the browser can't do WebAuthn or when the org enforces MFA (`config.is_mfa_enforced`), since passkey-as-sole-factor would let a user skip a required second factor. Make sure this is used as Child of `AuthorizerProvider`.