Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create tokens createByUserId #5968

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading