Skip to content

Commit

Permalink
fix: create tokens createByUserId (#5968)
Browse files Browse the repository at this point in the history
## About the changes
Applies a fix that is already in main with some modifications to adapt
it to 5.8 release
  • Loading branch information
gastonfournier authored Jan 19, 2024
1 parent 228d994 commit 3dc12a5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/lib/routes/admin-api/api-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -312,6 +312,7 @@ export class ApiTokenController extends Controller {
const token = await this.apiTokenService.createApiToken(
createToken,
extractUsername(req),
extractUserId(req),
);
this.openApiService.respondWithValidation(
201,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/routes/admin-api/project/api-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -190,6 +190,7 @@ export class ProjectApiTokenController extends Controller {
const token = await this.apiTokenService.createApiToken(
createToken,
extractUsername(req),
extractUserId(req),
);
this.openApiService.respondWithValidation(
201,
Expand Down
13 changes: 8 additions & 5 deletions src/lib/util/extract-user.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
9 changes: 7 additions & 2 deletions src/lib/util/extract-user.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand Down

0 comments on commit 3dc12a5

Please sign in to comment.