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>
40 lines
1.7 KiB
JavaScript
40 lines
1.7 KiB
JavaScript
// In-Memory-UserRepository mit demselben Vertrag wie server/storage/mongo/users.js (Test-Hilfe)
|
|
import { randomUUID } from 'crypto';
|
|
|
|
export const createMemoryUsers = ()=>{
|
|
const rows = [];
|
|
const plain = (u)=>u && ({ ...u });
|
|
return {
|
|
rows,
|
|
async create(u) {
|
|
if(rows.some((r)=>r.username === u.username)) throw new Error('duplicate username');
|
|
if(u.oidcSub && rows.some((r)=>r.oidcIssuer === u.oidcIssuer && r.oidcSub === u.oidcSub)) throw new Error('duplicate oidc');
|
|
const row = {
|
|
id : randomUUID(), username : u.username, oidcIssuer : u.oidcIssuer ?? null, oidcSub : u.oidcSub ?? null,
|
|
email : u.email ? u.email.toLowerCase() : null, emailVerified : !!u.emailVerified, displayName : u.displayName ?? null,
|
|
createdAt : new Date(), lastLoginAt : null
|
|
};
|
|
rows.push(row);
|
|
return plain(row);
|
|
},
|
|
async findById(id) { return plain(rows.find((r)=>r.id === id)) ?? null; },
|
|
async findByUsername(n) { return plain(rows.find((r)=>r.username === n)) ?? null; },
|
|
async usernameExists(n) { return rows.some((r)=>r.username === n); },
|
|
async findByOidc(i, s) { return plain(rows.find((r)=>r.oidcIssuer === i && r.oidcSub === s)) ?? null; },
|
|
async findUnlinkedByEmail(e) { return rows.filter((r)=>r.email === e.toLowerCase() && !r.oidcSub).map(plain); },
|
|
async update(id, patch) {
|
|
const row = rows.find((r)=>r.id === id);
|
|
if(!row) return null;
|
|
const { id: _i, username: _u, ...rest } = patch;
|
|
Object.entries(rest).forEach(([k, v])=>{ if(v !== undefined) row[k] = k === 'email' ? v.toLowerCase() : v; });
|
|
return plain(row);
|
|
},
|
|
async delete(id) {
|
|
const i = rows.findIndex((r)=>r.id === id);
|
|
if(i < 0) return false;
|
|
rows.splice(i, 1);
|
|
return true;
|
|
}
|
|
};
|
|
};
|