@thunderid/express 0.0.1
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/LICENSE +201 -0
- package/dist/ThunderIDExpressClient.d.ts +29 -0
- package/dist/ThunderIDExpressClient.d.ts.map +1 -0
- package/dist/__legacy__/client.d.ts +46 -0
- package/dist/__legacy__/client.d.ts.map +1 -0
- package/dist/__legacy__/constants/default-options.d.ts +26 -0
- package/dist/__legacy__/constants/default-options.d.ts.map +1 -0
- package/dist/__legacy__/constants/index.d.ts +20 -0
- package/dist/__legacy__/constants/index.d.ts.map +1 -0
- package/dist/__legacy__/constants/logger-config.d.ts +28 -0
- package/dist/__legacy__/constants/logger-config.d.ts.map +1 -0
- package/dist/__legacy__/middleware/authentication.d.ts +23 -0
- package/dist/__legacy__/middleware/authentication.d.ts.map +1 -0
- package/dist/__legacy__/middleware/index.d.ts +20 -0
- package/dist/__legacy__/middleware/index.d.ts.map +1 -0
- package/dist/__legacy__/middleware/protect-route.d.ts +22 -0
- package/dist/__legacy__/middleware/protect-route.d.ts.map +1 -0
- package/dist/__legacy__/models/client-config.d.ts +33 -0
- package/dist/__legacy__/models/client-config.d.ts.map +1 -0
- package/dist/__legacy__/models/data.d.ts +21 -0
- package/dist/__legacy__/models/data.d.ts.map +1 -0
- package/dist/__legacy__/models/index.d.ts +21 -0
- package/dist/__legacy__/models/index.d.ts.map +1 -0
- package/dist/__legacy__/models/protect-route.d.ts +20 -0
- package/dist/__legacy__/models/protect-route.d.ts.map +1 -0
- package/dist/__legacy__/utils/express-utils.d.ts +22 -0
- package/dist/__legacy__/utils/express-utils.d.ts.map +1 -0
- package/dist/cjs/index.cjs +257 -0
- package/dist/edge/index.js +227 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +227 -0
- package/dist/models/config.d.ts +30 -0
- package/dist/models/config.d.ts.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
let __thunderid_node = require("@thunderid/node");
|
|
25
|
+
__thunderid_node = __toESM(__thunderid_node);
|
|
26
|
+
let uuid = require("uuid");
|
|
27
|
+
uuid = __toESM(uuid);
|
|
28
|
+
let express = require("express");
|
|
29
|
+
express = __toESM(express);
|
|
30
|
+
|
|
31
|
+
//#region src/__legacy__/constants/default-options.ts
|
|
32
|
+
/**
|
|
33
|
+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
|
|
34
|
+
*
|
|
35
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
36
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
37
|
+
* in compliance with the License.
|
|
38
|
+
* You may obtain a copy of the License at
|
|
39
|
+
*
|
|
40
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
41
|
+
*
|
|
42
|
+
* Unless required by applicable law or agreed to in writing,
|
|
43
|
+
* software distributed under the License is distributed on an
|
|
44
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
45
|
+
* KIND, either express or implied. See the License for the
|
|
46
|
+
* specific language governing permissions and limitations
|
|
47
|
+
* under the License.
|
|
48
|
+
*/
|
|
49
|
+
let CookieConfig = /* @__PURE__ */ function(CookieConfig$1) {
|
|
50
|
+
CookieConfig$1[CookieConfig$1["defaultMaxAge"] = 9e4] = "defaultMaxAge";
|
|
51
|
+
CookieConfig$1["defaultHttpOnly"] = "true";
|
|
52
|
+
CookieConfig$1["defaultSameSite"] = "lax";
|
|
53
|
+
CookieConfig$1["defaultSecure"] = "false";
|
|
54
|
+
return CookieConfig$1;
|
|
55
|
+
}({});
|
|
56
|
+
const DEFAULT_LOGIN_PATH = "/login";
|
|
57
|
+
const DEFAULT_LOGOUT_PATH = "/logout";
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/__legacy__/middleware/protect-route.ts
|
|
61
|
+
const protectRoute = (thunderIDExpressClient, callback) => {
|
|
62
|
+
return async (req, res, next) => {
|
|
63
|
+
if (req.cookies.THUNDERID_SESSION_ID === void 0) {
|
|
64
|
+
__thunderid_node.Logger.error("No session ID found in the request cookies");
|
|
65
|
+
if (callback(res, "Unauthenticated")) return;
|
|
66
|
+
return next();
|
|
67
|
+
} else if (await thunderIDExpressClient.isSignedIn(req.cookies.THUNDERID_SESSION_ID)) return next();
|
|
68
|
+
else {
|
|
69
|
+
__thunderid_node.Logger.error("Invalid session ID found in the request cookies");
|
|
70
|
+
if (callback(res, "Invalid session cookie")) return;
|
|
71
|
+
return next();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/__legacy__/middleware/authentication.ts
|
|
78
|
+
const thunderIDExpressAuth = (thunderIDExpressClient, config, onSignIn, onSignOut, onError) => {
|
|
79
|
+
const router = new express.default.Router();
|
|
80
|
+
router.use(async (req, res, next) => {
|
|
81
|
+
req.thunderIDAuth = thunderIDExpressClient;
|
|
82
|
+
res.thunderIDAuth = thunderIDExpressClient;
|
|
83
|
+
next();
|
|
84
|
+
});
|
|
85
|
+
router.get(config.loginPath || DEFAULT_LOGIN_PATH, async (req, res, next) => {
|
|
86
|
+
try {
|
|
87
|
+
const response = await thunderIDExpressClient.signIn(req, res, next, config.signInConfig);
|
|
88
|
+
if (response.accessToken || response.idToken) onSignIn(res, response);
|
|
89
|
+
} catch (e) {
|
|
90
|
+
__thunderid_node.Logger.error(e.message);
|
|
91
|
+
onError(res, e);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
router.get(config.logoutPath || DEFAULT_LOGOUT_PATH, async (req, res, next) => {
|
|
95
|
+
if (req.query.state === "sign_out_success") {
|
|
96
|
+
onSignOut(res);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (req.cookies.THUNDERID_SESSION_ID === void 0) {
|
|
100
|
+
onError(res, new __thunderid_node.ThunderIDAuthException("EXPRESS-AUTH_MW-LOGOUT-NF01", "No cookie found in the request", "No cookie was sent with the request. The user may not have signed in yet."));
|
|
101
|
+
return;
|
|
102
|
+
} else try {
|
|
103
|
+
const signOutURL = await req.thunderIDAuth.signOut(req.cookies.THUNDERID_SESSION_ID);
|
|
104
|
+
if (signOutURL) {
|
|
105
|
+
res.cookie("THUNDERID_SESSION_ID", null, { maxAge: 0 });
|
|
106
|
+
res.redirect(signOutURL);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
} catch (e) {
|
|
110
|
+
onError(res, e);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return router;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/__legacy__/utils/express-utils.ts
|
|
119
|
+
var ExpressUtils = class {
|
|
120
|
+
static AUTH_CODE_REGEXP = /[?&]error=[^&]+/;
|
|
121
|
+
/**
|
|
122
|
+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
|
|
123
|
+
*
|
|
124
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
125
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
126
|
+
* in compliance with the License.
|
|
127
|
+
* You may obtain a copy of the License at
|
|
128
|
+
*
|
|
129
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
130
|
+
*
|
|
131
|
+
* Unless required by applicable law or agreed to in writing,
|
|
132
|
+
* software distributed under the License is distributed on an
|
|
133
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
134
|
+
* KIND, either express or implied. See the License for the
|
|
135
|
+
* specific language governing permissions and limitations
|
|
136
|
+
* under the License.
|
|
137
|
+
*/
|
|
138
|
+
static hasErrorInURL(url) {
|
|
139
|
+
return this.AUTH_CODE_REGEXP.test(url);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/__legacy__/client.ts
|
|
145
|
+
var ThunderIDExpressClient = class ThunderIDExpressClient {
|
|
146
|
+
_authClient;
|
|
147
|
+
_storage;
|
|
148
|
+
static _clientConfig;
|
|
149
|
+
static _instance;
|
|
150
|
+
constructor(config, storage) {
|
|
151
|
+
ThunderIDExpressClient._clientConfig = { ...config };
|
|
152
|
+
const nodeClientConfig = {
|
|
153
|
+
...config,
|
|
154
|
+
afterSignInUrl: config.appURL + (config.loginPath || DEFAULT_LOGIN_PATH),
|
|
155
|
+
afterSignOutUrl: config.appURL + (config.logoutPath || DEFAULT_LOGOUT_PATH)
|
|
156
|
+
};
|
|
157
|
+
if (storage) {
|
|
158
|
+
__thunderid_node.Logger.debug("Initializing user provided storage");
|
|
159
|
+
this._storage = storage;
|
|
160
|
+
}
|
|
161
|
+
this._authClient = new __thunderid_node.LegacyThunderIDNodeClient();
|
|
162
|
+
this._authClient.initialize(nodeClientConfig, this._storage);
|
|
163
|
+
}
|
|
164
|
+
static getInstance(config, storage) {
|
|
165
|
+
if (!ThunderIDExpressClient._instance && config) {
|
|
166
|
+
ThunderIDExpressClient._instance = new ThunderIDExpressClient(config, storage);
|
|
167
|
+
__thunderid_node.Logger.debug("Initialized ThunderIDExpressClient successfully");
|
|
168
|
+
}
|
|
169
|
+
if (!ThunderIDExpressClient._instance && !config) throw Error(new __thunderid_node.ThunderIDAuthException("EXPRESS-CLIENT-GI1-NF01", "User configuration is not found", "User config has not been passed to initialize ThunderIDExpressClient").toString());
|
|
170
|
+
return ThunderIDExpressClient._instance;
|
|
171
|
+
}
|
|
172
|
+
async signIn(req, res, next, signInConfig) {
|
|
173
|
+
if (ExpressUtils.hasErrorInURL(req.originalUrl)) return Promise.reject(new __thunderid_node.ThunderIDAuthException("EXPRESS-CLIENT-SI-IV01", "Invalid login request URL", "Login request contains an error query parameter in the URL"));
|
|
174
|
+
let userId = req.cookies.THUNDERID_SESSION_ID;
|
|
175
|
+
if (!userId) userId = (0, uuid.v4)();
|
|
176
|
+
const authRedirectCallback = (url) => {
|
|
177
|
+
if (url) {
|
|
178
|
+
__thunderid_node.Logger.debug("Redirecting to: " + url);
|
|
179
|
+
res.cookie("THUNDERID_SESSION_ID", userId, {
|
|
180
|
+
maxAge: ThunderIDExpressClient._clientConfig.cookieConfig?.maxAge ? ThunderIDExpressClient._clientConfig.cookieConfig.maxAge : CookieConfig.defaultMaxAge,
|
|
181
|
+
httpOnly: ThunderIDExpressClient._clientConfig.cookieConfig?.httpOnly ?? CookieConfig.defaultHttpOnly,
|
|
182
|
+
sameSite: ThunderIDExpressClient._clientConfig.cookieConfig?.sameSite ?? CookieConfig.defaultSameSite,
|
|
183
|
+
secure: ThunderIDExpressClient._clientConfig.cookieConfig?.secure ?? CookieConfig.defaultSecure
|
|
184
|
+
});
|
|
185
|
+
res.redirect(url);
|
|
186
|
+
next && typeof next === "function" && next();
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const authResponse = await this._authClient.signIn(authRedirectCallback, userId, req.query.code, req.query.session_state, req.query.state, signInConfig);
|
|
190
|
+
if (authResponse.accessToken || authResponse.idToken) return authResponse;
|
|
191
|
+
else return {
|
|
192
|
+
accessToken: "",
|
|
193
|
+
createdAt: 0,
|
|
194
|
+
expiresIn: "",
|
|
195
|
+
idToken: "",
|
|
196
|
+
refreshToken: "",
|
|
197
|
+
scope: "",
|
|
198
|
+
tokenType: ""
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
async signOut(userId) {
|
|
202
|
+
return this._authClient.signOut(userId);
|
|
203
|
+
}
|
|
204
|
+
async isSignedIn(userId) {
|
|
205
|
+
return this._authClient.isSignedIn(userId);
|
|
206
|
+
}
|
|
207
|
+
async getIdToken(userId) {
|
|
208
|
+
return this._authClient.getIdToken(userId);
|
|
209
|
+
}
|
|
210
|
+
async getUser(userId) {
|
|
211
|
+
return this._authClient.getUser(userId);
|
|
212
|
+
}
|
|
213
|
+
async getOpenIDProviderEndpoints() {
|
|
214
|
+
return this._authClient.getOpenIDProviderEndpoints();
|
|
215
|
+
}
|
|
216
|
+
async getDecodedIdToken(userId) {
|
|
217
|
+
return this._authClient.getDecodedIdToken(userId);
|
|
218
|
+
}
|
|
219
|
+
async getAccessToken(userId) {
|
|
220
|
+
return this._authClient.getAccessToken(userId);
|
|
221
|
+
}
|
|
222
|
+
async exchangeToken(config, userId) {
|
|
223
|
+
return this._authClient.exchangeToken(config, userId);
|
|
224
|
+
}
|
|
225
|
+
async reInitialize(config) {
|
|
226
|
+
return this._authClient.reInitialize(config);
|
|
227
|
+
}
|
|
228
|
+
async revokeAccessToken(userId) {
|
|
229
|
+
return this._authClient.revokeAccessToken(userId);
|
|
230
|
+
}
|
|
231
|
+
static didSignOutFail(afterSignOutUrl) {
|
|
232
|
+
return __thunderid_node.LegacyThunderIDNodeClient.didSignOutFail(afterSignOutUrl);
|
|
233
|
+
}
|
|
234
|
+
static isSignOutSuccessful(afterSignOutUrl) {
|
|
235
|
+
return __thunderid_node.LegacyThunderIDNodeClient.isSignOutSuccessful(afterSignOutUrl);
|
|
236
|
+
}
|
|
237
|
+
static protectRoute(callback) {
|
|
238
|
+
if (!this._instance) throw new __thunderid_node.ThunderIDAuthException("EXPRESS-CLIENT-PR-NF01", "ThunderIDExpressClient is not instantiated", "Create an instance of ThunderIDExpressClient before using calling this method.");
|
|
239
|
+
return protectRoute(this._instance, callback);
|
|
240
|
+
}
|
|
241
|
+
static thunderIDExpressAuth(onSignIn, onSignOut, onError) {
|
|
242
|
+
if (!this._instance) throw new __thunderid_node.ThunderIDAuthException("EXPRESS-CLIENT-AEA-NF01", "ThunderIDExpressClient is not instantiated", "Create an instance of ThunderIDExpressClient before using calling this method.");
|
|
243
|
+
return thunderIDExpressAuth(this._instance, ThunderIDExpressClient._clientConfig, onSignIn, onSignOut, onError);
|
|
244
|
+
}
|
|
245
|
+
async getStorageManager() {
|
|
246
|
+
return this._authClient.getStorageManager();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
//#endregion
|
|
251
|
+
exports.ThunderIDExpressClient = ThunderIDExpressClient;
|
|
252
|
+
Object.keys(__thunderid_node).forEach(function (k) {
|
|
253
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
254
|
+
enumerable: true,
|
|
255
|
+
get: function () { return __thunderid_node[k]; }
|
|
256
|
+
});
|
|
257
|
+
});
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { LegacyThunderIDNodeClient, Logger, ThunderIDAuthException } from "@thunderid/node";
|
|
2
|
+
import { v4 } from "uuid";
|
|
3
|
+
import express from "express";
|
|
4
|
+
|
|
5
|
+
export * from "@thunderid/node"
|
|
6
|
+
|
|
7
|
+
//#region src/__legacy__/constants/default-options.ts
|
|
8
|
+
/**
|
|
9
|
+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
|
|
10
|
+
*
|
|
11
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
12
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
13
|
+
* in compliance with the License.
|
|
14
|
+
* You may obtain a copy of the License at
|
|
15
|
+
*
|
|
16
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
+
*
|
|
18
|
+
* Unless required by applicable law or agreed to in writing,
|
|
19
|
+
* software distributed under the License is distributed on an
|
|
20
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
21
|
+
* KIND, either express or implied. See the License for the
|
|
22
|
+
* specific language governing permissions and limitations
|
|
23
|
+
* under the License.
|
|
24
|
+
*/
|
|
25
|
+
let CookieConfig = /* @__PURE__ */ function(CookieConfig$1) {
|
|
26
|
+
CookieConfig$1[CookieConfig$1["defaultMaxAge"] = 9e4] = "defaultMaxAge";
|
|
27
|
+
CookieConfig$1["defaultHttpOnly"] = "true";
|
|
28
|
+
CookieConfig$1["defaultSameSite"] = "lax";
|
|
29
|
+
CookieConfig$1["defaultSecure"] = "false";
|
|
30
|
+
return CookieConfig$1;
|
|
31
|
+
}({});
|
|
32
|
+
const DEFAULT_LOGIN_PATH = "/login";
|
|
33
|
+
const DEFAULT_LOGOUT_PATH = "/logout";
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/__legacy__/middleware/protect-route.ts
|
|
37
|
+
const protectRoute = (thunderIDExpressClient, callback) => {
|
|
38
|
+
return async (req, res, next) => {
|
|
39
|
+
if (req.cookies.THUNDERID_SESSION_ID === void 0) {
|
|
40
|
+
Logger.error("No session ID found in the request cookies");
|
|
41
|
+
if (callback(res, "Unauthenticated")) return;
|
|
42
|
+
return next();
|
|
43
|
+
} else if (await thunderIDExpressClient.isSignedIn(req.cookies.THUNDERID_SESSION_ID)) return next();
|
|
44
|
+
else {
|
|
45
|
+
Logger.error("Invalid session ID found in the request cookies");
|
|
46
|
+
if (callback(res, "Invalid session cookie")) return;
|
|
47
|
+
return next();
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/__legacy__/middleware/authentication.ts
|
|
54
|
+
const thunderIDExpressAuth = (thunderIDExpressClient, config, onSignIn, onSignOut, onError) => {
|
|
55
|
+
const router = new express.Router();
|
|
56
|
+
router.use(async (req, res, next) => {
|
|
57
|
+
req.thunderIDAuth = thunderIDExpressClient;
|
|
58
|
+
res.thunderIDAuth = thunderIDExpressClient;
|
|
59
|
+
next();
|
|
60
|
+
});
|
|
61
|
+
router.get(config.loginPath || DEFAULT_LOGIN_PATH, async (req, res, next) => {
|
|
62
|
+
try {
|
|
63
|
+
const response = await thunderIDExpressClient.signIn(req, res, next, config.signInConfig);
|
|
64
|
+
if (response.accessToken || response.idToken) onSignIn(res, response);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
Logger.error(e.message);
|
|
67
|
+
onError(res, e);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
router.get(config.logoutPath || DEFAULT_LOGOUT_PATH, async (req, res, next) => {
|
|
71
|
+
if (req.query.state === "sign_out_success") {
|
|
72
|
+
onSignOut(res);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (req.cookies.THUNDERID_SESSION_ID === void 0) {
|
|
76
|
+
onError(res, new ThunderIDAuthException("EXPRESS-AUTH_MW-LOGOUT-NF01", "No cookie found in the request", "No cookie was sent with the request. The user may not have signed in yet."));
|
|
77
|
+
return;
|
|
78
|
+
} else try {
|
|
79
|
+
const signOutURL = await req.thunderIDAuth.signOut(req.cookies.THUNDERID_SESSION_ID);
|
|
80
|
+
if (signOutURL) {
|
|
81
|
+
res.cookie("THUNDERID_SESSION_ID", null, { maxAge: 0 });
|
|
82
|
+
res.redirect(signOutURL);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
} catch (e) {
|
|
86
|
+
onError(res, e);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return router;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/__legacy__/utils/express-utils.ts
|
|
95
|
+
var ExpressUtils = class {
|
|
96
|
+
static AUTH_CODE_REGEXP = /[?&]error=[^&]+/;
|
|
97
|
+
/**
|
|
98
|
+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
|
|
99
|
+
*
|
|
100
|
+
* WSO2 LLC. licenses this file to you under the Apache License,
|
|
101
|
+
* Version 2.0 (the "License"); you may not use this file except
|
|
102
|
+
* in compliance with the License.
|
|
103
|
+
* You may obtain a copy of the License at
|
|
104
|
+
*
|
|
105
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
106
|
+
*
|
|
107
|
+
* Unless required by applicable law or agreed to in writing,
|
|
108
|
+
* software distributed under the License is distributed on an
|
|
109
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
110
|
+
* KIND, either express or implied. See the License for the
|
|
111
|
+
* specific language governing permissions and limitations
|
|
112
|
+
* under the License.
|
|
113
|
+
*/
|
|
114
|
+
static hasErrorInURL(url) {
|
|
115
|
+
return this.AUTH_CODE_REGEXP.test(url);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/__legacy__/client.ts
|
|
121
|
+
var ThunderIDExpressClient = class ThunderIDExpressClient {
|
|
122
|
+
_authClient;
|
|
123
|
+
_storage;
|
|
124
|
+
static _clientConfig;
|
|
125
|
+
static _instance;
|
|
126
|
+
constructor(config, storage) {
|
|
127
|
+
ThunderIDExpressClient._clientConfig = { ...config };
|
|
128
|
+
const nodeClientConfig = {
|
|
129
|
+
...config,
|
|
130
|
+
afterSignInUrl: config.appURL + (config.loginPath || DEFAULT_LOGIN_PATH),
|
|
131
|
+
afterSignOutUrl: config.appURL + (config.logoutPath || DEFAULT_LOGOUT_PATH)
|
|
132
|
+
};
|
|
133
|
+
if (storage) {
|
|
134
|
+
Logger.debug("Initializing user provided storage");
|
|
135
|
+
this._storage = storage;
|
|
136
|
+
}
|
|
137
|
+
this._authClient = new LegacyThunderIDNodeClient();
|
|
138
|
+
this._authClient.initialize(nodeClientConfig, this._storage);
|
|
139
|
+
}
|
|
140
|
+
static getInstance(config, storage) {
|
|
141
|
+
if (!ThunderIDExpressClient._instance && config) {
|
|
142
|
+
ThunderIDExpressClient._instance = new ThunderIDExpressClient(config, storage);
|
|
143
|
+
Logger.debug("Initialized ThunderIDExpressClient successfully");
|
|
144
|
+
}
|
|
145
|
+
if (!ThunderIDExpressClient._instance && !config) throw Error(new ThunderIDAuthException("EXPRESS-CLIENT-GI1-NF01", "User configuration is not found", "User config has not been passed to initialize ThunderIDExpressClient").toString());
|
|
146
|
+
return ThunderIDExpressClient._instance;
|
|
147
|
+
}
|
|
148
|
+
async signIn(req, res, next, signInConfig) {
|
|
149
|
+
if (ExpressUtils.hasErrorInURL(req.originalUrl)) return Promise.reject(new ThunderIDAuthException("EXPRESS-CLIENT-SI-IV01", "Invalid login request URL", "Login request contains an error query parameter in the URL"));
|
|
150
|
+
let userId = req.cookies.THUNDERID_SESSION_ID;
|
|
151
|
+
if (!userId) userId = v4();
|
|
152
|
+
const authRedirectCallback = (url) => {
|
|
153
|
+
if (url) {
|
|
154
|
+
Logger.debug("Redirecting to: " + url);
|
|
155
|
+
res.cookie("THUNDERID_SESSION_ID", userId, {
|
|
156
|
+
maxAge: ThunderIDExpressClient._clientConfig.cookieConfig?.maxAge ? ThunderIDExpressClient._clientConfig.cookieConfig.maxAge : CookieConfig.defaultMaxAge,
|
|
157
|
+
httpOnly: ThunderIDExpressClient._clientConfig.cookieConfig?.httpOnly ?? CookieConfig.defaultHttpOnly,
|
|
158
|
+
sameSite: ThunderIDExpressClient._clientConfig.cookieConfig?.sameSite ?? CookieConfig.defaultSameSite,
|
|
159
|
+
secure: ThunderIDExpressClient._clientConfig.cookieConfig?.secure ?? CookieConfig.defaultSecure
|
|
160
|
+
});
|
|
161
|
+
res.redirect(url);
|
|
162
|
+
next && typeof next === "function" && next();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const authResponse = await this._authClient.signIn(authRedirectCallback, userId, req.query.code, req.query.session_state, req.query.state, signInConfig);
|
|
166
|
+
if (authResponse.accessToken || authResponse.idToken) return authResponse;
|
|
167
|
+
else return {
|
|
168
|
+
accessToken: "",
|
|
169
|
+
createdAt: 0,
|
|
170
|
+
expiresIn: "",
|
|
171
|
+
idToken: "",
|
|
172
|
+
refreshToken: "",
|
|
173
|
+
scope: "",
|
|
174
|
+
tokenType: ""
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async signOut(userId) {
|
|
178
|
+
return this._authClient.signOut(userId);
|
|
179
|
+
}
|
|
180
|
+
async isSignedIn(userId) {
|
|
181
|
+
return this._authClient.isSignedIn(userId);
|
|
182
|
+
}
|
|
183
|
+
async getIdToken(userId) {
|
|
184
|
+
return this._authClient.getIdToken(userId);
|
|
185
|
+
}
|
|
186
|
+
async getUser(userId) {
|
|
187
|
+
return this._authClient.getUser(userId);
|
|
188
|
+
}
|
|
189
|
+
async getOpenIDProviderEndpoints() {
|
|
190
|
+
return this._authClient.getOpenIDProviderEndpoints();
|
|
191
|
+
}
|
|
192
|
+
async getDecodedIdToken(userId) {
|
|
193
|
+
return this._authClient.getDecodedIdToken(userId);
|
|
194
|
+
}
|
|
195
|
+
async getAccessToken(userId) {
|
|
196
|
+
return this._authClient.getAccessToken(userId);
|
|
197
|
+
}
|
|
198
|
+
async exchangeToken(config, userId) {
|
|
199
|
+
return this._authClient.exchangeToken(config, userId);
|
|
200
|
+
}
|
|
201
|
+
async reInitialize(config) {
|
|
202
|
+
return this._authClient.reInitialize(config);
|
|
203
|
+
}
|
|
204
|
+
async revokeAccessToken(userId) {
|
|
205
|
+
return this._authClient.revokeAccessToken(userId);
|
|
206
|
+
}
|
|
207
|
+
static didSignOutFail(afterSignOutUrl) {
|
|
208
|
+
return LegacyThunderIDNodeClient.didSignOutFail(afterSignOutUrl);
|
|
209
|
+
}
|
|
210
|
+
static isSignOutSuccessful(afterSignOutUrl) {
|
|
211
|
+
return LegacyThunderIDNodeClient.isSignOutSuccessful(afterSignOutUrl);
|
|
212
|
+
}
|
|
213
|
+
static protectRoute(callback) {
|
|
214
|
+
if (!this._instance) throw new ThunderIDAuthException("EXPRESS-CLIENT-PR-NF01", "ThunderIDExpressClient is not instantiated", "Create an instance of ThunderIDExpressClient before using calling this method.");
|
|
215
|
+
return protectRoute(this._instance, callback);
|
|
216
|
+
}
|
|
217
|
+
static thunderIDExpressAuth(onSignIn, onSignOut, onError) {
|
|
218
|
+
if (!this._instance) throw new ThunderIDAuthException("EXPRESS-CLIENT-AEA-NF01", "ThunderIDExpressClient is not instantiated", "Create an instance of ThunderIDExpressClient before using calling this method.");
|
|
219
|
+
return thunderIDExpressAuth(this._instance, ThunderIDExpressClient._clientConfig, onSignIn, onSignOut, onError);
|
|
220
|
+
}
|
|
221
|
+
async getStorageManager() {
|
|
222
|
+
return this._authClient.getStorageManager();
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
//#endregion
|
|
227
|
+
export { ThunderIDExpressClient };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
export * from './__legacy__/models';
|
|
19
|
+
export * from './__legacy__/client';
|
|
20
|
+
export * from '@thunderid/node';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AAEpC,cAAc,iBAAiB,CAAC"}
|