Add session abstraction, mobile bearer auth, and pluggable SSO
Refactor authentication into a provider-agnostic session layer and build
two new auth surfaces on top of it, without changing local password/TOTP
behavior. Every flow now issues sessions through
sessionService.createSession(user, authMethod).
Part 1 — Session abstraction (backward-compatible refactor):
- New server/src/auth/: token.js (JWT/cookie primitives), session.service.js
(create/validate/partial-TOTP/revoke), session.middleware.js
(attachSession/requireAuth/requireRole). utils/auth.js is now a thin
compat facade so existing imports are unchanged.
Part 2 — Mobile bearer auth (additive):
- /api/v1/auth/mobile/{login,refresh,logout}: short-lived access JWT +
long-lived refresh token, stored hashed and rotated on use, in a new
mobile_refresh_tokens table. Reuses web bot-scoring/backoff; single-request
TOTP. token.signToken gains a backward-compatible expiresIn option.
Part 3 — Pluggable SSO (Google, Discord, generic OIDC):
- OAuth2Provider base + built-in Google/Discord (fixed endpoints) + generic
OIDC, a registry with health/validation, PKCE+CSRF transaction state, and
discovery (GET /auth/providers), start/link/callback routes.
- Link-only policy: SSO signs in only to an already-linked account; external
identities are never auto-provisioned. Client secrets encrypted at rest
(AES-256-GCM, utils/secretBox.js). Admin CRUD (/admin/auth/providers) and
account linking (/admin/account/identities). New auth_providers +
user_identities tables.
Frontend:
- Login page renders provider buttons from /auth/providers (inline SVG icons,
graceful with zero providers). New Authentication admin view
(Local/Google/Discord/Custom). Account page linked-accounts section.
Tests: 83 passing (session, mobile, providers, registry, secretBox, ssoState,
ssoCallback) — all DB-free via fetch mocks + model stubs. README + .env.example
updated.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
34
server/src/auth/providers/google.provider.js
Normal file
34
server/src/auth/providers/google.provider.js
Normal file
@@ -0,0 +1,34 @@
|
||||
// Built-in Google provider (OAuth2 / OpenID Connect). Endpoints are hardcoded —
|
||||
// admins configure only Enabled + Client ID + Client Secret. Uses the OIDC
|
||||
// userinfo endpoint; `sub` is Google's stable per-user id.
|
||||
|
||||
const OAuth2Provider = require('./oauth2.provider')
|
||||
|
||||
class GoogleProvider extends OAuth2Provider {
|
||||
constructor(config = {}) {
|
||||
super({ kind: 'google', name: 'Google', ...config, id: config.id || 'google' })
|
||||
}
|
||||
|
||||
authEndpoint() {
|
||||
return 'https://accounts.google.com/o/oauth2/v2/auth'
|
||||
}
|
||||
tokenEndpoint() {
|
||||
return 'https://oauth2.googleapis.com/token'
|
||||
}
|
||||
userinfoEndpoint() {
|
||||
return 'https://openidconnect.googleapis.com/v1/userinfo'
|
||||
}
|
||||
scopeString() {
|
||||
return 'openid email profile'
|
||||
}
|
||||
authParams() {
|
||||
// Online access (no refresh token needed for login), and let the user pick
|
||||
// an account rather than silently reusing a signed-in one.
|
||||
return { access_type: 'online', prompt: 'select_account' }
|
||||
}
|
||||
normalizeProfile(p = {}) {
|
||||
return { subject: p.sub, email: p.email || null, name: p.name || p.email || null }
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GoogleProvider
|
||||
Reference in New Issue
Block a user