Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

more SSO endpoints generalisations & added github sso boilerplate

Blue Mouse78e46eef5364
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index 8343adc3e..ef6b03106 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -42,9 +42,11 @@ import {
ChangePasswordDto,
ConfirmChangeEmailDto,
RequestChangeEmailDto,
- AuthUserGoogleDto,
+ SSOGetJWTByHashDto,
ProcessSSOCodeDto,
SSOGenerateDto,
+ SSOLinkDto,
+ SSOUnlinkDto,
} from './dtos'
import { SSOProviders } from './dtos/sso-generate.dto'
import { JwtAccessTokenGuard, JwtRefreshTokenGuard, RolesGuard } from './guards'
@@ -457,46 +459,49 @@ export class AuthController {
@Public()
// Validates the authorisation code and returns the JWT tokens
async getJWTByHash(
- @Body() body: AuthUserGoogleDto,
+ @Body() body: SSOGetJWTByHashDto,
@Headers() headers: unknown,
@Ip() ip: string,
): Promise<any> {
- await checkRateLimit(ip, 'g-sso-hash', 15, 1800)
+ await checkRateLimit(ip, 'sso-hash', 15, 1800)
- const { hash } = body
+ const { hash, provider } = body
- return this.authService.authenticateGoogle(hash, headers, ip)
+ return this.authService.authenticateSSO(hash, headers, ip, provider)
}
- @ApiOperation({ summary: 'Link Google to an existing account' })
+ @ApiOperation({ summary: 'Link SSO provider to an existing account' })
@ApiOkResponse({
- description: 'Google linked to an existing account',
+ description: 'SSO provider linked to an existing account',
})
@UseGuards(RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
- @Post('google/link_by_hash')
+ @Post('sso/link_by_hash')
public async linkGoogleToAccount(
- @Body() body: AuthUserGoogleDto,
+ @Body() body: SSOLinkDto,
@CurrentUserId() userId: string,
@Ip() ip: string,
): Promise<void> {
- await checkRateLimit(ip, 'g-sso-link', 15, 1800)
+ await checkRateLimit(ip, 'sso-link', 15, 1800)
- const { hash } = body
+ const { hash, provider } = body
- await this.authService.linkGoogleAccount(userId, hash)
+ await this.authService.linkSSOAccount(userId, hash, provider)
}
- @ApiOperation({ summary: 'Unlink Google from an existing account' })
+ @ApiOperation({ summary: 'Unlink SSO provider from an existing account' })
@ApiOkResponse({
- description: 'Google unlinked from an existing account',
+ description: 'SSO provider unlinked from an existing account',
})
@UseGuards(RolesGuard)
@Roles(UserType.CUSTOMER, UserType.ADMIN)
- @Delete('google/unlink')
- public async unlinkGoogleFromAccount(
+ @Delete('sso/unlink')
+ public async unlinkSSOFromAccount(
+ @Body() body: SSOUnlinkDto,
@CurrentUserId() userId: string,
): Promise<void> {
- await this.authService.unlinkGoogleAccount(userId)
+ const { provider } = body
+
+ await this.authService.unlinkSSOAccount(userId, provider)
}
}
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index c86722b5a..81f76a303 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -560,6 +560,18 @@ export class AuthService {
return { sub, email }
}
+ async authenticateSSO(ssoHash: string, headers: unknown, ip: string, provider: SSOProviders) {
+ if (provider === SSOProviders.GOOGLE) {
+ return await this.authenticateGoogle(ssoHash, headers, ip)
+ }
+
+ if (provider === SSOProviders.GITHUB) {
+ return await this.authenticateGithub(ssoHash, headers, ip)
+ }
+
+ throw new BadRequestException('Unknown SSO provider supplied')
+ }
+
async authenticateGoogle(ssoHash: string, headers: unknown, ip: string) {
const { sub, email } = await this.processHash(ssoHash)
@@ -641,6 +653,18 @@ export class AuthService {
)
}
+ async linkSSOAccount(userId: string, ssoHash: string, provider: SSOProviders) {
+ if (provider === SSOProviders.GOOGLE) {
+ await this.linkGoogleAccount(userId, ssoHash)
+ }
+
+ if (provider === SSOProviders.GITHUB) {
+ await this.linkGithubAccount(userId, ssoHash)
+ }
+
+ throw new BadRequestException('Unknown SSO provider supplied')
+ }
+
async linkGoogleAccount(userId: string, ssoHash: string) {
const { sub } = await this.processHash(ssoHash)
@@ -661,6 +685,18 @@ export class AuthService {
})
}
+ async unlinkSSOAccount(userId: string, provider: SSOProviders) {
+ if (provider === SSOProviders.GOOGLE) {
+ await this.unlinkGoogleAccount(userId)
+ }
+
+ if (provider === SSOProviders.GITHUB) {
+ await this.unlinkGithubAccount(userId)
+ }
+
+ throw new BadRequestException('Unknown SSO provider supplied')
+ }
+
async unlinkGoogleAccount(userId: string) {
const user = await this.userService.findUserById(userId)
@@ -763,4 +799,16 @@ export class AuthService {
// REDIS_SSO_SESSION_TIMEOUT,
// )
}
+
+ async unlinkGithubAccount(userId: string) {
+ // TODO
+ }
+
+ async linkGithubAccount(userId: string, ssoHash: string) {
+ // TODO
+ }
+
+ async authenticateGithub(ssoHash: string, headers: unknown, ip: string) {
+ // TODO
+ }
}
diff --git a/src/auth/dtos/google-auth-user.dto.ts b/src/auth/dtos/google-auth-user.dto.ts
deleted file mode 100644
index fd143693b..000000000
--- a/src/auth/dtos/google-auth-user.dto.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import { ApiProperty } from '@nestjs/swagger'
-import { IsUUID } from 'class-validator'
-
-export class AuthUserGoogleDto {
- @ApiProperty({
- description: 'SSO session identifier',
- example: '123e4567-e89b-12d3-a456-426614174000',
- })
- @IsUUID()
- hash: string
-}
diff --git a/src/auth/dtos/index.ts b/src/auth/dtos/index.ts
index eec02f96d..7afca67e8 100644
--- a/src/auth/dtos/index.ts
+++ b/src/auth/dtos/index.ts
@@ -7,6 +7,8 @@ export * from './reset-password.dto'
export * from './change-password.dto'
export * from './request-change-email.dto'
export * from './confirm-change-email.dto'
-export * from './google-auth-user.dto'
export * from './process-sso-code.dto'
export * from './sso-generate.dto'
+export * from './sso-get-jwt-by-hash.dto'
+export * from './sso-link.dto'
+export * from './sso-unlink.dto'
diff --git a/src/auth/dtos/sso-generate.dto.ts b/src/auth/dtos/sso-generate.dto.ts
index 85949e99c..c0e43ab99 100644
--- a/src/auth/dtos/sso-generate.dto.ts
+++ b/src/auth/dtos/sso-generate.dto.ts
@@ -12,5 +12,5 @@ export class SSOGenerateDto {
enum: SSOProviders,
})
@IsEnum(SSOProviders)
- provider: string
+ provider: SSOProviders
}
diff --git a/src/auth/dtos/sso-get-jwt-by-hash.dto.ts b/src/auth/dtos/sso-get-jwt-by-hash.dto.ts
new file mode 100644
index 000000000..e32363569
--- /dev/null
+++ b/src/auth/dtos/sso-get-jwt-by-hash.dto.ts
@@ -0,0 +1,20 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsEnum, IsUUID } from 'class-validator'
+
+import { SSOProviders } from './sso-generate.dto'
+
+export class SSOGetJWTByHashDto {
+ @ApiProperty({
+ description: 'SSO session identifier',
+ example: '123e4567-e89b-12d3-a456-426614174000',
+ })
+ @IsUUID()
+ hash: string
+
+ @ApiProperty({
+ description: 'SSO provider name',
+ enum: SSOProviders,
+ })
+ @IsEnum(SSOProviders)
+ provider: SSOProviders
+}
diff --git a/src/auth/dtos/sso-link.dto.ts b/src/auth/dtos/sso-link.dto.ts
new file mode 100644
index 000000000..5e76c2f80
--- /dev/null
+++ b/src/auth/dtos/sso-link.dto.ts
@@ -0,0 +1,20 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsEnum, IsUUID } from 'class-validator'
+
+import { SSOProviders } from './sso-generate.dto'
+
+export class SSOLinkDto {
+ @ApiProperty({
+ description: 'SSO session identifier',
+ example: '123e4567-e89b-12d3-a456-426614174000',
+ })
+ @IsUUID()
+ hash: string
+
+ @ApiProperty({
+ description: 'SSO provider name',
+ enum: SSOProviders,
+ })
+ @IsEnum(SSOProviders)
+ provider: SSOProviders
+}
diff --git a/src/auth/dtos/sso-unlink.dto.ts b/src/auth/dtos/sso-unlink.dto.ts
new file mode 100644
index 000000000..f45697a9b
--- /dev/null
+++ b/src/auth/dtos/sso-unlink.dto.ts
@@ -0,0 +1,13 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsEnum } from 'class-validator'
+
+import { SSOProviders } from './sso-generate.dto'
+
+export class SSOUnlinkDto {
+ @ApiProperty({
+ description: 'SSO provider name',
+ enum: SSOProviders,
+ })
+ @IsEnum(SSOProviders)
+ provider: SSOProviders
+}