@thinkingcat/auth-utils 1.0.7 → 1.0.9
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 +631 -286
- package/dist/index.d.ts +80 -53
- package/dist/index.js +248 -144
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -104,18 +104,19 @@ export declare function verifyToken(accessToken: string, secret: string): Promis
|
|
|
104
104
|
/**
|
|
105
105
|
* payload에서 역할 추출 (서비스별)
|
|
106
106
|
* @param payload JWT payload
|
|
107
|
-
* @param serviceId 서비스 ID (
|
|
107
|
+
* @param serviceId 서비스 ID (필수)
|
|
108
108
|
* @param defaultRole 기본 역할 (기본값: 'ADMIN')
|
|
109
109
|
* @returns 추출된 역할
|
|
110
110
|
*/
|
|
111
|
-
export declare function extractRoleFromPayload(payload: JWTPayload, serviceId
|
|
111
|
+
export declare function extractRoleFromPayload(payload: JWTPayload, serviceId: string, defaultRole?: string): string;
|
|
112
112
|
/**
|
|
113
113
|
* payload에서 NextAuth JWT 객체 생성
|
|
114
114
|
* @param payload JWT payload
|
|
115
|
+
* @param serviceId 서비스 ID (필수)
|
|
115
116
|
* @param includeAcademies academies 정보 포함 여부
|
|
116
117
|
* @returns NextAuth JWT 객체
|
|
117
118
|
*/
|
|
118
|
-
export declare function createNextAuthJWT(payload: JWTPayload, includeAcademies?: boolean): JWT;
|
|
119
|
+
export declare function createNextAuthJWT(payload: JWTPayload, serviceId: string, includeAcademies?: boolean): JWT;
|
|
119
120
|
/**
|
|
120
121
|
* NextAuth JWT를 인코딩된 세션 토큰으로 변환
|
|
121
122
|
* @param jwt NextAuth JWT 객체
|
|
@@ -130,7 +131,7 @@ export declare function encodeNextAuthToken(jwt: JWT, secret: string, maxAge?: n
|
|
|
130
131
|
* @param accessToken access token
|
|
131
132
|
* @param refreshToken refresh token (선택)
|
|
132
133
|
* @param options 쿠키 설정 옵션
|
|
133
|
-
* @param options.cookiePrefix 쿠키 이름 접두사 (
|
|
134
|
+
* @param options.cookiePrefix 쿠키 이름 접두사 (필수)
|
|
134
135
|
* @param options.isProduction 프로덕션 환경 여부 (기본값: false)
|
|
135
136
|
*/
|
|
136
137
|
export declare function setCustomTokens(response: ResponseLike, accessToken: string, refreshToken: string, options?: {
|
|
@@ -157,10 +158,10 @@ export declare function setNextAuthToken(response: ResponseLike, sessionToken: s
|
|
|
157
158
|
/**
|
|
158
159
|
* 리다이렉트용 HTML 생성
|
|
159
160
|
* @param redirectPath 리다이렉트할 경로
|
|
160
|
-
* @param text 표시할 텍스트 (
|
|
161
|
+
* @param text 표시할 텍스트 (필수)
|
|
161
162
|
* @returns HTML 문자열
|
|
162
163
|
*/
|
|
163
|
-
export declare function createRedirectHTML(redirectPath: string, text
|
|
164
|
+
export declare function createRedirectHTML(redirectPath: string, text: string): string;
|
|
164
165
|
/**
|
|
165
166
|
* access token과 refresh token을 사용하여 완전한 인증 세션 생성
|
|
166
167
|
* @param accessToken access token
|
|
@@ -168,27 +169,30 @@ export declare function createRedirectHTML(redirectPath: string, text?: string):
|
|
|
168
169
|
* @param options 추가 옵션
|
|
169
170
|
* @param options.refreshToken refresh token (선택)
|
|
170
171
|
* @param options.redirectPath 리다이렉트할 경로 (기본값: 페이지 리로드)
|
|
171
|
-
* @param options.text 리다이렉트 HTML에 표시할 텍스트 (
|
|
172
|
-
* @param options.cookiePrefix 쿠키 이름 접두사 (
|
|
172
|
+
* @param options.text 리다이렉트 HTML에 표시할 텍스트 (선택사항)
|
|
173
|
+
* @param options.cookiePrefix 쿠키 이름 접두사 (필수)
|
|
173
174
|
* @param options.isProduction 프로덕션 환경 여부 (기본값: false)
|
|
174
175
|
* @param options.cookieDomain 쿠키 도메인 (선택)
|
|
175
176
|
* @returns NextResponse 객체
|
|
176
177
|
*/
|
|
177
|
-
export declare function createAuthResponse(accessToken: string, secret: string, options
|
|
178
|
+
export declare function createAuthResponse(accessToken: string, secret: string, options: {
|
|
178
179
|
refreshToken?: string;
|
|
179
180
|
redirectPath?: string;
|
|
180
181
|
text?: string;
|
|
181
182
|
cookiePrefix?: string;
|
|
182
183
|
isProduction?: boolean;
|
|
183
184
|
cookieDomain?: string;
|
|
185
|
+
serviceId: string;
|
|
186
|
+
licenseKey: string;
|
|
184
187
|
}): Promise<NextResponse>;
|
|
185
188
|
/**
|
|
186
189
|
* 서비스 구독 유효성 확인 함수
|
|
187
190
|
* @param services 서비스 정보 배열
|
|
188
191
|
* @param serviceId 확인할 서비스 ID
|
|
192
|
+
* @param ssoBaseURL SSO 서버 기본 URL (필수)
|
|
189
193
|
* @returns 구독 유효성 결과
|
|
190
194
|
*/
|
|
191
|
-
export declare function validateServiceSubscription(services: ServiceInfo[], serviceId: string): {
|
|
195
|
+
export declare function validateServiceSubscription(services: ServiceInfo[], serviceId: string, ssoBaseURL: string): {
|
|
192
196
|
isValid: boolean;
|
|
193
197
|
redirectUrl?: string;
|
|
194
198
|
service?: ServiceInfo;
|
|
@@ -197,12 +201,12 @@ export declare function validateServiceSubscription(services: ServiceInfo[], ser
|
|
|
197
201
|
* SSO 서버에서 refresh token을 사용하여 새로운 access token을 발급받는 함수
|
|
198
202
|
* @param refreshToken refresh token
|
|
199
203
|
* @param options 옵션
|
|
200
|
-
* @param options.ssoBaseURL SSO 서버 기본 URL (
|
|
204
|
+
* @param options.ssoBaseURL SSO 서버 기본 URL (필수)
|
|
201
205
|
* @param options.authServiceKey 인증 서비스 키 (기본값: 환경 변수)
|
|
202
206
|
* @returns SSO refresh token 응답
|
|
203
207
|
*/
|
|
204
|
-
export declare function refreshSSOToken(refreshToken: string, options
|
|
205
|
-
ssoBaseURL
|
|
208
|
+
export declare function refreshSSOToken(refreshToken: string, options: {
|
|
209
|
+
ssoBaseURL: string;
|
|
206
210
|
authServiceKey?: string;
|
|
207
211
|
}): Promise<SSORefreshTokenResponse>;
|
|
208
212
|
/**
|
|
@@ -210,35 +214,23 @@ export declare function refreshSSOToken(refreshToken: string, options?: {
|
|
|
210
214
|
* @param userId 사용자 ID
|
|
211
215
|
* @param accessToken access token
|
|
212
216
|
* @param options 옵션
|
|
213
|
-
* @param options.ssoBaseURL SSO 서버 기본 URL (
|
|
217
|
+
* @param options.ssoBaseURL SSO 서버 기본 URL (필수)
|
|
214
218
|
* @param options.authServiceKey 인증 서비스 키 (기본값: 환경 변수)
|
|
215
219
|
* @returns refresh token 또는 null
|
|
216
220
|
*/
|
|
217
|
-
export declare function getRefreshTokenFromSSO(userId: string, accessToken: string, options
|
|
218
|
-
ssoBaseURL
|
|
221
|
+
export declare function getRefreshTokenFromSSO(userId: string, accessToken: string, options: {
|
|
222
|
+
ssoBaseURL: string;
|
|
219
223
|
authServiceKey?: string;
|
|
220
224
|
}): Promise<string | null>;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
* @param secret - JWT 서명에 사용할 secret key
|
|
226
|
-
* @param options - 옵션
|
|
227
|
-
* @param options.cookiePrefix - 쿠키 이름 접두사 (기본값: 'checkon')
|
|
228
|
-
* @param options.isProduction - 프로덕션 환경 여부 (기본값: false)
|
|
229
|
-
* @param options.cookieDomain - 쿠키 도메인 (선택)
|
|
230
|
-
* @param options.text - 리다이렉트 HTML에 표시할 텍스트 (기본값: 'checkon')
|
|
231
|
-
* @param options.ssoBaseURL - SSO 서버 기본 URL (기본값: 환경 변수 또는 기본값)
|
|
232
|
-
* @param options.authServiceKey - 인증 서비스 키 (기본값: 환경 변수)
|
|
233
|
-
* @returns 검증 결과 및 필요시 리프레시된 응답
|
|
234
|
-
*/
|
|
235
|
-
export declare function verifyAndRefreshToken(req: NextRequest, secret: string, options?: {
|
|
236
|
-
cookiePrefix?: string;
|
|
237
|
-
isProduction?: boolean;
|
|
225
|
+
export declare function verifyAndRefreshToken(req: NextRequest, secret: string, options: {
|
|
226
|
+
cookiePrefix: string;
|
|
227
|
+
serviceId: string;
|
|
228
|
+
isProduction: boolean;
|
|
238
229
|
cookieDomain?: string;
|
|
239
230
|
text?: string;
|
|
240
231
|
ssoBaseURL?: string;
|
|
241
232
|
authServiceKey?: string;
|
|
233
|
+
licenseKey: string;
|
|
242
234
|
}): Promise<{
|
|
243
235
|
isValid: boolean;
|
|
244
236
|
response?: NextResponse;
|
|
@@ -257,17 +249,17 @@ export declare function redirectToError(req: NextRequest, code: string, message:
|
|
|
257
249
|
/**
|
|
258
250
|
* 인증 쿠키를 삭제하는 헬퍼 함수
|
|
259
251
|
* @param response NextResponse 객체
|
|
260
|
-
* @param cookiePrefix 쿠키 이름 접두사 (
|
|
252
|
+
* @param cookiePrefix 쿠키 이름 접두사 (필수)
|
|
261
253
|
* @returns NextResponse 객체
|
|
262
254
|
*/
|
|
263
|
-
export declare function clearAuthCookies(response: NextResponse, cookiePrefix
|
|
255
|
+
export declare function clearAuthCookies(response: NextResponse, cookiePrefix: string): NextResponse;
|
|
264
256
|
/**
|
|
265
257
|
* JWT에서 서비스별 역할을 추출하는 헬퍼 함수
|
|
266
258
|
* @param token NextAuth JWT 객체 또는 null
|
|
267
|
-
* @param serviceId 서비스 ID (
|
|
259
|
+
* @param serviceId 서비스 ID (필수)
|
|
268
260
|
* @returns 추출된 역할 또는 undefined
|
|
269
261
|
*/
|
|
270
|
-
export declare function getEffectiveRole(token: JWT | null, serviceId
|
|
262
|
+
export declare function getEffectiveRole(token: JWT | null, serviceId: string): string | undefined;
|
|
271
263
|
/**
|
|
272
264
|
* 구독이 필요한 경로인지 확인하는 헬퍼 함수
|
|
273
265
|
* @param pathname 경로명
|
|
@@ -314,6 +306,26 @@ export interface MiddlewareConfig {
|
|
|
314
306
|
/** 에러 페이지 경로 (기본값: '/error') */
|
|
315
307
|
errorPath?: string;
|
|
316
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* 미들웨어 실행 옵션
|
|
311
|
+
*/
|
|
312
|
+
export interface MiddlewareOptions {
|
|
313
|
+
/** NextAuth Secret (필수) */
|
|
314
|
+
secret: string;
|
|
315
|
+
/** 프로덕션 환경 여부 (필수) */
|
|
316
|
+
isProduction: boolean;
|
|
317
|
+
/** 쿠키 도메인 (선택사항) */
|
|
318
|
+
cookieDomain?: string;
|
|
319
|
+
/** NextAuth 토큰을 가져오는 함수 (선택사항) */
|
|
320
|
+
getNextAuthToken?: (req: NextRequest) => Promise<JWT | null>;
|
|
321
|
+
/** SSO 서버 기본 URL (필수) */
|
|
322
|
+
ssoBaseURL: string;
|
|
323
|
+
/** 인증 서비스 키 (선택사항) */
|
|
324
|
+
authServiceKey?: string;
|
|
325
|
+
/** 라이센스 키 (필수) */
|
|
326
|
+
licenseKey: string;
|
|
327
|
+
}
|
|
328
|
+
export declare function checkLicenseKey(licenseKey: string): void;
|
|
317
329
|
export declare function checkRoleAccess(pathname: string, role: string, roleConfig: RoleAccessConfig): {
|
|
318
330
|
allowed: boolean;
|
|
319
331
|
message?: string;
|
|
@@ -322,10 +334,10 @@ export declare function checkRoleAccess(pathname: string, role: string, roleConf
|
|
|
322
334
|
* SSO 로그인 페이지로 리다이렉트하는 헬퍼 함수
|
|
323
335
|
* @param req NextRequest 객체
|
|
324
336
|
* @param serviceId 서비스 ID
|
|
325
|
-
* @param ssoBaseURL SSO 서버 기본 URL (
|
|
337
|
+
* @param ssoBaseURL SSO 서버 기본 URL (필수)
|
|
326
338
|
* @returns NextResponse 리다이렉트 응답
|
|
327
339
|
*/
|
|
328
|
-
export declare function redirectToSSOLogin(req: NextRequest, serviceId: string, ssoBaseURL
|
|
340
|
+
export declare function redirectToSSOLogin(req: NextRequest, serviceId: string, ssoBaseURL: string): NextResponse;
|
|
329
341
|
/**
|
|
330
342
|
* 역할별 대시보드 경로로 리다이렉트하는 헬퍼 함수
|
|
331
343
|
* @param req NextRequest 객체
|
|
@@ -351,18 +363,18 @@ export declare function isValidToken(token: JWT | null): boolean;
|
|
|
351
363
|
* 특정 역할을 가지고 있는지 확인하는 함수
|
|
352
364
|
* @param token NextAuth JWT 객체
|
|
353
365
|
* @param role 확인할 역할
|
|
354
|
-
* @param serviceId 서비스 ID (
|
|
366
|
+
* @param serviceId 서비스 ID (필수)
|
|
355
367
|
* @returns 역할 보유 여부
|
|
356
368
|
*/
|
|
357
|
-
export declare function hasRole(token: JWT | null, role: string, serviceId
|
|
369
|
+
export declare function hasRole(token: JWT | null, role: string, serviceId: string): boolean;
|
|
358
370
|
/**
|
|
359
371
|
* 여러 역할 중 하나라도 가지고 있는지 확인하는 함수
|
|
360
372
|
* @param token NextAuth JWT 객체
|
|
361
373
|
* @param roles 확인할 역할 배열
|
|
362
|
-
* @param serviceId 서비스 ID (
|
|
374
|
+
* @param serviceId 서비스 ID (필수)
|
|
363
375
|
* @returns 역할 보유 여부
|
|
364
376
|
*/
|
|
365
|
-
export declare function hasAnyRole(token: JWT | null, roles: string[], serviceId
|
|
377
|
+
export declare function hasAnyRole(token: JWT | null, roles: string[], serviceId: string): boolean;
|
|
366
378
|
/**
|
|
367
379
|
* 공개 경로인지 확인하는 함수
|
|
368
380
|
* @param pathname 경로명
|
|
@@ -390,14 +402,16 @@ export declare function isProtectedApiPath(pathname: string, exemptPaths?: strin
|
|
|
390
402
|
* @param options 옵션
|
|
391
403
|
* @returns 인증 결과
|
|
392
404
|
*/
|
|
393
|
-
export declare function checkAuthentication(req: NextRequest, secret: string, options
|
|
394
|
-
cookiePrefix
|
|
395
|
-
|
|
405
|
+
export declare function checkAuthentication(req: NextRequest, secret: string, options: {
|
|
406
|
+
cookiePrefix: string;
|
|
407
|
+
serviceId: string;
|
|
408
|
+
isProduction: boolean;
|
|
396
409
|
cookieDomain?: string;
|
|
397
410
|
text?: string;
|
|
398
411
|
ssoBaseURL?: string;
|
|
399
412
|
authServiceKey?: string;
|
|
400
413
|
getNextAuthToken?: (req: NextRequest) => Promise<JWT | null>;
|
|
414
|
+
licenseKey: string;
|
|
401
415
|
}): Promise<{
|
|
402
416
|
isAuthenticated: boolean;
|
|
403
417
|
token?: JWT | null;
|
|
@@ -414,13 +428,15 @@ export declare function checkAuthentication(req: NextRequest, secret: string, op
|
|
|
414
428
|
* @param options 옵션
|
|
415
429
|
* @returns 인증 결과
|
|
416
430
|
*/
|
|
417
|
-
export declare function verifyAndRefreshTokenWithNextAuth(req: NextRequest, nextAuthToken: JWT | null, secret: string, options
|
|
418
|
-
cookiePrefix
|
|
419
|
-
|
|
431
|
+
export declare function verifyAndRefreshTokenWithNextAuth(req: NextRequest, nextAuthToken: JWT | null, secret: string, options: {
|
|
432
|
+
cookiePrefix: string;
|
|
433
|
+
serviceId: string;
|
|
434
|
+
isProduction: boolean;
|
|
420
435
|
cookieDomain?: string;
|
|
421
436
|
text?: string;
|
|
422
437
|
ssoBaseURL?: string;
|
|
423
438
|
authServiceKey?: string;
|
|
439
|
+
licenseKey: string;
|
|
424
440
|
}): Promise<{
|
|
425
441
|
isValid: boolean;
|
|
426
442
|
response?: NextResponse;
|
|
@@ -429,16 +445,27 @@ export declare function verifyAndRefreshTokenWithNextAuth(req: NextRequest, next
|
|
|
429
445
|
}>;
|
|
430
446
|
/**
|
|
431
447
|
* 기본 미들웨어 설정을 생성하는 함수
|
|
432
|
-
* @param config 커스텀 설정 (
|
|
448
|
+
* @param config 커스텀 설정 (필수: serviceId 포함)
|
|
449
|
+
* @param defaults 기본 설정값 (선택사항, 제공하지 않으면 최소 기본값 사용)
|
|
433
450
|
* @returns 미들웨어 설정 객체
|
|
434
451
|
*/
|
|
435
|
-
export declare function createMiddlewareConfig(config
|
|
452
|
+
export declare function createMiddlewareConfig(config: Partial<MiddlewareConfig> & {
|
|
453
|
+
serviceId: string;
|
|
454
|
+
}, defaults?: {
|
|
455
|
+
publicPaths?: string[];
|
|
456
|
+
subscriptionRequiredPaths?: string[];
|
|
457
|
+
subscriptionExemptApiPaths?: string[];
|
|
458
|
+
authApiPaths?: string[];
|
|
459
|
+
rolePaths?: Record<string, string>;
|
|
460
|
+
systemAdminRole?: string;
|
|
461
|
+
errorPath?: string;
|
|
462
|
+
}): Required<Omit<MiddlewareConfig, 'serviceId'>> & Pick<MiddlewareConfig, 'serviceId'>;
|
|
436
463
|
/**
|
|
437
464
|
* 통합 미들웨어 핸들러 함수
|
|
438
465
|
* 모든 인증, 권한, 구독 체크를 포함한 완전한 미들웨어 로직
|
|
439
466
|
* @param req NextRequest 객체
|
|
440
467
|
* @param config 미들웨어 설정
|
|
441
|
-
* @param
|
|
468
|
+
* @param options 미들웨어 실행 옵션 (secret 필수)
|
|
442
469
|
* @returns NextResponse 또는 null (다음 미들웨어로 진행)
|
|
443
470
|
*/
|
|
444
|
-
export declare function handleMiddleware(req: NextRequest, config: Required<Omit<MiddlewareConfig, 'serviceId'>> & Pick<MiddlewareConfig, 'serviceId'>,
|
|
471
|
+
export declare function handleMiddleware(req: NextRequest, config: Required<Omit<MiddlewareConfig, 'serviceId'>> & Pick<MiddlewareConfig, 'serviceId'>, options: MiddlewareOptions): Promise<NextResponse | null>;
|