@thinkingcat/auth-utils 1.0.4 → 1.0.5
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/README.md +26 -20
- package/dist/index.d.ts +22 -0
- package/dist/index.js +19 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# @thinkingcat/auth-
|
|
1
|
+
# @thinkingcat/auth-utils
|
|
2
2
|
|
|
3
|
-
ThinkingCat SSO 서비스를 위한 인증
|
|
3
|
+
ThinkingCat SSO 서비스를 위한 인증 유틸리티 패키지입니다. JWT 토큰 검증, NextAuth 세션 생성, 쿠키 설정, 역할 기반 접근 제어, 미들웨어 헬퍼 등의 광범위한 인증 기능을 제공합니다.
|
|
4
4
|
|
|
5
5
|
## 📑 목차 (Table of Contents)
|
|
6
6
|
|
|
@@ -35,19 +35,19 @@ ThinkingCat SSO 서비스를 위한 인증 토큰 유틸리티 패키지입니
|
|
|
35
35
|
### npm 사용
|
|
36
36
|
|
|
37
37
|
```bash
|
|
38
|
-
npm install @thinkingcat/auth-
|
|
38
|
+
npm install @thinkingcat/auth-utils
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
### yarn 사용
|
|
42
42
|
|
|
43
43
|
```bash
|
|
44
|
-
yarn add @thinkingcat/auth-
|
|
44
|
+
yarn add @thinkingcat/auth-utils
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
### pnpm 사용
|
|
48
48
|
|
|
49
49
|
```bash
|
|
50
|
-
pnpm add @thinkingcat/auth-
|
|
50
|
+
pnpm add @thinkingcat/auth-utils
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
## 📋 요구사항 (Requirements)
|
|
@@ -63,13 +63,13 @@ pnpm add @thinkingcat/auth-token-utils
|
|
|
63
63
|
npm에 배포된 패키지를 사용할 때는 **별도의 설정이 필요하지 않습니다**. 바로 사용할 수 있습니다.
|
|
64
64
|
|
|
65
65
|
```bash
|
|
66
|
-
npm install @thinkingcat/auth-
|
|
66
|
+
npm install @thinkingcat/auth-utils
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
```json
|
|
70
70
|
{
|
|
71
71
|
"dependencies": {
|
|
72
|
-
"@thinkingcat/auth-
|
|
72
|
+
"@thinkingcat/auth-utils": "^1.0.4"
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
```
|
|
@@ -87,7 +87,7 @@ npm install @thinkingcat/auth-token-utils
|
|
|
87
87
|
```typescript
|
|
88
88
|
/** @type {import('next').NextConfig} */
|
|
89
89
|
const nextConfig = {
|
|
90
|
-
transpilePackages: ["@thinkingcat/auth-
|
|
90
|
+
transpilePackages: ["@thinkingcat/auth-utils"],
|
|
91
91
|
// ... 기타 설정
|
|
92
92
|
};
|
|
93
93
|
|
|
@@ -107,7 +107,7 @@ module.exports = nextConfig;
|
|
|
107
107
|
```json
|
|
108
108
|
{
|
|
109
109
|
"dependencies": {
|
|
110
|
-
"@thinkingcat/auth-
|
|
110
|
+
"@thinkingcat/auth-utils": "^1.0.4"
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
```
|
|
@@ -134,7 +134,7 @@ import {
|
|
|
134
134
|
ServiceInfo,
|
|
135
135
|
ResponseLike,
|
|
136
136
|
JWTPayload,
|
|
137
|
-
} from "@thinkingcat/auth-
|
|
137
|
+
} from "@thinkingcat/auth-utils";
|
|
138
138
|
```
|
|
139
139
|
|
|
140
140
|
### 2. 환경 변수 설정
|
|
@@ -158,6 +158,12 @@ COOKIE_DOMAIN=.yourdomain.com # 선택사항
|
|
|
158
158
|
5. **쿠키 설정**: 자체 토큰 및 NextAuth 토큰 쿠키 설정
|
|
159
159
|
6. **리다이렉트 HTML**: 클라이언트 리다이렉트용 HTML 생성
|
|
160
160
|
7. **완전한 인증 응답**: 모든 인증 단계를 한 번에 처리
|
|
161
|
+
8. **미들웨어 헬퍼**: 에러 리다이렉트, 쿠키 삭제, SSO 로그인 리다이렉트 등
|
|
162
|
+
9. **역할 기반 접근 제어**: 경로별 역할 검증 및 접근 제어
|
|
163
|
+
10. **구독 검증**: 서비스 구독 상태 확인
|
|
164
|
+
11. **토큰 유효성 검사**: 토큰 만료 및 유효성 확인
|
|
165
|
+
12. **경로 체크**: 공개 경로, API 경로, 보호된 경로 확인
|
|
166
|
+
13. **통합 인증 체크**: NextAuth와 자체 토큰을 모두 확인하는 통합 함수
|
|
161
167
|
|
|
162
168
|
## 🔧 API 레퍼런스
|
|
163
169
|
|
|
@@ -412,7 +418,7 @@ import {
|
|
|
412
418
|
setCustomTokens,
|
|
413
419
|
createRedirectHTML,
|
|
414
420
|
JWTPayload,
|
|
415
|
-
} from "@thinkingcat/auth-
|
|
421
|
+
} from "@thinkingcat/auth-utils";
|
|
416
422
|
|
|
417
423
|
export async function GET(req: NextRequest) {
|
|
418
424
|
// URL 파라미터에서 토큰 가져오기 (초기 로그인 시)
|
|
@@ -458,7 +464,7 @@ export async function GET(req: NextRequest) {
|
|
|
458
464
|
|
|
459
465
|
```typescript
|
|
460
466
|
import { NextRequest, NextResponse } from "next/server";
|
|
461
|
-
import { verifyToken, JWTPayload } from "@thinkingcat/auth-
|
|
467
|
+
import { verifyToken, JWTPayload } from "@thinkingcat/auth-utils";
|
|
462
468
|
|
|
463
469
|
export async function GET(req: NextRequest) {
|
|
464
470
|
const secret = process.env.NEXTAUTH_SECRET!;
|
|
@@ -530,7 +536,7 @@ import {
|
|
|
530
536
|
createNextAuthJWT,
|
|
531
537
|
encodeNextAuthToken,
|
|
532
538
|
setNextAuthToken,
|
|
533
|
-
} from "@thinkingcat/auth-
|
|
539
|
+
} from "@thinkingcat/auth-utils";
|
|
534
540
|
|
|
535
541
|
export async function GET(req: NextRequest) {
|
|
536
542
|
const tokenParam = req.nextUrl.searchParams.get("token");
|
|
@@ -574,7 +580,7 @@ import {
|
|
|
574
580
|
setCustomTokens,
|
|
575
581
|
setNextAuthToken,
|
|
576
582
|
createRedirectHTML,
|
|
577
|
-
} from "@thinkingcat/auth-
|
|
583
|
+
} from "@thinkingcat/auth-utils";
|
|
578
584
|
|
|
579
585
|
export async function GET(req: NextRequest) {
|
|
580
586
|
const tokenParam = req.nextUrl.searchParams.get("token");
|
|
@@ -642,7 +648,7 @@ import {
|
|
|
642
648
|
setNextAuthToken,
|
|
643
649
|
createNextAuthJWT,
|
|
644
650
|
encodeNextAuthToken,
|
|
645
|
-
} from "@thinkingcat/auth-
|
|
651
|
+
} from "@thinkingcat/auth-utils";
|
|
646
652
|
|
|
647
653
|
export async function middleware(req: NextRequest) {
|
|
648
654
|
const secret = process.env.NEXTAUTH_SECRET!;
|
|
@@ -857,7 +863,7 @@ import type {
|
|
|
857
863
|
SSORefreshTokenResponse,
|
|
858
864
|
SSOGetRefreshTokenResponse,
|
|
859
865
|
SSORegisterResponse,
|
|
860
|
-
} from "@thinkingcat/auth-
|
|
866
|
+
} from "@thinkingcat/auth-utils";
|
|
861
867
|
|
|
862
868
|
// SSO API 호출 시 타입 사용
|
|
863
869
|
const response = await fetch("/api/sso/refresh", {
|
|
@@ -896,13 +902,13 @@ interface ResponseLike {
|
|
|
896
902
|
|
|
897
903
|
## 🐛 문제 해결 (Troubleshooting)
|
|
898
904
|
|
|
899
|
-
### 문제 1: "Cannot find module '@thinkingcat/auth-
|
|
905
|
+
### 문제 1: "Cannot find module '@thinkingcat/auth-utils'"
|
|
900
906
|
|
|
901
907
|
**해결 방법:**
|
|
902
908
|
|
|
903
909
|
```bash
|
|
904
910
|
# 패키지 재설치
|
|
905
|
-
npm install @thinkingcat/auth-
|
|
911
|
+
npm install @thinkingcat/auth-utils
|
|
906
912
|
|
|
907
913
|
# 또는 node_modules 삭제 후 재설치
|
|
908
914
|
rm -rf node_modules package-lock.json
|
|
@@ -932,8 +938,8 @@ npm install
|
|
|
932
938
|
|
|
933
939
|
## 📦 패키지 정보
|
|
934
940
|
|
|
935
|
-
- **패키지명**: `@thinkingcat/auth-
|
|
936
|
-
- **버전**: `1.0.
|
|
941
|
+
- **패키지명**: `@thinkingcat/auth-utils`
|
|
942
|
+
- **버전**: `1.0.4`
|
|
937
943
|
- **라이선스**: MIT
|
|
938
944
|
- **저장소**: npm registry
|
|
939
945
|
|
package/dist/index.d.ts
CHANGED
|
@@ -382,3 +382,25 @@ export declare function checkAuthentication(req: NextRequest, secret: string, op
|
|
|
382
382
|
response?: NextResponse;
|
|
383
383
|
error?: string;
|
|
384
384
|
}>;
|
|
385
|
+
/**
|
|
386
|
+
* NextAuth 토큰과 자체 토큰을 모두 확인하는 미들웨어용 함수
|
|
387
|
+
* NextAuth 토큰이 있으면 바로 통과, 없으면 자체 토큰을 확인
|
|
388
|
+
* @param req NextRequest 객체
|
|
389
|
+
* @param nextAuthToken NextAuth JWT 토큰 (null 가능)
|
|
390
|
+
* @param secret JWT 서명에 사용할 secret key
|
|
391
|
+
* @param options 옵션
|
|
392
|
+
* @returns 인증 결과
|
|
393
|
+
*/
|
|
394
|
+
export declare function verifyAndRefreshTokenWithNextAuth(req: NextRequest, nextAuthToken: JWT | null, secret: string, options?: {
|
|
395
|
+
cookiePrefix?: string;
|
|
396
|
+
isProduction?: boolean;
|
|
397
|
+
cookieDomain?: string;
|
|
398
|
+
text?: string;
|
|
399
|
+
ssoBaseURL?: string;
|
|
400
|
+
authServiceKey?: string;
|
|
401
|
+
}): Promise<{
|
|
402
|
+
isValid: boolean;
|
|
403
|
+
response?: NextResponse;
|
|
404
|
+
error?: string;
|
|
405
|
+
payload?: JWTPayload;
|
|
406
|
+
}>;
|
package/dist/index.js
CHANGED
|
@@ -27,6 +27,7 @@ exports.isPublicPath = isPublicPath;
|
|
|
27
27
|
exports.isApiPath = isApiPath;
|
|
28
28
|
exports.isProtectedApiPath = isProtectedApiPath;
|
|
29
29
|
exports.checkAuthentication = checkAuthentication;
|
|
30
|
+
exports.verifyAndRefreshTokenWithNextAuth = verifyAndRefreshTokenWithNextAuth;
|
|
30
31
|
const jwt_1 = require("next-auth/jwt");
|
|
31
32
|
const jose_1 = require("jose");
|
|
32
33
|
const server_1 = require("next/server");
|
|
@@ -707,3 +708,21 @@ async function checkAuthentication(req, secret, options) {
|
|
|
707
708
|
error: authCheck.error || 'NO_TOKEN',
|
|
708
709
|
};
|
|
709
710
|
}
|
|
711
|
+
/**
|
|
712
|
+
* NextAuth 토큰과 자체 토큰을 모두 확인하는 미들웨어용 함수
|
|
713
|
+
* NextAuth 토큰이 있으면 바로 통과, 없으면 자체 토큰을 확인
|
|
714
|
+
* @param req NextRequest 객체
|
|
715
|
+
* @param nextAuthToken NextAuth JWT 토큰 (null 가능)
|
|
716
|
+
* @param secret JWT 서명에 사용할 secret key
|
|
717
|
+
* @param options 옵션
|
|
718
|
+
* @returns 인증 결과
|
|
719
|
+
*/
|
|
720
|
+
async function verifyAndRefreshTokenWithNextAuth(req, nextAuthToken, secret, options) {
|
|
721
|
+
// 1. NextAuth 토큰이 있고 유효하면 통과
|
|
722
|
+
if (nextAuthToken && isValidToken(nextAuthToken)) {
|
|
723
|
+
return { isValid: true };
|
|
724
|
+
}
|
|
725
|
+
// 2. 자체 토큰 확인 및 리프레시
|
|
726
|
+
const authCheck = await verifyAndRefreshToken(req, secret, options);
|
|
727
|
+
return authCheck;
|
|
728
|
+
}
|