@leapdev/app-platform 0.1.0-beta.30 → 0.1.0-beta.32
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/package.json +4 -1
- package/src/index.d.ts +8 -0
- package/src/index.js +21 -1
- package/src/lib/app-platform.d.ts +22 -0
- package/src/lib/app-platform.js +128 -2
- package/src/lib/models/auth.model.d.ts +41 -0
- package/src/lib/models/auth.model.js +3 -0
- package/src/lib/models/leap.model.d.ts +4 -0
- package/src/lib/services/auth-agent/auth-agent.service.d.ts +35 -0
- package/src/lib/services/auth-agent/auth-agent.service.js +242 -0
- package/src/types.d.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leapdev/app-platform",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.32",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"tslib": "^2.3.0",
|
|
6
6
|
"vkbeautify": "^0.99.3",
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
"@microsoft/signalr": "^8.0.7",
|
|
12
12
|
"uuid": "^11.1.0"
|
|
13
13
|
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@leapdev/auth-agent": "^2.2.13-office.6"
|
|
16
|
+
},
|
|
14
17
|
"type": "commonjs",
|
|
15
18
|
"main": "./src/index.js",
|
|
16
19
|
"typings": "./src/index.d.ts"
|
package/src/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EvaluatePathsRequest } from './lib/models';
|
|
2
|
+
import { SignInConfig } from './lib/models/auth.model';
|
|
2
3
|
import { HostApplicationType, LeapDocumentData } from './lib/models/leap.model';
|
|
3
4
|
export * from './types';
|
|
4
5
|
export * from './lib/ports';
|
|
@@ -33,6 +34,13 @@ export declare const createDocument: (params: {
|
|
|
33
34
|
} | null>;
|
|
34
35
|
export declare const getLEAPTokenViaSignalR: (forceRefresh?: boolean) => Promise<string>;
|
|
35
36
|
export declare const getLEAPUserViaSignalR: () => Promise<import("./lib/models/leap.model").LEAPUser | undefined>;
|
|
37
|
+
export declare const bootstrapAuth: (config: SignInConfig) => Promise<boolean>;
|
|
38
|
+
export declare const signIn: (config: SignInConfig, options?: {
|
|
39
|
+
popup?: boolean;
|
|
40
|
+
}) => Promise<import("./types").SignInResult>;
|
|
41
|
+
export declare const getAccessToken: (forceRefresh?: boolean) => Promise<string>;
|
|
42
|
+
export declare const getAccessTokenSync: () => string;
|
|
43
|
+
export declare const getUser: () => Promise<import("./types").AuthUser | undefined>;
|
|
36
44
|
export declare const getInitDataViaSignalR: () => Promise<Omit<import("./lib/models/signalr.model").SignalRInitData, "code" | "codeChallenge"> | null>;
|
|
37
45
|
export declare const connectSignalR: (params: {
|
|
38
46
|
clientId: string;
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.saveDocument = exports.setBookmarkViaSignalR = exports.openMatterViaSignalR = exports.openDocumentViaSignalR = exports.connectSignalR = exports.getInitDataViaSignalR = exports.getLEAPUserViaSignalR = exports.getLEAPTokenViaSignalR = exports.createDocument = exports.evaluatePaths = exports.getAddinData = exports.updateDocumentData = exports.getDocumentData = exports.getHostInfo = exports.setupLeapEventsAndSetToken = exports.initAppPlatform = void 0;
|
|
3
|
+
exports.saveDocument = exports.setBookmarkViaSignalR = exports.openMatterViaSignalR = exports.openDocumentViaSignalR = exports.connectSignalR = exports.getInitDataViaSignalR = exports.getUser = exports.getAccessTokenSync = exports.getAccessToken = exports.signIn = exports.bootstrapAuth = exports.getLEAPUserViaSignalR = exports.getLEAPTokenViaSignalR = exports.createDocument = exports.evaluatePaths = exports.getAddinData = exports.updateDocumentData = exports.getDocumentData = exports.getHostInfo = exports.setupLeapEventsAndSetToken = exports.initAppPlatform = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const app_platform_1 = require("./lib/app-platform");
|
|
6
6
|
tslib_1.__exportStar(require("./types"), exports);
|
|
@@ -48,6 +48,26 @@ const getLEAPUserViaSignalR = () => {
|
|
|
48
48
|
return appPlatform.getLEAPUserViaSignalR();
|
|
49
49
|
};
|
|
50
50
|
exports.getLEAPUserViaSignalR = getLEAPUserViaSignalR;
|
|
51
|
+
const bootstrapAuth = (config) => {
|
|
52
|
+
return appPlatform.bootstrapAuth(config);
|
|
53
|
+
};
|
|
54
|
+
exports.bootstrapAuth = bootstrapAuth;
|
|
55
|
+
const signIn = (config, options) => {
|
|
56
|
+
return appPlatform.signIn(config, options);
|
|
57
|
+
};
|
|
58
|
+
exports.signIn = signIn;
|
|
59
|
+
const getAccessToken = (forceRefresh) => {
|
|
60
|
+
return appPlatform.getAccessToken(forceRefresh);
|
|
61
|
+
};
|
|
62
|
+
exports.getAccessToken = getAccessToken;
|
|
63
|
+
const getAccessTokenSync = () => {
|
|
64
|
+
return appPlatform.getAccessTokenSync();
|
|
65
|
+
};
|
|
66
|
+
exports.getAccessTokenSync = getAccessTokenSync;
|
|
67
|
+
const getUser = () => {
|
|
68
|
+
return appPlatform.getUser();
|
|
69
|
+
};
|
|
70
|
+
exports.getUser = getUser;
|
|
51
71
|
const getInitDataViaSignalR = () => {
|
|
52
72
|
return appPlatform.getInitDataViaSignalR();
|
|
53
73
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EvaluatePathsRequest, EvaluatePathsResponse } from './models';
|
|
2
|
+
import { AuthUser, SignInConfig, SignInResult } from './models/auth.model';
|
|
2
3
|
import { HostApplicationType, LeapDocumentData, LEAPUser } from './models/leap.model';
|
|
3
4
|
import { SignalRInitData } from './models/signalr.model';
|
|
4
5
|
export declare class AppPlatform {
|
|
@@ -30,6 +31,27 @@ export declare class AppPlatform {
|
|
|
30
31
|
hostType: string | null;
|
|
31
32
|
hostVersion: string | null;
|
|
32
33
|
}>;
|
|
34
|
+
/**
|
|
35
|
+
* Word add-in: initialiseAuth (src/taskpane/index.tsx). Run on page load, before the add-in
|
|
36
|
+
* renders, so auth-agent can restore a session or finish a ?code= redirect.
|
|
37
|
+
*/
|
|
38
|
+
bootstrapAuth(config: SignInConfig): Promise<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* Word add-in: the host branching of App.tsx's init (openTaskPaneThenLoginWithAuthAgent /
|
|
41
|
+
* openTaskPaneThenLoginWithSignalR). Picks the provider from the document and remembers it for
|
|
42
|
+
* getAccessToken and getUser. popup defaults to Office Online only, as the add-in always did.
|
|
43
|
+
*/
|
|
44
|
+
signIn(config: SignInConfig, options?: {
|
|
45
|
+
popup?: boolean;
|
|
46
|
+
}): Promise<SignInResult>;
|
|
47
|
+
/** Word add-in: authState's getAccessToken, keyed on the provider signIn chose. */
|
|
48
|
+
getAccessToken(forceRefresh?: boolean): Promise<string>;
|
|
49
|
+
/** Word add-in: readImmediateAccessToken (src/hooks/useLeapOfficeWordHub.ts). '' on SignalR. */
|
|
50
|
+
getAccessTokenSync(): string;
|
|
51
|
+
/** Word add-in: setLEAPUser's getLEAPUserViaSignalR, or auth-agent's userInfo via setUser. */
|
|
52
|
+
getUser(): Promise<AuthUser | undefined>;
|
|
53
|
+
private signInWithLeapAuth;
|
|
54
|
+
private getAuthAgentService;
|
|
33
55
|
getLEAPTokenViaSignalR(forceRefresh?: boolean): Promise<string>;
|
|
34
56
|
getLEAPUserViaSignalR(): Promise<LEAPUser | undefined>;
|
|
35
57
|
getInitDataViaSignalR(): Promise<Omit<SignalRInitData, 'code' | 'codeChallenge'> | null>;
|
package/src/lib/app-platform.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var _AppPlatform_leapDocumentDataService, _AppPlatform_authService, _AppPlatform_signalRInitData, _AppPlatform_initialized, _AppPlatform_connectedToSignalR, _AppPlatform_hostApplicationType;
|
|
2
|
+
var _AppPlatform_leapDocumentDataService, _AppPlatform_authService, _AppPlatform_authAgentService, _AppPlatform_activeAuthProvider, _AppPlatform_signalRInitData, _AppPlatform_initialized, _AppPlatform_connectedToSignalR, _AppPlatform_hostApplicationType;
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.AppPlatform = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
@@ -9,16 +9,25 @@ const constants_1 = require("./models/constants");
|
|
|
9
9
|
const leap_events_model_1 = require("./models/leap-events.model");
|
|
10
10
|
const leap_model_1 = require("./models/leap.model");
|
|
11
11
|
const services_1 = require("./services");
|
|
12
|
+
const auth_agent_service_1 = require("./services/auth-agent/auth-agent.service");
|
|
12
13
|
const auth_service_1 = require("./services/auth/auth.service");
|
|
13
14
|
const promise_helper_function_1 = require("./services/calc-api/promise-helper.function");
|
|
14
15
|
const custom_xml_part_service_1 = require("./services/custom-xml-part.service");
|
|
15
16
|
const leap_events_service_1 = require("./services/leap-events.service");
|
|
16
17
|
const signalr_service_1 = require("./services/signalr/signalr.service");
|
|
18
|
+
// Word add-in: isWordOnline. Word Online can't redirect the task pane, so it signs in via a dialog.
|
|
19
|
+
const isOfficeOnline = () => {
|
|
20
|
+
var _a, _b;
|
|
21
|
+
return typeof Office !== 'undefined' &&
|
|
22
|
+
((_b = (_a = Office.context) === null || _a === void 0 ? void 0 : _a.diagnostics) === null || _b === void 0 ? void 0 : _b.platform) === Office.PlatformType.OfficeOnline;
|
|
23
|
+
};
|
|
17
24
|
class AppPlatform {
|
|
18
25
|
constructor() {
|
|
19
26
|
_AppPlatform_leapDocumentDataService.set(this, null);
|
|
20
27
|
this._openViaLEAP = false;
|
|
21
28
|
_AppPlatform_authService.set(this, null);
|
|
29
|
+
_AppPlatform_authAgentService.set(this, null);
|
|
30
|
+
_AppPlatform_activeAuthProvider.set(this, null);
|
|
22
31
|
_AppPlatform_signalRInitData.set(this, null);
|
|
23
32
|
_AppPlatform_initialized.set(this, false);
|
|
24
33
|
_AppPlatform_connectedToSignalR.set(this, false);
|
|
@@ -175,6 +184,123 @@ class AppPlatform {
|
|
|
175
184
|
}
|
|
176
185
|
});
|
|
177
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Word add-in: initialiseAuth (src/taskpane/index.tsx). Run on page load, before the add-in
|
|
189
|
+
* renders, so auth-agent can restore a session or finish a ?code= redirect.
|
|
190
|
+
*/
|
|
191
|
+
bootstrapAuth(config) {
|
|
192
|
+
return this.getAuthAgentService().init(config.leapAuth);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Word add-in: the host branching of App.tsx's init (openTaskPaneThenLoginWithAuthAgent /
|
|
196
|
+
* openTaskPaneThenLoginWithSignalR). Picks the provider from the document and remembers it for
|
|
197
|
+
* getAccessToken and getUser. popup defaults to Office Online only, as the add-in always did.
|
|
198
|
+
*/
|
|
199
|
+
signIn(config, options = {}) {
|
|
200
|
+
var _a, _b, _c, _d, _e, _f;
|
|
201
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
202
|
+
const popup = (_a = options.popup) !== null && _a !== void 0 ? _a : isOfficeOnline();
|
|
203
|
+
const documentData = this._openViaLEAP
|
|
204
|
+
? yield this.getDocumentData()
|
|
205
|
+
: null;
|
|
206
|
+
if (!(documentData === null || documentData === void 0 ? void 0 : documentData.matterId)) {
|
|
207
|
+
this.loggerService.log('[signIn] Not opened via LEAP: LEAP Auth');
|
|
208
|
+
return this.signInWithLeapAuth(config, {
|
|
209
|
+
popup,
|
|
210
|
+
hostType: null,
|
|
211
|
+
hostVersion: null,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
if (((_b = documentData.hostType) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === 'web') {
|
|
215
|
+
this.loggerService.log('[signIn] LEAP Web: LEAP Auth with the document apiToken');
|
|
216
|
+
return this.signInWithLeapAuth(config, {
|
|
217
|
+
popup,
|
|
218
|
+
initToken: documentData.apiToken,
|
|
219
|
+
hostType: 'web',
|
|
220
|
+
hostVersion: null,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
const connected = yield this.connectToSignalR({
|
|
224
|
+
clientId: config.leapAuth.clientId,
|
|
225
|
+
});
|
|
226
|
+
if (connected) {
|
|
227
|
+
const initData = yield this.getInitDataViaSignalR();
|
|
228
|
+
tslib_1.__classPrivateFieldSet(this, _AppPlatform_activeAuthProvider, 'signalr', "f");
|
|
229
|
+
this.loggerService.log(`[signIn] LEAP Desktop ${(_c = initData === null || initData === void 0 ? void 0 : initData.hostVersion) !== null && _c !== void 0 ? _c : '2.6'}: SignalR auth code`);
|
|
230
|
+
return {
|
|
231
|
+
outcome: ((_d = tslib_1.__classPrivateFieldGet(this, _AppPlatform_authService, "f")) === null || _d === void 0 ? void 0 : _d.getLEAPUser())
|
|
232
|
+
? 'signed-in'
|
|
233
|
+
: 'not-signed-in',
|
|
234
|
+
provider: 'signalr',
|
|
235
|
+
hostType: 'desktop',
|
|
236
|
+
hostVersion: (_e = initData === null || initData === void 0 ? void 0 : initData.hostVersion) !== null && _e !== void 0 ? _e : '2.6',
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
yield this.setupLeapEventsAndSetToken();
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
this.loggerService.error(`[signIn] WordApp_GetToken failed: ${error}`);
|
|
244
|
+
return {
|
|
245
|
+
outcome: 'failed',
|
|
246
|
+
provider: null,
|
|
247
|
+
hostType: 'desktop',
|
|
248
|
+
hostVersion: '2.5',
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
this.loggerService.log('[signIn] LEAP Desktop 2.5: WordApp_GetToken, then LEAP Auth');
|
|
252
|
+
// Re-read the document: WordApp_GetToken has just written a fresh apiToken into it.
|
|
253
|
+
const refreshed = yield this.getDocumentData();
|
|
254
|
+
return this.signInWithLeapAuth(config, {
|
|
255
|
+
popup,
|
|
256
|
+
initToken: (_f = refreshed === null || refreshed === void 0 ? void 0 : refreshed.apiToken) !== null && _f !== void 0 ? _f : documentData.apiToken,
|
|
257
|
+
hostType: 'desktop',
|
|
258
|
+
hostVersion: '2.5',
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
/** Word add-in: authState's getAccessToken, keyed on the provider signIn chose. */
|
|
263
|
+
getAccessToken(forceRefresh) {
|
|
264
|
+
if (tslib_1.__classPrivateFieldGet(this, _AppPlatform_activeAuthProvider, "f") === 'signalr') {
|
|
265
|
+
return this.getLEAPTokenViaSignalR(forceRefresh);
|
|
266
|
+
}
|
|
267
|
+
return tslib_1.__classPrivateFieldGet(this, _AppPlatform_authAgentService, "f")
|
|
268
|
+
? tslib_1.__classPrivateFieldGet(this, _AppPlatform_authAgentService, "f").getAccessToken(forceRefresh)
|
|
269
|
+
: Promise.resolve('');
|
|
270
|
+
}
|
|
271
|
+
/** Word add-in: readImmediateAccessToken (src/hooks/useLeapOfficeWordHub.ts). '' on SignalR. */
|
|
272
|
+
getAccessTokenSync() {
|
|
273
|
+
var _a, _b;
|
|
274
|
+
return (_b = (_a = tslib_1.__classPrivateFieldGet(this, _AppPlatform_authAgentService, "f")) === null || _a === void 0 ? void 0 : _a.getAccessTokenSync()) !== null && _b !== void 0 ? _b : '';
|
|
275
|
+
}
|
|
276
|
+
/** Word add-in: setLEAPUser's getLEAPUserViaSignalR, or auth-agent's userInfo via setUser. */
|
|
277
|
+
getUser() {
|
|
278
|
+
var _a;
|
|
279
|
+
if (tslib_1.__classPrivateFieldGet(this, _AppPlatform_activeAuthProvider, "f") === 'signalr') {
|
|
280
|
+
const user = (_a = tslib_1.__classPrivateFieldGet(this, _AppPlatform_authService, "f")) === null || _a === void 0 ? void 0 : _a.getLEAPUser();
|
|
281
|
+
return Promise.resolve(user ? Object.assign({}, user) : undefined);
|
|
282
|
+
}
|
|
283
|
+
return tslib_1.__classPrivateFieldGet(this, _AppPlatform_authAgentService, "f")
|
|
284
|
+
? tslib_1.__classPrivateFieldGet(this, _AppPlatform_authAgentService, "f").getUser()
|
|
285
|
+
: Promise.resolve(undefined);
|
|
286
|
+
}
|
|
287
|
+
// Word add-in: the loginWithAuthAgent call in openTaskPaneThenLoginWithAuthAgent
|
|
288
|
+
signInWithLeapAuth(config, params) {
|
|
289
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
290
|
+
const { popup, initToken, hostType, hostVersion } = params;
|
|
291
|
+
const authAgentService = this.getAuthAgentService();
|
|
292
|
+
yield authAgentService.init(config.leapAuth);
|
|
293
|
+
tslib_1.__classPrivateFieldSet(this, _AppPlatform_activeAuthProvider, 'leap-auth', "f");
|
|
294
|
+
const outcome = yield authAgentService.signIn({ initToken, popup });
|
|
295
|
+
return { outcome, provider: 'leap-auth', hostType, hostVersion };
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
getAuthAgentService() {
|
|
299
|
+
if (!tslib_1.__classPrivateFieldGet(this, _AppPlatform_authAgentService, "f")) {
|
|
300
|
+
tslib_1.__classPrivateFieldSet(this, _AppPlatform_authAgentService, new auth_agent_service_1.AuthAgentService(this.loggerService), "f");
|
|
301
|
+
}
|
|
302
|
+
return tslib_1.__classPrivateFieldGet(this, _AppPlatform_authAgentService, "f");
|
|
303
|
+
}
|
|
178
304
|
getLEAPTokenViaSignalR(forceRefresh) {
|
|
179
305
|
if (tslib_1.__classPrivateFieldGet(this, _AppPlatform_authService, "f")) {
|
|
180
306
|
return tslib_1.__classPrivateFieldGet(this, _AppPlatform_authService, "f").getRefreshedAccessToken(forceRefresh);
|
|
@@ -463,4 +589,4 @@ class AppPlatform {
|
|
|
463
589
|
}
|
|
464
590
|
}
|
|
465
591
|
exports.AppPlatform = AppPlatform;
|
|
466
|
-
_AppPlatform_leapDocumentDataService = new WeakMap(), _AppPlatform_authService = new WeakMap(), _AppPlatform_signalRInitData = new WeakMap(), _AppPlatform_initialized = new WeakMap(), _AppPlatform_connectedToSignalR = new WeakMap(), _AppPlatform_hostApplicationType = new WeakMap();
|
|
592
|
+
_AppPlatform_leapDocumentDataService = new WeakMap(), _AppPlatform_authService = new WeakMap(), _AppPlatform_authAgentService = new WeakMap(), _AppPlatform_activeAuthProvider = new WeakMap(), _AppPlatform_signalRInitData = new WeakMap(), _AppPlatform_initialized = new WeakMap(), _AppPlatform_connectedToSignalR = new WeakMap(), _AppPlatform_hostApplicationType = new WeakMap();
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** LEAP Auth (auth-agent) settings. The add-ins read these from their env files. */
|
|
2
|
+
export type LeapAuthConfig = {
|
|
3
|
+
authHost: string;
|
|
4
|
+
clientId: string;
|
|
5
|
+
scopes: string[];
|
|
6
|
+
};
|
|
7
|
+
export type SignInConfig = {
|
|
8
|
+
leapAuth: LeapAuthConfig;
|
|
9
|
+
};
|
|
10
|
+
export type AuthProvider = 'signalr' | 'leap-auth' | 'auth0';
|
|
11
|
+
export type SignInOutcome = 'signed-in' | 'not-signed-in' | 'failed';
|
|
12
|
+
export type SignInResult = {
|
|
13
|
+
outcome: SignInOutcome;
|
|
14
|
+
provider: AuthProvider | null;
|
|
15
|
+
hostType: 'desktop' | 'web' | null;
|
|
16
|
+
hostVersion: string | null;
|
|
17
|
+
};
|
|
18
|
+
export type AuthExternalUser = {
|
|
19
|
+
linked: boolean;
|
|
20
|
+
userType: string;
|
|
21
|
+
userId: string;
|
|
22
|
+
email: string;
|
|
23
|
+
firstName: string;
|
|
24
|
+
lastName: string;
|
|
25
|
+
reauthentication: {
|
|
26
|
+
required: boolean;
|
|
27
|
+
url: string;
|
|
28
|
+
nonce: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export type AuthUser = {
|
|
32
|
+
userId: string;
|
|
33
|
+
firmId: string;
|
|
34
|
+
region: string;
|
|
35
|
+
env: string;
|
|
36
|
+
countryCode: number;
|
|
37
|
+
email?: string;
|
|
38
|
+
firstName?: string;
|
|
39
|
+
lastName?: string;
|
|
40
|
+
externalUser?: AuthExternalUser | null;
|
|
41
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AuthExternalUser } from './auth.model';
|
|
1
2
|
export declare class LeapDocumentData {
|
|
2
3
|
appId: string | undefined;
|
|
3
4
|
documentId: string | undefined;
|
|
@@ -37,6 +38,7 @@ export declare class LeapDocumentData {
|
|
|
37
38
|
documentName?: string;
|
|
38
39
|
server?: number;
|
|
39
40
|
requireLogout?: boolean;
|
|
41
|
+
documentSessionId?: string | undefined;
|
|
40
42
|
constructor(init?: Partial<LeapDocumentData>);
|
|
41
43
|
}
|
|
42
44
|
export declare enum PreviewMode {
|
|
@@ -66,12 +68,14 @@ export interface ActiveENote {
|
|
|
66
68
|
}
|
|
67
69
|
export type LEAPUser = {
|
|
68
70
|
userId: string;
|
|
71
|
+
firmId: string;
|
|
69
72
|
email: string;
|
|
70
73
|
firstName: string;
|
|
71
74
|
lastName: string;
|
|
72
75
|
env: string;
|
|
73
76
|
countryCode: number;
|
|
74
77
|
region: string;
|
|
78
|
+
externalUser?: AuthExternalUser | null;
|
|
75
79
|
};
|
|
76
80
|
export declare enum HostApplicationType {
|
|
77
81
|
UnknownApplication = 0,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AuthUser, LeapAuthConfig, SignInOutcome } from '../../models/auth.model';
|
|
2
|
+
import { LoggerService } from '../logger/logger.service';
|
|
3
|
+
type AuthAgentModule = typeof import('@leapdev/auth-agent');
|
|
4
|
+
export type AuthAgentLike = Pick<AuthAgentModule['AuthAgent'], 'init' | 'login' | 'logout' | 'getAccessToken' | 'getRefreshedAccessToken' | 'userInfo'>;
|
|
5
|
+
export type AuthAgentLoader = () => Promise<AuthAgentLike>;
|
|
6
|
+
/**
|
|
7
|
+
* LEAP Auth sign-in through @leapdev/auth-agent: LEAP Web, LEAP Desktop 2.5 and documents not
|
|
8
|
+
* opened via LEAP. Ported from the Word add-in's auth slice (src/taskpane/store/authState.ts).
|
|
9
|
+
*/
|
|
10
|
+
export declare class AuthAgentService {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(loggerService: LoggerService, loadAuthAgent?: AuthAgentLoader);
|
|
13
|
+
/**
|
|
14
|
+
* Word add-in: initialiseAuth (src/taskpane/index.tsx). Must run on page load so auth-agent can
|
|
15
|
+
* restore a session or finish a ?code= redirect. Resolves false only when auth-agent couldn't be
|
|
16
|
+
* loaded.
|
|
17
|
+
*/
|
|
18
|
+
init(config: LeapAuthConfig): Promise<boolean>;
|
|
19
|
+
/**
|
|
20
|
+
* Word add-in: loginWithAuthAgent, with its pendingLogins dedupe. initToken is the document's
|
|
21
|
+
* apiToken. Without popup the page redirects to the auth host, so there is no result.
|
|
22
|
+
*/
|
|
23
|
+
signIn(params: {
|
|
24
|
+
initToken?: string;
|
|
25
|
+
popup: boolean;
|
|
26
|
+
}): Promise<SignInOutcome>;
|
|
27
|
+
/** Word add-in: the AuthAgent.getRefreshedAccessToken branch of authState's getAccessToken. */
|
|
28
|
+
getAccessToken(force?: boolean): Promise<string>;
|
|
29
|
+
/** Word add-in: readImmediateAccessToken (src/hooks/useLeapOfficeWordHub.ts). No refresh. */
|
|
30
|
+
getAccessTokenSync(): string;
|
|
31
|
+
getUser(): Promise<AuthUser | undefined>;
|
|
32
|
+
/** auth-agent navigates the page to the auth host's logout. */
|
|
33
|
+
signOut(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _AuthAgentService_instances, _AuthAgentService_loggerService, _AuthAgentService_loadAuthAgent, _AuthAgentService_pendingSignIns, _AuthAgentService_started, _AuthAgentService_authAgent, _AuthAgentService_user, _AuthAgentService_start, _AuthAgentService_ready, _AuthAgentService_signInOnce, _AuthAgentService_reuseSession, _AuthAgentService_setUser, _AuthAgentService_userIdFromToken;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AuthAgentService = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
// Peer dependency, loaded only once a consumer starts LEAP Auth.
|
|
7
|
+
const defaultAuthAgentLoader = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
8
|
+
const { AuthAgent } = yield Promise.resolve().then(() => tslib_1.__importStar(require('@leapdev/auth-agent')));
|
|
9
|
+
return AuthAgent;
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* LEAP Auth sign-in through @leapdev/auth-agent: LEAP Web, LEAP Desktop 2.5 and documents not
|
|
13
|
+
* opened via LEAP. Ported from the Word add-in's auth slice (src/taskpane/store/authState.ts).
|
|
14
|
+
*/
|
|
15
|
+
class AuthAgentService {
|
|
16
|
+
constructor(loggerService, loadAuthAgent = defaultAuthAgentLoader) {
|
|
17
|
+
_AuthAgentService_instances.add(this);
|
|
18
|
+
_AuthAgentService_loggerService.set(this, void 0);
|
|
19
|
+
_AuthAgentService_loadAuthAgent.set(this, void 0);
|
|
20
|
+
_AuthAgentService_pendingSignIns.set(this, new Map());
|
|
21
|
+
_AuthAgentService_started.set(this, null);
|
|
22
|
+
_AuthAgentService_authAgent.set(this, null);
|
|
23
|
+
_AuthAgentService_user.set(this, void 0);
|
|
24
|
+
tslib_1.__classPrivateFieldSet(this, _AuthAgentService_loggerService, loggerService, "f");
|
|
25
|
+
tslib_1.__classPrivateFieldSet(this, _AuthAgentService_loadAuthAgent, loadAuthAgent, "f");
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Word add-in: initialiseAuth (src/taskpane/index.tsx). Must run on page load so auth-agent can
|
|
29
|
+
* restore a session or finish a ?code= redirect. Resolves false only when auth-agent couldn't be
|
|
30
|
+
* loaded.
|
|
31
|
+
*/
|
|
32
|
+
init(config) {
|
|
33
|
+
if (!tslib_1.__classPrivateFieldGet(this, _AuthAgentService_started, "f")) {
|
|
34
|
+
tslib_1.__classPrivateFieldSet(this, _AuthAgentService_started, tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_start).call(this, config), "f");
|
|
35
|
+
}
|
|
36
|
+
return tslib_1.__classPrivateFieldGet(this, _AuthAgentService_started, "f").then((authAgent) => !!authAgent);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Word add-in: loginWithAuthAgent, with its pendingLogins dedupe. initToken is the document's
|
|
40
|
+
* apiToken. Without popup the page redirects to the auth host, so there is no result.
|
|
41
|
+
*/
|
|
42
|
+
signIn(params) {
|
|
43
|
+
var _a;
|
|
44
|
+
const key = (_a = params.initToken) !== null && _a !== void 0 ? _a : '';
|
|
45
|
+
const pending = tslib_1.__classPrivateFieldGet(this, _AuthAgentService_pendingSignIns, "f").get(key);
|
|
46
|
+
if (pending) {
|
|
47
|
+
return pending;
|
|
48
|
+
}
|
|
49
|
+
const signIn = tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_signInOnce).call(this, params).finally(() => {
|
|
50
|
+
if (tslib_1.__classPrivateFieldGet(this, _AuthAgentService_pendingSignIns, "f").get(key) === signIn) {
|
|
51
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_pendingSignIns, "f").delete(key);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_pendingSignIns, "f").set(key, signIn);
|
|
55
|
+
return signIn;
|
|
56
|
+
}
|
|
57
|
+
/** Word add-in: the AuthAgent.getRefreshedAccessToken branch of authState's getAccessToken. */
|
|
58
|
+
getAccessToken(force) {
|
|
59
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const authAgent = yield tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_ready).call(this);
|
|
61
|
+
if (!authAgent) {
|
|
62
|
+
return '';
|
|
63
|
+
}
|
|
64
|
+
return (yield authAgent.getRefreshedAccessToken(force)) || '';
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Word add-in: readImmediateAccessToken (src/hooks/useLeapOfficeWordHub.ts). No refresh. */
|
|
68
|
+
getAccessTokenSync() {
|
|
69
|
+
var _a;
|
|
70
|
+
try {
|
|
71
|
+
return ((_a = tslib_1.__classPrivateFieldGet(this, _AuthAgentService_authAgent, "f")) === null || _a === void 0 ? void 0 : _a.getAccessToken()) || '';
|
|
72
|
+
}
|
|
73
|
+
catch (_b) {
|
|
74
|
+
return '';
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
getUser() {
|
|
78
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
if (tslib_1.__classPrivateFieldGet(this, _AuthAgentService_user, "f")) {
|
|
80
|
+
return tslib_1.__classPrivateFieldGet(this, _AuthAgentService_user, "f");
|
|
81
|
+
}
|
|
82
|
+
const authAgent = yield tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_ready).call(this);
|
|
83
|
+
if (!authAgent || !authAgent.getAccessToken()) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const userInfo = yield authAgent.userInfo();
|
|
88
|
+
if (userInfo) {
|
|
89
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_setUser).call(this, userInfo);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").error(`[AuthAgentService] Unable to get user info: ${error}`);
|
|
94
|
+
}
|
|
95
|
+
return tslib_1.__classPrivateFieldGet(this, _AuthAgentService_user, "f");
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/** auth-agent navigates the page to the auth host's logout. */
|
|
99
|
+
signOut() {
|
|
100
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
const authAgent = yield tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_ready).call(this);
|
|
102
|
+
tslib_1.__classPrivateFieldSet(this, _AuthAgentService_user, undefined, "f");
|
|
103
|
+
yield (authAgent === null || authAgent === void 0 ? void 0 : authAgent.logout());
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.AuthAgentService = AuthAgentService;
|
|
108
|
+
_AuthAgentService_loggerService = new WeakMap(), _AuthAgentService_loadAuthAgent = new WeakMap(), _AuthAgentService_pendingSignIns = new WeakMap(), _AuthAgentService_started = new WeakMap(), _AuthAgentService_authAgent = new WeakMap(), _AuthAgentService_user = new WeakMap(), _AuthAgentService_instances = new WeakSet(), _AuthAgentService_start = function _AuthAgentService_start(config) {
|
|
109
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
try {
|
|
111
|
+
tslib_1.__classPrivateFieldSet(this, _AuthAgentService_authAgent, yield tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loadAuthAgent, "f").call(this), "f");
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").error(`[AuthAgentService] Unable to load @leapdev/auth-agent: ${error}`);
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
yield tslib_1.__classPrivateFieldGet(this, _AuthAgentService_authAgent, "f").init({
|
|
119
|
+
authHost: config.authHost,
|
|
120
|
+
clientId: config.clientId,
|
|
121
|
+
scopes: config.scopes,
|
|
122
|
+
autoLogin: false,
|
|
123
|
+
uniqueSession: false,
|
|
124
|
+
autoLogout: false,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").error(`[AuthAgentService] init failed: ${error}`);
|
|
129
|
+
}
|
|
130
|
+
return tslib_1.__classPrivateFieldGet(this, _AuthAgentService_authAgent, "f");
|
|
131
|
+
});
|
|
132
|
+
}, _AuthAgentService_ready = function _AuthAgentService_ready() {
|
|
133
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
if (!tslib_1.__classPrivateFieldGet(this, _AuthAgentService_started, "f")) {
|
|
135
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").error('[AuthAgentService] init() has not been called');
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
return tslib_1.__classPrivateFieldGet(this, _AuthAgentService_started, "f");
|
|
139
|
+
});
|
|
140
|
+
}, _AuthAgentService_signInOnce = function _AuthAgentService_signInOnce(params) {
|
|
141
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
142
|
+
const authAgent = yield tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_ready).call(this);
|
|
143
|
+
if (!authAgent) {
|
|
144
|
+
return 'not-signed-in';
|
|
145
|
+
}
|
|
146
|
+
const { initToken, popup } = params;
|
|
147
|
+
removeHostInfo();
|
|
148
|
+
try {
|
|
149
|
+
const reused = yield tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_reuseSession).call(this, authAgent, initToken);
|
|
150
|
+
if (reused) {
|
|
151
|
+
return reused;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").error(`[AuthAgentService] Existing session check failed: ${error}`);
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").log(`[AuthAgentService] Signing in with LEAP Auth${initToken ? ' (passthrough)' : ''} ${popup ? 'in a dialog' : 'by redirect'}`);
|
|
159
|
+
const accessToken = yield authAgent.login(Object.assign({ popup }, (initToken ? { initToken } : {})));
|
|
160
|
+
if (!accessToken) {
|
|
161
|
+
return 'not-signed-in';
|
|
162
|
+
}
|
|
163
|
+
const userInfo = yield authAgent.userInfo();
|
|
164
|
+
if (!userInfo) {
|
|
165
|
+
return 'not-signed-in';
|
|
166
|
+
}
|
|
167
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_setUser).call(this, userInfo);
|
|
168
|
+
return 'signed-in';
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").error(`[AuthAgentService] Sign-in failed: ${error}`);
|
|
172
|
+
return 'not-signed-in';
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}, _AuthAgentService_reuseSession = function _AuthAgentService_reuseSession(authAgent, initToken) {
|
|
176
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
177
|
+
if (!authAgent.getAccessToken()) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const userInfo = yield authAgent.userInfo();
|
|
181
|
+
if (!userInfo) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
if (!initToken) {
|
|
185
|
+
// A document not opened via LEAP has no apiToken, so the signed-in session is reused.
|
|
186
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").log('[AuthAgentService] Reusing the LEAP Auth session');
|
|
187
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_setUser).call(this, userInfo);
|
|
188
|
+
return 'signed-in';
|
|
189
|
+
}
|
|
190
|
+
const documentUserId = tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_userIdFromToken).call(this, initToken);
|
|
191
|
+
if (!documentUserId) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
if (documentUserId === userInfo.userId) {
|
|
195
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").log('[AuthAgentService] Reusing the LEAP Auth session of the document user');
|
|
196
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_instances, "m", _AuthAgentService_setUser).call(this, userInfo);
|
|
197
|
+
return 'signed-in';
|
|
198
|
+
}
|
|
199
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").warn(`[AuthAgentService] Session is for user ${userInfo.userId}, not the document's ${documentUserId}; signing out`);
|
|
200
|
+
yield authAgent.logout();
|
|
201
|
+
return 'not-signed-in';
|
|
202
|
+
});
|
|
203
|
+
}, _AuthAgentService_setUser = function _AuthAgentService_setUser(userInfo) {
|
|
204
|
+
const { userId, firmId, region, env, countryCode, email, firstName, lastName, externalUser, } = userInfo;
|
|
205
|
+
tslib_1.__classPrivateFieldSet(this, _AuthAgentService_user, {
|
|
206
|
+
userId,
|
|
207
|
+
firmId,
|
|
208
|
+
region,
|
|
209
|
+
env,
|
|
210
|
+
countryCode,
|
|
211
|
+
email,
|
|
212
|
+
firstName,
|
|
213
|
+
lastName,
|
|
214
|
+
externalUser,
|
|
215
|
+
}, "f");
|
|
216
|
+
}, _AuthAgentService_userIdFromToken = function _AuthAgentService_userIdFromToken(token) {
|
|
217
|
+
try {
|
|
218
|
+
const { userId } = decodeJwtPayload(token);
|
|
219
|
+
return typeof userId === 'string' ? userId : undefined;
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
tslib_1.__classPrivateFieldGet(this, _AuthAgentService_loggerService, "f").error(`[AuthAgentService] Unable to decode the document's apiToken: ${error}`);
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
// JWTs use base64url; window.atob needs standard base64 with padding.
|
|
227
|
+
const decodeJwtPayload = (token) => {
|
|
228
|
+
const payload = token.split('.')[1];
|
|
229
|
+
if (!payload) {
|
|
230
|
+
throw new Error('Not a JWT');
|
|
231
|
+
}
|
|
232
|
+
const base64 = payload.replace(/-/g, '+').replace(/_/g, '/');
|
|
233
|
+
const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4);
|
|
234
|
+
return JSON.parse(window.atob(padded));
|
|
235
|
+
};
|
|
236
|
+
// Word add-in: removeHostInfo. Office appends _host_Info to the task pane URL, and its characters
|
|
237
|
+
// make the auth host reject the URL as redirect_uri (403).
|
|
238
|
+
const removeHostInfo = () => {
|
|
239
|
+
const url = new URL(window.location.href);
|
|
240
|
+
url.searchParams.delete('_host_Info');
|
|
241
|
+
window.history.replaceState(null, '', url.href);
|
|
242
|
+
};
|
package/src/types.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ import { EvaluatePathsRequest, EvaluatePathsResponse } from './lib/models';
|
|
|
2
2
|
import { HostApplicationType } from './lib/models/leap.model';
|
|
3
3
|
export { EvaluatePathsRequest, EvaluatePathsResponse, HostApplicationType, };
|
|
4
4
|
export { LeapDocumentData } from './lib/domains/leap-document-data';
|
|
5
|
+
export type { AuthExternalUser, AuthProvider, AuthUser, LeapAuthConfig, SignInConfig, SignInOutcome, SignInResult, } from './lib/models/auth.model';
|