@itwin/core-electron 3.0.0-extension.1 → 3.0.2
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/CHANGELOG.md +105 -7
- package/LICENSE.md +1 -1
- package/lib/cjs/ElectronBackend.d.ts +0 -1
- package/lib/cjs/ElectronBackend.d.ts.map +1 -1
- package/lib/cjs/ElectronBackend.js +0 -1
- package/lib/cjs/ElectronBackend.js.map +1 -1
- package/lib/cjs/backend/ElectronHost.d.ts +1 -9
- package/lib/cjs/backend/ElectronHost.d.ts.map +1 -1
- package/lib/cjs/backend/ElectronHost.js +1 -9
- package/lib/cjs/backend/ElectronHost.js.map +1 -1
- package/package.json +15 -16
- package/lib/cjs/backend/ElectronAuthorizationBackend.d.ts +0 -71
- package/lib/cjs/backend/ElectronAuthorizationBackend.d.ts.map +0 -1
- package/lib/cjs/backend/ElectronAuthorizationBackend.js +0 -265
- package/lib/cjs/backend/ElectronAuthorizationBackend.js.map +0 -1
- package/lib/cjs/backend/ElectronAuthorizationEvents.d.ts +0 -20
- package/lib/cjs/backend/ElectronAuthorizationEvents.d.ts.map +0 -1
- package/lib/cjs/backend/ElectronAuthorizationEvents.js +0 -26
- package/lib/cjs/backend/ElectronAuthorizationEvents.js.map +0 -1
- package/lib/cjs/backend/ElectronAuthorizationRequestHandler.d.ts +0 -25
- package/lib/cjs/backend/ElectronAuthorizationRequestHandler.d.ts.map +0 -1
- package/lib/cjs/backend/ElectronAuthorizationRequestHandler.js +0 -63
- package/lib/cjs/backend/ElectronAuthorizationRequestHandler.js.map +0 -1
- package/lib/cjs/backend/ElectronTokenStore.d.ts +0 -18
- package/lib/cjs/backend/ElectronTokenStore.d.ts.map +0 -1
- package/lib/cjs/backend/ElectronTokenStore.js +0 -66
- package/lib/cjs/backend/ElectronTokenStore.js.map +0 -1
- package/lib/cjs/backend/LoopbackWebServer.d.ts +0 -19
- package/lib/cjs/backend/LoopbackWebServer.d.ts.map +0 -1
- package/lib/cjs/backend/LoopbackWebServer.js +0 -101
- package/lib/cjs/backend/LoopbackWebServer.js.map +0 -1
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*---------------------------------------------------------------------------------------------
|
|
3
|
-
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
4
|
-
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
5
|
-
*--------------------------------------------------------------------------------------------*/
|
|
6
|
-
// Code based on the blog article @ https://authguidance.com
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.LoopbackWebServer = void 0;
|
|
9
|
-
/** @packageDocumentation
|
|
10
|
-
* @module Authentication
|
|
11
|
-
*/
|
|
12
|
-
const Http = require("http");
|
|
13
|
-
const ElectronAuthorizationBackend_1 = require("./ElectronAuthorizationBackend");
|
|
14
|
-
/** Utility to manage re-entrancy if there are multiple login attempts */
|
|
15
|
-
class AuthorizationState {
|
|
16
|
-
addState(state, authEvents) {
|
|
17
|
-
AuthorizationState._stateEventsMap.push([state, authEvents]);
|
|
18
|
-
}
|
|
19
|
-
removeState(state) {
|
|
20
|
-
AuthorizationState._stateEventsMap = AuthorizationState._stateEventsMap.filter((se) => se[0] !== state);
|
|
21
|
-
}
|
|
22
|
-
// Get events for a received login response
|
|
23
|
-
getEvents(state) {
|
|
24
|
-
const stateEventsPair = AuthorizationState._stateEventsMap.find((se) => se[0] === state);
|
|
25
|
-
if (stateEventsPair) {
|
|
26
|
-
return stateEventsPair[1];
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
AuthorizationState._stateEventsMap = [];
|
|
32
|
-
/**
|
|
33
|
-
* Web server to listen to authorization requests/responses for the DesktopAuthorizationClient
|
|
34
|
-
* @internal
|
|
35
|
-
*/
|
|
36
|
-
class LoopbackWebServer {
|
|
37
|
-
/** Start a web server to listen to the browser requests */
|
|
38
|
-
static start(clientConfiguration) {
|
|
39
|
-
var _a;
|
|
40
|
-
if (LoopbackWebServer._httpServer)
|
|
41
|
-
return;
|
|
42
|
-
LoopbackWebServer._httpServer = Http.createServer(LoopbackWebServer.onBrowserRequest);
|
|
43
|
-
const urlParts = new URL((_a = clientConfiguration.redirectUri) !== null && _a !== void 0 ? _a : ElectronAuthorizationBackend_1.ElectronAuthorizationBackend.defaultRedirectUri);
|
|
44
|
-
LoopbackWebServer._httpServer.listen(urlParts.port);
|
|
45
|
-
}
|
|
46
|
-
/** Add to the authorization state so that the correct response data is used for each request */
|
|
47
|
-
static addCorrelationState(state, authEvents) {
|
|
48
|
-
return LoopbackWebServer._authState.addState(state, authEvents);
|
|
49
|
-
}
|
|
50
|
-
/** Stop the web server after the authorization was completed */
|
|
51
|
-
static stop() {
|
|
52
|
-
if (!LoopbackWebServer._httpServer)
|
|
53
|
-
return;
|
|
54
|
-
LoopbackWebServer._httpServer.close();
|
|
55
|
-
LoopbackWebServer._httpServer = undefined;
|
|
56
|
-
}
|
|
57
|
-
/** Listen/Handle browser events */
|
|
58
|
-
static onBrowserRequest(httpRequest, httpResponse) {
|
|
59
|
-
if (!httpRequest.url)
|
|
60
|
-
return;
|
|
61
|
-
// Parse the request URL to determine the authorization code, state and errors if any
|
|
62
|
-
const redirectedUrl = new URL(httpRequest.url, ElectronAuthorizationBackend_1.ElectronAuthorizationBackend.defaultRedirectUri);
|
|
63
|
-
const searchParams = redirectedUrl.searchParams;
|
|
64
|
-
const state = searchParams.get("state") || undefined;
|
|
65
|
-
const code = searchParams.get("code");
|
|
66
|
-
const error = searchParams.get("error");
|
|
67
|
-
if (!state) {
|
|
68
|
-
// ignore irrelevant requests (e.g. favicon.ico)
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
// Look up context for the corresponding outgoing request
|
|
72
|
-
const authorizationEvents = LoopbackWebServer._authState.getEvents(state);
|
|
73
|
-
if (!authorizationEvents)
|
|
74
|
-
return;
|
|
75
|
-
// Notify listeners of the code response or error
|
|
76
|
-
let authorizationResponse = null;
|
|
77
|
-
let authorizationError = null;
|
|
78
|
-
if (error) {
|
|
79
|
-
const errorUri = searchParams.get("error_uri") || undefined;
|
|
80
|
-
const errorDescription = searchParams.get("error_description") || undefined;
|
|
81
|
-
authorizationError = { error, error_description: errorDescription, error_uri: errorUri, state }; // eslint-disable-line @typescript-eslint/naming-convention
|
|
82
|
-
httpResponse.write("<h1>Sign in error!</h1>"); // TODO: Needs localization
|
|
83
|
-
httpResponse.end();
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
authorizationResponse = { code: code, state };
|
|
87
|
-
httpResponse.writeHead(200, { "Content-Type": "text/html" }); // eslint-disable-line @typescript-eslint/naming-convention
|
|
88
|
-
httpResponse.write("<h1>Sign in was successful!</h1>You can close this browser window and return to the application"); // TODO: Needs localization
|
|
89
|
-
httpResponse.end();
|
|
90
|
-
}
|
|
91
|
-
authorizationEvents.onAuthorizationResponse.raiseEvent(authorizationError, authorizationResponse);
|
|
92
|
-
// Handle the authorization completed event
|
|
93
|
-
authorizationEvents.onAuthorizationResponseCompleted.addOnce((_authCompletedError) => {
|
|
94
|
-
// Stop the web server now that the signin attempt has finished
|
|
95
|
-
LoopbackWebServer.stop();
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
exports.LoopbackWebServer = LoopbackWebServer;
|
|
100
|
-
LoopbackWebServer._authState = new AuthorizationState();
|
|
101
|
-
//# sourceMappingURL=LoopbackWebServer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LoopbackWebServer.js","sourceRoot":"","sources":["../../../src/backend/LoopbackWebServer.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F,4DAA4D;;;AAE5D;;GAEG;AAEH,6BAA6B;AAI7B,iFAA8E;AAI9E,yEAAyE;AACzE,MAAM,kBAAkB;IAGf,QAAQ,CAAC,KAAa,EAAE,UAAuC;QACpE,kBAAkB,CAAC,eAAe,CAAC,IAAI,CAAC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K,CAAC,yBAAyB,CAAC,CAAC,CAAC,2BAA2B;YAC1E,YAAY,CAAC,GAAG,EAAE,CAAC;SACpB;aAAM;YACL,qBAAqB,GAAG,EAAE,IAAI,EAAE,IAAK,EAAE,KAAK,EAAE,CAAC;YAC/C,YAAY,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,4DAA4D;YAC1H,YAAY,CAAC,KAAK,CAAC,iGAAiG,CAAC,CAAC,CAAC,2BAA2B;YAClJ,YAAY,CAAC,GAAG,EAAE,CAAC;SACpB;QACD,mBAAmB,CAAC,uBAAuB,CAAC,UAAU,CAAC,kBAAkB,EAAE,qBAAqB,CAAC,CAAC;QAElG,2CAA2C;QAC3C,mBAAmB,CAAC,gCAAgC,CAAC,OAAO,CAAC,CAAC,mBAA4C,EAAE,EAAE;YAC5G,+DAA+D;YAC/D,iBAAiB,CAAC,IAAI,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC;;AAvEH,8CAwEC;AAtEgB,4BAAU,GAAuB,IAAI,kBAAkB,EAAE,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n// Code based on the blog article @ https://authguidance.com\r\n\r\n/** @packageDocumentation\r\n * @module Authentication\r\n */\r\n\r\nimport * as Http from \"http\";\r\nimport { NativeAppAuthorizationConfiguration } from \"@itwin/core-common\";\r\nimport { AuthorizationErrorJson, AuthorizationResponseJson } from \"@openid/appauth\";\r\nimport { ElectronAuthorizationEvents } from \"./ElectronAuthorizationEvents\";\r\nimport { ElectronAuthorizationBackend } from \"./ElectronAuthorizationBackend\";\r\n\r\ntype StateEventsPair = [string, ElectronAuthorizationEvents];\r\n\r\n/** Utility to manage re-entrancy if there are multiple login attempts */\r\nclass AuthorizationState {\r\n private static _stateEventsMap = [] as StateEventsPair[];\r\n\r\n public addState(state: string, authEvents: ElectronAuthorizationEvents): void {\r\n AuthorizationState._stateEventsMap.push([state, authEvents]);\r\n }\r\n\r\n public removeState(state: string): void {\r\n AuthorizationState._stateEventsMap = AuthorizationState._stateEventsMap.filter((se) => se[0] !== state);\r\n }\r\n\r\n // Get events for a received login response\r\n public getEvents(state: string): ElectronAuthorizationEvents | null {\r\n const stateEventsPair = AuthorizationState._stateEventsMap.find((se) => se[0] === state);\r\n if (stateEventsPair) {\r\n return stateEventsPair[1];\r\n }\r\n return null;\r\n }\r\n}\r\n\r\n/**\r\n * Web server to listen to authorization requests/responses for the DesktopAuthorizationClient\r\n * @internal\r\n */\r\nexport class LoopbackWebServer {\r\n private static _httpServer?: Http.Server;\r\n private static _authState: AuthorizationState = new AuthorizationState();\r\n\r\n /** Start a web server to listen to the browser requests */\r\n public static start(clientConfiguration: NativeAppAuthorizationConfiguration) {\r\n if (LoopbackWebServer._httpServer)\r\n return;\r\n\r\n LoopbackWebServer._httpServer = Http.createServer(LoopbackWebServer.onBrowserRequest);\r\n const urlParts: URL = new URL(clientConfiguration.redirectUri ?? ElectronAuthorizationBackend.defaultRedirectUri);\r\n LoopbackWebServer._httpServer.listen(urlParts.port);\r\n }\r\n\r\n /** Add to the authorization state so that the correct response data is used for each request */\r\n public static addCorrelationState(state: string, authEvents: ElectronAuthorizationEvents): void {\r\n return LoopbackWebServer._authState.addState(state, authEvents);\r\n }\r\n\r\n /** Stop the web server after the authorization was completed */\r\n private static stop() {\r\n if (!LoopbackWebServer._httpServer)\r\n return;\r\n LoopbackWebServer._httpServer.close();\r\n LoopbackWebServer._httpServer = undefined;\r\n }\r\n\r\n /** Listen/Handle browser events */\r\n private static onBrowserRequest(httpRequest: Http.IncomingMessage, httpResponse: Http.ServerResponse): void {\r\n if (!httpRequest.url)\r\n return;\r\n\r\n // Parse the request URL to determine the authorization code, state and errors if any\r\n const redirectedUrl = new URL(httpRequest.url, ElectronAuthorizationBackend.defaultRedirectUri);\r\n const searchParams = redirectedUrl.searchParams;\r\n\r\n const state = searchParams.get(\"state\") || undefined;\r\n const code = searchParams.get(\"code\");\r\n const error = searchParams.get(\"error\");\r\n if (!state) {\r\n // ignore irrelevant requests (e.g. favicon.ico)\r\n return;\r\n }\r\n\r\n // Look up context for the corresponding outgoing request\r\n const authorizationEvents = LoopbackWebServer._authState.getEvents(state);\r\n if (!authorizationEvents)\r\n return;\r\n\r\n // Notify listeners of the code response or error\r\n let authorizationResponse: AuthorizationResponseJson | null = null;\r\n let authorizationError: AuthorizationErrorJson | null = null;\r\n if (error) {\r\n const errorUri = searchParams.get(\"error_uri\") || undefined;\r\n const errorDescription = searchParams.get(\"error_description\") || undefined;\r\n authorizationError = { error, error_description: errorDescription, error_uri: errorUri, state }; // eslint-disable-line @typescript-eslint/naming-convention\r\n httpResponse.write(\"<h1>Sign in error!</h1>\"); // TODO: Needs localization\r\n httpResponse.end();\r\n } else {\r\n authorizationResponse = { code: code!, state };\r\n httpResponse.writeHead(200, { \"Content-Type\": \"text/html\" }); // eslint-disable-line @typescript-eslint/naming-convention\r\n httpResponse.write(\"<h1>Sign in was successful!</h1>You can close this browser window and return to the application\"); // TODO: Needs localization\r\n httpResponse.end();\r\n }\r\n authorizationEvents.onAuthorizationResponse.raiseEvent(authorizationError, authorizationResponse);\r\n\r\n // Handle the authorization completed event\r\n authorizationEvents.onAuthorizationResponseCompleted.addOnce((_authCompletedError?: AuthorizationErrorJson) => {\r\n // Stop the web server now that the signin attempt has finished\r\n LoopbackWebServer.stop();\r\n });\r\n }\r\n}\r\n"]}
|