@thinkingcat/auth-utils 1.0.24 → 1.0.26
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/dist/index.d.ts +3 -0
- package/dist/index.js +80 -38
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -130,11 +130,13 @@ export declare function encodeNextAuthToken(jwt: JWT, secret: string, maxAge?: n
|
|
|
130
130
|
export declare function setCustomTokens(response: ResponseLike, accessToken: string, refreshToken: string, options?: {
|
|
131
131
|
cookiePrefix?: string;
|
|
132
132
|
isProduction?: boolean;
|
|
133
|
+
cookieDomain?: string;
|
|
133
134
|
}): void;
|
|
134
135
|
export declare function setCustomTokens(response: ResponseLike, accessToken: string, options?: {
|
|
135
136
|
refreshToken?: string;
|
|
136
137
|
cookiePrefix?: string;
|
|
137
138
|
isProduction?: boolean;
|
|
139
|
+
cookieDomain?: string;
|
|
138
140
|
}): void;
|
|
139
141
|
/**
|
|
140
142
|
* NextAuth 세션 토큰만 설정
|
|
@@ -598,6 +600,7 @@ export declare function verifyAndRefreshTokenWithNextAuth(req: NextRequest, next
|
|
|
598
600
|
response?: NextResponse;
|
|
599
601
|
error?: string;
|
|
600
602
|
payload?: JWTPayload;
|
|
603
|
+
token?: JWT;
|
|
601
604
|
}>;
|
|
602
605
|
/**
|
|
603
606
|
* 기본 미들웨어 설정을 생성하는 함수
|
package/dist/index.js
CHANGED
|
@@ -259,15 +259,17 @@ function setCustomTokens(response, accessToken, optionsOrRefreshToken, options)
|
|
|
259
259
|
let refreshTokenValue;
|
|
260
260
|
let cookiePrefix;
|
|
261
261
|
let isProduction;
|
|
262
|
+
let cookieDomain;
|
|
262
263
|
if (typeof optionsOrRefreshToken === 'string') {
|
|
263
264
|
// 기존 방식: refreshToken이 문자열로 전달된 경우
|
|
264
265
|
refreshTokenValue = optionsOrRefreshToken;
|
|
265
|
-
const { cookiePrefix: prefix, isProduction: prod = false, } = options || {};
|
|
266
|
+
const { cookiePrefix: prefix, isProduction: prod = false, cookieDomain: domain, } = options || {};
|
|
266
267
|
if (!prefix) {
|
|
267
268
|
throw new Error('cookiePrefix is required');
|
|
268
269
|
}
|
|
269
270
|
cookiePrefix = prefix;
|
|
270
271
|
isProduction = prod;
|
|
272
|
+
cookieDomain = domain;
|
|
271
273
|
}
|
|
272
274
|
else {
|
|
273
275
|
// 새로운 방식: options 객체로 전달된 경우
|
|
@@ -278,27 +280,31 @@ function setCustomTokens(response, accessToken, optionsOrRefreshToken, options)
|
|
|
278
280
|
}
|
|
279
281
|
cookiePrefix = opts.cookiePrefix;
|
|
280
282
|
isProduction = opts.isProduction || false;
|
|
283
|
+
cookieDomain = opts.cookieDomain;
|
|
281
284
|
}
|
|
282
|
-
//
|
|
283
|
-
const
|
|
284
|
-
response.cookies.delete(accessTokenName);
|
|
285
|
-
response.cookies.set(accessTokenName, accessToken, {
|
|
285
|
+
// 쿠키 옵션 생성
|
|
286
|
+
const cookieOptions = {
|
|
286
287
|
httpOnly: true,
|
|
287
288
|
secure: isProduction,
|
|
288
289
|
sameSite: isProduction ? 'none' : 'lax',
|
|
289
|
-
maxAge: 15 * 60, // 15분
|
|
290
290
|
path: '/',
|
|
291
|
-
|
|
291
|
+
maxAge: 15 * 60, // access token: 15분
|
|
292
|
+
};
|
|
293
|
+
if (cookieDomain) {
|
|
294
|
+
cookieOptions.domain = cookieDomain;
|
|
295
|
+
}
|
|
296
|
+
// access_token 설정
|
|
297
|
+
const accessTokenName = `${cookiePrefix}_access_token`;
|
|
298
|
+
response.cookies.delete(accessTokenName);
|
|
299
|
+
response.cookies.set(accessTokenName, accessToken, cookieOptions);
|
|
292
300
|
// refresh_token 설정 (있는 경우)
|
|
293
301
|
if (refreshTokenValue) {
|
|
294
302
|
const refreshTokenName = `${cookiePrefix}_refresh_token`;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
path: '/',
|
|
301
|
-
});
|
|
303
|
+
const refreshCookieOptions = {
|
|
304
|
+
...cookieOptions,
|
|
305
|
+
maxAge: 30 * 24 * 60 * 60, // refresh token: 30일
|
|
306
|
+
};
|
|
307
|
+
response.cookies.set(refreshTokenName, refreshTokenValue, refreshCookieOptions);
|
|
302
308
|
}
|
|
303
309
|
}
|
|
304
310
|
/**
|
|
@@ -468,12 +474,14 @@ async function createAuthResponse(accessToken, secret, options) {
|
|
|
468
474
|
setCustomTokens(response, accessToken, refreshToken, {
|
|
469
475
|
cookiePrefix,
|
|
470
476
|
isProduction,
|
|
477
|
+
cookieDomain,
|
|
471
478
|
});
|
|
472
479
|
}
|
|
473
480
|
else {
|
|
474
481
|
setCustomTokens(response, accessToken, {
|
|
475
482
|
cookiePrefix,
|
|
476
483
|
isProduction,
|
|
484
|
+
cookieDomain,
|
|
477
485
|
});
|
|
478
486
|
}
|
|
479
487
|
debugLog('createAuthResponse', 'Custom tokens set successfully');
|
|
@@ -1259,7 +1267,23 @@ async function verifyAndRefreshTokenWithNextAuth(req, nextAuthToken, secret, opt
|
|
|
1259
1267
|
// NextAuth cookie와 access token이 모두 유효하면 통과
|
|
1260
1268
|
if (hasValidNextAuthToken && hasValidAccessToken) {
|
|
1261
1269
|
debugLog('verifyAndRefreshTokenWithNextAuth', 'Both NextAuth and access tokens are valid');
|
|
1262
|
-
|
|
1270
|
+
// payload 추출
|
|
1271
|
+
let payload;
|
|
1272
|
+
if (accessToken) {
|
|
1273
|
+
try {
|
|
1274
|
+
const secretBytes = new TextEncoder().encode(secret);
|
|
1275
|
+
const result = await (0, jose_1.jwtVerify)(accessToken, secretBytes);
|
|
1276
|
+
payload = result.payload;
|
|
1277
|
+
}
|
|
1278
|
+
catch {
|
|
1279
|
+
// 이미 검증됐으므로 실패하지 않을 것
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
return {
|
|
1283
|
+
isValid: true,
|
|
1284
|
+
token: nextAuthToken,
|
|
1285
|
+
payload
|
|
1286
|
+
};
|
|
1263
1287
|
}
|
|
1264
1288
|
// NextAuth cookie가 없거나 access token이 없으면 refresh 시도
|
|
1265
1289
|
if (refreshToken && (!hasValidNextAuthToken || !hasValidAccessToken)) {
|
|
@@ -1268,12 +1292,37 @@ async function verifyAndRefreshTokenWithNextAuth(req, nextAuthToken, secret, opt
|
|
|
1268
1292
|
...options,
|
|
1269
1293
|
forceRefresh: true,
|
|
1270
1294
|
});
|
|
1271
|
-
|
|
1295
|
+
// refresh 후 NextAuth token 재조회 (새로 생성된 cookie에서)
|
|
1296
|
+
let refreshedToken = null;
|
|
1297
|
+
if (authCheck.isValid && authCheck.payload) {
|
|
1298
|
+
// payload에서 JWT 생성
|
|
1299
|
+
refreshedToken = createNextAuthJWT(authCheck.payload, options.serviceId);
|
|
1300
|
+
}
|
|
1301
|
+
return {
|
|
1302
|
+
...authCheck,
|
|
1303
|
+
token: refreshedToken || undefined
|
|
1304
|
+
};
|
|
1272
1305
|
}
|
|
1273
1306
|
// 하나라도 유효하면 일단 통과 (refresh token이 없는 경우)
|
|
1274
1307
|
if (hasValidNextAuthToken || hasValidAccessToken) {
|
|
1275
1308
|
debugLog('verifyAndRefreshTokenWithNextAuth', 'At least one token is valid (no refresh token)');
|
|
1276
|
-
|
|
1309
|
+
// payload 추출
|
|
1310
|
+
let payload;
|
|
1311
|
+
if (accessToken && hasValidAccessToken) {
|
|
1312
|
+
try {
|
|
1313
|
+
const secretBytes = new TextEncoder().encode(secret);
|
|
1314
|
+
const result = await (0, jose_1.jwtVerify)(accessToken, secretBytes);
|
|
1315
|
+
payload = result.payload;
|
|
1316
|
+
}
|
|
1317
|
+
catch {
|
|
1318
|
+
// 무시
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
return {
|
|
1322
|
+
isValid: true,
|
|
1323
|
+
token: nextAuthToken || (payload ? createNextAuthJWT(payload, options.serviceId) : undefined),
|
|
1324
|
+
payload
|
|
1325
|
+
};
|
|
1277
1326
|
}
|
|
1278
1327
|
debugLog('verifyAndRefreshTokenWithNextAuth', 'No tokens available');
|
|
1279
1328
|
return { isValid: false, error: 'NO_TOKEN' };
|
|
@@ -1492,28 +1541,21 @@ async function handleMiddleware(req, config, options) {
|
|
|
1492
1541
|
const ssoBaseURL = options.ssoBaseURL;
|
|
1493
1542
|
return await redirectToSSOLogin(req, serviceId, ssoBaseURL);
|
|
1494
1543
|
}
|
|
1495
|
-
// 5. 토큰 확인
|
|
1496
|
-
let finalToken = token;
|
|
1497
|
-
|
|
1498
|
-
finalToken = await getNextAuthToken(req);
|
|
1499
|
-
}
|
|
1500
|
-
else if (!finalToken) {
|
|
1501
|
-
try {
|
|
1502
|
-
const { getToken } = await Promise.resolve().then(() => __importStar(require('next-auth/jwt')));
|
|
1503
|
-
finalToken = await getToken({ req, secret });
|
|
1504
|
-
}
|
|
1505
|
-
catch {
|
|
1506
|
-
// NextAuth가 없으면 null 유지
|
|
1507
|
-
}
|
|
1508
|
-
}
|
|
1509
|
-
// verifyAndRefreshToken이 성공했는데 NextAuth 토큰이 없으면, 자체 토큰을 사용
|
|
1544
|
+
// 5. 토큰 확인 - authCheck 결과 재사용 (중복 검증 제거)
|
|
1545
|
+
let finalToken = authCheck.token || token;
|
|
1546
|
+
// authCheck에서 토큰을 반환하지 않았지만 유효한 경우 (드문 케이스)
|
|
1510
1547
|
if (!finalToken && authCheck.isValid) {
|
|
1511
|
-
|
|
1512
|
-
if (
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1548
|
+
debugLog('handleMiddleware', 'authCheck valid but no token, trying to get NextAuth token');
|
|
1549
|
+
if (getNextAuthToken) {
|
|
1550
|
+
finalToken = await getNextAuthToken(req);
|
|
1551
|
+
}
|
|
1552
|
+
else {
|
|
1553
|
+
try {
|
|
1554
|
+
const { getToken } = await Promise.resolve().then(() => __importStar(require('next-auth/jwt')));
|
|
1555
|
+
finalToken = await getToken({ req, secret });
|
|
1556
|
+
}
|
|
1557
|
+
catch {
|
|
1558
|
+
// NextAuth가 없으면 null 유지
|
|
1517
1559
|
}
|
|
1518
1560
|
}
|
|
1519
1561
|
}
|
package/package.json
CHANGED