@ahhaohho/auth-middleware 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahhaohho/auth-middleware",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "Shared authentication and authorization middleware for ahhaohho microservices",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -179,8 +179,6 @@ async function authenticateHybrid(req, res, next) {
179
179
 
180
180
  // 3. Refresh token 검증
181
181
  passport.authenticate('refresh', { session: false }, async (refreshErr, refreshUser, refreshInfo) => {
182
- console.log('[@ahhaohho/auth-middleware] 🔍 Refresh callback:', { hasError: !!refreshErr, hasUser: !!refreshUser, userId: refreshUser?.userId });
183
-
184
182
  if (refreshErr) {
185
183
  console.error('[@ahhaohho/auth-middleware] Refresh token error:', refreshErr.message);
186
184
  return res.status(500).json({
@@ -4,36 +4,37 @@ const { isBlacklisted } = require('../utils/blacklist');
4
4
  const jwt = require('jsonwebtoken');
5
5
 
6
6
  /**
7
- * 쿠키 또는 Bearer 헤더에서 토큰을 추출하는 커스텀 함수
8
- * 우선순위: 1. 쿠키 (flc_auth_token) 2. Authorization 헤더
7
+ * Bearer 헤더 또는 쿠키에서 토큰을 추출하는 커스텀 함수
8
+ * 우선순위: 1. Authorization 헤더 (Bearer) 2. 쿠키 (flc_auth_token)
9
+ *
10
+ * Bearer 헤더 우선: 클라이언트가 명시적으로 설정한 토큰이 가장 신선함.
11
+ * 쿠키는 브라우저가 자동 전송하므로 stale 토큰이 남아있을 수 있음.
9
12
  */
10
13
  const extractJwtFromRequest = (req) => {
11
- // 1. 쿠키에서 FLC 토큰 확인 (HttpOnly 쿠키 방식)
12
- if (req.cookies && req.cookies.flc_auth_token) {
13
- return req.cookies.flc_auth_token;
14
- }
15
-
16
- // 2. Authorization 헤더에서 토큰 확인 (기존 방식 호환)
14
+ // 1. Authorization 헤더에서 토큰 확인 (Bearer 방식 - 우선)
17
15
  const authHeader = req.headers.authorization || req.headers.Authorization;
18
16
 
19
- if (!authHeader) {
20
- return null;
21
- }
17
+ if (authHeader) {
18
+ // Bearer 접두사가 있는 경우
19
+ if (authHeader.startsWith('Bearer ')) {
20
+ return authHeader.substring(7);
21
+ }
22
22
 
23
- // Bearer 접두사가 있는 경우
24
- if (authHeader.startsWith('Bearer ')) {
25
- return authHeader.substring(7);
26
- }
23
+ // bearer (소문자)인 경우
24
+ if (authHeader.startsWith('bearer ')) {
25
+ return authHeader.substring(7);
26
+ }
27
27
 
28
- // bearer (소문자)인 경우
29
- if (authHeader.startsWith('bearer ')) {
30
- return authHeader.substring(7);
28
+ // Bearer 접두사 없이 토큰만 있는 경우
29
+ // JWT 형식인지 확인 (xxx.yyy.zzz 형태)
30
+ if (authHeader.split('.').length === 3) {
31
+ return authHeader;
32
+ }
31
33
  }
32
34
 
33
- // Bearer 접두사 없이 토큰만 있는 경우
34
- // JWT 형식인지 확인 (xxx.yyy.zzz 형태)
35
- if (authHeader.split('.').length === 3) {
36
- return authHeader;
35
+ // 2. 쿠키에서 FLC 토큰 확인 (HttpOnly 쿠키 방식 - 폴백)
36
+ if (req.cookies && req.cookies.flc_auth_token) {
37
+ return req.cookies.flc_auth_token;
37
38
  }
38
39
 
39
40
  return null;
@@ -77,23 +78,9 @@ function createJwtStrategy() {
77
78
  '[@ahhaohho/auth-middleware] ⚠️ Token verified with previous key (fallback)'
78
79
  );
79
80
  } catch (previousKeyError) {
80
- // 🚨 임시: invalid signature도 허용 (다음 앱 배포 전까지)
81
- if (currentKeyError.message.includes('invalid signature') || currentKeyError.message.includes('jwt malformed')) {
82
- console.warn('[@ahhaohho/auth-middleware] ⚠️ [TEMPORARY] Allowing invalid signature');
83
- request._jwtDecoded = { userId: 'unknown', userRole: 'guest' };
84
- request._jwtKeyUsed = 'bypassed';
85
- return done(null, keys.current);
86
- }
87
81
  return done(currentKeyError, false);
88
82
  }
89
83
  } else {
90
- // 🚨 임시: invalid signature도 허용 (다음 앱 배포 전까지)
91
- if (currentKeyError.message.includes('invalid signature') || currentKeyError.message.includes('jwt malformed')) {
92
- console.warn('[@ahhaohho/auth-middleware] ⚠️ [TEMPORARY] Allowing invalid signature');
93
- request._jwtDecoded = { userId: 'unknown', userRole: 'guest' };
94
- request._jwtKeyUsed = 'bypassed';
95
- return done(null, keys.current);
96
- }
97
84
  return done(currentKeyError, false);
98
85
  }
99
86
  }
@@ -3,19 +3,12 @@ const { verifyTokenWithFallback } = require('../utils/jwtValidator');
3
3
  const { isBlacklisted } = require('../utils/blacklist');
4
4
 
5
5
  /**
6
- * 쿠키 또는 헤더에서 Refresh Token 추출
7
- * 우선순위: 1. 쿠키 (flc_refresh_token) 2. refresh-token 헤더
6
+ * 헤더 또는 쿠키에서 Refresh Token 추출
7
+ * 우선순위: 1. refresh-token 헤더 2. 쿠키 (flc_refresh_token)
8
8
  * Bearer 접두사가 있든 없든 처리
9
9
  */
10
10
  function extractRefreshToken(req) {
11
- // 1. 쿠키에서 FLC 리프레시 토큰 확인 (HttpOnly 쿠키 방식)
12
- if (req && req.cookies && req.cookies.flc_refresh_token) {
13
- return req.cookies.flc_refresh_token;
14
- }
15
-
16
- // 2. 헤더에서 리프레시 토큰 확인 (기존 방식 호환)
17
- let token = null;
18
-
11
+ // 1. 헤더에서 리프레시 토큰 확인 (우선)
19
12
  if (req && req.headers) {
20
13
  let refreshToken = req.headers['refresh-token'] || req.headers['refreshtoken'];
21
14
 
@@ -27,11 +20,16 @@ function extractRefreshToken(req) {
27
20
  refreshToken = refreshToken.substring(7);
28
21
  }
29
22
 
30
- token = refreshToken.trim();
23
+ return refreshToken.trim();
31
24
  }
32
25
  }
33
26
 
34
- return token;
27
+ // 2. 쿠키에서 FLC 리프레시 토큰 확인 (폴백)
28
+ if (req && req.cookies && req.cookies.flc_refresh_token) {
29
+ return req.cookies.flc_refresh_token;
30
+ }
31
+
32
+ return null;
35
33
  }
36
34
 
37
35
  /**