@inrupt/solid-client-authn-browser 1.17.2 → 1.17.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ClientAuthentication.d.ts +7 -0
- package/dist/Session.d.ts +174 -0
- package/dist/defaultSession.d.ts +70 -0
- package/dist/dependencies.d.ts +12 -0
- package/dist/index.js +783 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +783 -6
- package/dist/index.mjs.map +1 -1
- package/dist/login/oidc/AggregateOidcHandler.d.ts +10 -0
- package/dist/login/oidc/AggregateRedirectHandler.d.ts +10 -0
- package/dist/login/oidc/ClientRegistrar.d.ts +7 -0
- package/dist/login/oidc/IssuerConfigFetcher.d.ts +10 -0
- package/dist/login/oidc/OidcLoginHandler.d.ts +10 -0
- package/dist/login/oidc/Redirector.d.ts +7 -0
- package/dist/login/oidc/incomingRedirectHandler/AuthCodeRedirectHandler.d.ts +3 -0
- package/dist/login/oidc/incomingRedirectHandler/ErrorOidcHandler.d.ts +10 -0
- package/dist/login/oidc/incomingRedirectHandler/FallbackRedirectHandler.d.ts +10 -0
- package/dist/login/oidc/oidcHandlers/AuthorizationCodeWithPkceOidcHandler.d.ts +12 -0
- package/dist/login/oidc/refresh/TokenRefresher.d.ts +7 -0
- package/dist/logout/buildRpInitiatedLogout.d.ts +7 -0
- package/dist/sessionInfo/SessionInfoManager.d.ts +18 -0
- package/dist/solid-client-authn.bundle.js +5 -5
- package/dist/solid-client-authn.bundle.js.map +1 -1
- package/dist/storage/BrowserStorage.d.ts +7 -0
- package/dist/storage/StorageUtility.d.ts +14 -0
- package/dist/util/UuidGenerator.d.ts +6 -0
- package/package.json +6 -6
- package/src/ClientAuthentication.spec.ts +40 -19
- package/src/ClientAuthentication.ts +14 -3
- package/src/Session.spec.ts +10 -13
package/dist/index.js
CHANGED
|
@@ -6,18 +6,82 @@ var EventEmitter = require('events');
|
|
|
6
6
|
var oidcClientExt = require('@inrupt/oidc-client-ext');
|
|
7
7
|
var universalFetch = require('@inrupt/universal-fetch');
|
|
8
8
|
|
|
9
|
+
//
|
|
10
|
+
// Copyright Inrupt Inc.
|
|
11
|
+
//
|
|
12
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
14
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
15
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
16
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
17
|
+
// subject to the following conditions:
|
|
18
|
+
//
|
|
19
|
+
// The above copyright notice and this permission notice shall be included in
|
|
20
|
+
// all copies or substantial portions of the Software.
|
|
21
|
+
//
|
|
22
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
23
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
24
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
25
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
26
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
27
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
28
|
+
//
|
|
29
|
+
/**
|
|
30
|
+
* This class in a no-value-added extension of StorageUtility from the core module.
|
|
31
|
+
* The reason it has to be declared is for TSyringe to find the decorators in the
|
|
32
|
+
* same modules as where the dependency container is declared (in this case,
|
|
33
|
+
* the browser module, with the dependancy container in dependencies.ts).
|
|
34
|
+
* @hidden
|
|
35
|
+
*/
|
|
9
36
|
class StorageUtilityBrowser extends solidClientAuthnCore.StorageUtility {
|
|
10
37
|
constructor(secureStorage, insecureStorage) {
|
|
11
38
|
super(secureStorage, insecureStorage);
|
|
12
39
|
}
|
|
13
40
|
}
|
|
14
41
|
|
|
42
|
+
//
|
|
43
|
+
// Copyright Inrupt Inc.
|
|
44
|
+
//
|
|
45
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
46
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
47
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
48
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
49
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
50
|
+
// subject to the following conditions:
|
|
51
|
+
//
|
|
52
|
+
// The above copyright notice and this permission notice shall be included in
|
|
53
|
+
// all copies or substantial portions of the Software.
|
|
54
|
+
//
|
|
55
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
56
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
57
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
58
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
59
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
60
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
61
|
+
//
|
|
62
|
+
/**
|
|
63
|
+
* @hidden
|
|
64
|
+
*/
|
|
15
65
|
class ClientAuthentication extends solidClientAuthnCore.ClientAuthentication {
|
|
16
66
|
constructor() {
|
|
17
67
|
super(...arguments);
|
|
68
|
+
// Define these functions as properties so that they don't get accidentally re-bound.
|
|
69
|
+
// Isn't Javascript fun?
|
|
18
70
|
this.login = async (options, eventEmitter) => {
|
|
19
71
|
var _a, _b;
|
|
72
|
+
// In order to get a clean start, make sure that the session is logged out
|
|
73
|
+
// on login.
|
|
74
|
+
// But we may want to preserve our client application info, particularly if
|
|
75
|
+
// we used Dynamic Client Registration to register (since we don't
|
|
76
|
+
// necessarily want the user to have to register this app each time they
|
|
77
|
+
// login).
|
|
20
78
|
await this.sessionInfoManager.clear(options.sessionId);
|
|
79
|
+
// In the case of the user hitting the 'back' button in their browser, they
|
|
80
|
+
// could return to a previous redirect URL that contains OIDC params that
|
|
81
|
+
// are now longer valid. To be safe, strip relevant params now.
|
|
82
|
+
// If the user is providing a redirect IRI, it should not be modified, so
|
|
83
|
+
// normalization only applies if we default to the current location (which is
|
|
84
|
+
// a bad practice and should be discouraged).
|
|
21
85
|
const redirectUrl = (_a = options.redirectUrl) !== null && _a !== void 0 ? _a : oidcClientExt.removeOidcQueryParam(window.location.href);
|
|
22
86
|
if (!solidClientAuthnCore.isValidRedirectUrl(redirectUrl)) {
|
|
23
87
|
throw new Error(`${redirectUrl} is not a valid redirect URL, it is either a malformed IRI, includes a hash fragment, or reserved query parameters ('code' or 'state').`);
|
|
@@ -25,10 +89,15 @@ class ClientAuthentication extends solidClientAuthnCore.ClientAuthentication {
|
|
|
25
89
|
await this.loginHandler.handle({
|
|
26
90
|
...options,
|
|
27
91
|
redirectUrl,
|
|
92
|
+
// If no clientName is provided, the clientId may be used instead.
|
|
28
93
|
clientName: (_b = options.clientName) !== null && _b !== void 0 ? _b : options.clientId,
|
|
29
94
|
eventEmitter,
|
|
30
95
|
});
|
|
31
96
|
};
|
|
97
|
+
// Collects session information from storage, and returns them. Returns null
|
|
98
|
+
// if the expected information cannot be found.
|
|
99
|
+
// Note that the ID token is not stored, which means the session information
|
|
100
|
+
// cannot be validated at this point.
|
|
32
101
|
this.validateCurrentSession = async (currentSessionId) => {
|
|
33
102
|
const sessionInfo = await this.sessionInfoManager.get(currentSessionId);
|
|
34
103
|
if (sessionInfo === undefined ||
|
|
@@ -41,9 +110,14 @@ class ClientAuthentication extends solidClientAuthnCore.ClientAuthentication {
|
|
|
41
110
|
this.handleIncomingRedirect = async (url, eventEmitter) => {
|
|
42
111
|
try {
|
|
43
112
|
const redirectInfo = await this.redirectHandler.handle(url, eventEmitter);
|
|
113
|
+
// The `FallbackRedirectHandler` directly returns the global `fetch` for
|
|
114
|
+
// his value, so we should ensure it's bound to `window` rather than to
|
|
115
|
+
// ClientAuthentication, to avoid the following error:
|
|
116
|
+
// > 'fetch' called on an object that does not implement interface Window.
|
|
44
117
|
this.fetch = redirectInfo.fetch.bind(window);
|
|
45
118
|
this.boundLogout = redirectInfo.getLogoutUrl;
|
|
46
|
-
|
|
119
|
+
// Strip the oauth params:
|
|
120
|
+
await this.cleanUrlAfterRedirect(url);
|
|
47
121
|
return {
|
|
48
122
|
isLoggedIn: redirectInfo.isLoggedIn,
|
|
49
123
|
webId: redirectInfo.webId,
|
|
@@ -52,31 +126,79 @@ class ClientAuthentication extends solidClientAuthnCore.ClientAuthentication {
|
|
|
52
126
|
};
|
|
53
127
|
}
|
|
54
128
|
catch (err) {
|
|
55
|
-
|
|
129
|
+
// Strip the oauth params:
|
|
130
|
+
await this.cleanUrlAfterRedirect(url);
|
|
131
|
+
// FIXME: EVENTS.ERROR should be errorCode, errorDescription
|
|
132
|
+
//
|
|
133
|
+
// I'm not sure if "redirect" is a good error code, and in theory `err`
|
|
134
|
+
// maybe an Error object and not a string; Maybe we want to just hardcode
|
|
135
|
+
// a description instead?
|
|
56
136
|
eventEmitter.emit(solidClientAuthnCore.EVENTS.ERROR, "redirect", err);
|
|
57
137
|
return undefined;
|
|
58
138
|
}
|
|
59
139
|
};
|
|
60
140
|
}
|
|
61
|
-
cleanUrlAfterRedirect(url) {
|
|
141
|
+
async cleanUrlAfterRedirect(url) {
|
|
62
142
|
const cleanedUpUrl = new URL(url);
|
|
63
143
|
cleanedUpUrl.searchParams.delete("state");
|
|
144
|
+
// For auth code flow
|
|
64
145
|
cleanedUpUrl.searchParams.delete("code");
|
|
146
|
+
// For implicit flow
|
|
65
147
|
cleanedUpUrl.searchParams.delete("id_token");
|
|
66
148
|
cleanedUpUrl.searchParams.delete("access_token");
|
|
149
|
+
// For login error
|
|
67
150
|
cleanedUpUrl.searchParams.delete("error");
|
|
68
151
|
cleanedUpUrl.searchParams.delete("error_description");
|
|
69
152
|
cleanedUpUrl.searchParams.delete("iss");
|
|
153
|
+
// Remove OAuth-specific query params (since the login flow finishes with
|
|
154
|
+
// the browser being redirected back with OAuth2 query params (e.g. for
|
|
155
|
+
// 'code' and 'state'), and so if the user simply refreshes this page our
|
|
156
|
+
// authentication library will be called again with what are now invalid
|
|
157
|
+
// query parameters!).
|
|
70
158
|
window.history.replaceState(null, "", cleanedUpUrl.toString());
|
|
159
|
+
while (window.location.href !== cleanedUpUrl.href) {
|
|
160
|
+
// Poll the current URL every ms. Active polling is required because
|
|
161
|
+
// window.history.replaceState is asynchronous, but the associated
|
|
162
|
+
// 'popstate' event which should be listened to is only sent on active
|
|
163
|
+
// navigation, which we will not have here.
|
|
164
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event#when_popstate_is_sent
|
|
165
|
+
// eslint-disable-next-line no-await-in-loop
|
|
166
|
+
await new Promise((resolve) => {
|
|
167
|
+
setTimeout(() => resolve(), 1);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
71
170
|
}
|
|
72
171
|
}
|
|
73
172
|
|
|
173
|
+
//
|
|
174
|
+
// Copyright Inrupt Inc.
|
|
175
|
+
//
|
|
176
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
177
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
178
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
179
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
180
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
181
|
+
// subject to the following conditions:
|
|
182
|
+
//
|
|
183
|
+
// The above copyright notice and this permission notice shall be included in
|
|
184
|
+
// all copies or substantial portions of the Software.
|
|
185
|
+
//
|
|
186
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
187
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
188
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
189
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
190
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
191
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
192
|
+
//
|
|
74
193
|
function hasIssuer(options) {
|
|
75
194
|
return typeof options.oidcIssuer === "string";
|
|
76
195
|
}
|
|
77
196
|
function hasRedirectUrl(options) {
|
|
78
197
|
return typeof options.redirectUrl === "string";
|
|
79
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* @hidden
|
|
201
|
+
*/
|
|
80
202
|
class OidcLoginHandler {
|
|
81
203
|
constructor(storageUtility, oidcHandler, issuerConfigFetcher, clientRegistrar) {
|
|
82
204
|
this.storageUtility = storageUtility;
|
|
@@ -98,22 +220,57 @@ class OidcLoginHandler {
|
|
|
98
220
|
if (!hasRedirectUrl(options)) {
|
|
99
221
|
throw new solidClientAuthnCore.ConfigurationError(`OidcLoginHandler requires a redirect URL: missing property 'redirectUrl' in ${JSON.stringify(options)}`);
|
|
100
222
|
}
|
|
223
|
+
// Fetch issuer config.
|
|
101
224
|
const issuerConfig = await this.issuerConfigFetcher.fetchConfig(options.oidcIssuer);
|
|
102
225
|
const clientRegistration = await solidClientAuthnCore.handleRegistration(options, issuerConfig, this.storageUtility, this.clientRegistrar);
|
|
226
|
+
// Construct OIDC Options
|
|
103
227
|
const OidcOptions = {
|
|
228
|
+
// Note that here, the issuer is not the one from the received options, but
|
|
229
|
+
// from the issuer's config. This enforces the canonical URL is used and stored,
|
|
230
|
+
// which is also the one present in the ID token, so storing a technically
|
|
231
|
+
// valid, but different issuer URL (e.g. using a trailing slash or not) now
|
|
232
|
+
// could prevent from validating the ID token later.
|
|
104
233
|
issuer: issuerConfig.issuer,
|
|
234
|
+
// TODO: differentiate if DPoP should be true
|
|
105
235
|
dpop: options.tokenType.toLowerCase() === "dpop",
|
|
106
236
|
...options,
|
|
107
237
|
issuerConfiguration: issuerConfig,
|
|
108
238
|
client: clientRegistration,
|
|
109
239
|
};
|
|
240
|
+
// Call proper OIDC Handler
|
|
110
241
|
return this.oidcHandler.handle(OidcOptions);
|
|
111
242
|
}
|
|
112
243
|
}
|
|
113
244
|
|
|
245
|
+
//
|
|
246
|
+
// Copyright Inrupt Inc.
|
|
247
|
+
//
|
|
248
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
249
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
250
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
251
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
252
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
253
|
+
// subject to the following conditions:
|
|
254
|
+
//
|
|
255
|
+
// The above copyright notice and this permission notice shall be included in
|
|
256
|
+
// all copies or substantial portions of the Software.
|
|
257
|
+
//
|
|
258
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
259
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
260
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
261
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
262
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
263
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
264
|
+
//
|
|
265
|
+
/**
|
|
266
|
+
* @hidden
|
|
267
|
+
* Authorization code flow spec: https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth
|
|
268
|
+
* PKCE: https://tools.ietf.org/html/rfc7636
|
|
269
|
+
*/
|
|
114
270
|
class AuthorizationCodeWithPkceOidcHandler extends solidClientAuthnCore.AuthorizationCodeWithPkceOidcHandlerBase {
|
|
115
271
|
async handle(oidcLoginOptions) {
|
|
116
272
|
var _a;
|
|
273
|
+
/* eslint-disable camelcase */
|
|
117
274
|
const oidcOptions = {
|
|
118
275
|
authority: oidcLoginOptions.issuer.toString(),
|
|
119
276
|
client_id: oidcLoginOptions.client.clientId,
|
|
@@ -123,28 +280,58 @@ class AuthorizationCodeWithPkceOidcHandler extends solidClientAuthnCore.Authoriz
|
|
|
123
280
|
response_type: "code",
|
|
124
281
|
scope: solidClientAuthnCore.DEFAULT_SCOPES,
|
|
125
282
|
filterProtocolClaims: true,
|
|
283
|
+
// The userinfo endpoint on NSS fails, so disable this for now
|
|
284
|
+
// Note that in Solid, information should be retrieved from the
|
|
285
|
+
// profile referenced by the WebId.
|
|
126
286
|
loadUserInfo: false,
|
|
127
287
|
code_verifier: true,
|
|
128
288
|
prompt: (_a = oidcLoginOptions.prompt) !== null && _a !== void 0 ? _a : "consent",
|
|
129
289
|
};
|
|
290
|
+
/* eslint-enable camelcase */
|
|
130
291
|
const oidcClientLibrary = new oidcClientExt.OidcClient(oidcOptions);
|
|
131
292
|
try {
|
|
132
293
|
const signingRequest = await oidcClientLibrary.createSigninRequest();
|
|
294
|
+
// Make sure to await the promise before returning so that the error is caught.
|
|
133
295
|
return await this.handleRedirect({
|
|
134
296
|
oidcLoginOptions,
|
|
297
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
135
298
|
state: signingRequest.state._id,
|
|
299
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
136
300
|
codeVerifier: signingRequest.state._code_verifier,
|
|
137
301
|
targetUrl: signingRequest.url.toString(),
|
|
138
302
|
});
|
|
139
303
|
}
|
|
140
304
|
catch (err) {
|
|
305
|
+
// eslint-disable-next-line no-console
|
|
141
306
|
console.error(err);
|
|
142
307
|
}
|
|
308
|
+
// The login is only completed AFTER redirect, so nothing to return here.
|
|
143
309
|
return undefined;
|
|
144
310
|
}
|
|
145
311
|
}
|
|
146
312
|
|
|
313
|
+
//
|
|
314
|
+
// Copyright Inrupt Inc.
|
|
315
|
+
//
|
|
316
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
317
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
318
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
319
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
320
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
321
|
+
// subject to the following conditions:
|
|
322
|
+
//
|
|
323
|
+
// The above copyright notice and this permission notice shall be included in
|
|
324
|
+
// all copies or substantial portions of the Software.
|
|
325
|
+
//
|
|
326
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
327
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
328
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
329
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
330
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
331
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
332
|
+
//
|
|
147
333
|
const WELL_KNOWN_OPENID_CONFIG = ".well-known/openid-configuration";
|
|
334
|
+
/* eslint-disable camelcase */
|
|
148
335
|
const issuerConfigKeyMap = {
|
|
149
336
|
issuer: {
|
|
150
337
|
toKey: "issuer",
|
|
@@ -232,10 +419,15 @@ const issuerConfigKeyMap = {
|
|
|
232
419
|
convertToUrl: true,
|
|
233
420
|
},
|
|
234
421
|
};
|
|
422
|
+
/* eslint-enable camelcase */
|
|
235
423
|
function processConfig(config) {
|
|
236
424
|
const parsedConfig = {};
|
|
237
425
|
Object.keys(config).forEach((key) => {
|
|
238
426
|
if (issuerConfigKeyMap[key]) {
|
|
427
|
+
// TODO: PMcB55: Validate URL if "issuerConfigKeyMap[key].convertToUrl"
|
|
428
|
+
// if (issuerConfigKeyMap[key].convertToUrl) {
|
|
429
|
+
// validateUrl(config[key]);
|
|
430
|
+
// }
|
|
239
431
|
parsedConfig[issuerConfigKeyMap[key].toKey] = config[key];
|
|
240
432
|
}
|
|
241
433
|
});
|
|
@@ -244,33 +436,71 @@ function processConfig(config) {
|
|
|
244
436
|
}
|
|
245
437
|
return parsedConfig;
|
|
246
438
|
}
|
|
439
|
+
/**
|
|
440
|
+
* @hidden
|
|
441
|
+
*/
|
|
247
442
|
class IssuerConfigFetcher {
|
|
248
443
|
constructor(storageUtility) {
|
|
249
444
|
this.storageUtility = storageUtility;
|
|
250
445
|
this.storageUtility = storageUtility;
|
|
251
446
|
}
|
|
447
|
+
// This method needs no state (so can be static), and can be exposed to allow
|
|
448
|
+
// callers to know where this implementation puts state it needs.
|
|
252
449
|
static getLocalStorageKey(issuer) {
|
|
253
450
|
return `issuerConfig:${issuer}`;
|
|
254
451
|
}
|
|
255
452
|
async fetchConfig(issuer) {
|
|
256
453
|
let issuerConfig;
|
|
257
|
-
const openIdConfigUrl = new URL(WELL_KNOWN_OPENID_CONFIG,
|
|
454
|
+
const openIdConfigUrl = new URL(WELL_KNOWN_OPENID_CONFIG,
|
|
455
|
+
// Make sure to append a slash at issuer URL, so that the .well-known URL
|
|
456
|
+
// includes the full issuer path. See https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig.
|
|
457
|
+
issuer.endsWith("/") ? issuer : `${issuer}/`).href;
|
|
258
458
|
const issuerConfigRequestBody = await universalFetch.fetch.call(globalThis, openIdConfigUrl);
|
|
459
|
+
// Check the validity of the fetched config
|
|
259
460
|
try {
|
|
260
461
|
issuerConfig = processConfig(await issuerConfigRequestBody.json());
|
|
261
462
|
}
|
|
262
463
|
catch (err) {
|
|
263
464
|
throw new solidClientAuthnCore.ConfigurationError(`[${issuer.toString()}] has an invalid configuration: ${err.message}`);
|
|
264
465
|
}
|
|
466
|
+
// Update store with fetched config
|
|
265
467
|
await this.storageUtility.set(IssuerConfigFetcher.getLocalStorageKey(issuer), JSON.stringify(issuerConfig));
|
|
266
468
|
return issuerConfig;
|
|
267
469
|
}
|
|
268
470
|
}
|
|
269
471
|
|
|
472
|
+
//
|
|
473
|
+
// Copyright Inrupt Inc.
|
|
474
|
+
//
|
|
475
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
476
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
477
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
478
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
479
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
480
|
+
// subject to the following conditions:
|
|
481
|
+
//
|
|
482
|
+
// The above copyright notice and this permission notice shall be included in
|
|
483
|
+
// all copies or substantial portions of the Software.
|
|
484
|
+
//
|
|
485
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
486
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
487
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
488
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
489
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
490
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
491
|
+
//
|
|
492
|
+
/**
|
|
493
|
+
* @param sessionId
|
|
494
|
+
* @param storage
|
|
495
|
+
* @hidden
|
|
496
|
+
*/
|
|
270
497
|
async function clear(sessionId, storage) {
|
|
271
498
|
await solidClientAuthnCore.clear(sessionId, storage);
|
|
272
499
|
await oidcClientExt.clearOidcPersistentStorage();
|
|
273
500
|
}
|
|
501
|
+
/**
|
|
502
|
+
* @hidden
|
|
503
|
+
*/
|
|
274
504
|
class SessionInfoManager extends solidClientAuthnCore.SessionInfoManagerBase {
|
|
275
505
|
async get(sessionId) {
|
|
276
506
|
const [isLoggedIn, webId, clientId, clientSecret, redirectUrl, refreshToken, issuer, tokenType,] = await Promise.all([
|
|
@@ -300,6 +530,9 @@ class SessionInfoManager extends solidClientAuthnCore.SessionInfoManagerBase {
|
|
|
300
530
|
}),
|
|
301
531
|
]);
|
|
302
532
|
if (typeof redirectUrl === "string" && !solidClientAuthnCore.isValidRedirectUrl(redirectUrl)) {
|
|
533
|
+
// This resolves the issue for people experiencing https://github.com/inrupt/solid-client-authn-js/issues/2891.
|
|
534
|
+
// An invalid redirect URL is present in the storage, and the session should
|
|
535
|
+
// be cleared to get a fresh start. This will require the user to log back in.
|
|
303
536
|
await Promise.all([
|
|
304
537
|
this.storageUtility.deleteAllUserData(sessionId, { secure: false }),
|
|
305
538
|
this.storageUtility.deleteAllUserData(sessionId, { secure: true }),
|
|
@@ -324,17 +557,52 @@ class SessionInfoManager extends solidClientAuthnCore.SessionInfoManagerBase {
|
|
|
324
557
|
issuer,
|
|
325
558
|
clientAppId: clientId,
|
|
326
559
|
clientAppSecret: clientSecret,
|
|
560
|
+
// Default the token type to DPoP if unspecified.
|
|
327
561
|
tokenType: tokenType !== null && tokenType !== void 0 ? tokenType : "DPoP",
|
|
328
562
|
};
|
|
329
563
|
}
|
|
564
|
+
/**
|
|
565
|
+
* This function removes all session-related information from storage.
|
|
566
|
+
* @param sessionId the session identifier
|
|
567
|
+
* @param storage the storage where session info is stored
|
|
568
|
+
* @hidden
|
|
569
|
+
*/
|
|
330
570
|
async clear(sessionId) {
|
|
331
571
|
return clear(sessionId, this.storageUtility);
|
|
332
572
|
}
|
|
333
573
|
}
|
|
334
574
|
|
|
575
|
+
//
|
|
576
|
+
// Copyright Inrupt Inc.
|
|
577
|
+
//
|
|
578
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
579
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
580
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
581
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
582
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
583
|
+
// subject to the following conditions:
|
|
584
|
+
//
|
|
585
|
+
// The above copyright notice and this permission notice shall be included in
|
|
586
|
+
// all copies or substantial portions of the Software.
|
|
587
|
+
//
|
|
588
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
589
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
590
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
591
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
592
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
593
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
594
|
+
//
|
|
595
|
+
/**
|
|
596
|
+
* This class handles redirect IRIs without any query params, and returns an unauthenticated
|
|
597
|
+
* session. It serves as a fallback so that consuming libraries don't have to test
|
|
598
|
+
* for the query params themselves, and can always try to use them as a redirect IRI.
|
|
599
|
+
* @hidden
|
|
600
|
+
*/
|
|
335
601
|
class FallbackRedirectHandler {
|
|
336
602
|
async canHandle(redirectUrl) {
|
|
337
603
|
try {
|
|
604
|
+
// The next URL object is built for validating it.
|
|
605
|
+
// eslint-disable-next-line no-new
|
|
338
606
|
new URL(redirectUrl);
|
|
339
607
|
return true;
|
|
340
608
|
}
|
|
@@ -342,12 +610,44 @@ class FallbackRedirectHandler {
|
|
|
342
610
|
throw new Error(`[${redirectUrl}] is not a valid URL, and cannot be used as a redirect URL: ${e}`);
|
|
343
611
|
}
|
|
344
612
|
}
|
|
345
|
-
async handle(
|
|
613
|
+
async handle(
|
|
614
|
+
// The argument is ignored, but must be present to implement the interface
|
|
615
|
+
_redirectUrl) {
|
|
346
616
|
return solidClientAuthnCore.getUnauthenticatedSession();
|
|
347
617
|
}
|
|
348
618
|
}
|
|
349
619
|
|
|
620
|
+
//
|
|
621
|
+
// Copyright Inrupt Inc.
|
|
622
|
+
//
|
|
623
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
624
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
625
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
626
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
627
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
628
|
+
// subject to the following conditions:
|
|
629
|
+
//
|
|
630
|
+
// The above copyright notice and this permission notice shall be included in
|
|
631
|
+
// all copies or substantial portions of the Software.
|
|
632
|
+
//
|
|
633
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
634
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
635
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
636
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
637
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
638
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
639
|
+
//
|
|
640
|
+
/**
|
|
641
|
+
* @hidden
|
|
642
|
+
* @packageDocumentation
|
|
643
|
+
*/
|
|
644
|
+
// FIXME: The following doesn't work in the browser, it results in all the fetches
|
|
645
|
+
// being unauthenticated. This should be looked into when migrating to universal-fetch.
|
|
646
|
+
// import { fetch } from "cross-fetch";
|
|
350
647
|
const globalFetch = (...args) => universalFetch.fetch.call(globalThis, ...args);
|
|
648
|
+
/**
|
|
649
|
+
* @hidden
|
|
650
|
+
*/
|
|
351
651
|
class AuthCodeRedirectHandler {
|
|
352
652
|
constructor(storageUtility, sessionInfoManager, issuerConfigFetcher, clientRegistrar, tokerRefresher) {
|
|
353
653
|
this.storageUtility = storageUtility;
|
|
@@ -397,10 +697,15 @@ class AuthCodeRedirectHandler {
|
|
|
397
697
|
if (isDpop) {
|
|
398
698
|
tokens = await oidcClientExt.getDpopToken(issuerConfig, client, {
|
|
399
699
|
grantType: "authorization_code",
|
|
700
|
+
// We rely on our 'canHandle' function checking that the OAuth 'code'
|
|
701
|
+
// parameter is present in our query string.
|
|
400
702
|
code: url.searchParams.get("code"),
|
|
401
703
|
codeVerifier,
|
|
402
704
|
redirectUrl: storedRedirectIri,
|
|
403
705
|
});
|
|
706
|
+
// Delete oidc-client-specific session information from storage. This is
|
|
707
|
+
// done automatically when retrieving a bearer token, but since the DPoP
|
|
708
|
+
// binding uses our custom code, this needs to be done manually.
|
|
404
709
|
window.localStorage.removeItem(`oidc.${oauthState}`);
|
|
405
710
|
}
|
|
406
711
|
else {
|
|
@@ -441,12 +746,58 @@ class AuthCodeRedirectHandler {
|
|
|
441
746
|
}
|
|
442
747
|
}
|
|
443
748
|
|
|
749
|
+
//
|
|
750
|
+
// Copyright Inrupt Inc.
|
|
751
|
+
//
|
|
752
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
753
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
754
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
755
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
756
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
757
|
+
// subject to the following conditions:
|
|
758
|
+
//
|
|
759
|
+
// The above copyright notice and this permission notice shall be included in
|
|
760
|
+
// all copies or substantial portions of the Software.
|
|
761
|
+
//
|
|
762
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
763
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
764
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
765
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
766
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
767
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
768
|
+
//
|
|
769
|
+
/**
|
|
770
|
+
* @hidden
|
|
771
|
+
*/
|
|
444
772
|
class AggregateRedirectHandler extends solidClientAuthnCore.AggregateHandler {
|
|
445
773
|
constructor(redirectHandlers) {
|
|
446
774
|
super(redirectHandlers);
|
|
447
775
|
}
|
|
448
776
|
}
|
|
449
777
|
|
|
778
|
+
//
|
|
779
|
+
// Copyright Inrupt Inc.
|
|
780
|
+
//
|
|
781
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
782
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
783
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
784
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
785
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
786
|
+
// subject to the following conditions:
|
|
787
|
+
//
|
|
788
|
+
// The above copyright notice and this permission notice shall be included in
|
|
789
|
+
// all copies or substantial portions of the Software.
|
|
790
|
+
//
|
|
791
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
792
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
793
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
794
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
795
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
796
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
797
|
+
//
|
|
798
|
+
/**
|
|
799
|
+
* @hidden
|
|
800
|
+
*/
|
|
450
801
|
class BrowserStorage {
|
|
451
802
|
get storage() {
|
|
452
803
|
return window.localStorage;
|
|
@@ -462,6 +813,29 @@ class BrowserStorage {
|
|
|
462
813
|
}
|
|
463
814
|
}
|
|
464
815
|
|
|
816
|
+
//
|
|
817
|
+
// Copyright Inrupt Inc.
|
|
818
|
+
//
|
|
819
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
820
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
821
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
822
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
823
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
824
|
+
// subject to the following conditions:
|
|
825
|
+
//
|
|
826
|
+
// The above copyright notice and this permission notice shall be included in
|
|
827
|
+
// all copies or substantial portions of the Software.
|
|
828
|
+
//
|
|
829
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
830
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
831
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
832
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
833
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
834
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
835
|
+
//
|
|
836
|
+
/**
|
|
837
|
+
* @hidden
|
|
838
|
+
*/
|
|
465
839
|
class Redirector {
|
|
466
840
|
redirect(redirectUrl, options) {
|
|
467
841
|
if (options && options.handleRedirect) {
|
|
@@ -476,19 +850,49 @@ class Redirector {
|
|
|
476
850
|
}
|
|
477
851
|
}
|
|
478
852
|
|
|
853
|
+
//
|
|
854
|
+
// Copyright Inrupt Inc.
|
|
855
|
+
//
|
|
856
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
857
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
858
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
859
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
860
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
861
|
+
// subject to the following conditions:
|
|
862
|
+
//
|
|
863
|
+
// The above copyright notice and this permission notice shall be included in
|
|
864
|
+
// all copies or substantial portions of the Software.
|
|
865
|
+
//
|
|
866
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
867
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
868
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
869
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
870
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
871
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
872
|
+
//
|
|
873
|
+
/**
|
|
874
|
+
* @hidden
|
|
875
|
+
*/
|
|
479
876
|
class ClientRegistrar {
|
|
480
877
|
constructor(storageUtility) {
|
|
481
878
|
this.storageUtility = storageUtility;
|
|
482
879
|
this.storageUtility = storageUtility;
|
|
483
880
|
}
|
|
484
881
|
async getClient(options, issuerConfig) {
|
|
485
|
-
|
|
882
|
+
// If client secret and/or client id are stored in storage, use those.
|
|
883
|
+
const [storedClientId, storedClientSecret,
|
|
884
|
+
// storedClientName,
|
|
885
|
+
] = await Promise.all([
|
|
486
886
|
this.storageUtility.getForUser(options.sessionId, "clientId", {
|
|
487
887
|
secure: false,
|
|
488
888
|
}),
|
|
489
889
|
this.storageUtility.getForUser(options.sessionId, "clientSecret", {
|
|
490
890
|
secure: false,
|
|
491
891
|
}),
|
|
892
|
+
// this.storageUtility.getForUser(options.sessionId, "clientName", {
|
|
893
|
+
// // FIXME: figure out how to persist secure storage at reload
|
|
894
|
+
// secure: false,
|
|
895
|
+
// }),
|
|
492
896
|
]);
|
|
493
897
|
if (storedClientId) {
|
|
494
898
|
return {
|
|
@@ -499,6 +903,7 @@ class ClientRegistrar {
|
|
|
499
903
|
}
|
|
500
904
|
try {
|
|
501
905
|
const registeredClient = await oidcClientExt.registerClient(options, issuerConfig);
|
|
906
|
+
// Save info
|
|
502
907
|
const infoToSave = {
|
|
503
908
|
clientId: registeredClient.clientId,
|
|
504
909
|
};
|
|
@@ -510,6 +915,9 @@ class ClientRegistrar {
|
|
|
510
915
|
registeredClient.idTokenSignedResponseAlg;
|
|
511
916
|
}
|
|
512
917
|
await this.storageUtility.setForUser(options.sessionId, infoToSave, {
|
|
918
|
+
// FIXME: figure out how to persist secure storage at reload
|
|
919
|
+
// Otherwise, the client info cannot be retrieved from storage, and
|
|
920
|
+
// the lib tries to re-register the client on each fetch
|
|
513
921
|
secure: false,
|
|
514
922
|
});
|
|
515
923
|
return registeredClient;
|
|
@@ -520,9 +928,36 @@ class ClientRegistrar {
|
|
|
520
928
|
}
|
|
521
929
|
}
|
|
522
930
|
|
|
931
|
+
//
|
|
932
|
+
// Copyright Inrupt Inc.
|
|
933
|
+
//
|
|
934
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
935
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
936
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
937
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
938
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
939
|
+
// subject to the following conditions:
|
|
940
|
+
//
|
|
941
|
+
// The above copyright notice and this permission notice shall be included in
|
|
942
|
+
// all copies or substantial portions of the Software.
|
|
943
|
+
//
|
|
944
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
945
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
946
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
947
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
948
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
949
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
950
|
+
//
|
|
951
|
+
/**
|
|
952
|
+
* This class handles redirect IRIs without any query params, and returns an unauthenticated
|
|
953
|
+
* session. It serves as a fallback so that consuming libraries don't have to test
|
|
954
|
+
* for the query params themselves, and can always try to use them as a redirect IRI.
|
|
955
|
+
* @hidden
|
|
956
|
+
*/
|
|
523
957
|
class ErrorOidcHandler {
|
|
524
958
|
async canHandle(redirectUrl) {
|
|
525
959
|
try {
|
|
960
|
+
// eslint-disable-next-line no-new
|
|
526
961
|
return new URL(redirectUrl).searchParams.has("error");
|
|
527
962
|
}
|
|
528
963
|
catch (e) {
|
|
@@ -540,6 +975,32 @@ class ErrorOidcHandler {
|
|
|
540
975
|
}
|
|
541
976
|
}
|
|
542
977
|
|
|
978
|
+
//
|
|
979
|
+
// Copyright Inrupt Inc.
|
|
980
|
+
//
|
|
981
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
982
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
983
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
984
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
985
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
986
|
+
// subject to the following conditions:
|
|
987
|
+
//
|
|
988
|
+
// The above copyright notice and this permission notice shall be included in
|
|
989
|
+
// all copies or substantial portions of the Software.
|
|
990
|
+
//
|
|
991
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
992
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
993
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
994
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
995
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
996
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
997
|
+
//
|
|
998
|
+
// Some identifiers are not in camelcase on purpose, as they are named using the
|
|
999
|
+
// official names from the OIDC/OAuth2 specifications.
|
|
1000
|
+
/* eslint-disable camelcase */
|
|
1001
|
+
/**
|
|
1002
|
+
* @hidden
|
|
1003
|
+
*/
|
|
543
1004
|
class TokenRefresher {
|
|
544
1005
|
constructor(storageUtility, issuerConfigFetcher, clientRegistrar) {
|
|
545
1006
|
this.storageUtility = storageUtility;
|
|
@@ -551,8 +1012,10 @@ class TokenRefresher {
|
|
|
551
1012
|
}
|
|
552
1013
|
async refresh(sessionId, refreshToken, dpopKey, eventEmitter) {
|
|
553
1014
|
const oidcContext = await solidClientAuthnCore.loadOidcContextFromStorage(sessionId, this.storageUtility, this.issuerConfigFetcher);
|
|
1015
|
+
// This should also retrieve the client from storage
|
|
554
1016
|
const clientInfo = await this.clientRegistrar.getClient({ sessionId }, oidcContext.issuerConfig);
|
|
555
1017
|
if (refreshToken === undefined) {
|
|
1018
|
+
// TODO: in a next PR, look up storage for a refresh token
|
|
556
1019
|
throw new Error(`Session [${sessionId}] has no refresh token to allow it to refresh its access token.`);
|
|
557
1020
|
}
|
|
558
1021
|
if (oidcContext.dpop && dpopKey === undefined) {
|
|
@@ -569,6 +1032,31 @@ class TokenRefresher {
|
|
|
569
1032
|
}
|
|
570
1033
|
}
|
|
571
1034
|
|
|
1035
|
+
//
|
|
1036
|
+
// Copyright Inrupt Inc.
|
|
1037
|
+
//
|
|
1038
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
1039
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
1040
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
1041
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
1042
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
1043
|
+
// subject to the following conditions:
|
|
1044
|
+
//
|
|
1045
|
+
// The above copyright notice and this permission notice shall be included in
|
|
1046
|
+
// all copies or substantial portions of the Software.
|
|
1047
|
+
//
|
|
1048
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
1049
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
1050
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
1051
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
1052
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
1053
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1054
|
+
//
|
|
1055
|
+
/**
|
|
1056
|
+
*
|
|
1057
|
+
* @param dependencies
|
|
1058
|
+
* @deprecated This function will be removed from the external API in an upcoming release.
|
|
1059
|
+
*/
|
|
572
1060
|
function getClientAuthenticationWithDependencies(dependencies) {
|
|
573
1061
|
const inMemoryStorage = new solidClientAuthnCore.InMemoryStorage();
|
|
574
1062
|
const secureStorage = dependencies.secureStorage || inMemoryStorage;
|
|
@@ -579,22 +1067,69 @@ function getClientAuthenticationWithDependencies(dependencies) {
|
|
|
579
1067
|
const sessionInfoManager = new SessionInfoManager(storageUtility);
|
|
580
1068
|
const tokenRefresher = new TokenRefresher(storageUtility, issuerConfigFetcher, clientRegistrar);
|
|
581
1069
|
const redirector = new Redirector();
|
|
1070
|
+
// make new handler for redirect and login
|
|
582
1071
|
const loginHandler = new OidcLoginHandler(storageUtility, new AuthorizationCodeWithPkceOidcHandler(storageUtility, redirector), issuerConfigFetcher, clientRegistrar);
|
|
583
1072
|
const redirectHandler = new AggregateRedirectHandler([
|
|
584
1073
|
new ErrorOidcHandler(),
|
|
585
1074
|
new AuthCodeRedirectHandler(storageUtility, sessionInfoManager, issuerConfigFetcher, clientRegistrar, tokenRefresher),
|
|
1075
|
+
// This catch-all class will always be able to handle the
|
|
1076
|
+
// redirect IRI, so it must be registered last.
|
|
586
1077
|
new FallbackRedirectHandler(),
|
|
587
1078
|
]);
|
|
588
1079
|
return new ClientAuthentication(loginHandler, redirectHandler, new solidClientAuthnCore.IWaterfallLogoutHandler(sessionInfoManager, redirector), sessionInfoManager, issuerConfigFetcher);
|
|
589
1080
|
}
|
|
590
1081
|
|
|
1082
|
+
//
|
|
1083
|
+
// Copyright Inrupt Inc.
|
|
1084
|
+
//
|
|
1085
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
1086
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
1087
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
1088
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
1089
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
1090
|
+
// subject to the following conditions:
|
|
1091
|
+
//
|
|
1092
|
+
// The above copyright notice and this permission notice shall be included in
|
|
1093
|
+
// all copies or substantial portions of the Software.
|
|
1094
|
+
//
|
|
1095
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
1096
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
1097
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
1098
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
1099
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
1100
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1101
|
+
//
|
|
591
1102
|
const KEY_CURRENT_SESSION = `${solidClientAuthnCore.SOLID_CLIENT_AUTHN_KEY_PREFIX}currentSession`;
|
|
592
1103
|
const KEY_CURRENT_URL = `${solidClientAuthnCore.SOLID_CLIENT_AUTHN_KEY_PREFIX}currentUrl`;
|
|
593
1104
|
|
|
1105
|
+
//
|
|
1106
|
+
// Copyright Inrupt Inc.
|
|
1107
|
+
//
|
|
1108
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
1109
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
1110
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
1111
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
1112
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
1113
|
+
// subject to the following conditions:
|
|
1114
|
+
//
|
|
1115
|
+
// The above copyright notice and this permission notice shall be included in
|
|
1116
|
+
// all copies or substantial portions of the Software.
|
|
1117
|
+
//
|
|
1118
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
1119
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
1120
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
1121
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
1122
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
1123
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1124
|
+
//
|
|
594
1125
|
async function silentlyAuthenticate(sessionId, clientAuthn, session) {
|
|
595
1126
|
var _a;
|
|
596
1127
|
const storedSessionInfo = await clientAuthn.validateCurrentSession(sessionId);
|
|
597
1128
|
if (storedSessionInfo !== null) {
|
|
1129
|
+
// It can be really useful to save the user's current browser location,
|
|
1130
|
+
// so that we can restore it after completing the silent authentication
|
|
1131
|
+
// on incoming redirect. This way, the user is eventually redirected back
|
|
1132
|
+
// to the page they were on and not to the app's redirect page.
|
|
598
1133
|
window.localStorage.setItem(KEY_CURRENT_URL, window.location.href);
|
|
599
1134
|
await clientAuthn.login({
|
|
600
1135
|
sessionId,
|
|
@@ -612,21 +1147,68 @@ async function silentlyAuthenticate(sessionId, clientAuthn, session) {
|
|
|
612
1147
|
function isLoggedIn(sessionInfo) {
|
|
613
1148
|
return !!(sessionInfo === null || sessionInfo === void 0 ? void 0 : sessionInfo.isLoggedIn);
|
|
614
1149
|
}
|
|
1150
|
+
/**
|
|
1151
|
+
* A {@link Session} object represents a user's session on an application. The session holds state, as it stores information enabling acces to private resources after login for instance.
|
|
1152
|
+
*/
|
|
615
1153
|
class Session extends EventEmitter {
|
|
1154
|
+
/**
|
|
1155
|
+
* Session object constructor. Typically called as follows:
|
|
1156
|
+
*
|
|
1157
|
+
* ```typescript
|
|
1158
|
+
* const session = new Session();
|
|
1159
|
+
* ```
|
|
1160
|
+
*
|
|
1161
|
+
* See also [getDefaultSession](https://docs.inrupt.com/developer-tools/api/javascript/solid-client-authn-browser/functions.html#getdefaultsession).
|
|
1162
|
+
*
|
|
1163
|
+
* @param sessionOptions The options enabling the correct instantiation of
|
|
1164
|
+
* the session. Either both storages or clientAuthentication are required. For
|
|
1165
|
+
* more information, see {@link ISessionOptions}.
|
|
1166
|
+
* @param sessionId A string uniquely identifying the session.
|
|
1167
|
+
*
|
|
1168
|
+
*/
|
|
616
1169
|
constructor(sessionOptions = {}, sessionId = undefined) {
|
|
617
1170
|
super();
|
|
618
1171
|
this.tokenRequestInProgress = false;
|
|
1172
|
+
/**
|
|
1173
|
+
* Triggers the login process. Note that this method will redirect the user away from your app.
|
|
1174
|
+
*
|
|
1175
|
+
* @param options Parameter to customize the login behaviour. In particular, two options are mandatory: `options.oidcIssuer`, the user's identity provider, and `options.redirectUrl`, the URL to which the user will be redirected after logging in their identity provider.
|
|
1176
|
+
* @returns This method should redirect the user away from the app: it does not return anything. The login process is completed by {@linkcode handleIncomingRedirect}.
|
|
1177
|
+
*/
|
|
1178
|
+
// Define these functions as properties so that they don't get accidentally re-bound.
|
|
1179
|
+
// Isn't Javascript fun?
|
|
619
1180
|
this.login = async (options) => {
|
|
620
1181
|
var _a;
|
|
621
1182
|
await this.clientAuthentication.login({
|
|
622
1183
|
sessionId: this.info.sessionId,
|
|
623
1184
|
...options,
|
|
1185
|
+
// Defaults the token type to DPoP
|
|
624
1186
|
tokenType: (_a = options.tokenType) !== null && _a !== void 0 ? _a : "DPoP",
|
|
625
1187
|
}, this.events);
|
|
1188
|
+
// `login` redirects the user away from the app,
|
|
1189
|
+
// so unless it throws an error, there is no code that should run afterwards
|
|
1190
|
+
// (since there is no "after" in the lifetime of the script).
|
|
1191
|
+
// Hence, this Promise never resolves:
|
|
626
1192
|
return new Promise(() => { });
|
|
627
1193
|
};
|
|
1194
|
+
/**
|
|
1195
|
+
* Fetches data using available login information. If the user is not logged in, this will behave as a regular `fetch`. The signature of this method is identical to the [canonical `fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
|
|
1196
|
+
*
|
|
1197
|
+
* @param url The URL from which data should be fetched.
|
|
1198
|
+
* @param init Optional parameters customizing the request, by specifying an HTTP method, headers, a body, etc. Follows the [WHATWG Fetch Standard](https://fetch.spec.whatwg.org/).
|
|
1199
|
+
*/
|
|
628
1200
|
this.fetch = (url, init) => this.clientAuthentication.fetch(url, init);
|
|
1201
|
+
/**
|
|
1202
|
+
* An internal logout function, to control whether or not the logout signal
|
|
1203
|
+
* should be sent, i.e. if the logout was user-initiated or is the result of
|
|
1204
|
+
* an external event.
|
|
1205
|
+
*
|
|
1206
|
+
* @hidden
|
|
1207
|
+
*/
|
|
629
1208
|
this.internalLogout = async (emitSignal, options) => {
|
|
1209
|
+
// Clearing this value means that silent refresh will no longer be attempted.
|
|
1210
|
+
// In particular, in the case of a silent authentication error it prevents
|
|
1211
|
+
// from getting stuck in an authentication retries loop.
|
|
630
1212
|
window.localStorage.removeItem(KEY_CURRENT_SESSION);
|
|
631
1213
|
await this.clientAuthentication.logout(this.info.sessionId, options);
|
|
632
1214
|
this.info.isLoggedIn = false;
|
|
@@ -634,7 +1216,50 @@ class Session extends EventEmitter {
|
|
|
634
1216
|
this.events.emit(solidClientAuthnCore.EVENTS.LOGOUT);
|
|
635
1217
|
}
|
|
636
1218
|
};
|
|
1219
|
+
/**
|
|
1220
|
+
* Logs the user out of the application.
|
|
1221
|
+
*
|
|
1222
|
+
* There are 2 types of logout supported by this library,
|
|
1223
|
+
* `app` logout and `idp` logout.
|
|
1224
|
+
*
|
|
1225
|
+
* App logout will log the user out within the application
|
|
1226
|
+
* by clearing any session data from the browser. It does
|
|
1227
|
+
* not log the user out of their Solid identity provider,
|
|
1228
|
+
* and should not redirect the user away.
|
|
1229
|
+
* App logout can be performed as follows:
|
|
1230
|
+
* ```typescript
|
|
1231
|
+
* await session.logout({ logoutType: 'app' });
|
|
1232
|
+
* ```
|
|
1233
|
+
*
|
|
1234
|
+
* IDP logout will log the user out of their Solid identity provider,
|
|
1235
|
+
* and will redirect the user away from the application to do so. In order
|
|
1236
|
+
* for users to be redirected back to `postLogoutUrl` you MUST include the
|
|
1237
|
+
* `postLogoutUrl` value in the `post_logout_redirect_uris` field in the
|
|
1238
|
+
* [Client ID Document](https://docs.inrupt.com/ess/latest/security/authentication/#client-identifier-client-id).
|
|
1239
|
+
* IDP logout can be performed as follows:
|
|
1240
|
+
* ```typescript
|
|
1241
|
+
* await session.logout({
|
|
1242
|
+
* logoutType: 'idp',
|
|
1243
|
+
* // An optional URL to redirect to after logout has completed;
|
|
1244
|
+
* // this MUST match a logout URL listed in the Client ID Document
|
|
1245
|
+
* // of the application that is logged in.
|
|
1246
|
+
* // If the application is logged in with a Client ID that is not
|
|
1247
|
+
* // a URI dereferencing to a Client ID Document then users will
|
|
1248
|
+
* // not be redirected back to the `postLogoutUrl` after logout.
|
|
1249
|
+
* postLogoutUrl: 'https://example.com/logout',
|
|
1250
|
+
* // An optional value to be included in the query parameters
|
|
1251
|
+
* // when the IDP provider redirects the user to the postLogoutRedirectUrl.
|
|
1252
|
+
* state: "my-state"
|
|
1253
|
+
* });
|
|
1254
|
+
* ```
|
|
1255
|
+
*/
|
|
637
1256
|
this.logout = async (options) => this.internalLogout(true, options);
|
|
1257
|
+
/**
|
|
1258
|
+
* Completes the login process by processing the information provided by the
|
|
1259
|
+
* Solid identity provider through redirect.
|
|
1260
|
+
*
|
|
1261
|
+
* @param options See {@see IHandleIncomingRedirectOptions}.
|
|
1262
|
+
*/
|
|
638
1263
|
this.handleIncomingRedirect = async (inputOptions = {}) => {
|
|
639
1264
|
var _a;
|
|
640
1265
|
if (this.info.isLoggedIn) {
|
|
@@ -651,17 +1276,33 @@ class Session extends EventEmitter {
|
|
|
651
1276
|
this.setSessionInfo(sessionInfo);
|
|
652
1277
|
const currentUrl = window.localStorage.getItem(KEY_CURRENT_URL);
|
|
653
1278
|
if (currentUrl === null) {
|
|
1279
|
+
// The login event can only be triggered **after** the user has been
|
|
1280
|
+
// redirected from the IdP with access and ID tokens.
|
|
654
1281
|
this.events.emit(solidClientAuthnCore.EVENTS.LOGIN);
|
|
655
1282
|
}
|
|
656
1283
|
else {
|
|
1284
|
+
// If an URL is stored in local storage, we are being logged in after a
|
|
1285
|
+
// silent authentication, so remove our currently stored URL location
|
|
1286
|
+
// to clean up our state now that we are completing the re-login process.
|
|
657
1287
|
window.localStorage.removeItem(KEY_CURRENT_URL);
|
|
658
1288
|
this.events.emit(solidClientAuthnCore.EVENTS.SESSION_RESTORED, currentUrl);
|
|
659
1289
|
}
|
|
660
1290
|
}
|
|
661
1291
|
else if (options.restorePreviousSession === true) {
|
|
1292
|
+
// Silent authentication happens after a refresh, which means there are no
|
|
1293
|
+
// OAuth params in the current location IRI. It can only succeed if a session
|
|
1294
|
+
// was previously logged in, in which case its ID will be present with a known
|
|
1295
|
+
// identifier in local storage.
|
|
1296
|
+
// Check if we have a locally stored session ID...
|
|
662
1297
|
const storedSessionId = window.localStorage.getItem(KEY_CURRENT_SESSION);
|
|
1298
|
+
// ...if not, then there is no ID token, and so silent authentication cannot happen, but
|
|
1299
|
+
// if we do have a stored session ID, attempt to re-authenticate now silently.
|
|
663
1300
|
if (storedSessionId !== null) {
|
|
664
1301
|
const attemptedSilentAuthentication = await silentlyAuthenticate(storedSessionId, this.clientAuthentication, this);
|
|
1302
|
+
// At this point, we know that the main window will imminently be redirected.
|
|
1303
|
+
// However, this redirect is asynchronous and there is no way to halt execution
|
|
1304
|
+
// until it happens precisely. That's why the current Promise simply does not
|
|
1305
|
+
// resolve.
|
|
665
1306
|
if (attemptedSilentAuthentication) {
|
|
666
1307
|
return new Promise(() => { });
|
|
667
1308
|
}
|
|
@@ -670,6 +1311,10 @@ class Session extends EventEmitter {
|
|
|
670
1311
|
this.tokenRequestInProgress = false;
|
|
671
1312
|
return sessionInfo;
|
|
672
1313
|
};
|
|
1314
|
+
// Until Session no longer implements EventEmitter, this.events is just a proxy
|
|
1315
|
+
// to this (with some interface filtering). When we make the breaking change,
|
|
1316
|
+
// this.events will be a regular EventEmitter (implementing ISessionEventEmitter):
|
|
1317
|
+
// this.events = new EventEmitter();
|
|
673
1318
|
this.events = new Proxy(this, solidClientAuthnCore.buildProxyHandler(Session.prototype, "events only implements ISessionEventListener"));
|
|
674
1319
|
if (sessionOptions.clientAuthentication) {
|
|
675
1320
|
this.clientAuthentication = sessionOptions.clientAuthentication;
|
|
@@ -696,22 +1341,63 @@ class Session extends EventEmitter {
|
|
|
696
1341
|
isLoggedIn: false,
|
|
697
1342
|
};
|
|
698
1343
|
}
|
|
1344
|
+
// When a session is logged in, we want to track its ID in local storage to
|
|
1345
|
+
// enable silent refresh. The current session ID specifically stored in 'localStorage'
|
|
1346
|
+
// (as opposed to using our storage abstraction layer) because it is only
|
|
1347
|
+
// used in a browser-specific mechanism.
|
|
699
1348
|
this.events.on(solidClientAuthnCore.EVENTS.LOGIN, () => window.localStorage.setItem(KEY_CURRENT_SESSION, this.info.sessionId));
|
|
700
1349
|
this.events.on(solidClientAuthnCore.EVENTS.SESSION_EXPIRED, () => this.internalLogout(false));
|
|
701
1350
|
this.events.on(solidClientAuthnCore.EVENTS.ERROR, () => this.internalLogout(false));
|
|
702
1351
|
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Register a callback function to be called when a user completes login.
|
|
1354
|
+
*
|
|
1355
|
+
* The callback is called when {@link handleIncomingRedirect} completes successfully.
|
|
1356
|
+
*
|
|
1357
|
+
* @param callback The function called when a user completes login.
|
|
1358
|
+
* @deprecated Prefer session.events.on(EVENTS.LOGIN, callback)
|
|
1359
|
+
*/
|
|
703
1360
|
onLogin(callback) {
|
|
704
1361
|
this.events.on(solidClientAuthnCore.EVENTS.LOGIN, callback);
|
|
705
1362
|
}
|
|
1363
|
+
/**
|
|
1364
|
+
* Register a callback function to be called when a user logs out:
|
|
1365
|
+
*
|
|
1366
|
+
* @param callback The function called when a user completes logout.
|
|
1367
|
+
* @deprecated Prefer session.events.on(EVENTS.LOGOUT, callback)
|
|
1368
|
+
*/
|
|
706
1369
|
onLogout(callback) {
|
|
707
1370
|
this.events.on(solidClientAuthnCore.EVENTS.LOGOUT, callback);
|
|
708
1371
|
}
|
|
1372
|
+
/**
|
|
1373
|
+
* Register a callback function to be called when a user logs out:
|
|
1374
|
+
*
|
|
1375
|
+
* @param callback The function called when an error occurs.
|
|
1376
|
+
* @since 1.11.0
|
|
1377
|
+
* @deprecated Prefer session.events.on(EVENTS.ERROR, callback)
|
|
1378
|
+
*/
|
|
709
1379
|
onError(callback) {
|
|
710
1380
|
this.events.on(solidClientAuthnCore.EVENTS.ERROR, callback);
|
|
711
1381
|
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Register a callback function to be called when a session is restored.
|
|
1384
|
+
*
|
|
1385
|
+
* Note: the callback will be called with the saved value of the 'current URL'
|
|
1386
|
+
* at the time the session was restored.
|
|
1387
|
+
*
|
|
1388
|
+
* @param callback The function called when a user's already logged-in session is restored, e.g., after a silent authentication is completed after a page refresh.
|
|
1389
|
+
* @deprecated Prefer session.events.on(EVENTS.SESSION_RESTORED, callback)
|
|
1390
|
+
*/
|
|
712
1391
|
onSessionRestore(callback) {
|
|
713
1392
|
this.events.on(solidClientAuthnCore.EVENTS.SESSION_RESTORED, callback);
|
|
714
1393
|
}
|
|
1394
|
+
/**
|
|
1395
|
+
* Register a callback that runs when the session expires and can no longer
|
|
1396
|
+
* make authenticated requests, but following a user logout.
|
|
1397
|
+
* @param callback The function that runs on session expiration.
|
|
1398
|
+
* @since 1.11.0
|
|
1399
|
+
* @deprecated Prefer session.events.on(EVENTS.SESSION_EXPIRED, callback)
|
|
1400
|
+
*/
|
|
715
1401
|
onSessionExpiration(callback) {
|
|
716
1402
|
this.events.on(solidClientAuthnCore.EVENTS.SESSION_EXPIRED, callback);
|
|
717
1403
|
}
|
|
@@ -726,41 +1412,132 @@ class Session extends EventEmitter {
|
|
|
726
1412
|
}
|
|
727
1413
|
}
|
|
728
1414
|
|
|
1415
|
+
//
|
|
1416
|
+
// Copyright Inrupt Inc.
|
|
1417
|
+
//
|
|
1418
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
1419
|
+
// of this software and associated documentation files (the "Software"), to deal in
|
|
1420
|
+
// the Software without restriction, including without limitation the rights to use,
|
|
1421
|
+
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
1422
|
+
// Software, and to permit persons to whom the Software is furnished to do so,
|
|
1423
|
+
// subject to the following conditions:
|
|
1424
|
+
//
|
|
1425
|
+
// The above copyright notice and this permission notice shall be included in
|
|
1426
|
+
// all copies or substantial portions of the Software.
|
|
1427
|
+
//
|
|
1428
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
1429
|
+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
1430
|
+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
1431
|
+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
1432
|
+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
1433
|
+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1434
|
+
//
|
|
729
1435
|
let defaultSession;
|
|
1436
|
+
/**
|
|
1437
|
+
* Obtain the {@link Session} used when not explicitly instantiating one yourself.
|
|
1438
|
+
*
|
|
1439
|
+
* When using the top-level exports {@link fetch}, {@link login}, {@link logout},
|
|
1440
|
+
* {@link handleIncomingRedirect}, {@link onLogin} and {@link onLogout}, these apply to an
|
|
1441
|
+
* implicitly-instantiated {@link Session}.
|
|
1442
|
+
* This function returns a reference to that Session in order to obtain e.g. the current user's
|
|
1443
|
+
* WebID.
|
|
1444
|
+
* @since 1.3.0
|
|
1445
|
+
*/
|
|
730
1446
|
function getDefaultSession() {
|
|
731
1447
|
if (typeof defaultSession === "undefined") {
|
|
732
1448
|
defaultSession = new Session();
|
|
733
1449
|
}
|
|
734
1450
|
return defaultSession;
|
|
735
1451
|
}
|
|
1452
|
+
/**
|
|
1453
|
+
* This function's signature is equal to `window.fetch`, but if the current user is authenticated
|
|
1454
|
+
* (see [[login]] and [[handleIncomingRedirect]]), requests made using it will include that user's
|
|
1455
|
+
* credentials. If not, this will behave just like the regular `window.fetch`.
|
|
1456
|
+
*
|
|
1457
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch}
|
|
1458
|
+
* @since 1.3.0
|
|
1459
|
+
*/
|
|
1460
|
+
/* eslint-disable-next-line no-shadow */
|
|
736
1461
|
const fetch = (...args) => {
|
|
737
1462
|
const session = getDefaultSession();
|
|
738
1463
|
return session.fetch(...args);
|
|
739
1464
|
};
|
|
1465
|
+
/**
|
|
1466
|
+
* Triggers the login process. Note that this method will redirect the user away from your app.
|
|
1467
|
+
*
|
|
1468
|
+
* @param options Parameter to customize the login behaviour. In particular, two options are mandatory: `options.oidcIssuer`, the user's identity provider, and `options.redirectUrl`, the URL to which the user will be redirected after logging in their identity provider.
|
|
1469
|
+
* @returns This method should redirect the user away from the app: it does not return anything. The login process is completed by [[handleIncomingRedirect]].
|
|
1470
|
+
* @since 1.3.0
|
|
1471
|
+
*/
|
|
740
1472
|
const login = (...args) => {
|
|
741
1473
|
const session = getDefaultSession();
|
|
742
1474
|
return session.login(...args);
|
|
743
1475
|
};
|
|
1476
|
+
/**
|
|
1477
|
+
* Logs the user out of the application.
|
|
1478
|
+
*
|
|
1479
|
+
* By default this does not log the user out of their Solid identity provider.
|
|
1480
|
+
* In order to do so, you must set the logoutType to `idp`. For usage details
|
|
1481
|
+
* see {@link Session.logout}.
|
|
1482
|
+
*
|
|
1483
|
+
* @since 1.3.0
|
|
1484
|
+
*/
|
|
744
1485
|
const logout = (...args) => {
|
|
745
1486
|
const session = getDefaultSession();
|
|
746
1487
|
return session.logout(...args);
|
|
747
1488
|
};
|
|
1489
|
+
/**
|
|
1490
|
+
* Completes the login process by processing the information provided by the Solid identity provider through redirect.
|
|
1491
|
+
*
|
|
1492
|
+
* @param url The URL of the page handling the redirect, including the query parameters — these contain the information to process the login.
|
|
1493
|
+
* @since 1.3.0
|
|
1494
|
+
*/
|
|
748
1495
|
const handleIncomingRedirect = (...args) => {
|
|
749
1496
|
const session = getDefaultSession();
|
|
750
1497
|
return session.handleIncomingRedirect(...args);
|
|
751
1498
|
};
|
|
1499
|
+
/**
|
|
1500
|
+
* Register a callback function to be called when a user completes login.
|
|
1501
|
+
*
|
|
1502
|
+
* The callback is called when {@link handleIncomingRedirect} completes successfully.
|
|
1503
|
+
* @since 1.3.0
|
|
1504
|
+
*
|
|
1505
|
+
* @param callback The function called when a user completes login.
|
|
1506
|
+
* @deprecated Prefer events.on(EVENTS.LOGIN, callback)
|
|
1507
|
+
|
|
1508
|
+
*/
|
|
752
1509
|
const onLogin = (...args) => {
|
|
753
1510
|
const session = getDefaultSession();
|
|
754
1511
|
return session.onLogin(...args);
|
|
755
1512
|
};
|
|
1513
|
+
/**
|
|
1514
|
+
* Register a callback function to be called when a user logs out:
|
|
1515
|
+
*
|
|
1516
|
+
* @param callback The function called when a user completes logout.
|
|
1517
|
+
* @since 1.3.0
|
|
1518
|
+
* @deprecated Prefer events.on(EVENTS.LOGOUT, callback)
|
|
1519
|
+
*
|
|
1520
|
+
*/
|
|
756
1521
|
const onLogout = (...args) => {
|
|
757
1522
|
const session = getDefaultSession();
|
|
758
1523
|
return session.onLogout(...args);
|
|
759
1524
|
};
|
|
1525
|
+
/**
|
|
1526
|
+
* Register a callback function to be called when a session is restored:
|
|
1527
|
+
*
|
|
1528
|
+
* @param callback The function called when a session is restored.
|
|
1529
|
+
* @since 1.3.0
|
|
1530
|
+
* @deprecated Prefer events.on(EVENTS.SESSION_RESTORED, callback)
|
|
1531
|
+
*/
|
|
760
1532
|
const onSessionRestore = (...args) => {
|
|
761
1533
|
const session = getDefaultSession();
|
|
762
1534
|
return session.onSessionRestore(...args);
|
|
763
1535
|
};
|
|
1536
|
+
/**
|
|
1537
|
+
* {@link SessionEventEmitter} instance to subscribe to events by the default session.
|
|
1538
|
+
*
|
|
1539
|
+
* @since 1.14.0
|
|
1540
|
+
*/
|
|
764
1541
|
const events = () => {
|
|
765
1542
|
return getDefaultSession().events;
|
|
766
1543
|
};
|