Repository navigation
feat(auth): router la connexion GET /api/v1/auth vers server-nestjs #2824
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
apps/server-nestjs/src/modules/auth/auth.controller.spec.ts
This file contains hidden or 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,41 @@ | ||
| import type { TestingModule } from '@nestjs/testing' | ||
| import type { User } from '@prisma/client' | ||
| import type { MockProxy } from 'vitest-mock-extended' | ||
| import { Test } from '@nestjs/testing' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
| import { mock } from 'vitest-mock-extended' | ||
| import { UserGuard } from '../infrastructure/permission/user/user.guard' | ||
| import { makeUser } from '../user/user-testing.utils' | ||
| import { toContractUser } from '../user/user.utils' | ||
| import { AuthController } from './auth.controller' | ||
| import { AuthUserService } from './auth.service' | ||
|
|
||
| describe('authController', () => { | ||
| let module: TestingModule | ||
| let controller: AuthController | ||
| let service: MockProxy<AuthUserService> | ||
|
|
||
| beforeEach(async () => { | ||
| service = mock<AuthUserService>() | ||
|
|
||
| module = await Test.createTestingModule({ | ||
| controllers: [AuthController], | ||
| providers: [ | ||
| { provide: AuthUserService, useValue: service }, | ||
| ], | ||
| }) | ||
| .overrideGuard(UserGuard) | ||
| .useValue({ canActivate: () => true }) | ||
| .compile() | ||
|
|
||
| controller = module.get<AuthController>(AuthController) | ||
| }) | ||
|
|
||
| it('routes an authenticated session to the service', async () => { | ||
| const user: User = makeUser() | ||
| service.getUser.mockResolvedValue(user) | ||
|
|
||
| expect(await controller.auth({ userId: user.id })).toEqual(toContractUser(user)) | ||
| expect(service.getUser).toHaveBeenCalledWith(user.id) | ||
| }) | ||
| }) |
This file contains hidden or 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,18 @@ | ||
| import type { User } from '@cpn-console/shared' | ||
| import type { UserContext } from '../infrastructure/auth/auth-user.decorator' | ||
| import { Controller, Get, Inject, UseGuards } from '@nestjs/common' | ||
| import { AuthUser } from '../infrastructure/auth/auth-user.decorator' | ||
| import { UserGuard } from '../infrastructure/permission/user/user.guard' | ||
| import { toContractUser } from '../user/user.utils' | ||
| import { AuthUserService } from './auth.service' | ||
|
|
||
| @Controller('api/v1/auth') | ||
| export class AuthController { | ||
| constructor(@Inject(AuthUserService) private readonly authUserService: AuthUserService) {} | ||
|
|
||
| @Get() | ||
| @UseGuards(UserGuard) | ||
| async auth(@AuthUser() user: UserContext): Promise<User> { | ||
| return toContractUser(await this.authUserService.getUser(user.userId)) | ||
|
StephaneTrebel marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or 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,13 @@ | ||
| import { Module } from '@nestjs/common' | ||
| import { AuthModule as InfrastructureAuthModule } from '../infrastructure/auth/auth.module' | ||
| import { DatabaseModule } from '../infrastructure/database/database.module' | ||
| import { UserPermissionModule } from '../infrastructure/permission/user/user.module' | ||
| import { AuthController } from './auth.controller' | ||
| import { AuthUserService } from './auth.service' | ||
|
|
||
| @Module({ | ||
| imports: [InfrastructureAuthModule, DatabaseModule, UserPermissionModule], | ||
| controllers: [AuthController], | ||
| providers: [AuthUserService], | ||
| }) | ||
| export class AuthModule {} |
This file contains hidden or 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,16 @@ | ||
| import type { User } from '@prisma/client' | ||
| import { Inject, Injectable, NotFoundException } from '@nestjs/common' | ||
| import { PrismaService } from '../infrastructure/database/prisma.service' | ||
|
|
||
| @Injectable() | ||
| export class AuthUserService { | ||
| constructor(@Inject(PrismaService) private readonly prisma: PrismaService) {} | ||
|
|
||
| async getUser(id: string): Promise<User> { | ||
| const user = await this.prisma.user.findUnique({ where: { id } }) | ||
| if (!user) { | ||
| throw new NotFoundException(`Unable to find user ${id}`) | ||
| } | ||
| return user | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
.../server-nestjs/src/modules/infrastructure/auth/keycloak-jwt/keycloak-jwt-queries.utils.ts
This file contains hidden or 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,68 @@ | ||
| import type { Prisma } from '@prisma/client' | ||
| import type { AuthRequirements } from '../auth.utils' | ||
|
|
||
| export const userSelect = { | ||
| id: true, | ||
| adminRoleIds: true, | ||
| type: true, | ||
| } satisfies Prisma.UserSelect | ||
|
|
||
| export const userIdSelect = { | ||
| id: true, | ||
| } satisfies Prisma.UserSelect | ||
|
|
||
| export type UserRecord = Prisma.UserGetPayload<{ select: ReturnType<typeof makeUserSelect> }> | ||
|
|
||
| export function makeUserSelect(requirements?: AuthRequirements): Prisma.UserSelect { | ||
| const includeAdminRoleIds = requirements?.includeAdminRoleIds ?? true | ||
| const includeUserType = requirements?.includeUserType ?? true | ||
| return { | ||
| id: true, | ||
| ...(includeAdminRoleIds ? { adminRoleIds: true } : {}), | ||
| ...(includeUserType ? { type: true } : {}), | ||
| } | ||
| } | ||
|
|
||
| export function upsertUser( | ||
| tx: Prisma.TransactionClient, | ||
| payload: { sub: string, email: string, given_name: string, family_name: string }, | ||
| select: Prisma.UserSelect, | ||
| ) { | ||
| return tx.user.upsert({ | ||
| where: { id: payload.sub }, | ||
| create: { | ||
| id: payload.sub, | ||
| email: payload.email, | ||
| firstName: payload.given_name, | ||
| lastName: payload.family_name, | ||
| adminRoleIds: [], | ||
| type: 'human', | ||
| lastLogin: new Date().toISOString(), | ||
| }, | ||
| update: { | ||
| email: payload.email, | ||
| firstName: payload.given_name, | ||
| lastName: payload.family_name, | ||
| lastLogin: new Date().toISOString(), | ||
| }, | ||
| select, | ||
| }) | ||
| } | ||
|
|
||
| // Roles match the token groups (legacy parity), the persisted ids, or are global. | ||
| function adminRoleWhere(groups: string[], adminRoleIds: string[]): Prisma.AdminRoleWhereInput { | ||
| return { | ||
| OR: [ | ||
| { oidcGroup: { in: groups } }, | ||
| { id: { in: adminRoleIds } }, | ||
| { type: 'global' }, | ||
| ], | ||
| } | ||
| } | ||
|
|
||
| export function listMatchingAdminRoles(tx: Prisma.TransactionClient, groups: string[], adminRoleIds: string[]) { | ||
| return tx.adminRole.findMany({ | ||
| where: adminRoleWhere(groups, adminRoleIds), | ||
| orderBy: { position: 'asc' }, | ||
| }) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.