-
-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: admin token calls get an admin token user (#5924)
## About the changes Whenever we get a call from an admin token we want to associate it with the [admin token user](https://github.com/Unleash/unleash/blob/4d42093a07a86dcff1766425c8429b20dade8e0d/src/lib/types/core.ts#L34-L41). This should give us the needed audit for this type of calls that currently were lacking a user id (we only stored a string with the token name in the event log). We consciously decided not to use `id` as the property to prevent any unforeseen side effects. The reason is that only `IUser` type has an id and adding an id to `IApiUser` might lead to confusion.
- Loading branch information
1 parent
6a5ce1f
commit ceaaf3d
Showing
11 changed files
with
231 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ADMIN_TOKEN_USER, IApiUser } from '../types'; | ||
import { createTestConfig } from '../../test/config/test-config'; | ||
import { createFakeEventsService } from '../../lib/features'; | ||
import { ApiTokenType } from '../../lib/types/models/api-token'; | ||
|
||
test('when using an admin token should get the username of the token and the id from internalAdminTokenUserId', async () => { | ||
const adminToken: IApiUser = { | ||
projects: ['*'], | ||
environment: '*', | ||
type: ApiTokenType.ADMIN, | ||
secret: '', | ||
username: 'admin-token-username', | ||
permissions: [], | ||
internalAdminTokenUserId: ADMIN_TOKEN_USER.id, | ||
}; | ||
const eventService = createFakeEventsService(createTestConfig()); | ||
const userDetails = eventService.getUserDetails(adminToken); | ||
expect(userDetails.createdBy).toBe('admin-token-username'); | ||
expect(userDetails.createdByUserId).toBe(ADMIN_TOKEN_USER.id); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import { IAuthRequest, IUser } from '../server-impl'; | ||
import { SYSTEM_USER } from '../../lib/types'; | ||
import { IApiRequest, IApiUser, IAuthRequest, IUser } from '../server-impl'; | ||
|
||
export function extractUsernameFromUser(user: IUser): string { | ||
return user?.email || user?.username || 'unknown'; | ||
export function extractUsernameFromUser(user: IUser | IApiUser): string { | ||
return (user as IUser)?.email || user?.username || SYSTEM_USER.username; | ||
} | ||
|
||
export function extractUsername(req: IAuthRequest): string { | ||
export function extractUsername(req: IAuthRequest | IApiRequest): string { | ||
return extractUsernameFromUser(req.user); | ||
} | ||
|
||
export const extractUserId = (req: IAuthRequest) => req.user.id; | ||
export const extractUserIdFromUser = (user: IUser | IApiUser) => | ||
(user as IUser)?.id || | ||
(user as IApiUser)?.internalAdminTokenUserId || | ||
SYSTEM_USER.id; | ||
|
||
export const extractUserInfo = (req: IAuthRequest) => ({ | ||
export const extractUserId = (req: IAuthRequest | IApiRequest) => | ||
extractUserIdFromUser(req.user); | ||
|
||
export const extractUserInfo = (req: IAuthRequest | IApiRequest) => ({ | ||
id: extractUserId(req), | ||
username: extractUsername(req), | ||
}); |
Oops, something went wrong.