-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from kir-dev/authentication
Authentication
- Loading branch information
Showing
44 changed files
with
1,030 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This .env file is used only when using docker compose | ||
# When not using compose, the components load their own .env files | ||
|
||
# postgres | ||
POSTGRES_DB: pek-infinity | ||
POSTGRES_USER: pek-infinity | ||
POSTGRES_PASSWORD: pek-infinity | ||
POSTGRES_HOST: postgres | ||
|
||
# backend | ||
FRONTEND_CALLBACK: http://localhost:3000/login/jwt | ||
JWT_SECRET: secret | ||
AUTHSCH_CLIENT_ID: | ||
AUTHSCH_CLIENT_SECRET: | ||
POSTGRES_PRISMA_URL: postgres://pek-infinity:pek-infinity@postgres/pek-infinity | ||
POSTGRES_URL_NON_POOLING: postgres://pek-infinity:pek-infinity@postgres/pek-infinity | ||
|
||
# frontend | ||
NEXT_PUBLIC_API_URL: http://localhost:3300 | ||
NEXT_PUBLIC_PRIVATE_API_URL: http://backend:3300 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -46,5 +46,5 @@ COPY --from=builder /app/backend/dist/ ./ | |
|
||
|
||
|
||
EXPOSE 3000 | ||
EXPOSE 3300 | ||
CMD ["node", "./main.js"] |
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,5 @@ | ||
AUTHSCH_CLIENT_ID= | ||
AUTHSCH_CLIENT_SECRET= | ||
FRONTEND_CALLBACK=http://localhost:3000/login/jwt | ||
JWT_SECRET=secret | ||
PORT=3300 |
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,19 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { AuthController } from './auth.controller'; | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
}).compile(); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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,45 @@ | ||
import { CurrentUser } from '@kir-dev/passport-authsch'; | ||
import { Controller, Get, Redirect, UseGuards } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiQuery, ApiResponse } from '@nestjs/swagger'; | ||
|
||
import { FRONTEND_CALLBACK } from '@/config/environment.config'; | ||
|
||
import { AuthService } from './auth.service'; | ||
import { UserDto } from './entities/user.entity'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
@UseGuards(AuthGuard('authsch')) | ||
@Get('login') | ||
@ApiResponse({ | ||
status: 302, | ||
description: 'Redirects to the AuthSch login page.', | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
login() {} | ||
|
||
@Get('callback') | ||
@UseGuards(AuthGuard('authsch')) | ||
@Redirect() | ||
@ApiResponse({ | ||
status: 302, | ||
description: 'Redirects to the frontend with the JWT in the query string.', | ||
}) | ||
@ApiQuery({ name: 'code', required: true }) | ||
oauthRedirect(@CurrentUser() user: UserDto) { | ||
const jwt = this.authService.login(user); | ||
return { | ||
url: `${FRONTEND_CALLBACK}?jwt=${jwt}`, | ||
}; | ||
} | ||
|
||
@Get('me') | ||
@UseGuards(AuthGuard('jwt')) | ||
@ApiResponse({ type: UserDto }) | ||
me(@CurrentUser() user: UserDto): UserDto { | ||
return user; | ||
} | ||
} |
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,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
|
||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthSchStrategy } from './authsch.strategy'; | ||
import { JwtStrategy } from './jwt.strategy'; | ||
|
||
@Module({ | ||
providers: [AuthService, AuthSchStrategy, JwtStrategy], | ||
controllers: [AuthController], | ||
imports: [PassportModule, JwtModule], | ||
}) | ||
export class AuthModule {} |
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,19 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService], | ||
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,18 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
import { JWT_SECRET } from '@/config/environment.config'; | ||
|
||
import { UserDto } from './entities/user.entity'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor(private jwtService: JwtService) {} | ||
|
||
login(user: UserDto): string { | ||
return this.jwtService.sign(user, { | ||
secret: JWT_SECRET, | ||
expiresIn: '7 days', | ||
}); | ||
} | ||
} |
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,31 @@ | ||
import { | ||
AuthSchProfile, | ||
AuthSchScope, | ||
Strategy, | ||
} from '@kir-dev/passport-authsch'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
|
||
import { | ||
AUTHSCH_CLIENT_ID, | ||
AUTHSCH_CLIENT_SECRET, | ||
} from '@/config/environment.config'; | ||
|
||
import { UserDto } from './entities/user.entity'; | ||
|
||
@Injectable() | ||
export class AuthSchStrategy extends PassportStrategy(Strategy) { | ||
constructor() { | ||
super({ | ||
clientId: AUTHSCH_CLIENT_ID, | ||
clientSecret: AUTHSCH_CLIENT_SECRET, | ||
scopes: [AuthSchScope.PROFILE, AuthSchScope.PEK_PROFILE], | ||
}); | ||
} | ||
|
||
validate(userProfile: AuthSchProfile): Promise<UserDto> { | ||
return Promise.resolve({ | ||
name: userProfile.fullName, | ||
}); | ||
} | ||
} |
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,6 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class UserDto { | ||
@ApiProperty() | ||
name: string; | ||
} |
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 { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
|
||
import { JWT_SECRET } from '@/config/environment.config'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor() { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: JWT_SECRET, | ||
}); | ||
} | ||
|
||
validate(payload: unknown): unknown { | ||
return payload; | ||
} | ||
} |
Oops, something went wrong.