How to sign in with OIDC
Dari docs/how-to/oidc-sign-in.md di repository framework.
Halaman ini belum diterjemahkan. Kamu sedang membaca versi bahasa Inggris.
The oidc guard of @samveys/auth signs users in with the Organization's identity provider, using the authorization code flow with PKCE. Which provider is used is still open. The guard works with any OpenID Connect provider, so the answer only changes configuration.
import { auth, oidcGuard, sessionGuard } from '@samveys/auth'
const users = ormUserProvider(User) // must implement findOrCreateFromClaims(claims)
const sso = oidcGuard({
issuer: process.env.OIDC_ISSUER!, // e.g. https://accounts.google.com
clientId: process.env.OIDC_CLIENT_ID!,
clientSecret: process.env.OIDC_CLIENT_SECRET,
redirectUri: 'https://app.example.com/auth/callback',
provider: users,
})
export const authPlugin = auth({ guards: { web: sessionGuard({ provider: users }), sso }, default: 'web' })
router.group({ use: ['session'] }, (web) => {
web.get('/auth/login', (ctx) => sso.redirectToProvider(ctx))
web.get('/auth/callback', async (ctx) => {
await sso.handleCallback(ctx) // validates, signs in, regenerates the session id
return ctx.response.redirect('/')
})
})| Provider | issuer |
|---|---|
| Google Workspace | https://accounts.google.com |
| Microsoft Entra ID | https://login.microsoftonline.com/<tenant-id>/v2.0 |
| Keycloak | https://<host>/realms/<realm> |
What is checked on the callback. Any failure is 400 SVS_E095, and no user is signed in.
statematches the value stored in the session.- The PKCE verifier is sent to the token endpoint.
- The ID token signature verifies against the provider's JWKS (RS256 or ES256).
issequals the issuer.audcontains the client id.expandiatare within 60 s of clock skew.noncematches the value stored in the session.
CI tests the whole flow against a local test provider that runs in the test process. The tests also check that a wrong state, a tampered signature, a wrong nonce, and a PKCE mismatch are all rejected.