|
| 1 | +import { Injectable, BadRequestException } from '@nestjs/common'; |
| 2 | +import { JwtService } from '@nestjs/jwt'; |
| 3 | +import { nanoid } from 'nanoid'; |
| 4 | +import { OAuthConfig, DEFAULT_OAUTH_CONFIG } from './types'; |
| 5 | +import { UserService } from 'omniboxd/user/user.service'; |
| 6 | + |
| 7 | +interface AuthorizationCode { |
| 8 | + code: string; |
| 9 | + userId: string; |
| 10 | + clientId: string; |
| 11 | + redirectUri: string; |
| 12 | + expiresAt: Date; |
| 13 | +} |
| 14 | + |
| 15 | +@Injectable() |
| 16 | +export class OAuthService { |
| 17 | + private config: OAuthConfig = DEFAULT_OAUTH_CONFIG; |
| 18 | + private authorizationCodes: Map<string, AuthorizationCode> = new Map(); |
| 19 | + |
| 20 | + constructor( |
| 21 | + private readonly jwtService: JwtService, |
| 22 | + private readonly userService: UserService, |
| 23 | + ) {} |
| 24 | + |
| 25 | + createAuthorizationCode( |
| 26 | + userId: string, |
| 27 | + clientId: string, |
| 28 | + redirectUri: string, |
| 29 | + ) { |
| 30 | + const code = nanoid(32); |
| 31 | + const expiresAt = new Date(); |
| 32 | + expiresAt.setMinutes( |
| 33 | + expiresAt.getMinutes() + this.config.authorizationCodeExpiryMinutes, |
| 34 | + ); |
| 35 | + |
| 36 | + const authCode: AuthorizationCode = { |
| 37 | + code, |
| 38 | + userId, |
| 39 | + clientId, |
| 40 | + redirectUri, |
| 41 | + expiresAt, |
| 42 | + }; |
| 43 | + |
| 44 | + this.cleanupExpiredCodes(); |
| 45 | + this.authorizationCodes.set(code, authCode); |
| 46 | + return code; |
| 47 | + } |
| 48 | + |
| 49 | + async exchangeCodeForTokens(code: string, clientId: string) { |
| 50 | + const authCode = this.authorizationCodes.get(code); |
| 51 | + |
| 52 | + if (!authCode || authCode.expiresAt < new Date()) { |
| 53 | + throw new BadRequestException('Invalid or expired authorization code'); |
| 54 | + } |
| 55 | + |
| 56 | + if (authCode.clientId !== clientId) { |
| 57 | + throw new BadRequestException('Invalid authorization code'); |
| 58 | + } |
| 59 | + |
| 60 | + this.authorizationCodes.delete(code); |
| 61 | + this.cleanupExpiredCodes(); |
| 62 | + return await this.generateAccessToken(authCode.userId, clientId); |
| 63 | + } |
| 64 | + |
| 65 | + private async generateAccessToken(userId: string, clientId: string) { |
| 66 | + const user = await this.userService.find(userId); |
| 67 | + if (!user) { |
| 68 | + throw new BadRequestException('Invalid user'); |
| 69 | + } |
| 70 | + const payload = { |
| 71 | + sub: userId, |
| 72 | + client_id: clientId, |
| 73 | + email: user.email, |
| 74 | + }; |
| 75 | + |
| 76 | + const accessToken = this.jwtService.sign(payload); |
| 77 | + |
| 78 | + return { |
| 79 | + access_token: accessToken, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + private cleanupExpiredCodes() { |
| 84 | + const now = new Date(); |
| 85 | + for (const [code, authCode] of this.authorizationCodes.entries()) { |
| 86 | + if (authCode.expiresAt < now) { |
| 87 | + this.authorizationCodes.delete(code); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments