mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(oidc): OIDC_USERNAME_CLAIM picks the claim a new account's username comes from
Providers like Pocket ID send the full name in 'name', which became 'JaneDoe'. OIDC_USERNAME_CLAIM=preferred_username builds the username from that claim instead. Unset, or with the claim empty, the old order applies (name, preferred_username, the email's local part), and it is only read when an account is created. (#1677)
This commit is contained in:
@@ -37,6 +37,7 @@ OIDC_DISPLAY_NAME=SSO # Label shown on the SSO login button
|
||||
OIDC_ONLY=true # Disable local password auth entirely (SSO only). Equivalent to setting password_login=false and password_registration=false in Admin > Settings.
|
||||
OIDC_ADMIN_CLAIM=groups # OIDC claim used to identify admin users
|
||||
OIDC_ADMIN_VALUE=app-trek-admins # Value of the OIDC claim that grants admin role
|
||||
OIDC_USERNAME_CLAIM= # Claim a new account's username comes from, e.g. preferred_username. Unset: name, then preferred_username
|
||||
OIDC_DISCOVERY_URL= # Override the auto-constructed OIDC discovery endpoint. Useful for providers (e.g. Authentik) that expose it at a non-standard path. Example: https://auth.example.com/application/o/trek/.well-known/openid-configuration
|
||||
OIDC_SCOPE=openid email profile # Fully overrides the default. Add extra scopes as needed (e.g. add groups if using OIDC_ADMIN_CLAIM)
|
||||
|
||||
|
||||
@@ -206,6 +206,9 @@ export function deriveOidc(raw: RawEnv) {
|
||||
only: parseBool(raw.OIDC_ONLY) === true,
|
||||
adminClaim: raw.OIDC_ADMIN_CLAIM || 'groups',
|
||||
adminValue: raw.OIDC_ADMIN_VALUE,
|
||||
// Unset keeps the old order (name, then preferred_username), so an instance
|
||||
// that never sets it names new accounts exactly as before (#1677).
|
||||
usernameClaim: raw.OIDC_USERNAME_CLAIM?.trim() || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@ export const envSchema = z.object({
|
||||
OIDC_ONLY: boolStr,
|
||||
OIDC_ADMIN_CLAIM: anyString,
|
||||
OIDC_ADMIN_VALUE: anyString,
|
||||
OIDC_USERNAME_CLAIM: anyString,
|
||||
|
||||
// SMTP
|
||||
SMTP_HOST: anyString,
|
||||
|
||||
@@ -607,6 +607,20 @@ export class OidcService implements OnModuleDestroy {
|
||||
// Find or create user by OIDC sub / email
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The claim a new account's username comes from (#1677). OIDC_USERNAME_CLAIM
|
||||
* names one, typically `preferred_username` for providers whose `name` is the
|
||||
* full "Jane Doe"; when it is unset, or the provider leaves that claim empty,
|
||||
* the old order applies. Only read when an account is created: a username the
|
||||
* user has changed since is never overwritten on a later login.
|
||||
*/
|
||||
private usernameSource(userInfo: OidcUserInfo): string | undefined {
|
||||
const claimKey = readEnv().oidc.usernameClaim;
|
||||
const claimed = claimKey ? userInfo[claimKey] : undefined;
|
||||
if (typeof claimed === 'string' && claimed.trim()) return claimed.trim();
|
||||
return userInfo.name || userInfo.preferred_username;
|
||||
}
|
||||
|
||||
findOrCreateUser(
|
||||
userInfo: OidcUserInfo,
|
||||
config: OidcConfig,
|
||||
@@ -617,7 +631,7 @@ export class OidcService implements OnModuleDestroy {
|
||||
// `oidc_error=' + result.error` pass-through if reached here.
|
||||
if (!userInfo.email) return { error: 'no_email' };
|
||||
const email = userInfo.email.trim().toLowerCase();
|
||||
const name = userInfo.name || userInfo.preferred_username || email.split('@')[0];
|
||||
const name = this.usernameSource(userInfo) || email.split('@')[0];
|
||||
const sub = userInfo.sub;
|
||||
const picture = safeOidcPicture(userInfo.picture);
|
||||
|
||||
|
||||
@@ -146,6 +146,12 @@ describe('deriveOidc', () => {
|
||||
expect(deriveOidc({ OIDC_ADMIN_CLAIM: 'roles' }).adminClaim).toBe('roles');
|
||||
});
|
||||
|
||||
it('usernameClaim stays unset unless a claim is named (#1677)', () => {
|
||||
expect(deriveOidc({}).usernameClaim).toBeUndefined();
|
||||
expect(deriveOidc({ OIDC_USERNAME_CLAIM: ' ' }).usernameClaim).toBeUndefined();
|
||||
expect(deriveOidc({ OIDC_USERNAME_CLAIM: ' preferred_username ' }).usernameClaim).toBe('preferred_username');
|
||||
});
|
||||
|
||||
it('OIDC_ONLY coerces the boolean-like family', () => {
|
||||
expect(deriveOidc({ OIDC_ONLY: 'True' }).only).toBe(true);
|
||||
expect(deriveOidc({ OIDC_ONLY: '1' }).only).toBe(true);
|
||||
|
||||
@@ -145,6 +145,7 @@ beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
delete process.env.OIDC_ADMIN_VALUE;
|
||||
delete process.env.OIDC_ADMIN_CLAIM;
|
||||
delete process.env.OIDC_USERNAME_CLAIM;
|
||||
delete process.env.NODE_ENV;
|
||||
});
|
||||
|
||||
@@ -529,6 +530,48 @@ describe('findOrCreateUser', () => {
|
||||
expect((result as { user: any }).user.role).toBe('admin');
|
||||
});
|
||||
|
||||
it('OIDC-SVC-022b: without OIDC_USERNAME_CLAIM the username comes from name, as before (#1677)', () => {
|
||||
createUser(testDb, { email: 'someone@example.com' });
|
||||
const result = svc.findOrCreateUser(
|
||||
{ sub: 'sub-jane-1', email: 'jane@example.com', name: 'Jane Doe', preferred_username: 'jane' },
|
||||
MOCK_CONFIG
|
||||
);
|
||||
expect((result as { user: { username: string } }).user.username).toBe('JaneDoe');
|
||||
});
|
||||
|
||||
it('OIDC-SVC-022c: OIDC_USERNAME_CLAIM picks the claim the username is built from (#1677)', () => {
|
||||
createUser(testDb, { email: 'someone@example.com' });
|
||||
process.env.OIDC_USERNAME_CLAIM = 'preferred_username';
|
||||
const result = svc.findOrCreateUser(
|
||||
{ sub: 'sub-jane-2', email: 'jane2@example.com', name: 'Jane Doe', preferred_username: 'jane.d' },
|
||||
MOCK_CONFIG
|
||||
);
|
||||
expect((result as { user: { username: string } }).user.username).toBe('jane.d');
|
||||
});
|
||||
|
||||
it('OIDC-SVC-022d: a named claim the provider leaves empty falls back to name, then the email (#1677)', () => {
|
||||
createUser(testDb, { email: 'someone@example.com' });
|
||||
process.env.OIDC_USERNAME_CLAIM = 'nickname';
|
||||
const withName = svc.findOrCreateUser(
|
||||
{ sub: 'sub-jane-3', email: 'jane3@example.com', name: 'Jane Three', nickname: ' ' },
|
||||
MOCK_CONFIG
|
||||
);
|
||||
expect((withName as { user: { username: string } }).user.username).toBe('JaneThree');
|
||||
const bare = svc.findOrCreateUser({ sub: 'sub-jane-4', email: 'j.four@example.com' }, MOCK_CONFIG);
|
||||
expect((bare as { user: { username: string } }).user.username).toBe('j.four');
|
||||
});
|
||||
|
||||
it('OIDC-SVC-022e: a later login never renames the account to the claim', () => {
|
||||
const { user } = createUser(testDb, { email: 'kept@example.com', username: 'chosen' });
|
||||
testDb.prepare('UPDATE users SET oidc_sub = ?, oidc_issuer = ? WHERE id = ?').run('sub-kept', MOCK_CONFIG.issuer, user.id);
|
||||
process.env.OIDC_USERNAME_CLAIM = 'preferred_username';
|
||||
const result = svc.findOrCreateUser(
|
||||
{ sub: 'sub-kept', email: 'kept@example.com', name: 'Kept', preferred_username: 'idp-name' },
|
||||
MOCK_CONFIG
|
||||
);
|
||||
expect((result as { user: { username: string } }).user.username).toBe('chosen');
|
||||
});
|
||||
|
||||
it('OIDC-SVC-024: returns registration_disabled error when registration is off', () => {
|
||||
createUser(testDb, { email: 'existing@example.com' });
|
||||
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allow_registration', 'false')").run();
|
||||
|
||||
Reference in New Issue
Block a user