Skip to content

Commit 976e7ad

Browse files
committed
fix todo0 app auth
1 parent b38bb0f commit 976e7ad

File tree

2 files changed

+11
-68
lines changed

2 files changed

+11
-68
lines changed

packages/todo0/src/app-server.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ interface AppServerConfig {
1919
oktaClientId: string;
2020
oktaClientSecret: string;
2121
oktaRedirectUri: string;
22-
expectedAudience: string;
2322
}
2423

2524
/**
@@ -34,8 +33,7 @@ function validateAppEnv(): AppServerConfig {
3433
'OKTA_ISSUER',
3534
'OKTA_CLIENT_ID',
3635
'OKTA_CLIENT_SECRET',
37-
'OKTA_REDIRECT_URI',
38-
'EXPECTED_AUDIENCE',
36+
'OKTA_REDIRECT_URI'
3937
];
4038

4139
for (const varName of requiredVars) {
@@ -78,24 +76,19 @@ function validateAppEnv(): AppServerConfig {
7876

7977
// Return typed configuration object
8078
return {
81-
port: parseInt(process.env.PORT || '5001', 10),
79+
port: parseInt(process.env.PORT || '', 10),
8280
oktaIssuer: process.env.OKTA_ISSUER!,
8381
oktaClientId: process.env.OKTA_CLIENT_ID!,
8482
oktaClientSecret: process.env.OKTA_CLIENT_SECRET!,
8583
oktaRedirectUri: process.env.OKTA_REDIRECT_URI!,
86-
expectedAudience: process.env.EXPECTED_AUDIENCE!,
8784
};
8885
}
8986

9087
// Validate environment and get typed configuration
9188
const config = validateAppEnv();
9289

9390
// Create configured modules
94-
const requireAuth = createRequireAuth({
95-
oktaIssuer: config.oktaIssuer,
96-
oktaClientId: config.oktaClientId,
97-
expectedAudience: config.expectedAudience,
98-
});
91+
const requireAuth = createRequireAuth();
9992

10093
const authRouter = createAuthRouter({
10194
oktaIssuer: config.oktaIssuer,

packages/todo0/src/middleware/requireAuth.ts

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,14 @@
11
import { Request, Response, NextFunction } from 'express';
2-
import OktaJwtVerifier from '@okta/jwt-verifier';
3-
4-
export interface AuthMiddlewareConfig {
5-
oktaIssuer: string;
6-
oktaClientId: string;
7-
expectedAudience: string;
8-
}
9-
10-
export function createRequireAuth(config: AuthMiddlewareConfig) {
11-
const { oktaIssuer, oktaClientId, expectedAudience } = config;
12-
13-
console.log('🔐 Auth Middleware Configuration:');
14-
console.log(` Issuer: ${oktaIssuer}`);
15-
console.log(` Client ID: ${oktaClientId}`);
16-
console.log(` Expected Audience: ${expectedAudience}`);
17-
18-
const oktaJwtVerifier = new OktaJwtVerifier({
19-
issuer: oktaIssuer,
20-
clientId: oktaClientId,
21-
assertClaims: {
22-
aud: expectedAudience,
23-
},
24-
});
252

3+
export function createRequireAuth() {
264
return async function requireAuth(req: Request, res: Response, next: NextFunction) {
27-
// 1. Check for session-based authentication
28-
if (req.session && (req.session as any).access_token) {
29-
console.log('✓ Session-based authentication found');
30-
return next();
31-
}
32-
33-
// 2. Check for Bearer token authentication
34-
const authHeader = req.headers.authorization || '';
35-
const match = authHeader.match(/^Bearer (.+)$/);
36-
if (!match) {
37-
console.log('✗ No Bearer token found in Authorization header');
38-
return res.status(401).json({ error: 'Missing or invalid Authorization header or session' });
39-
}
40-
41-
const accessToken = match[1];
42-
console.log('🔍 Verifying access token...');
43-
44-
try {
45-
// Verify the access token
46-
const jwt = await oktaJwtVerifier.verifyAccessToken(accessToken, expectedAudience);
47-
48-
console.log('✅ Token verified successfully');
49-
console.log(' Subject:', jwt.claims.sub);
50-
console.log(' Scopes:', jwt.claims.scp);
51-
console.log(' Client ID:', jwt.claims.cid);
52-
53-
(req as any).user = jwt.claims;
54-
return next();
55-
} catch (err: any) {
56-
console.error('❌ Token verification failed:', err.message);
57-
return res.status(401).json({
58-
error: 'Invalid or expired token',
59-
details: err.message
60-
});
61-
}
5+
// 1. Check for session-based authentication
6+
if (req.session && (req.session as any).access_token) {
7+
console.log('✓ Session-based authentication found');
8+
return next();
9+
} else {
10+
return res.status(401).json({ error: 'Missing or invalid session' });
11+
}
6212
};
6313
}
6414

0 commit comments

Comments
 (0)