@ahhaohho/auth-middleware 2.3.1 → 2.3.3

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.3.1",
3
+ "version": "2.3.3",
4
4
  "description": "Shared authentication and authorization middleware for ahhaohho microservices",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -38,7 +38,7 @@ function authenticateJWT(req, res, next) {
38
38
  if (err || !user) {
39
39
  return res.status(401).json({
40
40
  error: 'Unauthorized',
41
- message: err?.message || info?.message || 'Invalid or expired token'
41
+ message: info?.message || 'Invalid or expired token'
42
42
  });
43
43
  }
44
44
 
@@ -69,7 +69,7 @@ function authenticateRefresh(req, res, next) {
69
69
  if (err || !user) {
70
70
  return res.status(401).json({
71
71
  error: 'Invalid refresh token',
72
- message: err?.message || info?.message || 'Invalid or expired refresh token'
72
+ message: info?.message || 'Invalid or expired refresh token'
73
73
  });
74
74
  }
75
75
 
@@ -163,7 +163,7 @@ async function authenticateHybrid(req, res, next) {
163
163
  if (refreshErr || !refreshUser) {
164
164
  return res.status(401).json({
165
165
  error: 'Unauthorized',
166
- message: refreshErr?.message || 'Both access and refresh tokens are invalid'
166
+ message: 'Both access and refresh tokens are invalid'
167
167
  });
168
168
  }
169
169
 
@@ -196,7 +196,7 @@ async function authenticateHybrid(req, res, next) {
196
196
  console.error('[@ahhaohho/auth-middleware] Failed to generate new token:', tokenError.message);
197
197
  return res.status(500).json({
198
198
  error: 'Token generation error',
199
- message: tokenError.message
199
+ message: 'Failed to generate new access token'
200
200
  });
201
201
  }
202
202
  })(req, res, next);
@@ -1,6 +1,19 @@
1
1
  const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager');
2
2
  const redisManager = require('../config/redis');
3
3
 
4
+ /**
5
+ * NODE_ENV에 따라 시크릿에서 올바른 키를 선택
6
+ * Secret 구조: { dev: "...", staging: "...", prod: "..." }
7
+ * @param {object} secret - AWS Secrets Manager에서 가져온 시크릿 객체
8
+ * @returns {string|undefined}
9
+ */
10
+ function resolveKeyByEnv(secret) {
11
+ const env = process.env.NODE_ENV;
12
+ if (env === 'production') return secret.prod;
13
+ if (env === 'staging') return secret.staging;
14
+ return secret.dev;
15
+ }
16
+
4
17
  /**
5
18
  * AWS Secrets Manager에서 JWT 키 가져오기
6
19
  *
@@ -56,7 +69,7 @@ class SecretManager {
56
69
  }
57
70
 
58
71
  const secret = JSON.parse(response.SecretString);
59
- const currentKey = secret.current || secret.jwt_secret_key || secret.dev;
72
+ const currentKey = secret.current || secret.jwt_secret_key || resolveKeyByEnv(secret);
60
73
 
61
74
  // previous 키 결정: secret 내 previous 필드 → AWSPREVIOUS 버전 순서
62
75
  let previousKey = secret.previous || null;
@@ -70,7 +83,7 @@ class SecretManager {
70
83
  const prevResponse = await this.client.send(prevCommand);
71
84
  if (prevResponse.SecretString) {
72
85
  const prevSecret = JSON.parse(prevResponse.SecretString);
73
- const prevCandidate = prevSecret.current || prevSecret.jwt_secret_key || prevSecret.dev;
86
+ const prevCandidate = prevSecret.current || prevSecret.jwt_secret_key || resolveKeyByEnv(prevSecret);
74
87
  // 이전 키가 현재 키와 다를 때만 사용
75
88
  if (prevCandidate && prevCandidate !== currentKey) {
76
89
  previousKey = prevCandidate;