Modul server/fork/auth (Config, Session-Cookie, OIDC-Flow, Kontozuordnung), Einhängen in server/app.js, Client-Helfer für Login/Logout, PUT /api/user/rename für OIDC-Sessions gesperrt (A01). Tests gegen Mock-IdP. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
5.0 KiB
JavaScript
109 lines
5.0 KiB
JavaScript
import { mapAccount, sanitizeUsername } from './accountMapping.js';
|
|
import { createMemoryUsers } from './testUsers.js';
|
|
|
|
const ISS = 'https://idp';
|
|
const identity = (sub, claims = {})=>({ iss: ISS, sub, claims });
|
|
let users; let authors;
|
|
const deps = (extra = {})=>({
|
|
users, usernameClaim : 'preferred_username', linkByEmail : false, logger : { error: jest.fn() },
|
|
brewAuthorExists : async (n)=>authors.has(n), ...extra
|
|
});
|
|
beforeEach(()=>{ users = createMemoryUsers(); authors = new Set(); });
|
|
|
|
describe('sanitizeUsername', ()=>{
|
|
test.each([
|
|
['alice', 'alice'], ['Alice Smith', 'Alice-Smith'], ['a@b.test', 'a-user'.length ? 'a' + 'user' : ''],
|
|
['müller', 'm-ller'], ['x', 'xuser'], ['', 'user'], ['..bob..', 'bob'], ['a'.repeat(60), 'a'.repeat(40)]
|
|
])('%j -> %j', (i, o)=>expect(sanitizeUsername(i)).toBe(o));
|
|
test('E-Mail wird nie als Benutzername übernommen', ()=>{
|
|
expect(sanitizeUsername('carol@example.test')).toBe('carol');
|
|
});
|
|
});
|
|
|
|
describe('mapAccount', ()=>{
|
|
test('Neuanlage aus preferred_username', async ()=>{
|
|
const u = await mapAccount(identity('s1', { preferred_username: 'alice', email: 'A@x.test', email_verified: true, name: 'Alice' }), deps());
|
|
expect(u).toMatchObject({ username: 'alice', oidcIssuer: ISS, oidcSub: 's1', email: 'a@x.test', emailVerified: true, displayName: 'Alice' });
|
|
});
|
|
|
|
test('Fallback: E-Mail-Teil, dann "user"', async ()=>{
|
|
expect((await mapAccount(identity('s1', { email: 'bob@x.test' }), deps())).username).toBe('bob');
|
|
expect((await mapAccount(identity('s2', {}), deps())).username).toBe('user');
|
|
});
|
|
|
|
test('eigener Username-Claim', async ()=>{
|
|
const u = await mapAccount(identity('s1', { upn: 'dora', preferred_username: 'ignored' }), deps({ usernameClaim: 'upn' }));
|
|
expect(u.username).toBe('dora');
|
|
});
|
|
|
|
test('zweiter Login: gleicher Benutzer, username unverändert, Profil aktualisiert', async ()=>{
|
|
const a = await mapAccount(identity('s1', { preferred_username: 'alice', email: 'a@x.test' }), deps());
|
|
const b = await mapAccount(identity('s1', { preferred_username: 'alice-neu', email: 'neu@x.test', email_verified: true }), deps());
|
|
expect(b.id).toBe(a.id);
|
|
expect(b.username).toBe('alice');
|
|
expect(b.email).toBe('neu@x.test');
|
|
expect(b.lastLoginAt).toBeInstanceOf(Date);
|
|
expect(users.rows).toHaveLength(1);
|
|
});
|
|
|
|
test('gleicher sub bei anderem Issuer ist ein anderer Benutzer', async ()=>{
|
|
await mapAccount(identity('s1', { preferred_username: 'alice' }), deps());
|
|
const other = await mapAccount({ iss: 'https://other', sub: 's1', claims: { preferred_username: 'alice' } }, deps());
|
|
expect(other.username).toBe('alice-2');
|
|
});
|
|
|
|
test('Namenskollision mit Benutzer und mit Brew-Autor: Suffix', async ()=>{
|
|
await users.create({ username: 'alice' });
|
|
authors.add('alice-2');
|
|
const u = await mapAccount(identity('s9', { preferred_username: 'alice' }), deps());
|
|
expect(u.username).toBe('alice-3');
|
|
});
|
|
|
|
test('Suffix hält die Maximallänge ein', async ()=>{
|
|
const long = 'a'.repeat(40);
|
|
await users.create({ username: long });
|
|
const u = await mapAccount(identity('s1', { preferred_username: long }), deps());
|
|
expect(u.username).toHaveLength(40);
|
|
expect(u.username.endsWith('-2')).toBe(true);
|
|
});
|
|
|
|
describe('E-Mail-Verknüpfung', ()=>{
|
|
const claims = { preferred_username: 'neuer-name', email: 'carol@x.test', email_verified: true };
|
|
|
|
test('aus (Standard): keine Verknüpfung, Neuanlage', async ()=>{
|
|
const pre = await users.create({ username: 'carol', email: 'carol@x.test' });
|
|
const u = await mapAccount(identity('s1', claims), deps());
|
|
expect(u.id).not.toBe(pre.id);
|
|
expect(u.username).toBe('neuer-name');
|
|
});
|
|
|
|
test('an + verifiziert: bindet vorab angelegten Benutzer, Name bleibt', async ()=>{
|
|
const pre = await users.create({ username: 'carol', email: 'carol@x.test' });
|
|
const u = await mapAccount(identity('s1', claims), deps({ linkByEmail: true }));
|
|
expect(u).toMatchObject({ id: pre.id, username: 'carol', oidcSub: 's1', oidcIssuer: ISS });
|
|
});
|
|
|
|
test('an, aber E-Mail nicht verifiziert: keine Verknüpfung', async ()=>{
|
|
const pre = await users.create({ username: 'carol', email: 'carol@x.test' });
|
|
const u = await mapAccount(identity('s1', { ...claims, email_verified: false }), deps({ linkByEmail: true }));
|
|
expect(u.id).not.toBe(pre.id);
|
|
});
|
|
|
|
test('an, Benutzer hat schon OIDC-Bindung: keine Übernahme', async ()=>{
|
|
const pre = await users.create({ username: 'carol', email: 'carol@x.test', oidcIssuer: ISS, oidcSub: 'andere' });
|
|
const u = await mapAccount(identity('s1', claims), deps({ linkByEmail: true }));
|
|
expect(u.id).not.toBe(pre.id);
|
|
expect((await users.findById(pre.id)).oidcSub).toBe('andere');
|
|
});
|
|
|
|
test('an, mehrere Treffer: keine Verknüpfung, Fehler wird geloggt', async ()=>{
|
|
await users.create({ username: 'carol1', email: 'carol@x.test' });
|
|
await users.create({ username: 'carol2', email: 'carol@x.test' });
|
|
const d = deps({ linkByEmail: true });
|
|
const u = await mapAccount(identity('s1', claims), d);
|
|
expect(u.username).toBe('neuer-name');
|
|
expect(d.logger.error).toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|