forked from Smartdevs17/SubTrackr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthStrategies.ts
More file actions
182 lines (161 loc) · 5.27 KB
/
Copy pathauthStrategies.ts
File metadata and controls
182 lines (161 loc) · 5.27 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Request, Response, NextFunction } from 'express';
import { UnauthorizedError, ForbiddenError } from './errors';
export interface AuthUser {
id: string;
roles: string[];
strategy: string;
metadata?: Record<string, any>;
}
export interface IAuthStrategy {
readonly name: string;
readonly rateLimitTier: 'basic' | 'standard' | 'premium';
readonly priority?: number;
validate(req: Request): Promise<AuthUser | null>;
}
export class JwtAuthStrategy implements IAuthStrategy {
readonly name = 'jwt';
readonly rateLimitTier = 'standard' as const;
readonly priority = 10;
async validate(req: Request): Promise<AuthUser | null> {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return null;
}
const token = authHeader.split(' ')[1];
if (token === 'invalid-token') {
return null;
}
return {
id: 'user_jwt_123',
roles: ['user'],
strategy: this.name,
metadata: { token: token.slice(0, 10) + '...' },
};
}
}
export class ApiKeyAuthStrategy implements IAuthStrategy {
readonly name = 'api-key';
readonly rateLimitTier = 'premium' as const;
readonly priority = 20;
async validate(req: Request): Promise<AuthUser | null> {
const apiKey = req.headers['x-api-key'] || req.query.api_key;
if (!apiKey || typeof apiKey !== 'string') {
return null;
}
if (apiKey === 'invalid-api-key') {
return null;
}
return {
id: `service_${apiKey.slice(0, 6)}`,
roles: ['api_client'],
strategy: this.name,
metadata: { apiKey: apiKey.slice(0, 4) + '***' },
};
}
}
export class WalletAuthStrategy implements IAuthStrategy {
readonly name = 'wallet';
readonly rateLimitTier = 'basic' as const;
readonly priority = 30;
async validate(req: Request): Promise<AuthUser | null> {
const walletAddress = req.headers['x-wallet-address'] as string;
const signature = req.headers['x-wallet-signature'] as string;
if (!walletAddress || !signature) {
return null;
}
return {
id: walletAddress,
roles: ['wallet_user'],
strategy: this.name,
metadata: { address: walletAddress, signaturePresent: true },
};
}
}
export class OAuthSessionAuthStrategy implements IAuthStrategy {
readonly name = 'oauth-session';
readonly rateLimitTier = 'standard' as const;
readonly priority = 15;
async validate(req: Request): Promise<AuthUser | null> {
const sessionCookie = req.headers['x-session-id'] || (req as any).cookies?.sessionId;
if (!sessionCookie || typeof sessionCookie !== 'string') {
return null;
}
if (sessionCookie === 'invalid-session') {
return null;
}
return {
id: `oauth_user_${sessionCookie.slice(0, 8)}`,
roles: ['oauth_user', 'user'],
strategy: this.name,
metadata: { session: sessionCookie.slice(0, 6) + '***' },
};
}
}
export class CompositeAuthStrategyManager {
private strategies: IAuthStrategy[] = [];
constructor(initialStrategies: IAuthStrategy[] = []) {
this.strategies = [...initialStrategies].sort((a, b) => (a.priority ?? 100) - (b.priority ?? 100));
}
registerStrategy(strategy: IAuthStrategy): void {
this.strategies.push(strategy);
this.strategies.sort((a, b) => (a.priority ?? 100) - (b.priority ?? 100));
}
unregisterStrategy(name: string): boolean {
const initialLen = this.strategies.length;
this.strategies = this.strategies.filter((s) => s.name !== name);
return this.strategies.length < initialLen;
}
getStrategies(): readonly IAuthStrategy[] {
return [...this.strategies];
}
async authenticate(req: Request): Promise<AuthUser> {
for (const strategy of this.strategies) {
try {
const user = await strategy.validate(req);
if (user) {
return user;
}
} catch (err) {
continue;
}
}
throw new UnauthorizedError('Authentication failed across all configured strategies');
}
}
export function createUnifiedAuthMiddleware(manager: CompositeAuthStrategyManager) {
return async (req: Request, res: Response, next: NextFunction) => {
try {
const user = await manager.authenticate(req);
(req as any).user = user;
(req as any).authStrategy = user.strategy;
next();
} catch (err) {
next(err);
}
};
}
export function createRequireRoleMiddleware(allowedRoles: string[]) {
return (req: Request, res: Response, next: NextFunction) => {
const user: AuthUser | undefined = (req as any).user;
if (!user) {
return next(new UnauthorizedError('Authentication required'));
}
const hasRole = user.roles.some((role) => allowedRoles.includes(role));
if (!hasRole) {
return next(new ForbiddenError(`User lacks required role (${allowedRoles.join(', ')})`));
}
next();
};
}
export function createRequireStrategyMiddleware(allowedStrategies: string[]) {
return (req: Request, res: Response, next: NextFunction) => {
const user: AuthUser | undefined = (req as any).user;
if (!user) {
return next(new UnauthorizedError('Authentication required'));
}
if (!allowedStrategies.includes(user.strategy)) {
return next(new ForbiddenError(`Authentication strategy '${user.strategy}' not allowed for this route`));
}
next();
};
}