Player accounts backend: schema, registration, self-service, SSO provision

- Widen users.role enum to include 'player'; make password_hash nullable;
  add email/email_verified/status/last_login_ip; pin username _ci collation.
- POST /auth/register (honeypot + registerLimiter + botScore, reserved-name
  blocklist, duplicate->409, auto-login). player_registration setting gates it.
- SSO auto-provision in finishLogin (setting-gated); return/portal-aware SSO
  redirects for the player portal; status refusal on login + requireAuth.
- New /player self-service group (account, change username/password, TOTP,
  identities), reusing account.controller; accountChangeLimiter.
- Admin: 'player' role + status/email on user create/update, role/status audit,
  player_registration enum validation, derived public registration flags.
- usernamePolicy module (reserved, sanitize, derive, dedup) + unit tests;
  extend SSO callback tests. 133 server tests green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019rao86n5cXpwAyjdBFEshV
This commit is contained in:
2026-07-06 01:36:51 -05:00
parent cdd916e199
commit f8bcc7f6a3
18 changed files with 934 additions and 55 deletions

View File

@@ -14,6 +14,7 @@ const users = require('../src/model/users/users.model')
const activity = require('../src/model/activity/activity.model')
const authProviders = require('../src/model/authProviders/authProviders.model')
const userIdentities = require('../src/model/userIdentities/userIdentities.model')
const settings = require('../src/model/settings/settings.model')
const registry = require('../src/auth/providers/registry')
const totp = require('../src/utils/totp')
const db = require('../src/utils/db')
@@ -34,6 +35,9 @@ beforeEach(() => {
userIdentities.link = async () => 1
users.getById = async (id) => ({ id, username: 'alice', role: 'admin' })
users.recordLogin = async () => {} // avoid the real DB on the success path
// Default: registration closed, so login stays strictly link-only unless a
// test opts into SSO sign-up.
settings.getRegistrationMode = async () => 'disabled'
})
function mockRes() {
@@ -87,6 +91,39 @@ test('UNLINKED identity → no session, redirect to not_linked (link-only policy
assert.equal(logged.length, 0)
})
test('UNLINKED identity + SSO sign-up enabled → auto-provisions a player and logs in', async () => {
settings.getRegistrationMode = async () => 'both'
userIdentities.findByProviderSubject = async () => null
let created = null
users.createUser = async (args) => {
created = args
return { id: 42, username: args.username, role: 'player', status: 'active' }
}
let linkArgs = null
userIdentities.link = async (args) => { linkArgs = args; return 1 }
const tx = ssoState.createTx({ provider: 'google', mode: 'login' })
const res = mockRes()
await ssoCtrl.callback(makeReq(tx), res)
assert.equal(created.role, 'player')
assert.equal(created.email, 'alice@example.com')
assert.equal(linkArgs.userId, 42)
assert.ok(res.cookies[token.COOKIE_NAME], 'session cookie set for the new player')
assert.equal(res.redirectedTo, '/admin')
// Both the provision and the login are audited.
assert.deepEqual(logged.map((e) => e.action), ['auth.sso.provision', 'auth.sso.login'])
})
test('UNLINKED identity from the player portal lands back in /account', async () => {
settings.getRegistrationMode = async () => 'both'
userIdentities.findByProviderSubject = async () => null
users.createUser = async (args) => ({ id: 43, username: args.username, role: 'player', status: 'active' })
const tx = ssoState.createTx({ provider: 'google', mode: 'login', returnTo: '/account' })
const res = mockRes()
await ssoCtrl.callback(makeReq(tx), res)
assert.equal(res.redirectedTo, '/account')
})
test('link mode → identity linked to the acting user, redirect to account', async () => {
let linkArgs = null
userIdentities.link = async (args) => { linkArgs = args; return 1 }