diff --git a/src/lib/routes/admin-api/api-token.ts b/src/lib/routes/admin-api/api-token.ts index a494c2859077..6e7547a751c5 100644 --- a/src/lib/routes/admin-api/api-token.ts +++ b/src/lib/routes/admin-api/api-token.ts @@ -42,7 +42,7 @@ import { getStandardResponses, } from '../../openapi/util/standard-responses'; import { ProxyService } from '../../services/proxy-service'; -import { extractUsername } from '../../util'; +import { extractUserId, extractUsername } from '../../util'; import { OperationDeniedError } from '../../error'; interface TokenParam { @@ -312,6 +312,7 @@ export class ApiTokenController extends Controller { const token = await this.apiTokenService.createApiToken( createToken, extractUsername(req), + extractUserId(req), ); this.openApiService.respondWithValidation( 201, diff --git a/src/lib/routes/admin-api/project/api-token.ts b/src/lib/routes/admin-api/project/api-token.ts index 3a1631794f3d..2eb4b23d4e8f 100644 --- a/src/lib/routes/admin-api/project/api-token.ts +++ b/src/lib/routes/admin-api/project/api-token.ts @@ -27,7 +27,7 @@ import { ProjectService, ProxyService, } from '../../../services'; -import { extractUsername } from '../../../util'; +import { extractUserId, extractUsername } from '../../../util'; import { IAuthRequest } from '../../unleash-types'; import Controller from '../../controller'; import { Logger } from '../../../logger'; @@ -190,6 +190,7 @@ export class ProjectApiTokenController extends Controller { const token = await this.apiTokenService.createApiToken( createToken, extractUsername(req), + extractUserId(req), ); this.openApiService.respondWithValidation( 201, diff --git a/src/lib/util/extract-user.test.ts b/src/lib/util/extract-user.test.ts index 85494876e1d5..322f88a25e2c 100644 --- a/src/lib/util/extract-user.test.ts +++ b/src/lib/util/extract-user.test.ts @@ -1,5 +1,6 @@ +import { SYSTEM_USER } from '../../lib/types'; import { IUser } from '../server-impl'; -import { extractUsernameFromUser } from './extract-user'; +import { extractUserIdFromUser, extractUsernameFromUser } from './extract-user'; describe('extractUsernameFromUser', () => { test('Should return the email if it exists', () => { @@ -19,14 +20,16 @@ describe('extractUsernameFromUser', () => { expect(extractUsernameFromUser(user)).toBe(user.username); }); - test('Should return "unknown" if neither email nor username exists', () => { + test('Should return the system user if neither email nor username exists', () => { const user = {} as IUser; - expect(extractUsernameFromUser(user)).toBe('unknown'); + expect(extractUsernameFromUser(user)).toBe(SYSTEM_USER.username); + expect(extractUserIdFromUser(user)).toBe(SYSTEM_USER.id); }); - test('Should return "unknown" if user is null', () => { + test('Should return the system user if user is null', () => { const user = null as unknown as IUser; - expect(extractUsernameFromUser(user)).toBe('unknown'); + expect(extractUsernameFromUser(user)).toBe(SYSTEM_USER.username); + expect(extractUserIdFromUser(user)).toBe(SYSTEM_USER.id); }); }); diff --git a/src/lib/util/extract-user.ts b/src/lib/util/extract-user.ts index d58e8d8946c4..2f12be9dabf4 100644 --- a/src/lib/util/extract-user.ts +++ b/src/lib/util/extract-user.ts @@ -1,14 +1,19 @@ +import { SYSTEM_USER } from '../../lib/types'; import { IAuthRequest, IUser } from '../server-impl'; export function extractUsernameFromUser(user: IUser): string { - return user?.email || user?.username || 'unknown'; + return user?.email || user?.username || SYSTEM_USER.username; } export function extractUsername(req: IAuthRequest): string { return extractUsernameFromUser(req.user); } -export const extractUserId = (req: IAuthRequest) => req.user.id; +export const extractUserIdFromUser = (user: IUser) => + user?.id || SYSTEM_USER.id; + +export const extractUserId = (req: IAuthRequest) => + extractUserIdFromUser(req.user); export const extractUserInfo = (req: IAuthRequest) => ({ id: extractUserId(req),