SSO auth (1)

This commit is contained in:
2026-03-17 15:08:08 +08:00
parent a62fae913a
commit 7b4d2e789f
35 changed files with 1762 additions and 196 deletions

30
src/auth/auth.utils.ts Normal file
View File

@@ -0,0 +1,30 @@
import { createHash, randomBytes } from 'crypto';
import type { SsoProvider } from './dto/sso-provider';
export function randomOpaqueToken(size = 32): string {
return randomBytes(size).toString('base64url');
}
export function sha256(value: string): string {
return createHash('sha256').update(value).digest('hex');
}
export function isLoopbackRedirect(value: string): boolean {
try {
const parsed = new URL(value);
return (
(parsed.protocol === 'http:' || parsed.protocol === 'https:') &&
['127.0.0.1', 'localhost'].includes(parsed.hostname)
);
} catch {
return false;
}
}
export function asProviderName(value: SsoProvider): 'GOOGLE' | 'DISCORD' {
return value === 'google' ? 'GOOGLE' : 'DISCORD';
}
export function normalizeEmail(email: string): string {
return email.trim().toLowerCase();
}