@asgardeo/nextjs 0.1.9 → 0.1.11
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/AsgardeoNextClient.js +8 -1
- package/dist/AsgardeoNextClient.js.map +1 -1
- package/dist/cjs/index.js +19 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/middleware/asgardeoMiddleware.d.ts +50 -13
- package/dist/middleware/asgardeoMiddleware.js +116 -63
- package/dist/middleware/asgardeoMiddleware.js.map +1 -1
- package/dist/middleware/createRouteMatcher.d.ts +39 -0
- package/dist/middleware/createRouteMatcher.js +52 -0
- package/dist/middleware/createRouteMatcher.js.map +1 -0
- package/dist/server/AsgardeoProvider.js +38 -13
- package/dist/server/AsgardeoProvider.js.map +1 -1
- package/dist/server/actions/getMyOrganizations.js +22 -1
- package/dist/server/actions/getMyOrganizations.js.map +1 -1
- package/dist/server/actions/getSessionId.d.ts +6 -0
- package/dist/server/actions/getSessionId.js +18 -2
- package/dist/server/actions/getSessionId.js.map +1 -1
- package/dist/server/actions/getSessionPayload.d.ts +26 -0
- package/dist/server/actions/getSessionPayload.js +41 -0
- package/dist/server/actions/getSessionPayload.js.map +1 -0
- package/dist/server/actions/handleOAuthCallbackAction.js +32 -12
- package/dist/server/actions/handleOAuthCallbackAction.js.map +1 -1
- package/dist/server/actions/isSignedIn.d.ts +8 -1
- package/dist/server/actions/isSignedIn.js +40 -3
- package/dist/server/actions/isSignedIn.js.map +1 -1
- package/dist/server/actions/signInAction.js +51 -18
- package/dist/server/actions/signInAction.js.map +1 -1
- package/dist/server/actions/signOutAction.d.ts +6 -0
- package/dist/server/actions/signOutAction.js +18 -3
- package/dist/server/actions/signOutAction.js.map +1 -1
- package/dist/utils/SessionManager.d.ts +95 -0
- package/dist/utils/SessionManager.js +143 -0
- package/dist/utils/SessionManager.js.map +1 -0
- package/dist/utils/createRouteMatcher.d.ts +38 -0
- package/dist/utils/createRouteMatcher.js +51 -0
- package/dist/utils/createRouteMatcher.js.map +1 -0
- package/dist/utils/sessionUtils.d.ts +59 -0
- package/dist/utils/sessionUtils.js +112 -0
- package/dist/utils/sessionUtils.js.map +1 -0
- package/package.json +5 -4
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
|
|
3
|
+
*
|
|
4
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
5
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
6
|
+
* in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing,
|
|
12
|
+
* software distributed under the License is distributed on an
|
|
13
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
* KIND, either express or implied. See the License for the
|
|
15
|
+
* specific language governing permissions and limitations
|
|
16
|
+
* under the License.
|
|
17
|
+
*/
|
|
18
|
+
import { JWTPayload } from 'jose';
|
|
19
|
+
/**
|
|
20
|
+
* Session token payload interface
|
|
21
|
+
*/
|
|
22
|
+
export interface SessionTokenPayload extends JWTPayload {
|
|
23
|
+
/** User ID */
|
|
24
|
+
sub: string;
|
|
25
|
+
/** Session ID */
|
|
26
|
+
sessionId: string;
|
|
27
|
+
/** OAuth scopes */
|
|
28
|
+
scopes: string[];
|
|
29
|
+
/** Organization ID if applicable */
|
|
30
|
+
organizationId?: string;
|
|
31
|
+
/** Issued at timestamp */
|
|
32
|
+
iat: number;
|
|
33
|
+
/** Expiration timestamp */
|
|
34
|
+
exp: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Session management utility class for JWT-based session cookies
|
|
38
|
+
*/
|
|
39
|
+
declare class SessionManager {
|
|
40
|
+
private static readonly SESSION_COOKIE_NAME;
|
|
41
|
+
private static readonly TEMP_SESSION_COOKIE_NAME;
|
|
42
|
+
private static readonly DEFAULT_EXPIRY_SECONDS;
|
|
43
|
+
/**
|
|
44
|
+
* Get the signing secret from environment variable
|
|
45
|
+
* Throws error in production if not set
|
|
46
|
+
*/
|
|
47
|
+
private static getSecret;
|
|
48
|
+
/**
|
|
49
|
+
* Create a temporary session cookie for login initiation
|
|
50
|
+
*/
|
|
51
|
+
static createTempSession(sessionId: string): Promise<string>;
|
|
52
|
+
/**
|
|
53
|
+
* Create a session cookie with user information
|
|
54
|
+
*/
|
|
55
|
+
static createSessionToken(userId: string, sessionId: string, scopes: string[], organizationId?: string, expirySeconds?: number): Promise<string>;
|
|
56
|
+
/**
|
|
57
|
+
* Verify and decode a session token
|
|
58
|
+
*/
|
|
59
|
+
static verifySessionToken(token: string): Promise<SessionTokenPayload>;
|
|
60
|
+
/**
|
|
61
|
+
* Verify and decode a temporary session token
|
|
62
|
+
*/
|
|
63
|
+
static verifyTempSession(token: string): Promise<{
|
|
64
|
+
sessionId: string;
|
|
65
|
+
}>;
|
|
66
|
+
/**
|
|
67
|
+
* Get session cookie options
|
|
68
|
+
*/
|
|
69
|
+
static getSessionCookieOptions(): {
|
|
70
|
+
httpOnly: boolean;
|
|
71
|
+
secure: boolean;
|
|
72
|
+
sameSite: "lax";
|
|
73
|
+
path: string;
|
|
74
|
+
maxAge: number;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Get temporary session cookie options
|
|
78
|
+
*/
|
|
79
|
+
static getTempSessionCookieOptions(): {
|
|
80
|
+
httpOnly: boolean;
|
|
81
|
+
secure: boolean;
|
|
82
|
+
sameSite: "lax";
|
|
83
|
+
path: string;
|
|
84
|
+
maxAge: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Get session cookie name
|
|
88
|
+
*/
|
|
89
|
+
static getSessionCookieName(): string;
|
|
90
|
+
/**
|
|
91
|
+
* Get temporary session cookie name
|
|
92
|
+
*/
|
|
93
|
+
static getTempSessionCookieName(): string;
|
|
94
|
+
}
|
|
95
|
+
export default SessionManager;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
|
|
3
|
+
*
|
|
4
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
5
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
6
|
+
* in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing,
|
|
12
|
+
* software distributed under the License is distributed on an
|
|
13
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
* KIND, either express or implied. See the License for the
|
|
15
|
+
* specific language governing permissions and limitations
|
|
16
|
+
* under the License.
|
|
17
|
+
*/
|
|
18
|
+
import { SignJWT, jwtVerify } from 'jose';
|
|
19
|
+
import { AsgardeoRuntimeError } from '@asgardeo/node';
|
|
20
|
+
/**
|
|
21
|
+
* Session management utility class for JWT-based session cookies
|
|
22
|
+
*/
|
|
23
|
+
class SessionManager {
|
|
24
|
+
static SESSION_COOKIE_NAME = 'asgardeo_session';
|
|
25
|
+
static TEMP_SESSION_COOKIE_NAME = 'asgardeo_temp_session';
|
|
26
|
+
static DEFAULT_EXPIRY_SECONDS = 3600; // 1 hour
|
|
27
|
+
/**
|
|
28
|
+
* Get the signing secret from environment variable
|
|
29
|
+
* Throws error in production if not set
|
|
30
|
+
*/
|
|
31
|
+
static getSecret() {
|
|
32
|
+
const secret = process.env['ASGARDEO_SECRET'];
|
|
33
|
+
if (!secret) {
|
|
34
|
+
if (process.env['NODE_ENV'] === 'production') {
|
|
35
|
+
throw new AsgardeoRuntimeError('ASGARDEO_SECRET environment variable is required in production', 'session-secret-required', 'nextjs', 'Set the ASGARDEO_SECRET environment variable with a secure random string');
|
|
36
|
+
}
|
|
37
|
+
// Use a default secret for development (not secure)
|
|
38
|
+
console.warn('⚠️ Using default secret for development. Set ASGARDEO_SECRET for production!');
|
|
39
|
+
return new TextEncoder().encode('development-secret-not-for-production');
|
|
40
|
+
}
|
|
41
|
+
return new TextEncoder().encode(secret);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a temporary session cookie for login initiation
|
|
45
|
+
*/
|
|
46
|
+
static async createTempSession(sessionId) {
|
|
47
|
+
const secret = this.getSecret();
|
|
48
|
+
const jwt = await new SignJWT({
|
|
49
|
+
sessionId,
|
|
50
|
+
type: 'temp',
|
|
51
|
+
})
|
|
52
|
+
.setProtectedHeader({ alg: 'HS256' })
|
|
53
|
+
.setIssuedAt()
|
|
54
|
+
.setExpirationTime('15m') // Temporary sessions expire in 15 minutes
|
|
55
|
+
.sign(secret);
|
|
56
|
+
return jwt;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a session cookie with user information
|
|
60
|
+
*/
|
|
61
|
+
static async createSessionToken(userId, sessionId, scopes, organizationId, expirySeconds = this.DEFAULT_EXPIRY_SECONDS) {
|
|
62
|
+
const secret = this.getSecret();
|
|
63
|
+
const jwt = await new SignJWT({
|
|
64
|
+
sessionId,
|
|
65
|
+
scopes,
|
|
66
|
+
organizationId,
|
|
67
|
+
type: 'session',
|
|
68
|
+
})
|
|
69
|
+
.setProtectedHeader({ alg: 'HS256' })
|
|
70
|
+
.setSubject(userId)
|
|
71
|
+
.setIssuedAt()
|
|
72
|
+
.setExpirationTime(Date.now() / 1000 + expirySeconds)
|
|
73
|
+
.sign(secret);
|
|
74
|
+
return jwt;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Verify and decode a session token
|
|
78
|
+
*/
|
|
79
|
+
static async verifySessionToken(token) {
|
|
80
|
+
try {
|
|
81
|
+
const secret = this.getSecret();
|
|
82
|
+
const { payload } = await jwtVerify(token, secret);
|
|
83
|
+
return payload;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
throw new AsgardeoRuntimeError(`Invalid session token: ${error instanceof Error ? error.message : 'Unknown error'}`, 'invalid-session-token', 'nextjs', 'Session token verification failed');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Verify and decode a temporary session token
|
|
91
|
+
*/
|
|
92
|
+
static async verifyTempSession(token) {
|
|
93
|
+
try {
|
|
94
|
+
const secret = this.getSecret();
|
|
95
|
+
const { payload } = await jwtVerify(token, secret);
|
|
96
|
+
if (payload['type'] !== 'temp') {
|
|
97
|
+
throw new Error('Invalid token type');
|
|
98
|
+
}
|
|
99
|
+
return { sessionId: payload['sessionId'] };
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
throw new AsgardeoRuntimeError(`Invalid temporary session token: ${error instanceof Error ? error.message : 'Unknown error'}`, 'invalid-temp-session-token', 'nextjs', 'Temporary session token verification failed');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get session cookie options
|
|
107
|
+
*/
|
|
108
|
+
static getSessionCookieOptions() {
|
|
109
|
+
return {
|
|
110
|
+
httpOnly: true,
|
|
111
|
+
secure: process.env['NODE_ENV'] === 'production',
|
|
112
|
+
sameSite: 'lax',
|
|
113
|
+
path: '/',
|
|
114
|
+
maxAge: this.DEFAULT_EXPIRY_SECONDS,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get temporary session cookie options
|
|
119
|
+
*/
|
|
120
|
+
static getTempSessionCookieOptions() {
|
|
121
|
+
return {
|
|
122
|
+
httpOnly: true,
|
|
123
|
+
secure: process.env['NODE_ENV'] === 'production',
|
|
124
|
+
sameSite: 'lax',
|
|
125
|
+
path: '/',
|
|
126
|
+
maxAge: 15 * 60, // 15 minutes
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get session cookie name
|
|
131
|
+
*/
|
|
132
|
+
static getSessionCookieName() {
|
|
133
|
+
return this.SESSION_COOKIE_NAME;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Get temporary session cookie name
|
|
137
|
+
*/
|
|
138
|
+
static getTempSessionCookieName() {
|
|
139
|
+
return this.TEMP_SESSION_COOKIE_NAME;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export default SessionManager;
|
|
143
|
+
//# sourceMappingURL=SessionManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SessionManager.js","sourceRoot":"","sources":["../../src/utils/SessionManager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAC,OAAO,EAAE,SAAS,EAAa,MAAM,MAAM,CAAC;AACpD,OAAO,EAAC,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;AAoBpD;;GAEG;AACH,MAAM,cAAc;IACV,MAAM,CAAU,mBAAmB,GAAG,kBAAkB,CAAC;IACzD,MAAM,CAAU,wBAAwB,GAAG,uBAAuB,CAAC;IACnE,MAAM,CAAU,sBAAsB,GAAG,IAAI,CAAC,CAAC,SAAS;IAEhE;;;OAGG;IACK,MAAM,CAAC,SAAS;QACtB,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QAE9C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,YAAY,EAAE,CAAC;gBAC7C,MAAM,IAAI,oBAAoB,CAC5B,gEAAgE,EAChE,yBAAyB,EACzB,QAAQ,EACR,0EAA0E,CAC3E,CAAC;YACJ,CAAC;YACD,oDAAoD;YACpD,OAAO,CAAC,IAAI,CAAC,+EAA+E,CAAC,CAAC;YAC9F,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,uCAAuC,CAAC,CAAC;QAC3E,CAAC;Q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,2BAA2B;QAChC,OAAO;YACL,QAAQ,EAAE,IAAI;YACd,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,YAAY;YAChD,QAAQ,EAAE,KAAc;YACxB,IAAI,EAAE,GAAG;YACT,MAAM,EAAE,EAAE,GAAG,EAAE,EAAE,aAAa;SAC/B,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,oBAAoB;QACzB,OAAO,IAAI,CAAC,mBAAmB,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,wBAAwB;QAC7B,OAAO,IAAI,CAAC,wBAAwB,CAAC;IACvC,CAAC;;AAGH,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
|
|
3
|
+
*
|
|
4
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
5
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
6
|
+
* in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing,
|
|
12
|
+
* software distributed under the License is distributed on an
|
|
13
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
* KIND, either express or implied. See the License for the
|
|
15
|
+
* specific language governing permissions and limitations
|
|
16
|
+
* under the License.
|
|
17
|
+
*/
|
|
18
|
+
import { NextRequest } from 'next/server';
|
|
19
|
+
/**
|
|
20
|
+
* Creates a route matcher function that tests if a request matches any of the given patterns.
|
|
21
|
+
*
|
|
22
|
+
* @param patterns - Array of route patterns to match. Supports glob-like patterns.
|
|
23
|
+
* @returns Function that tests if a request matches any of the patterns
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const isProtectedRoute = createRouteMatcher([
|
|
28
|
+
* '/dashboard(.*)',
|
|
29
|
+
* '/admin(.*)',
|
|
30
|
+
* '/profile'
|
|
31
|
+
* ]);
|
|
32
|
+
*
|
|
33
|
+
* if (isProtectedRoute(req)) {
|
|
34
|
+
* // Route is protected
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare const createRouteMatcher: (patterns: string[]) => (req: NextRequest) => boolean;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
|
|
3
|
+
*
|
|
4
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
5
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
6
|
+
* in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing,
|
|
12
|
+
* software distributed under the License is distributed on an
|
|
13
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
* KIND, either express or implied. See the License for the
|
|
15
|
+
* specific language governing permissions and limitations
|
|
16
|
+
* under the License.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Creates a route matcher function that tests if a request matches any of the given patterns.
|
|
20
|
+
*
|
|
21
|
+
* @param patterns - Array of route patterns to match. Supports glob-like patterns.
|
|
22
|
+
* @returns Function that tests if a request matches any of the patterns
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const isProtectedRoute = createRouteMatcher([
|
|
27
|
+
* '/dashboard(.*)',
|
|
28
|
+
* '/admin(.*)',
|
|
29
|
+
* '/profile'
|
|
30
|
+
* ]);
|
|
31
|
+
*
|
|
32
|
+
* if (isProtectedRoute(req)) {
|
|
33
|
+
* // Route is protected
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export const createRouteMatcher = (patterns) => {
|
|
38
|
+
const regexPatterns = patterns.map(pattern => {
|
|
39
|
+
// Convert glob-like patterns to regex
|
|
40
|
+
const regexPattern = pattern
|
|
41
|
+
.replace(/\./g, '\\.') // Escape dots
|
|
42
|
+
.replace(/\*/g, '.*') // Convert * to .*
|
|
43
|
+
.replace(/\(\.\*\)/g, '(.*)'); // Handle explicit (.*) patterns
|
|
44
|
+
return new RegExp(`^${regexPattern}$`);
|
|
45
|
+
});
|
|
46
|
+
return (req) => {
|
|
47
|
+
const pathname = req.nextUrl.pathname;
|
|
48
|
+
return regexPatterns.some(regex => regex.test(pathname));
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=createRouteMatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRouteMatcher.js","sourceRoot":"","sources":["../../src/utils/createRouteMatcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,QAAkB,EAAE,EAAE;IACvD,MAAM,aAAa,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;QAC3C,sCAAsC;QACtC,MAAM,YAAY,GAAG,OAAO;aACzB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,cAAc;aACpC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,kBAAkB;aACvC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,CAAC,gCAAgC;QAEjE,OAAO,IAAI,MAAM,CAAC,IAAI,YAAY,GAAG,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;IAEH,OAAO,CAAC,GAAgB,EAAW,EAAE;QACnC,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC;QACtC,OAAO,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC3D,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
|
|
3
|
+
*
|
|
4
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
5
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
6
|
+
* in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing,
|
|
12
|
+
* software distributed under the License is distributed on an
|
|
13
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
* KIND, either express or implied. See the License for the
|
|
15
|
+
* specific language governing permissions and limitations
|
|
16
|
+
* under the License.
|
|
17
|
+
*/
|
|
18
|
+
import { NextRequest } from 'next/server';
|
|
19
|
+
import { SessionTokenPayload } from './SessionManager';
|
|
20
|
+
/**
|
|
21
|
+
* Checks if a request has a valid session cookie (JWT).
|
|
22
|
+
* This verifies the JWT signature and expiration.
|
|
23
|
+
*
|
|
24
|
+
* @param request - The Next.js request object
|
|
25
|
+
* @returns True if a valid session exists, false otherwise
|
|
26
|
+
*/
|
|
27
|
+
export declare const hasValidSession: (request: NextRequest) => Promise<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* Gets the session payload from the request cookies.
|
|
30
|
+
* This includes user ID, session ID, and scopes.
|
|
31
|
+
*
|
|
32
|
+
* @param request - The Next.js request object
|
|
33
|
+
* @returns The session payload if valid, undefined otherwise
|
|
34
|
+
*/
|
|
35
|
+
export declare const getSessionFromRequest: (request: NextRequest) => Promise<SessionTokenPayload | undefined>;
|
|
36
|
+
/**
|
|
37
|
+
* Gets the session ID from the request cookies (legacy support).
|
|
38
|
+
* First tries to get from JWT session, then falls back to legacy session ID cookie.
|
|
39
|
+
*
|
|
40
|
+
* @param request - The Next.js request object
|
|
41
|
+
* @returns The session ID if it exists, undefined otherwise
|
|
42
|
+
*/
|
|
43
|
+
export declare const getSessionIdFromRequest: (request: NextRequest) => Promise<string | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Gets the temporary session ID from request cookies.
|
|
46
|
+
*
|
|
47
|
+
* @param request - The Next.js request object
|
|
48
|
+
* @returns The temporary session ID if valid, undefined otherwise
|
|
49
|
+
*/
|
|
50
|
+
export declare const getTempSessionFromRequest: (request: NextRequest) => Promise<string | undefined>;
|
|
51
|
+
/**
|
|
52
|
+
* Legacy function for backward compatibility.
|
|
53
|
+
* Checks if a request has a valid session ID in cookies.
|
|
54
|
+
*
|
|
55
|
+
* @deprecated Use hasValidSession instead for JWT-based sessions
|
|
56
|
+
* @param request - The Next.js request object
|
|
57
|
+
* @returns True if a session ID exists, false otherwise
|
|
58
|
+
*/
|
|
59
|
+
export declare const hasValidSessionLegacy: (request: NextRequest) => boolean;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
|
|
3
|
+
*
|
|
4
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
5
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
6
|
+
* in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing,
|
|
12
|
+
* software distributed under the License is distributed on an
|
|
13
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
* KIND, either express or implied. See the License for the
|
|
15
|
+
* specific language governing permissions and limitations
|
|
16
|
+
* under the License.
|
|
17
|
+
*/
|
|
18
|
+
import SessionManager from './SessionManager';
|
|
19
|
+
import { CookieConfig } from '@asgardeo/node';
|
|
20
|
+
/**
|
|
21
|
+
* Checks if a request has a valid session cookie (JWT).
|
|
22
|
+
* This verifies the JWT signature and expiration.
|
|
23
|
+
*
|
|
24
|
+
* @param request - The Next.js request object
|
|
25
|
+
* @returns True if a valid session exists, false otherwise
|
|
26
|
+
*/
|
|
27
|
+
export const hasValidSession = async (request) => {
|
|
28
|
+
try {
|
|
29
|
+
const sessionToken = request.cookies.get(SessionManager.getSessionCookieName())?.value;
|
|
30
|
+
if (!sessionToken) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
await SessionManager.verifySessionToken(sessionToken);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Gets the session payload from the request cookies.
|
|
42
|
+
* This includes user ID, session ID, and scopes.
|
|
43
|
+
*
|
|
44
|
+
* @param request - The Next.js request object
|
|
45
|
+
* @returns The session payload if valid, undefined otherwise
|
|
46
|
+
*/
|
|
47
|
+
export const getSessionFromRequest = async (request) => {
|
|
48
|
+
try {
|
|
49
|
+
const sessionToken = request.cookies.get(SessionManager.getSessionCookieName())?.value;
|
|
50
|
+
if (!sessionToken) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
return await SessionManager.verifySessionToken(sessionToken);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Gets the session ID from the request cookies (legacy support).
|
|
61
|
+
* First tries to get from JWT session, then falls back to legacy session ID cookie.
|
|
62
|
+
*
|
|
63
|
+
* @param request - The Next.js request object
|
|
64
|
+
* @returns The session ID if it exists, undefined otherwise
|
|
65
|
+
*/
|
|
66
|
+
export const getSessionIdFromRequest = async (request) => {
|
|
67
|
+
try {
|
|
68
|
+
// Try JWT session first
|
|
69
|
+
const sessionPayload = await getSessionFromRequest(request);
|
|
70
|
+
if (sessionPayload) {
|
|
71
|
+
return sessionPayload.sessionId;
|
|
72
|
+
}
|
|
73
|
+
// Fall back to legacy session ID cookie for backward compatibility
|
|
74
|
+
return request.cookies.get(CookieConfig.SESSION_COOKIE_NAME)?.value;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// Fall back to legacy session ID cookie
|
|
78
|
+
return request.cookies.get(CookieConfig.SESSION_COOKIE_NAME)?.value;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Gets the temporary session ID from request cookies.
|
|
83
|
+
*
|
|
84
|
+
* @param request - The Next.js request object
|
|
85
|
+
* @returns The temporary session ID if valid, undefined otherwise
|
|
86
|
+
*/
|
|
87
|
+
export const getTempSessionFromRequest = async (request) => {
|
|
88
|
+
try {
|
|
89
|
+
const tempToken = request.cookies.get(SessionManager.getTempSessionCookieName())?.value;
|
|
90
|
+
if (!tempToken) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
const tempSession = await SessionManager.verifyTempSession(tempToken);
|
|
94
|
+
return tempSession.sessionId;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Legacy function for backward compatibility.
|
|
102
|
+
* Checks if a request has a valid session ID in cookies.
|
|
103
|
+
*
|
|
104
|
+
* @deprecated Use hasValidSession instead for JWT-based sessions
|
|
105
|
+
* @param request - The Next.js request object
|
|
106
|
+
* @returns True if a session ID exists, false otherwise
|
|
107
|
+
*/
|
|
108
|
+
export const hasValidSessionLegacy = (request) => {
|
|
109
|
+
const sessionId = request.cookies.get(CookieConfig.SESSION_COOKIE_NAME)?.value;
|
|
110
|
+
return Boolean(sessionId && sessionId.trim().length > 0);
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=sessionUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sessionUtils.js","sourceRoot":"","sources":["../../src/utils/sessionUtils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,cAAqC,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAAE,OAAoB,EAAoB,EAAE;IAC9E,IAAI,CAAC;QACH,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,oBAAoB,EAAE,CAAC,EAAE,KAAK,CAAC;QACvF,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,OAAO,KAAK,CAAC;QACf,CAAC;QAED,MAAM,cAAc,CAAC,kBAAkB,CAAC,YAAY,CAAC,CAAC;QACtD,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,EAAE,OAAoB,EAA4C,EAAE;IAC5G,IAAI,CAAC;QACH,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,oBAAoB,EAAE,CAAC,EAAE,KAAK,CAAC;QACvF,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,OAAO,MAAM,cAAc,CAAC,kBAAkB,CAAC,YAAY,CAAC,CAAC;IAC/D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,KAAK,EAAE,OAAoB,EAA+B,EAAE;IACj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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asgardeo/nextjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "Next.js implementation of Asgardeo JavaScript SDK.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"asgardeo",
|
|
@@ -37,9 +37,10 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@types/react": "^19.1.4",
|
|
40
|
+
"jose": "^5.10.0",
|
|
40
41
|
"tslib": "^2.8.1",
|
|
41
|
-
"@asgardeo/node": "^0.0.
|
|
42
|
-
"@asgardeo/react": "^0.5.
|
|
42
|
+
"@asgardeo/node": "^0.0.9",
|
|
43
|
+
"@asgardeo/react": "^0.5.11"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@types/node": "^22.15.3",
|
|
@@ -50,8 +51,8 @@
|
|
|
50
51
|
"eslint": "8.57.0",
|
|
51
52
|
"next": "^15.3.2",
|
|
52
53
|
"prettier": "^2.6.2",
|
|
53
|
-
"rimraf": "^6.0.1",
|
|
54
54
|
"react": "^19.1.0",
|
|
55
|
+
"rimraf": "^6.0.1",
|
|
55
56
|
"typescript": "~5.7.2",
|
|
56
57
|
"vitest": "^3.1.3"
|
|
57
58
|
},
|