# SSO with Google and GitHub

> Single sign-on (SSO): let people sign in with Google or GitHub, with PKCE, and link those sign-ins to an existing account.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/auth/oauth-providers/

For agents: Provider sign-in: Google (OpenID Connect) and GitHub (OAuth 2.0); signInWithOAuth({provider, redirectTo}) → GET /auth/v1/authorize with PKCE S256 → provider → /auth/v1/callback → app gets ?code= → exchangeCodeForSession (grant_type=pkce). Configure client id and secret per project; callback URL https://<ref>.data.sypnia.com/auth/v1/callback. Identities stored in sypnia_auth.identities; linking by verified e-mail only.

**SSO (single sign-on)**: people sign in with an account they already have, so your project does not have to send e-mails at all: the provider has already checked the person. The providers are **Google** (OpenID Connect) and **GitHub** (OAuth 2.0).

## Set it up

1. Create an OAuth app at the provider:
   - Google: Google Cloud console → APIs & Services → Credentials → OAuth client ID (Web application).
   - GitHub: Settings → Developer settings → OAuth Apps → New.
2. As the **callback** (redirect) URL, use `https://<ref>.data.sypnia.com/auth/v1/callback` (self-hosted: your engine's address).
3. In the console → Auth → Providers (or `PATCH /auth/v1/admin/settings`), paste the client id and secret. The secret is stored encrypted.

## The flow (PKCE)

```ts
await sypnia.auth.signInWithOAuth({ provider: 'google', options: { redirectTo: 'https://myapp.com/auth/done' } });
// on /auth/done:
await sypnia.auth.exchangeCodeForSession(new URL(location.href).searchParams.get('code')!);
```

1. The SDK makes a random **code verifier**, keeps it, and opens `/auth/v1/authorize?provider=google&code_challenge=…&code_challenge_method=S256`.
2. The engine sends the person to Google, with its own `state` and `nonce`.
3. Google returns to `/auth/v1/callback`; the engine checks `state`, exchanges the code with Google and reads the verified e-mail.
4. The engine redirects to your `redirectTo` with a one-time `?code=`.
5. Your app exchanges that code **and the verifier** for a session. Someone who steals the code cannot use it without the verifier.

## Identities and linking

A sign-in with a provider is an **identity** (`sypnia_auth.identities`). One user can have several: e-mail, Google and GitHub.

- If the provider gives a **verified** e-mail that matches an existing user, the identity is linked to that user.
- An **unverified** e-mail is never linked; a new user is created instead.
- A signed-in user can link another provider with `linkIdentity({ provider })` and unlink it, as long as one way to sign in remains.
