All files / src/features/auth/providers local.provider.ts

87.5% Statements 21/24
75% Branches 15/20
100% Functions 5/5
87.5% Lines 21/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77              12x   12x     6x   6x         6x 2x     4x 3x     1x             2x 1x     1x               3x     3x 1x       2x 2x 2x 1x           1x       3x                  
import { Injectable, Logger } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { AuthProvider, AuthUser } from './auth-provider.interface'
import * as argon2 from 'argon2'
 
@Injectable()
export class LocalAuthProvider implements AuthProvider {
    private readonly logger = new Logger(LocalAuthProvider.name)
 
    constructor(private configService: ConfigService) {}
 
    async verify(credentials: Record<string, any>): Promise<AuthUser | null> {
        const adminEmail = this.configService.get<string>('ADMIN_EMAIL')
 
        Iif (!adminEmail) {
            this.logger.warn('ADMIN_EMAIL is not configured')
            return null
        }
 
        if (credentials.isTrusted) {
            return this.verifyTrustedProvider(credentials, adminEmail)
        }
 
        if (credentials.email === adminEmail && credentials.password) {
            return this.verifyPassword(credentials.password, adminEmail)
        }
 
        return null
    }
 
    private verifyTrustedProvider(
        credentials: Record<string, any>,
        adminEmail: string,
    ): AuthUser | null {
        if (credentials.email !== adminEmail) {
            return null
        }
 
        return this.createAdminUser(
            adminEmail,
            (credentials.name as string) || 'Administrator',
            credentials.picture as string,
        )
    }
 
    private async verifyPassword(password: string, adminEmail: string): Promise<AuthUser | null> {
        const adminPassHash = this.configService.get<string>('ADMIN_PASSWORD') || ''
 
        // 1. Plain Text Check
        if (password === adminPassHash) {
            return this.createAdminUser(adminEmail, 'Administrator')
        }
 
        // 2. Argon2 Check
        try {
            const isMatch = await argon2.verify(adminPassHash, password)
            if (isMatch) {
                return this.createAdminUser(adminEmail, 'Administrator')
            }
        } catch (e) {
            this.logger.error(`Error verifying password: ${e instanceof Error ? e.message : e}`)
        }
 
        return null
    }
 
    private createAdminUser(email: string, name: string, picture?: string): AuthUser {
        return {
            id: 'admin',
            email,
            role: 'ADMIN',
            name,
            picture,
        }
    }
}