@coreiam/auth 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/dist/core/index.js +5 -3
- package/dist/nextjs/index.d.ts +1 -1
- package/dist/nextjs/index.js +4 -1
- package/dist/nextjs/middleware.d.ts +8 -1
- package/dist/nextjs/middleware.js +57 -69
- package/package.json +1 -1
package/dist/core/index.js
CHANGED
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
// src/core/index.ts
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.CoreIAM = void 0;
|
|
5
|
-
const DEFAULT_GATEWAY_URL = "https://api.e-qalam.com";
|
|
6
5
|
class CoreIAM {
|
|
7
6
|
apiKey = '';
|
|
8
7
|
baseUrl = '';
|
|
9
8
|
constructor(config) {
|
|
10
9
|
this.apiKey = config?.apiKey || process.env.COREIAM_API_KEY || '';
|
|
11
|
-
|
|
10
|
+
// FIX: Removed hardcoded URL. Throw if missing.
|
|
11
|
+
this.baseUrl = config?.baseUrl || process.env.COREIAM_API_URL || '';
|
|
12
12
|
if (!this.apiKey)
|
|
13
|
-
throw new Error('CoreIAM: Missing API Key');
|
|
13
|
+
throw new Error('CoreIAM: Missing API Key (COREIAM_API_KEY)');
|
|
14
|
+
if (!this.baseUrl)
|
|
15
|
+
throw new Error('CoreIAM: Missing Gateway URL (COREIAM_API_URL)');
|
|
14
16
|
}
|
|
15
17
|
// Exposed publicly so the Next.js layer can use it for getSession()
|
|
16
18
|
async refreshTokens(headers) {
|
package/dist/nextjs/index.d.ts
CHANGED
|
@@ -5,6 +5,6 @@ import { NextRequest, NextResponse } from 'next/server';
|
|
|
5
5
|
import { CoreIAM } from '../core';
|
|
6
6
|
export declare class NextHandlers {
|
|
7
7
|
private iam;
|
|
8
|
-
constructor(iam
|
|
8
|
+
constructor(iam?: CoreIAM);
|
|
9
9
|
handleProxy(req: NextRequest, internalPath: string): Promise<NextResponse<any>>;
|
|
10
10
|
}
|
package/dist/nextjs/index.js
CHANGED
|
@@ -10,13 +10,16 @@ Object.defineProperty(exports, "loginActionSdk", { enumerable: true, get: functi
|
|
|
10
10
|
Object.defineProperty(exports, "registerActionSdk", { enumerable: true, get: function () { return actions_1.registerActionSdk; } });
|
|
11
11
|
Object.defineProperty(exports, "logoutActionSdk", { enumerable: true, get: function () { return actions_1.logoutActionSdk; } });
|
|
12
12
|
Object.defineProperty(exports, "getSession", { enumerable: true, get: function () { return actions_1.getSession; } });
|
|
13
|
+
// FIX: Export the new handleCoreIamAuth function instead of the old middleware
|
|
13
14
|
var middleware_1 = require("./middleware");
|
|
14
15
|
Object.defineProperty(exports, "coreIamMiddleware", { enumerable: true, get: function () { return middleware_1.coreIamMiddleware; } });
|
|
15
16
|
const server_1 = require("next/server");
|
|
17
|
+
const core_1 = require("../core");
|
|
16
18
|
class NextHandlers {
|
|
17
19
|
iam;
|
|
18
20
|
constructor(iam) {
|
|
19
|
-
|
|
21
|
+
// If no iam client is provided, instantiate one using the env variables
|
|
22
|
+
this.iam = iam || new core_1.CoreIAM({ apiKey: process.env.COREIAM_API_KEY, baseUrl: process.env.COREIAM_API_URL });
|
|
20
23
|
}
|
|
21
24
|
async handleProxy(req, internalPath) {
|
|
22
25
|
const method = internalPath === '/csrf' ? 'POST' : req.method;
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Handles CoreIAM token refresh and header rewriting.
|
|
4
|
+
* Returns the NextResponse to return, and whether the user is authenticated.
|
|
5
|
+
*/
|
|
6
|
+
export declare function coreIamMiddleware(request: NextRequest): Promise<{
|
|
7
|
+
response: NextResponse;
|
|
8
|
+
isAuthenticated: boolean;
|
|
9
|
+
}>;
|
|
@@ -4,91 +4,79 @@ exports.coreIamMiddleware = coreIamMiddleware;
|
|
|
4
4
|
// src/nextjs/middleware.ts
|
|
5
5
|
const server_1 = require("next/server");
|
|
6
6
|
const jwt_decode_1 = require("jwt-decode");
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
else {
|
|
23
|
-
if (refreshTokenCookie)
|
|
24
|
-
needsRefresh = true;
|
|
25
|
-
}
|
|
7
|
+
/**
|
|
8
|
+
* Handles CoreIAM token refresh and header rewriting.
|
|
9
|
+
* Returns the NextResponse to return, and whether the user is authenticated.
|
|
10
|
+
*/
|
|
11
|
+
async function coreIamMiddleware(request) {
|
|
12
|
+
const jwtCookie = request.cookies.get("jwt");
|
|
13
|
+
const refreshTokenCookie = request.cookies.get("refreshToken");
|
|
14
|
+
const gatewayUrl = process.env.COREIAM_API_URL;
|
|
15
|
+
let isAuthenticated = false;
|
|
16
|
+
let needsRefresh = false;
|
|
17
|
+
if (jwtCookie) {
|
|
18
|
+
try {
|
|
19
|
+
const decoded = (0, jwt_decode_1.jwtDecode)(jwtCookie.value);
|
|
20
|
+
if (decoded.exp && decoded.exp * 1000 > Date.now()) {
|
|
21
|
+
isAuthenticated = true;
|
|
26
22
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
needsRefresh = true;
|
|
23
|
+
else if (refreshTokenCookie) {
|
|
24
|
+
needsRefresh = true;
|
|
30
25
|
}
|
|
31
26
|
}
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
catch {
|
|
28
|
+
if (refreshTokenCookie)
|
|
29
|
+
needsRefresh = true;
|
|
34
30
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
31
|
+
}
|
|
32
|
+
else if (refreshTokenCookie) {
|
|
33
|
+
needsRefresh = true;
|
|
34
|
+
}
|
|
35
|
+
// 1. ATTEMPT SILENT REFRESH
|
|
36
|
+
if (needsRefresh && gatewayUrl) {
|
|
37
|
+
try {
|
|
38
|
+
const refreshRes = await fetch(`${gatewayUrl}/auth/refresh-token`, {
|
|
39
|
+
method: 'POST',
|
|
40
|
+
headers: {
|
|
41
|
+
'X-API-Key': process.env.COREIAM_API_KEY,
|
|
42
|
+
'cookie': `refreshToken=${refreshTokenCookie?.value || ''}; jwt=${jwtCookie?.value || ''}`,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
if (refreshRes.ok) {
|
|
46
|
+
const setCookies = refreshRes.headers.getSetCookie();
|
|
47
|
+
if (setCookies && setCookies.length > 0) {
|
|
48
|
+
let newJwt = '';
|
|
49
|
+
let newRefreshToken = '';
|
|
50
|
+
setCookies.forEach(cookie => {
|
|
51
|
+
if (cookie.startsWith('jwt='))
|
|
52
|
+
newJwt = cookie.split(';')[0].split('=')[1];
|
|
53
|
+
if (cookie.startsWith('refreshToken='))
|
|
54
|
+
newRefreshToken = cookie.split(';')[0].split('=')[1];
|
|
55
|
+
});
|
|
56
|
+
if (newJwt) {
|
|
61
57
|
const requestHeaders = new Headers(request.headers);
|
|
62
58
|
requestHeaders.set('cookie', `jwt=${newJwt}; refreshToken=${newRefreshToken}`);
|
|
63
59
|
requestHeaders.set('Authorization', `Bearer ${newJwt}`);
|
|
64
60
|
const response = server_1.NextResponse.next({
|
|
65
61
|
request: { headers: requestHeaders },
|
|
66
62
|
});
|
|
67
|
-
// Set the new cookies on the browser
|
|
68
63
|
setCookies.forEach(cookie => {
|
|
69
64
|
response.headers.append('set-cookie', cookie);
|
|
70
65
|
});
|
|
71
|
-
return response
|
|
66
|
+
return { response, isAuthenticated: true };
|
|
72
67
|
}
|
|
73
68
|
}
|
|
74
69
|
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
// Refresh failed, fall through to clear cookies
|
|
77
|
-
}
|
|
78
|
-
// 2. IF REFRESH FAILED, CLEAR COOKIES TO PREVENT INFINITE LOOP
|
|
79
|
-
const res = server_1.NextResponse.redirect(new URL('/auth/login', nextUrl));
|
|
80
|
-
res.cookies.delete('jwt');
|
|
81
|
-
res.cookies.delete('refreshToken');
|
|
82
|
-
return res;
|
|
83
|
-
}
|
|
84
|
-
// 3. STANDARD ROUTE PROTECTION
|
|
85
|
-
const isAuthRoute = pathname.startsWith('/auth/login') || pathname.startsWith('/auth/register');
|
|
86
|
-
if (isAuthRoute && isLoggedIn) {
|
|
87
|
-
return server_1.NextResponse.redirect(new URL('/', nextUrl));
|
|
88
70
|
}
|
|
89
|
-
|
|
90
|
-
|
|
71
|
+
catch (error) {
|
|
72
|
+
// Refresh failed, fall through to clear cookies
|
|
91
73
|
}
|
|
92
|
-
|
|
93
|
-
|
|
74
|
+
// 2. IF REFRESH FAILED, CLEAR COOKIES
|
|
75
|
+
const response = server_1.NextResponse.next();
|
|
76
|
+
response.cookies.delete('jwt');
|
|
77
|
+
response.cookies.delete('refreshToken');
|
|
78
|
+
return { response, isAuthenticated: false };
|
|
79
|
+
}
|
|
80
|
+
// 3. NO REFRESH NEEDED (Token is valid or completely missing)
|
|
81
|
+
return { response: server_1.NextResponse.next(), isAuthenticated };
|
|
94
82
|
}
|