@asgardeo/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/README.md +72 -0
- package/dist/AsgardeoExpressClient.d.ts +28 -0
- package/dist/__legacy__/client.d.ts +45 -0
- package/dist/__legacy__/constants/default-options.d.ts +25 -0
- package/dist/__legacy__/constants/index.d.ts +19 -0
- package/dist/__legacy__/constants/logger-config.d.ts +27 -0
- package/dist/__legacy__/middleware/authentication.d.ts +22 -0
- package/dist/__legacy__/middleware/index.d.ts +19 -0
- package/dist/__legacy__/middleware/protect-route.d.ts +21 -0
- package/dist/__legacy__/models/client-config.d.ts +32 -0
- package/dist/__legacy__/models/data.d.ts +20 -0
- package/dist/__legacy__/models/index.d.ts +20 -0
- package/dist/__legacy__/models/protect-route.d.ts +19 -0
- package/dist/__legacy__/utils/express-utils.d.ts +11 -0
- package/dist/cjs/index.js +302 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +270 -0
- package/dist/index.js.map +7 -0
- package/dist/models/config.d.ts +29 -0
- package/package.json +65 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
31
|
+
|
|
32
|
+
// src/index.ts
|
|
33
|
+
var index_exports = {};
|
|
34
|
+
__export(index_exports, {
|
|
35
|
+
AsgardeoExpressClient: () => AsgardeoExpressClient
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
|
|
39
|
+
// src/__legacy__/client.ts
|
|
40
|
+
var import_node3 = require("@asgardeo/node");
|
|
41
|
+
|
|
42
|
+
// src/__legacy__/constants/default-options.ts
|
|
43
|
+
var DEFAULT_LOGIN_PATH = "/login";
|
|
44
|
+
var DEFAULT_LOGOUT_PATH = "/logout";
|
|
45
|
+
|
|
46
|
+
// src/__legacy__/client.ts
|
|
47
|
+
var import_uuid = require("uuid");
|
|
48
|
+
|
|
49
|
+
// src/__legacy__/middleware/protect-route.ts
|
|
50
|
+
var import_node = require("@asgardeo/node");
|
|
51
|
+
var protectRoute = (asgardeoExpressClient, callback) => {
|
|
52
|
+
return async (req, res, next) => {
|
|
53
|
+
if (req.cookies.ASGARDEO_SESSION_ID === void 0) {
|
|
54
|
+
import_node.Logger.error("No session ID found in the request cookies");
|
|
55
|
+
if (callback(res, "Unauthenticated")) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
return next();
|
|
59
|
+
} else {
|
|
60
|
+
const isCookieValid = await asgardeoExpressClient.isSignedIn(req.cookies.ASGARDEO_SESSION_ID);
|
|
61
|
+
if (isCookieValid) {
|
|
62
|
+
return next();
|
|
63
|
+
} else {
|
|
64
|
+
import_node.Logger.error("Invalid session ID found in the request cookies");
|
|
65
|
+
if (callback(res, "Invalid session cookie")) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
return next();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/__legacy__/middleware/authentication.ts
|
|
75
|
+
var import_node2 = require("@asgardeo/node");
|
|
76
|
+
var import_express = __toESM(require("express"), 1);
|
|
77
|
+
var asgardeoExpressAuth = (asgardeoExpressClient, config, onSignIn, onSignOut, onError) => {
|
|
78
|
+
const router = new import_express.default.Router();
|
|
79
|
+
router.use(async (req, res, next) => {
|
|
80
|
+
req.asgardeoAuth = asgardeoExpressClient;
|
|
81
|
+
res.asgardeoAuth = asgardeoExpressClient;
|
|
82
|
+
next();
|
|
83
|
+
});
|
|
84
|
+
router.get(
|
|
85
|
+
config.loginPath || DEFAULT_LOGIN_PATH,
|
|
86
|
+
async (req, res, next) => {
|
|
87
|
+
try {
|
|
88
|
+
const response = await asgardeoExpressClient.signIn(req, res, next, config.signInConfig);
|
|
89
|
+
if (response.accessToken || response.idToken) {
|
|
90
|
+
onSignIn(res, response);
|
|
91
|
+
}
|
|
92
|
+
} catch (e) {
|
|
93
|
+
import_node2.Logger.error(e.message);
|
|
94
|
+
onError(res, e);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
router.get(
|
|
99
|
+
config.logoutPath || DEFAULT_LOGOUT_PATH,
|
|
100
|
+
async (req, res, next) => {
|
|
101
|
+
if (req.query.state === "sign_out_success") {
|
|
102
|
+
onSignOut(res);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (req.cookies.ASGARDEO_SESSION_ID === void 0) {
|
|
106
|
+
onError(
|
|
107
|
+
res,
|
|
108
|
+
new import_node2.AsgardeoAuthException(
|
|
109
|
+
"EXPRESS-AUTH_MW-LOGOUT-NF01",
|
|
110
|
+
"No cookie found in the request",
|
|
111
|
+
"No cookie was sent with the request. The user may not have signed in yet."
|
|
112
|
+
)
|
|
113
|
+
);
|
|
114
|
+
return;
|
|
115
|
+
} else {
|
|
116
|
+
try {
|
|
117
|
+
const signOutURL = await req.asgardeoAuth.signOut(req.cookies.ASGARDEO_SESSION_ID);
|
|
118
|
+
if (signOutURL) {
|
|
119
|
+
res.cookie("ASGARDEO_SESSION_ID", null, { maxAge: 0 });
|
|
120
|
+
res.redirect(signOutURL);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
} catch (e) {
|
|
124
|
+
onError(res, e);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
);
|
|
130
|
+
return router;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// src/__legacy__/utils/express-utils.ts
|
|
134
|
+
var ExpressUtils = class {
|
|
135
|
+
/**
|
|
136
|
+
* Util function to check if the URL contains an error.
|
|
137
|
+
*
|
|
138
|
+
* @param url - URL to be checked.
|
|
139
|
+
*
|
|
140
|
+
* @returns {boolean} - True if the URL contains an error.
|
|
141
|
+
*/
|
|
142
|
+
static hasErrorInURL(url) {
|
|
143
|
+
return this.AUTH_CODE_REGEXP.test(url);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
__publicField(ExpressUtils, "AUTH_CODE_REGEXP", /[?&]error=[^&]+/);
|
|
147
|
+
|
|
148
|
+
// src/__legacy__/client.ts
|
|
149
|
+
var _AsgardeoExpressClient = class _AsgardeoExpressClient {
|
|
150
|
+
constructor(config, storage) {
|
|
151
|
+
__publicField(this, "_authClient");
|
|
152
|
+
__publicField(this, "_storage");
|
|
153
|
+
_AsgardeoExpressClient._clientConfig = { ...config };
|
|
154
|
+
const nodeClientConfig = {
|
|
155
|
+
...config,
|
|
156
|
+
afterSignInUrl: config.appURL + (config.loginPath || DEFAULT_LOGIN_PATH),
|
|
157
|
+
afterSignOutUrl: config.appURL + (config.logoutPath || DEFAULT_LOGOUT_PATH)
|
|
158
|
+
};
|
|
159
|
+
if (storage) {
|
|
160
|
+
import_node3.Logger.debug("Initializing user provided storage");
|
|
161
|
+
this._storage = storage;
|
|
162
|
+
}
|
|
163
|
+
this._authClient = new import_node3.LegacyAsgardeoNodeClient();
|
|
164
|
+
this._authClient.initialize(nodeClientConfig, this._storage);
|
|
165
|
+
}
|
|
166
|
+
static getInstance(config, storage) {
|
|
167
|
+
if (!_AsgardeoExpressClient._instance && config) {
|
|
168
|
+
_AsgardeoExpressClient._instance = new _AsgardeoExpressClient(config, storage);
|
|
169
|
+
import_node3.Logger.debug("Initialized AsgardeoExpressClient successfully");
|
|
170
|
+
}
|
|
171
|
+
if (!_AsgardeoExpressClient._instance && !config) {
|
|
172
|
+
throw Error(
|
|
173
|
+
new import_node3.AsgardeoAuthException(
|
|
174
|
+
"EXPRESS-CLIENT-GI1-NF01",
|
|
175
|
+
"User configuration is not found",
|
|
176
|
+
"User config has not been passed to initialize AsgardeoExpressClient"
|
|
177
|
+
).toString()
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return _AsgardeoExpressClient._instance;
|
|
181
|
+
}
|
|
182
|
+
async signIn(req, res, next, signInConfig) {
|
|
183
|
+
if (ExpressUtils.hasErrorInURL(req.originalUrl)) {
|
|
184
|
+
return Promise.reject(
|
|
185
|
+
new import_node3.AsgardeoAuthException(
|
|
186
|
+
"EXPRESS-CLIENT-SI-IV01",
|
|
187
|
+
"Invalid login request URL",
|
|
188
|
+
"Login request contains an error query parameter in the URL"
|
|
189
|
+
)
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
let userId = req.cookies.ASGARDEO_SESSION_ID;
|
|
193
|
+
if (!userId) {
|
|
194
|
+
userId = (0, import_uuid.v4)();
|
|
195
|
+
}
|
|
196
|
+
const authRedirectCallback = (url) => {
|
|
197
|
+
if (url) {
|
|
198
|
+
import_node3.Logger.debug("Redirecting to: " + url);
|
|
199
|
+
res.cookie("ASGARDEO_SESSION_ID", userId, {
|
|
200
|
+
maxAge: _AsgardeoExpressClient._clientConfig.cookieConfig?.maxAge ? _AsgardeoExpressClient._clientConfig.cookieConfig.maxAge : 9e4 /* defaultMaxAge */,
|
|
201
|
+
httpOnly: _AsgardeoExpressClient._clientConfig.cookieConfig?.httpOnly ?? "true" /* defaultHttpOnly */,
|
|
202
|
+
sameSite: _AsgardeoExpressClient._clientConfig.cookieConfig?.sameSite ?? "lax" /* defaultSameSite */,
|
|
203
|
+
secure: _AsgardeoExpressClient._clientConfig.cookieConfig?.secure ?? "false" /* defaultSecure */
|
|
204
|
+
});
|
|
205
|
+
res.redirect(url);
|
|
206
|
+
next && typeof next === "function" && next();
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
const authResponse = await this._authClient.signIn(
|
|
210
|
+
authRedirectCallback,
|
|
211
|
+
userId,
|
|
212
|
+
req.query.code,
|
|
213
|
+
req.query.session_state,
|
|
214
|
+
req.query.state,
|
|
215
|
+
signInConfig
|
|
216
|
+
);
|
|
217
|
+
if (authResponse.accessToken || authResponse.idToken) {
|
|
218
|
+
return authResponse;
|
|
219
|
+
} else {
|
|
220
|
+
return {
|
|
221
|
+
accessToken: "",
|
|
222
|
+
createdAt: 0,
|
|
223
|
+
expiresIn: "",
|
|
224
|
+
idToken: "",
|
|
225
|
+
refreshToken: "",
|
|
226
|
+
scope: "",
|
|
227
|
+
tokenType: ""
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async signOut(userId) {
|
|
232
|
+
return this._authClient.signOut(userId);
|
|
233
|
+
}
|
|
234
|
+
async isSignedIn(userId) {
|
|
235
|
+
return this._authClient.isSignedIn(userId);
|
|
236
|
+
}
|
|
237
|
+
async getIdToken(userId) {
|
|
238
|
+
return this._authClient.getIdToken(userId);
|
|
239
|
+
}
|
|
240
|
+
async getUser(userId) {
|
|
241
|
+
return this._authClient.getUser(userId);
|
|
242
|
+
}
|
|
243
|
+
async getOpenIDProviderEndpoints() {
|
|
244
|
+
return this._authClient.getOpenIDProviderEndpoints();
|
|
245
|
+
}
|
|
246
|
+
async getDecodedIdToken(userId) {
|
|
247
|
+
return this._authClient.getDecodedIdToken(userId);
|
|
248
|
+
}
|
|
249
|
+
async getAccessToken(userId) {
|
|
250
|
+
return this._authClient.getAccessToken(userId);
|
|
251
|
+
}
|
|
252
|
+
async exchangeToken(config, userId) {
|
|
253
|
+
return this._authClient.exchangeToken(config, userId);
|
|
254
|
+
}
|
|
255
|
+
async reInitialize(config) {
|
|
256
|
+
return this._authClient.reInitialize(config);
|
|
257
|
+
}
|
|
258
|
+
async revokeAccessToken(userId) {
|
|
259
|
+
return this._authClient.revokeAccessToken(userId);
|
|
260
|
+
}
|
|
261
|
+
static didSignOutFail(afterSignOutUrl) {
|
|
262
|
+
return import_node3.LegacyAsgardeoNodeClient.didSignOutFail(afterSignOutUrl);
|
|
263
|
+
}
|
|
264
|
+
static isSignOutSuccessful(afterSignOutUrl) {
|
|
265
|
+
return import_node3.LegacyAsgardeoNodeClient.isSignOutSuccessful(afterSignOutUrl);
|
|
266
|
+
}
|
|
267
|
+
static protectRoute(callback) {
|
|
268
|
+
if (!this._instance) {
|
|
269
|
+
throw new import_node3.AsgardeoAuthException(
|
|
270
|
+
"EXPRESS-CLIENT-PR-NF01",
|
|
271
|
+
"AsgardeoExpressClient is not instantiated",
|
|
272
|
+
"Create an instance of AsgardeoExpressClient before using calling this method."
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
return protectRoute(this._instance, callback);
|
|
276
|
+
}
|
|
277
|
+
static asgardeoExpressAuth(onSignIn, onSignOut, onError) {
|
|
278
|
+
if (!this._instance) {
|
|
279
|
+
throw new import_node3.AsgardeoAuthException(
|
|
280
|
+
"EXPRESS-CLIENT-AEA-NF01",
|
|
281
|
+
"AsgardeoExpressClient is not instantiated",
|
|
282
|
+
"Create an instance of AsgardeoExpressClient before using calling this method."
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return asgardeoExpressAuth(this._instance, _AsgardeoExpressClient._clientConfig, onSignIn, onSignOut, onError);
|
|
286
|
+
}
|
|
287
|
+
async getStorageManager() {
|
|
288
|
+
return this._authClient.getStorageManager();
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
__publicField(_AsgardeoExpressClient, "_clientConfig");
|
|
292
|
+
__publicField(_AsgardeoExpressClient, "_instance");
|
|
293
|
+
var AsgardeoExpressClient = _AsgardeoExpressClient;
|
|
294
|
+
|
|
295
|
+
// src/index.ts
|
|
296
|
+
__reExport(index_exports, require("@asgardeo/node"), module.exports);
|
|
297
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
298
|
+
0 && (module.exports = {
|
|
299
|
+
AsgardeoExpressClient,
|
|
300
|
+
...require("@asgardeo/node")
|
|
301
|
+
});
|
|
302
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.ts", "../../src/__legacy__/client.ts", "../../src/__legacy__/constants/default-options.ts", "../../src/__legacy__/middleware/protect-route.ts", "../../src/__legacy__/middleware/authentication.ts", "../../src/__legacy__/utils/express-utils.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com) All Rights Reserved.\n *\n * WSO2 Inc. licenses this file to you under the Apache License,\n * Version 2.0 (the \"License\"); you may not use this file except\n * in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing,\n * software distributed under the License is distributed on an\n * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n * KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations\n * under the License.\n */\n\nexport * from \"./__legacy__/models\";\nexport * from \"./__legacy__/client\";\n\nexport * from '@asgardeo/node';\n", "/**\n * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com) All Rights Reserved.\n *\n * WSO2 Inc. licenses this file to you under the Apache License,\n * Version 2.0 (the \"License\"); you may not use this file except\n * in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing,\n * software distributed under the License is distributed on an\n * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n * KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations\n * under the License.\n */\n\nimport {\n LegacyAsgardeoNodeClient,\n AuthClientConfig,\n AuthURLCallback,\n TokenResponse,\n Storage,\n User,\n OIDCEndpoints,\n IdTokenPayload,\n TokenExchangeRequestConfig,\n AsgardeoAuthException,\n Logger,\n} from '@asgardeo/node';\nimport {CookieConfig, DEFAULT_LOGIN_PATH, DEFAULT_LOGOUT_PATH} from './constants';\nimport {ExpressClientConfig, UnauthenticatedCallback} from './models';\nimport express from 'express';\nimport {v4 as uuidv4} from 'uuid';\nimport {asgardeoExpressAuth, protectRoute} from './middleware';\nimport {ExpressUtils} from './utils/express-utils';\n\nexport class AsgardeoExpressClient {\n private _authClient: LegacyAsgardeoNodeClient<AuthClientConfig>;\n private _storage?: Storage;\n private static _clientConfig: ExpressClientConfig;\n\n private static _instance: AsgardeoExpressClient;\n\n private constructor(config: ExpressClientConfig, storage?: Storage) {\n //Set the client config\n AsgardeoExpressClient._clientConfig = {...config};\n\n //Add the afterSignInUrl and afterSignOutUrl\n //Add custom paths if the user has already declared any or else use the defaults\n const nodeClientConfig: AuthClientConfig = {\n ...config,\n afterSignInUrl: config.appURL + (config.loginPath || DEFAULT_LOGIN_PATH),\n afterSignOutUrl: config.appURL + (config.logoutPath || DEFAULT_LOGOUT_PATH),\n };\n\n //Initialize the user provided storage if there is any\n if (storage) {\n Logger.debug('Initializing user provided storage');\n this._storage = storage;\n }\n\n //Initialize the Auth Client\n this._authClient = new LegacyAsgardeoNodeClient();\n this._authClient.initialize(nodeClientConfig, this._storage);\n }\n\n public static getInstance(config: ExpressClientConfig, storage?: Storage): AsgardeoExpressClient;\n public static getInstance(): AsgardeoExpressClient;\n public static getInstance(config?: ExpressClientConfig, storage?: Storage): AsgardeoExpressClient {\n //Create a new instance if its not instantiated already\n if (!AsgardeoExpressClient._instance && config) {\n AsgardeoExpressClient._instance = new AsgardeoExpressClient(config, storage);\n Logger.debug('Initialized AsgardeoExpressClient successfully');\n }\n\n if (!AsgardeoExpressClient._instance && !config) {\n throw Error(\n new AsgardeoAuthException(\n 'EXPRESS-CLIENT-GI1-NF01',\n 'User configuration is not found',\n 'User config has not been passed to initialize AsgardeoExpressClient',\n ).toString(),\n );\n }\n\n return AsgardeoExpressClient._instance;\n }\n\n public async signIn(\n req: express.Request,\n res: express.Response,\n next: express.nextFunction,\n signInConfig?: Record<string, string | boolean>,\n ): Promise<TokenResponse> {\n if (ExpressUtils.hasErrorInURL(req.originalUrl)) {\n return Promise.reject(\n new AsgardeoAuthException(\n 'EXPRESS-CLIENT-SI-IV01',\n 'Invalid login request URL',\n 'Login request contains an error query parameter in the URL',\n ),\n );\n }\n\n //Check if the user has a valid user ID and if not create one\n let userId = req.cookies.ASGARDEO_SESSION_ID;\n if (!userId) {\n userId = uuidv4();\n }\n\n //Handle signIn() callback\n const authRedirectCallback = (url: string) => {\n if (url) {\n //DEBUG\n Logger.debug('Redirecting to: ' + url);\n res.cookie('ASGARDEO_SESSION_ID', userId, {\n maxAge: AsgardeoExpressClient._clientConfig.cookieConfig?.maxAge\n ? AsgardeoExpressClient._clientConfig.cookieConfig.maxAge\n : CookieConfig.defaultMaxAge,\n httpOnly: AsgardeoExpressClient._clientConfig.cookieConfig?.httpOnly ?? CookieConfig.defaultHttpOnly,\n sameSite: AsgardeoExpressClient._clientConfig.cookieConfig?.sameSite ?? CookieConfig.defaultSameSite,\n secure: AsgardeoExpressClient._clientConfig.cookieConfig?.secure ?? CookieConfig.defaultSecure,\n });\n res.redirect(url);\n\n next && typeof next === 'function' && next();\n }\n };\n\n const authResponse: TokenResponse = await this._authClient.signIn(\n authRedirectCallback,\n userId,\n req.query.code,\n req.query.session_state,\n req.query.state,\n signInConfig,\n );\n\n if (authResponse.accessToken || authResponse.idToken) {\n return authResponse;\n } else {\n return {\n accessToken: '',\n createdAt: 0,\n expiresIn: '',\n idToken: '',\n refreshToken: '',\n scope: '',\n tokenType: '',\n };\n }\n }\n\n public async signOut(userId: string): Promise<string> {\n return this._authClient.signOut(userId);\n }\n\n public async isSignedIn(userId: string): Promise<boolean> {\n return this._authClient.isSignedIn(userId);\n }\n\n public async getIdToken(userId: string): Promise<string> {\n return this._authClient.getIdToken(userId);\n }\n\n public async getUser(userId: string): Promise<User> {\n return this._authClient.getUser(userId);\n }\n\n public async getOpenIDProviderEndpoints(): Promise<OIDCEndpoints> {\n return this._authClient.getOpenIDProviderEndpoints();\n }\n\n public async getDecodedIdToken(userId?: string): Promise<IdTokenPayload> {\n return this._authClient.getDecodedIdToken(userId);\n }\n\n public async getAccessToken(userId?: string): Promise<string> {\n return this._authClient.getAccessToken(userId);\n }\n\n public async exchangeToken(\n config: TokenExchangeRequestConfig,\n userId?: string,\n ): Promise<TokenResponse | Response> {\n return this._authClient.exchangeToken(config, userId);\n }\n\n public async reInitialize(config: Partial<AuthClientConfig>): Promise<void> {\n return this._authClient.reInitialize(config);\n }\n\n public async revokeAccessToken(userId?: string): Promise<Response> {\n return this._authClient.revokeAccessToken(userId);\n }\n\n public static didSignOutFail(afterSignOutUrl: string): boolean {\n return LegacyAsgardeoNodeClient.didSignOutFail(afterSignOutUrl);\n }\n\n public static isSignOutSuccessful(afterSignOutUrl: string): boolean {\n return LegacyAsgardeoNodeClient.isSignOutSuccessful(afterSignOutUrl);\n }\n\n public static protectRoute(\n callback: UnauthenticatedCallback,\n ): (req: express.Request, res: express.Response, next: express.nextFunction) => Promise<void> {\n if (!this._instance) {\n throw new AsgardeoAuthException(\n 'EXPRESS-CLIENT-PR-NF01',\n 'AsgardeoExpressClient is not instantiated',\n 'Create an instance of AsgardeoExpressClient before using calling this method.',\n );\n }\n\n return protectRoute(this._instance, callback);\n }\n\n public static asgardeoExpressAuth(\n onSignIn: (response: TokenResponse) => void,\n onSignOut: () => void,\n onError: (exception: AsgardeoAuthException) => void,\n ): any {\n if (!this._instance) {\n throw new AsgardeoAuthException(\n 'EXPRESS-CLIENT-AEA-NF01',\n 'AsgardeoExpressClient is not instantiated',\n 'Create an instance of AsgardeoExpressClient before using calling this method.',\n );\n }\n\n return asgardeoExpressAuth(this._instance, AsgardeoExpressClient._clientConfig, onSignIn, onSignOut, onError);\n }\n\n public async getStorageManager() {\n return this._authClient.getStorageManager();\n }\n}\n", "/**\n * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com) All Rights Reserved.\n *\n * WSO2 Inc. licenses this file to you under the Apache License,\n * Version 2.0 (the \"License\"); you may not use this file except\n * in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing,\n * software distributed under the License is distributed on an\n * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n * KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations\n * under the License.\n */\n\nexport enum CookieConfig {\n defaultMaxAge = 90000,\n defaultHttpOnly = 'true',\n defaultSameSite = 'lax',\n defaultSecure = 'false'\n}\n\nexport const DEFAULT_LOGIN_PATH = \"/login\";\n\nexport const DEFAULT_LOGOUT_PATH = \"/logout\";\n", "/**\n * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com) All Rights Reserved.\n *\n * WSO2 Inc. licenses this file to you under the Apache License,\n * Version 2.0 (the \"License\"); you may not use this file except\n * in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing,\n * software distributed under the License is distributed on an\n * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n * KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations\n * under the License.\n */\n\nimport express from 'express';\nimport {AsgardeoExpressClient} from '../client';\nimport {UnauthenticatedCallback} from '../models';\nimport {Logger} from '@asgardeo/node';\n\nexport const protectRoute = (\n asgardeoExpressClient: AsgardeoExpressClient,\n callback: UnauthenticatedCallback,\n): ((req: express.Request, res: express.Response, next: express.nextFunction) => Promise<void>) => {\n return async (req: express.Request, res: express.Response, next: express.nextFunction): Promise<void> => {\n if (req.cookies.ASGARDEO_SESSION_ID === undefined) {\n Logger.error('No session ID found in the request cookies');\n\n if (callback(res, 'Unauthenticated')) {\n return;\n }\n\n return next();\n } else {\n //validate the cookie\n const isCookieValid = await asgardeoExpressClient.isSignedIn(req.cookies.ASGARDEO_SESSION_ID);\n if (isCookieValid) {\n return next();\n } else {\n Logger.error('Invalid session ID found in the request cookies');\n if (callback(res, 'Invalid session cookie')) {\n return;\n }\n\n return next();\n }\n }\n };\n};\n", "/**\n * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com) All Rights Reserved.\n *\n * WSO2 Inc. licenses this file to you under the Apache License,\n * Version 2.0 (the \"License\"); you may not use this file except\n * in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing,\n * software distributed under the License is distributed on an\n * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n * KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations\n * under the License.\n */\n\nimport { AsgardeoAuthException, Storage, TokenResponse, Logger } from \"@asgardeo/node\";\nimport express from \"express\";\nimport { AsgardeoExpressClient } from \"../client\";\nimport { DEFAULT_LOGIN_PATH, DEFAULT_LOGOUT_PATH } from \"../constants\";\nimport { ExpressClientConfig } from \"../models\";\n\nexport const asgardeoExpressAuth = (\n asgardeoExpressClient: AsgardeoExpressClient,\n config: ExpressClientConfig,\n onSignIn: (res: express.Response, tokenResponse: TokenResponse) => void,\n onSignOut: (res: express.Response) => void,\n onError: (res: express.Response, exception: AsgardeoAuthException) => void\n): any => {\n //Create the router\n const router = new express.Router();\n\n //Patch AuthClient to the request and the response\n router.use(async (req: express.Request, res: express.Response, next: express.nextFunction): Promise<void> => {\n req.asgardeoAuth = asgardeoExpressClient;\n res.asgardeoAuth = asgardeoExpressClient;\n next();\n });\n\n //Patch in '/login' route\n router.get(\n config.loginPath || DEFAULT_LOGIN_PATH,\n async (req: express.Request, res: express.Response, next: express.nextFunction): Promise<void> => {\n try {\n const response: TokenResponse = await asgardeoExpressClient.signIn(req, res, next, config.signInConfig);\n if (response.accessToken || response.idToken) {\n onSignIn(res, response);\n }\n } catch (e: any) {\n Logger.error(e.message);\n onError(res, e);\n }\n }\n );\n\n //Patch in '/logout' route\n router.get(\n config.logoutPath || DEFAULT_LOGOUT_PATH,\n async (req: express.Request, res: express.Response, next: express.nextFunction): Promise<void> => {\n //Check if it is a logout success response\n if (req.query.state === \"sign_out_success\") {\n onSignOut(res);\n\n return;\n }\n\n //Check if the cookie exists\n if (req.cookies.ASGARDEO_SESSION_ID === undefined) {\n onError(\n res,\n new AsgardeoAuthException(\n \"EXPRESS-AUTH_MW-LOGOUT-NF01\",\n \"No cookie found in the request\",\n \"No cookie was sent with the request. The user may not have signed in yet.\"\n )\n );\n\n return;\n } else {\n //Get the signout URL\n try {\n const signOutURL = await req.asgardeoAuth.signOut(req.cookies.ASGARDEO_SESSION_ID);\n if (signOutURL) {\n res.cookie(\"ASGARDEO_SESSION_ID\", null, { maxAge: 0 });\n res.redirect(signOutURL);\n\n return;\n }\n } catch (e: any) {\n onError(res, e);\n\n return;\n }\n }\n }\n );\n\n return router;\n};\n", "export class ExpressUtils {\n\n private static readonly AUTH_CODE_REGEXP: RegExp = /[?&]error=[^&]+/;\n\n /**\n * Util function to check if the URL contains an error.\n *\n * @param url - URL to be checked.\n *\n * @returns {boolean} - True if the URL contains an error.\n */\n public static hasErrorInURL(url: string): boolean {\n\n return this.AUTH_CODE_REGEXP.test(url);\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkBA,IAAAA,eAYO;;;ACLA,IAAM,qBAAqB;AAE3B,IAAM,sBAAsB;;;ADOnC,kBAA2B;;;AEb3B,kBAAqB;AAEd,IAAM,eAAe,CAC1B,uBACA,aACiG;AACjG,SAAO,OAAO,KAAsB,KAAuB,SAA8C;AACvG,QAAI,IAAI,QAAQ,wBAAwB,QAAW;AACjD,yBAAO,MAAM,4CAA4C;AAEzD,UAAI,SAAS,KAAK,iBAAiB,GAAG;AACpC;AAAA,MACF;AAEA,aAAO,KAAK;AAAA,IACd,OAAO;AAEL,YAAM,gBAAgB,MAAM,sBAAsB,WAAW,IAAI,QAAQ,mBAAmB;AAC5F,UAAI,eAAe;AACjB,eAAO,KAAK;AAAA,MACd,OAAO;AACL,2BAAO,MAAM,iDAAiD;AAC9D,YAAI,SAAS,KAAK,wBAAwB,GAAG;AAC3C;AAAA,QACF;AAEA,eAAO,KAAK;AAAA,MACd;AAAA,IACF;AAAA,EACF;AACF;;;ACjCA,IAAAC,eAAsE;AACtE,qBAAoB;AAKb,IAAM,sBAAsB,CAC/B,uBACA,QACA,UACA,WACA,YACM;AAEN,QAAM,SAAS,IAAI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KAAK,YAAY,eAAe,MAAM;AAAA,EAC/C;AAAA,EAEA,MAAa,cACX,QACA,QACmC;AACnC,WAAO,KAAK,YAAY,cAAc,QAAQ,MAAM;AAAA,EACtD;AAAA,EAEA,MAAa,aAAa,QAAkD;AAC1E,WAAO,KAAK,YAAY,aAAa,MAAM;AAAA,EAC7C;AAAA,EAEA,MAAa,kBAAkB,QAAoC;AACjE,WAAO,KAAK,YAAY,kBAAkB,MAAM;AAAA,EAClD;AAAA,EAEA,OAAc,eAAe,iBAAkC;AAC7D,WAAO,sCAAyB,eAAe,eAAe;AAAA,EAChE;AAAA,EAEA,OAAc,oBAAoB,iBAAkC;AAClE,WAAO,sCAAyB,oBAAoB,eAAe;AAAA,EACrE;AAAA,EAEA,OAAc,aACZ,UAC4F;AAC5F,QAAI,CAAC,KAAK,WAAW;AACnB,YAAM,IAAI;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAEA,WAAO,aAAa,KAAK,WAAW,QAAQ;AAAA,EAC9C;AAAA,EAEA,OAAc,oBACZ,UACA,WACA,SACK;AACL,QAAI,CAAC,KAAK,WAAW;AACnB,YAAM,IAAI;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF;AAEA,WAAO,oBAAoB,KAAK,WAAW,uBAAsB,eAAe,UAAU,WAAW,OAAO;AAAA,EAC9G;AAAA,EAEA,MAAa,oBAAoB;AAC/B,WAAO,KAAK,YAAY,kBAAkB;AAAA,EAC5C;AACF;AAtME,cAHW,wBAGI;AAEf,cALW,wBAKI;AALV,IAAM,wBAAN;;;ADjBP,0BAAc,2BArBd;",
|
|
6
|
+
"names": ["import_node", "import_node", "express", "uuidv4"]
|
|
7
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.com) All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* WSO2 Inc. 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 '@asgardeo/node';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
|
|
5
|
+
// src/__legacy__/client.ts
|
|
6
|
+
import {
|
|
7
|
+
LegacyAsgardeoNodeClient,
|
|
8
|
+
AsgardeoAuthException as AsgardeoAuthException2,
|
|
9
|
+
Logger as Logger3
|
|
10
|
+
} from "@asgardeo/node";
|
|
11
|
+
|
|
12
|
+
// src/__legacy__/constants/default-options.ts
|
|
13
|
+
var DEFAULT_LOGIN_PATH = "/login";
|
|
14
|
+
var DEFAULT_LOGOUT_PATH = "/logout";
|
|
15
|
+
|
|
16
|
+
// src/__legacy__/client.ts
|
|
17
|
+
import { v4 as uuidv4 } from "uuid";
|
|
18
|
+
|
|
19
|
+
// src/__legacy__/middleware/protect-route.ts
|
|
20
|
+
import { Logger } from "@asgardeo/node";
|
|
21
|
+
var protectRoute = (asgardeoExpressClient, callback) => {
|
|
22
|
+
return async (req, res, next) => {
|
|
23
|
+
if (req.cookies.ASGARDEO_SESSION_ID === void 0) {
|
|
24
|
+
Logger.error("No session ID found in the request cookies");
|
|
25
|
+
if (callback(res, "Unauthenticated")) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
return next();
|
|
29
|
+
} else {
|
|
30
|
+
const isCookieValid = await asgardeoExpressClient.isSignedIn(req.cookies.ASGARDEO_SESSION_ID);
|
|
31
|
+
if (isCookieValid) {
|
|
32
|
+
return next();
|
|
33
|
+
} else {
|
|
34
|
+
Logger.error("Invalid session ID found in the request cookies");
|
|
35
|
+
if (callback(res, "Invalid session cookie")) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
return next();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/__legacy__/middleware/authentication.ts
|
|
45
|
+
import { AsgardeoAuthException, Logger as Logger2 } from "@asgardeo/node";
|
|
46
|
+
import express from "express";
|
|
47
|
+
var asgardeoExpressAuth = (asgardeoExpressClient, config, onSignIn, onSignOut, onError) => {
|
|
48
|
+
const router = new express.Router();
|
|
49
|
+
router.use(async (req, res, next) => {
|
|
50
|
+
req.asgardeoAuth = asgardeoExpressClient;
|
|
51
|
+
res.asgardeoAuth = asgardeoExpressClient;
|
|
52
|
+
next();
|
|
53
|
+
});
|
|
54
|
+
router.get(
|
|
55
|
+
config.loginPath || DEFAULT_LOGIN_PATH,
|
|
56
|
+
async (req, res, next) => {
|
|
57
|
+
try {
|
|
58
|
+
const response = await asgardeoExpressClient.signIn(req, res, next, config.signInConfig);
|
|
59
|
+
if (response.accessToken || response.idToken) {
|
|
60
|
+
onSignIn(res, response);
|
|
61
|
+
}
|
|
62
|
+
} catch (e) {
|
|
63
|
+
Logger2.error(e.message);
|
|
64
|
+
onError(res, e);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
router.get(
|
|
69
|
+
config.logoutPath || DEFAULT_LOGOUT_PATH,
|
|
70
|
+
async (req, res, next) => {
|
|
71
|
+
if (req.query.state === "sign_out_success") {
|
|
72
|
+
onSignOut(res);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (req.cookies.ASGARDEO_SESSION_ID === void 0) {
|
|
76
|
+
onError(
|
|
77
|
+
res,
|
|
78
|
+
new AsgardeoAuthException(
|
|
79
|
+
"EXPRESS-AUTH_MW-LOGOUT-NF01",
|
|
80
|
+
"No cookie found in the request",
|
|
81
|
+
"No cookie was sent with the request. The user may not have signed in yet."
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
return;
|
|
85
|
+
} else {
|
|
86
|
+
try {
|
|
87
|
+
const signOutURL = await req.asgardeoAuth.signOut(req.cookies.ASGARDEO_SESSION_ID);
|
|
88
|
+
if (signOutURL) {
|
|
89
|
+
res.cookie("ASGARDEO_SESSION_ID", null, { maxAge: 0 });
|
|
90
|
+
res.redirect(signOutURL);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
} catch (e) {
|
|
94
|
+
onError(res, e);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
return router;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/__legacy__/utils/express-utils.ts
|
|
104
|
+
var ExpressUtils = class {
|
|
105
|
+
/**
|
|
106
|
+
* Util function to check if the URL contains an error.
|
|
107
|
+
*
|
|
108
|
+
* @param url - URL to be checked.
|
|
109
|
+
*
|
|
110
|
+
* @returns {boolean} - True if the URL contains an error.
|
|
111
|
+
*/
|
|
112
|
+
static hasErrorInURL(url) {
|
|
113
|
+
return this.AUTH_CODE_REGEXP.test(url);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
__publicField(ExpressUtils, "AUTH_CODE_REGEXP", /[?&]error=[^&]+/);
|
|
117
|
+
|
|
118
|
+
// src/__legacy__/client.ts
|
|
119
|
+
var _AsgardeoExpressClient = class _AsgardeoExpressClient {
|
|
120
|
+
constructor(config, storage) {
|
|
121
|
+
__publicField(this, "_authClient");
|
|
122
|
+
__publicField(this, "_storage");
|
|
123
|
+
_AsgardeoExpressClient._clientConfig = { ...config };
|
|
124
|
+
const nodeClientConfig = {
|
|
125
|
+
...config,
|
|
126
|
+
afterSignInUrl: config.appURL + (config.loginPath || DEFAULT_LOGIN_PATH),
|
|
127
|
+
afterSignOutUrl: config.appURL + (config.logoutPath || DEFAULT_LOGOUT_PATH)
|
|
128
|
+
};
|
|
129
|
+
if (storage) {
|
|
130
|
+
Logger3.debug("Initializing user provided storage");
|
|
131
|
+
this._storage = storage;
|
|
132
|
+
}
|
|
133
|
+
this._authClient = new LegacyAsgardeoNodeClient();
|
|
134
|
+
this._authClient.initialize(nodeClientConfig, this._storage);
|
|
135
|
+
}
|
|
136
|
+
static getInstance(config, storage) {
|
|
137
|
+
if (!_AsgardeoExpressClient._instance && config) {
|
|
138
|
+
_AsgardeoExpressClient._instance = new _AsgardeoExpressClient(config, storage);
|
|
139
|
+
Logger3.debug("Initialized AsgardeoExpressClient successfully");
|
|
140
|
+
}
|
|
141
|
+
if (!_AsgardeoExpressClient._instance && !config) {
|
|
142
|
+
throw Error(
|
|
143
|
+
new AsgardeoAuthException2(
|
|
144
|
+
"EXPRESS-CLIENT-GI1-NF01",
|
|
145
|
+
"User configuration is not found",
|
|
146
|
+
"User config has not been passed to initialize AsgardeoExpressClient"
|
|
147
|
+
).toString()
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
return _AsgardeoExpressClient._instance;
|
|
151
|
+
}
|
|
152
|
+
async signIn(req, res, next, signInConfig) {
|
|
153
|
+
if (ExpressUtils.hasErrorInURL(req.originalUrl)) {
|
|
154
|
+
return Promise.reject(
|
|
155
|
+
new AsgardeoAuthException2(
|
|
156
|
+
"EXPRESS-CLIENT-SI-IV01",
|
|
157
|
+
"Invalid login request URL",
|
|
158
|
+
"Login request contains an error query parameter in the URL"
|
|
159
|
+
)
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
let userId = req.cookies.ASGARDEO_SESSION_ID;
|
|
163
|
+
if (!userId) {
|
|
164
|
+
userId = uuidv4();
|
|
165
|
+
}
|
|
166
|
+
const authRedirectCallback = (url) => {
|
|
167
|
+
if (url) {
|
|
168
|
+
Logger3.debug("Redirecting to: " + url);
|
|
169
|
+
res.cookie("ASGARDEO_SESSION_ID", userId, {
|
|
170
|
+
maxAge: _AsgardeoExpressClient._clientConfig.cookieConfig?.maxAge ? _AsgardeoExpressClient._clientConfig.cookieConfig.maxAge : 9e4 /* defaultMaxAge */,
|
|
171
|
+
httpOnly: _AsgardeoExpressClient._clientConfig.cookieConfig?.httpOnly ?? "true" /* defaultHttpOnly */,
|
|
172
|
+
sameSite: _AsgardeoExpressClient._clientConfig.cookieConfig?.sameSite ?? "lax" /* defaultSameSite */,
|
|
173
|
+
secure: _AsgardeoExpressClient._clientConfig.cookieConfig?.secure ?? "false" /* defaultSecure */
|
|
174
|
+
});
|
|
175
|
+
res.redirect(url);
|
|
176
|
+
next && typeof next === "function" && next();
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
const authResponse = await this._authClient.signIn(
|
|
180
|
+
authRedirectCallback,
|
|
181
|
+
userId,
|
|
182
|
+
req.query.code,
|
|
183
|
+
req.query.session_state,
|
|
184
|
+
req.query.state,
|
|
185
|
+
signInConfig
|
|
186
|
+
);
|
|
187
|
+
if (authResponse.accessToken || authResponse.idToken) {
|
|
188
|
+
return authResponse;
|
|
189
|
+
} else {
|
|
190
|
+
return {
|
|
191
|
+
accessToken: "",
|
|
192
|
+
createdAt: 0,
|
|
193
|
+
expiresIn: "",
|
|
194
|
+
idToken: "",
|
|
195
|
+
refreshToken: "",
|
|
196
|
+
scope: "",
|
|
197
|
+
tokenType: ""
|
|
198
|
+
};
|
|
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 LegacyAsgardeoNodeClient.didSignOutFail(afterSignOutUrl);
|
|
233
|
+
}
|
|
234
|
+
static isSignOutSuccessful(afterSignOutUrl) {
|
|
235
|
+
return LegacyAsgardeoNodeClient.isSignOutSuccessful(afterSignOutUrl);
|
|
236
|
+
}
|
|
237
|
+
static protectRoute(callback) {
|
|
238
|
+
if (!this._instance) {
|
|
239
|
+
throw new AsgardeoAuthException2(
|
|
240
|
+
"EXPRESS-CLIENT-PR-NF01",
|
|
241
|
+
"AsgardeoExpressClient is not instantiated",
|
|
242
|
+
"Create an instance of AsgardeoExpressClient before using calling this method."
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
return protectRoute(this._instance, callback);
|
|
246
|
+
}
|
|
247
|
+
static asgardeoExpressAuth(onSignIn, onSignOut, onError) {
|
|
248
|
+
if (!this._instance) {
|
|
249
|
+
throw new AsgardeoAuthException2(
|
|
250
|
+
"EXPRESS-CLIENT-AEA-NF01",
|
|
251
|
+
"AsgardeoExpressClient is not instantiated",
|
|
252
|
+
"Create an instance of AsgardeoExpressClient before using calling this method."
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
return asgardeoExpressAuth(this._instance, _AsgardeoExpressClient._clientConfig, onSignIn, onSignOut, onError);
|
|
256
|
+
}
|
|
257
|
+
async getStorageManager() {
|
|
258
|
+
return this._authClient.getStorageManager();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
__publicField(_AsgardeoExpressClient, "_clientConfig");
|
|
262
|
+
__publicField(_AsgardeoExpressClient, "_instance");
|
|
263
|
+
var AsgardeoExpressClient = _AsgardeoExpressClient;
|
|
264
|
+
|
|
265
|
+
// src/index.ts
|
|
266
|
+
export * from "@asgardeo/node";
|
|
267
|
+
export {
|
|
268
|
+
AsgardeoExpressClient
|
|
269
|
+
};
|
|
270
|
+
//# sourceMappingURL=index.js.map
|