@inrupt/solid-client-authn-browser 1.16.0 → 1.17.0
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 +4 -13
- package/dist/Session.d.ts +4 -4
- package/dist/dependencies.d.ts +1 -1
- package/dist/index.js +772 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +745 -0
- package/dist/index.mjs.map +1 -0
- package/dist/login/oidc/AggregateOidcHandler.d.ts +2 -1
- package/dist/login/oidc/AggregateRedirectHandler.d.ts +2 -1
- package/dist/login/oidc/ClientRegistrar.d.ts +1 -1
- package/dist/login/oidc/IssuerConfigFetcher.d.ts +1 -1
- package/dist/login/oidc/OidcLoginHandler.d.ts +1 -1
- package/dist/login/oidc/Redirector.d.ts +1 -1
- package/dist/login/oidc/incomingRedirectHandler/AuthCodeRedirectHandler.d.ts +3 -6
- package/dist/login/oidc/incomingRedirectHandler/ErrorOidcHandler.d.ts +1 -1
- package/dist/login/oidc/incomingRedirectHandler/FallbackRedirectHandler.d.ts +1 -1
- package/dist/login/oidc/oidcHandlers/AuthorizationCodeWithPkceOidcHandler.d.ts +3 -6
- package/dist/login/oidc/oidcHandlers/OidcHandlerCanHandleTest.d.ts +1 -1
- package/dist/login/oidc/refresh/TokenRefresher.d.ts +2 -2
- package/dist/logout/buildRpInitiatedLogout.d.ts +2 -0
- package/dist/sessionInfo/SessionInfoManager.d.ts +4 -13
- package/dist/solid-client-authn.bundle.js +32 -2
- package/dist/solid-client-authn.bundle.js.map +1 -0
- package/dist/storage/BrowserStorage.d.ts +1 -1
- package/dist/storage/StorageUtility.d.ts +2 -1
- package/package.json +17 -17
- package/rollup.config.mjs +46 -0
- package/src/ClientAuthentication.spec.ts +4 -4
- package/src/ClientAuthentication.ts +8 -45
- package/src/Session.spec.ts +4 -4
- package/src/Session.ts +44 -13
- package/src/__mocks__/ClientAuthentication.ts +3 -3
- package/src/constant.ts +1 -1
- package/src/defaultSession.spec.ts +1 -1
- package/src/defaultSession.ts +5 -3
- package/src/dependencies.node.spec.ts +1 -1
- package/src/dependencies.spec.ts +1 -1
- package/src/dependencies.ts +10 -5
- package/src/index.browser.spec.ts +1 -1
- package/src/index.browser.ts +1 -1
- package/src/index.spec.ts +1 -1
- package/src/index.ts +1 -1
- package/src/login/__mocks__/LoginHandler.ts +2 -2
- package/src/login/oidc/AggregateOidcHandler.spec.ts +3 -5
- package/src/login/oidc/AggregateOidcHandler.ts +3 -3
- package/src/login/oidc/AggregateRedirectHandler.ts +3 -3
- package/src/login/oidc/ClientRegistrar.spec.ts +1 -1
- package/src/login/oidc/ClientRegistrar.ts +2 -2
- package/src/login/oidc/IssuerConfigFetcher.spec.ts +8 -4
- package/src/login/oidc/IssuerConfigFetcher.ts +7 -3
- package/src/login/oidc/OidcLoginHandler.spec.ts +3 -5
- package/src/login/oidc/OidcLoginHandler.ts +5 -3
- package/src/login/oidc/Redirector.spec.ts +1 -1
- package/src/login/oidc/Redirector.ts +2 -2
- package/src/login/oidc/__mocks__/ClientRegistrar.ts +2 -2
- package/src/login/oidc/__mocks__/IOidcHandler.ts +2 -2
- package/src/login/oidc/__mocks__/IOidcOptions.ts +2 -2
- package/src/login/oidc/__mocks__/IssuerConfigFetcher.ts +2 -2
- package/src/login/oidc/__mocks__/Redirector.ts +2 -2
- package/src/login/oidc/incomingRedirectHandler/AuthCodeRedirectHandler.spec.ts +37 -39
- package/src/login/oidc/incomingRedirectHandler/AuthCodeRedirectHandler.ts +17 -13
- package/src/login/oidc/incomingRedirectHandler/ErrorOidcHandler.spec.ts +3 -4
- package/src/login/oidc/incomingRedirectHandler/ErrorOidcHandler.ts +3 -3
- package/src/login/oidc/incomingRedirectHandler/FallbackRedirectHandler.spec.ts +3 -4
- package/src/login/oidc/incomingRedirectHandler/FallbackRedirectHandler.ts +2 -2
- package/src/login/oidc/oidcHandlers/AuthorizationCodeWithPkceOidcHandler.spec.ts +3 -3
- package/src/login/oidc/oidcHandlers/AuthorizationCodeWithPkceOidcHandler.ts +13 -51
- package/src/login/oidc/oidcHandlers/OidcHandlerCanHandleTest.ts +2 -2
- package/src/login/oidc/refresh/TokenRefresher.spec.ts +7 -6
- package/src/login/oidc/refresh/TokenRefresher.ts +6 -4
- package/src/login/oidc/refresh/__mocks__/TokenRefresher.ts +2 -2
- package/src/logout/{GeneralLogoutHandler.ts → buildRpInitiatedLogout.ts} +21 -20
- package/src/sessionInfo/SessionInfoManager.spec.ts +2 -2
- package/src/sessionInfo/SessionInfoManager.ts +13 -91
- package/src/sessionInfo/__mocks__/SessionInfoManager.ts +2 -2
- package/src/storage/BrowserStorage.spec.ts +1 -1
- package/src/storage/BrowserStorage.ts +2 -2
- package/src/storage/StorageUtility.ts +3 -2
- package/src/storage/__mocks__/LocalStorage.ts +1 -1
- package/src/util/UUIDGenerator.spec.ts +1 -1
- package/src/util/UuidGenerator.ts +1 -1
- package/src/util/__mocks__/UuidGenerator.ts +2 -2
- package/dist/ClientAuthentication.js +0 -80
- package/dist/ClientAuthentication.js.map +0 -1
- package/dist/Session.js +0 -150
- package/dist/Session.js.map +0 -1
- package/dist/constant.js +0 -7
- package/dist/constant.js.map +0 -1
- package/dist/defaultSession.js +0 -52
- package/dist/defaultSession.js.map +0 -1
- package/dist/dependencies.js +0 -41
- package/dist/dependencies.js.map +0 -1
- package/dist/index.browser.js +0 -28
- package/dist/index.browser.js.map +0 -1
- package/dist/login/oidc/AggregateOidcHandler.js +0 -10
- package/dist/login/oidc/AggregateOidcHandler.js.map +0 -1
- package/dist/login/oidc/AggregateRedirectHandler.js +0 -10
- package/dist/login/oidc/AggregateRedirectHandler.js.map +0 -1
- package/dist/login/oidc/ClientRegistrar.js +0 -47
- package/dist/login/oidc/ClientRegistrar.js.map +0 -1
- package/dist/login/oidc/IssuerConfigFetcher.js +0 -124
- package/dist/login/oidc/IssuerConfigFetcher.js.map +0 -1
- package/dist/login/oidc/OidcLoginHandler.js +0 -40
- package/dist/login/oidc/OidcLoginHandler.js.map +0 -1
- package/dist/login/oidc/Redirector.js +0 -17
- package/dist/login/oidc/Redirector.js.map +0 -1
- package/dist/login/oidc/__mocks__/IOidcOptions.d.ts +0 -2
- package/dist/login/oidc/__mocks__/IOidcOptions.js +0 -25
- package/dist/login/oidc/__mocks__/IOidcOptions.js.map +0 -1
- package/dist/login/oidc/incomingRedirectHandler/AuthCodeRedirectHandler.js +0 -91
- package/dist/login/oidc/incomingRedirectHandler/AuthCodeRedirectHandler.js.map +0 -1
- package/dist/login/oidc/incomingRedirectHandler/ErrorOidcHandler.js +0 -26
- package/dist/login/oidc/incomingRedirectHandler/ErrorOidcHandler.js.map +0 -1
- package/dist/login/oidc/incomingRedirectHandler/FallbackRedirectHandler.js +0 -20
- package/dist/login/oidc/incomingRedirectHandler/FallbackRedirectHandler.js.map +0 -1
- package/dist/login/oidc/oidcHandlers/AuthorizationCodeWithPkceOidcHandler.js +0 -56
- package/dist/login/oidc/oidcHandlers/AuthorizationCodeWithPkceOidcHandler.js.map +0 -1
- package/dist/login/oidc/oidcHandlers/OidcHandlerCanHandleTest.js +0 -47
- package/dist/login/oidc/oidcHandlers/OidcHandlerCanHandleTest.js.map +0 -1
- package/dist/login/oidc/refresh/TokenRefresher.js +0 -31
- package/dist/login/oidc/refresh/TokenRefresher.js.map +0 -1
- package/dist/logout/GeneralLogoutHandler.d.ts +0 -7
- package/dist/logout/GeneralLogoutHandler.js +0 -15
- package/dist/logout/GeneralLogoutHandler.js.map +0 -1
- package/dist/sessionInfo/SessionInfoManager.js +0 -96
- package/dist/sessionInfo/SessionInfoManager.js.map +0 -1
- package/dist/solid-client-authn.bundle.js.LICENSE.txt +0 -17
- package/dist/storage/BrowserStorage.js +0 -18
- package/dist/storage/BrowserStorage.js.map +0 -1
- package/dist/storage/StorageUtility.js +0 -10
- package/dist/storage/StorageUtility.js.map +0 -1
- package/dist/util/UuidGenerator.js +0 -10
- package/dist/util/UuidGenerator.js.map +0 -1
- package/licenses.csv +0 -147
- package/src/logout/GeneralLogoutHandler.spec.ts +0 -72
- package/src/logout/__mocks__/LogoutHandler.ts +0 -38
- package/webpack.browser.js +0 -39
- package/webpack.common.js +0 -42
- package/webpack.config.js +0 -28
- package/webpack.dev.js +0 -37
- package/webpack.prod.js +0 -23
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,745 @@
|
|
|
1
|
+
import { StorageUtility, ClientAuthentication as ClientAuthentication$1, isValidRedirectUrl, EVENTS, ConfigurationError, handleRegistration, AuthorizationCodeWithPkceOidcHandlerBase, DEFAULT_SCOPES, SessionInfoManagerBase, isSupportedTokenType, clear as clear$1, getUnauthenticatedSession, loadOidcContextFromStorage, buildAuthenticatedFetch, maybeBuildRpInitiatedLogout, AggregateHandler, InMemoryStorage, IWaterfallLogoutHandler, SOLID_CLIENT_AUTHN_KEY_PREFIX, buildProxyHandler } from '@inrupt/solid-client-authn-core';
|
|
2
|
+
export { ConfigurationError, EVENTS, InMemoryStorage, NotImplementedError } from '@inrupt/solid-client-authn-core';
|
|
3
|
+
import { v4 } from 'uuid';
|
|
4
|
+
import EventEmitter from 'events';
|
|
5
|
+
import { removeOidcQueryParam, OidcClient, clearOidcPersistentStorage, getDpopToken, getBearerToken, registerClient, refresh } from '@inrupt/oidc-client-ext';
|
|
6
|
+
import { fetch as fetch$1 } from '@inrupt/universal-fetch';
|
|
7
|
+
|
|
8
|
+
class StorageUtilityBrowser extends StorageUtility {
|
|
9
|
+
constructor(secureStorage, insecureStorage) {
|
|
10
|
+
super(secureStorage, insecureStorage);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class ClientAuthentication extends ClientAuthentication$1 {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
this.login = async (options, eventEmitter) => {
|
|
18
|
+
var _a, _b;
|
|
19
|
+
await this.sessionInfoManager.clear(options.sessionId);
|
|
20
|
+
const redirectUrl = (_a = options.redirectUrl) !== null && _a !== void 0 ? _a : removeOidcQueryParam(window.location.href);
|
|
21
|
+
if (!isValidRedirectUrl(redirectUrl)) {
|
|
22
|
+
throw new Error(`${redirectUrl} is not a valid redirect URL, it is either a malformed IRI or it includes a hash fragment.`);
|
|
23
|
+
}
|
|
24
|
+
await this.loginHandler.handle({
|
|
25
|
+
...options,
|
|
26
|
+
redirectUrl,
|
|
27
|
+
clientName: (_b = options.clientName) !== null && _b !== void 0 ? _b : options.clientId,
|
|
28
|
+
eventEmitter,
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
this.validateCurrentSession = async (currentSessionId) => {
|
|
32
|
+
const sessionInfo = await this.sessionInfoManager.get(currentSessionId);
|
|
33
|
+
if (sessionInfo === undefined ||
|
|
34
|
+
sessionInfo.clientAppId === undefined ||
|
|
35
|
+
sessionInfo.issuer === undefined) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return sessionInfo;
|
|
39
|
+
};
|
|
40
|
+
this.handleIncomingRedirect = async (url, eventEmitter) => {
|
|
41
|
+
try {
|
|
42
|
+
const redirectInfo = await this.redirectHandler.handle(url, eventEmitter);
|
|
43
|
+
this.fetch = redirectInfo.fetch.bind(window);
|
|
44
|
+
this.boundLogout = redirectInfo.getLogoutUrl;
|
|
45
|
+
this.cleanUrlAfterRedirect(url);
|
|
46
|
+
return {
|
|
47
|
+
isLoggedIn: redirectInfo.isLoggedIn,
|
|
48
|
+
webId: redirectInfo.webId,
|
|
49
|
+
sessionId: redirectInfo.sessionId,
|
|
50
|
+
expirationDate: redirectInfo.expirationDate,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
this.cleanUrlAfterRedirect(url);
|
|
55
|
+
eventEmitter.emit(EVENTS.ERROR, "redirect", err);
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
cleanUrlAfterRedirect(url) {
|
|
61
|
+
const cleanedUpUrl = new URL(url);
|
|
62
|
+
cleanedUpUrl.searchParams.delete("state");
|
|
63
|
+
cleanedUpUrl.searchParams.delete("code");
|
|
64
|
+
cleanedUpUrl.searchParams.delete("id_token");
|
|
65
|
+
cleanedUpUrl.searchParams.delete("access_token");
|
|
66
|
+
cleanedUpUrl.searchParams.delete("error");
|
|
67
|
+
cleanedUpUrl.searchParams.delete("error_description");
|
|
68
|
+
cleanedUpUrl.searchParams.delete("iss");
|
|
69
|
+
window.history.replaceState(null, "", cleanedUpUrl.toString());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function hasIssuer(options) {
|
|
74
|
+
return typeof options.oidcIssuer === "string";
|
|
75
|
+
}
|
|
76
|
+
function hasRedirectUrl(options) {
|
|
77
|
+
return typeof options.redirectUrl === "string";
|
|
78
|
+
}
|
|
79
|
+
class OidcLoginHandler {
|
|
80
|
+
constructor(storageUtility, oidcHandler, issuerConfigFetcher, clientRegistrar) {
|
|
81
|
+
this.storageUtility = storageUtility;
|
|
82
|
+
this.oidcHandler = oidcHandler;
|
|
83
|
+
this.issuerConfigFetcher = issuerConfigFetcher;
|
|
84
|
+
this.clientRegistrar = clientRegistrar;
|
|
85
|
+
}
|
|
86
|
+
async canHandle(options) {
|
|
87
|
+
return hasIssuer(options) && hasRedirectUrl(options);
|
|
88
|
+
}
|
|
89
|
+
async handle(options) {
|
|
90
|
+
if (!hasIssuer(options)) {
|
|
91
|
+
throw new ConfigurationError(`OidcLoginHandler requires an OIDC issuer: missing property 'oidcIssuer' in ${JSON.stringify(options)}`);
|
|
92
|
+
}
|
|
93
|
+
if (!hasRedirectUrl(options)) {
|
|
94
|
+
throw new ConfigurationError(`OidcLoginHandler requires a redirect URL: missing property 'redirectUrl' in ${JSON.stringify(options)}`);
|
|
95
|
+
}
|
|
96
|
+
const issuerConfig = await this.issuerConfigFetcher.fetchConfig(options.oidcIssuer);
|
|
97
|
+
const clientRegistration = await handleRegistration(options, issuerConfig, this.storageUtility, this.clientRegistrar);
|
|
98
|
+
const OidcOptions = {
|
|
99
|
+
issuer: issuerConfig.issuer,
|
|
100
|
+
dpop: options.tokenType.toLowerCase() === "dpop",
|
|
101
|
+
...options,
|
|
102
|
+
issuerConfiguration: issuerConfig,
|
|
103
|
+
client: clientRegistration,
|
|
104
|
+
};
|
|
105
|
+
return this.oidcHandler.handle(OidcOptions);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
class AuthorizationCodeWithPkceOidcHandler extends AuthorizationCodeWithPkceOidcHandlerBase {
|
|
110
|
+
async handle(oidcLoginOptions) {
|
|
111
|
+
var _a;
|
|
112
|
+
const oidcOptions = {
|
|
113
|
+
authority: oidcLoginOptions.issuer.toString(),
|
|
114
|
+
client_id: oidcLoginOptions.client.clientId,
|
|
115
|
+
client_secret: oidcLoginOptions.client.clientSecret,
|
|
116
|
+
redirect_uri: oidcLoginOptions.redirectUrl.toString(),
|
|
117
|
+
post_logout_redirect_uri: oidcLoginOptions.redirectUrl.toString(),
|
|
118
|
+
response_type: "code",
|
|
119
|
+
scope: DEFAULT_SCOPES,
|
|
120
|
+
filterProtocolClaims: true,
|
|
121
|
+
loadUserInfo: false,
|
|
122
|
+
code_verifier: true,
|
|
123
|
+
prompt: (_a = oidcLoginOptions.prompt) !== null && _a !== void 0 ? _a : "consent",
|
|
124
|
+
};
|
|
125
|
+
const oidcClientLibrary = new OidcClient(oidcOptions);
|
|
126
|
+
try {
|
|
127
|
+
const signingRequest = await oidcClientLibrary.createSigninRequest();
|
|
128
|
+
return await this.handleRedirect({
|
|
129
|
+
oidcLoginOptions,
|
|
130
|
+
state: signingRequest.state._id,
|
|
131
|
+
codeVerifier: signingRequest.state._code_verifier,
|
|
132
|
+
targetUrl: signingRequest.url.toString(),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
console.error(err);
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const WELL_KNOWN_OPENID_CONFIG = ".well-known/openid-configuration";
|
|
143
|
+
const issuerConfigKeyMap = {
|
|
144
|
+
issuer: {
|
|
145
|
+
toKey: "issuer",
|
|
146
|
+
convertToUrl: true,
|
|
147
|
+
},
|
|
148
|
+
authorization_endpoint: {
|
|
149
|
+
toKey: "authorizationEndpoint",
|
|
150
|
+
convertToUrl: true,
|
|
151
|
+
},
|
|
152
|
+
token_endpoint: {
|
|
153
|
+
toKey: "tokenEndpoint",
|
|
154
|
+
convertToUrl: true,
|
|
155
|
+
},
|
|
156
|
+
userinfo_endpoint: {
|
|
157
|
+
toKey: "userinfoEndpoint",
|
|
158
|
+
convertToUrl: true,
|
|
159
|
+
},
|
|
160
|
+
jwks_uri: {
|
|
161
|
+
toKey: "jwksUri",
|
|
162
|
+
convertToUrl: true,
|
|
163
|
+
},
|
|
164
|
+
registration_endpoint: {
|
|
165
|
+
toKey: "registrationEndpoint",
|
|
166
|
+
convertToUrl: true,
|
|
167
|
+
},
|
|
168
|
+
end_session_endpoint: {
|
|
169
|
+
toKey: "endSessionEndpoint",
|
|
170
|
+
convertToUrl: true,
|
|
171
|
+
},
|
|
172
|
+
scopes_supported: { toKey: "scopesSupported" },
|
|
173
|
+
response_types_supported: { toKey: "responseTypesSupported" },
|
|
174
|
+
response_modes_supported: { toKey: "responseModesSupported" },
|
|
175
|
+
grant_types_supported: { toKey: "grantTypesSupported" },
|
|
176
|
+
acr_values_supported: { toKey: "acrValuesSupported" },
|
|
177
|
+
subject_types_supported: { toKey: "subjectTypesSupported" },
|
|
178
|
+
id_token_signing_alg_values_supported: {
|
|
179
|
+
toKey: "idTokenSigningAlgValuesSupported",
|
|
180
|
+
},
|
|
181
|
+
id_token_encryption_alg_values_supported: {
|
|
182
|
+
toKey: "idTokenEncryptionAlgValuesSupported",
|
|
183
|
+
},
|
|
184
|
+
id_token_encryption_enc_values_supported: {
|
|
185
|
+
toKey: "idTokenEncryptionEncValuesSupported",
|
|
186
|
+
},
|
|
187
|
+
userinfo_signing_alg_values_supported: {
|
|
188
|
+
toKey: "userinfoSigningAlgValuesSupported",
|
|
189
|
+
},
|
|
190
|
+
userinfo_encryption_alg_values_supported: {
|
|
191
|
+
toKey: "userinfoEncryptionAlgValuesSupported",
|
|
192
|
+
},
|
|
193
|
+
userinfo_encryption_enc_values_supported: {
|
|
194
|
+
toKey: "userinfoEncryptionEncValuesSupported",
|
|
195
|
+
},
|
|
196
|
+
request_object_signing_alg_values_supported: {
|
|
197
|
+
toKey: "requestObjectSigningAlgValuesSupported",
|
|
198
|
+
},
|
|
199
|
+
request_object_encryption_alg_values_supported: {
|
|
200
|
+
toKey: "requestObjectEncryptionAlgValuesSupported",
|
|
201
|
+
},
|
|
202
|
+
request_object_encryption_enc_values_supported: {
|
|
203
|
+
toKey: "requestObjectEncryptionEncValuesSupported",
|
|
204
|
+
},
|
|
205
|
+
token_endpoint_auth_methods_supported: {
|
|
206
|
+
toKey: "tokenEndpointAuthMethodsSupported",
|
|
207
|
+
},
|
|
208
|
+
token_endpoint_auth_signing_alg_values_supported: {
|
|
209
|
+
toKey: "tokenEndpointAuthSigningAlgValuesSupported",
|
|
210
|
+
},
|
|
211
|
+
display_values_supported: { toKey: "displayValuesSupported" },
|
|
212
|
+
claim_types_supported: { toKey: "claimTypesSupported" },
|
|
213
|
+
claims_supported: { toKey: "claimsSupported" },
|
|
214
|
+
service_documentation: { toKey: "serviceDocumentation" },
|
|
215
|
+
claims_locales_supported: { toKey: "claimsLocalesSupported" },
|
|
216
|
+
ui_locales_supported: { toKey: "uiLocalesSupported" },
|
|
217
|
+
claims_parameter_supported: { toKey: "claimsParameterSupported" },
|
|
218
|
+
request_parameter_supported: { toKey: "requestParameterSupported" },
|
|
219
|
+
request_uri_parameter_supported: { toKey: "requestUriParameterSupported" },
|
|
220
|
+
require_request_uri_registration: { toKey: "requireRequestUriRegistration" },
|
|
221
|
+
op_policy_uri: {
|
|
222
|
+
toKey: "opPolicyUri",
|
|
223
|
+
convertToUrl: true,
|
|
224
|
+
},
|
|
225
|
+
op_tos_uri: {
|
|
226
|
+
toKey: "opTosUri",
|
|
227
|
+
convertToUrl: true,
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
function processConfig(config) {
|
|
231
|
+
const parsedConfig = {};
|
|
232
|
+
Object.keys(config).forEach((key) => {
|
|
233
|
+
if (issuerConfigKeyMap[key]) {
|
|
234
|
+
parsedConfig[issuerConfigKeyMap[key].toKey] = config[key];
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
if (!Array.isArray(parsedConfig.scopesSupported)) {
|
|
238
|
+
parsedConfig.scopesSupported = ["openid"];
|
|
239
|
+
}
|
|
240
|
+
return parsedConfig;
|
|
241
|
+
}
|
|
242
|
+
class IssuerConfigFetcher {
|
|
243
|
+
constructor(storageUtility) {
|
|
244
|
+
this.storageUtility = storageUtility;
|
|
245
|
+
}
|
|
246
|
+
static getLocalStorageKey(issuer) {
|
|
247
|
+
return `issuerConfig:${issuer}`;
|
|
248
|
+
}
|
|
249
|
+
async fetchConfig(issuer) {
|
|
250
|
+
let issuerConfig;
|
|
251
|
+
const openIdConfigUrl = new URL(WELL_KNOWN_OPENID_CONFIG, issuer.endsWith("/") ? issuer : `${issuer}/`).href;
|
|
252
|
+
const issuerConfigRequestBody = await fetch$1(openIdConfigUrl);
|
|
253
|
+
try {
|
|
254
|
+
issuerConfig = processConfig(await issuerConfigRequestBody.json());
|
|
255
|
+
}
|
|
256
|
+
catch (err) {
|
|
257
|
+
throw new ConfigurationError(`[${issuer.toString()}] has an invalid configuration: ${err.message}`);
|
|
258
|
+
}
|
|
259
|
+
await this.storageUtility.set(IssuerConfigFetcher.getLocalStorageKey(issuer), JSON.stringify(issuerConfig));
|
|
260
|
+
return issuerConfig;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async function clear(sessionId, storage) {
|
|
265
|
+
await clear$1(sessionId, storage);
|
|
266
|
+
await clearOidcPersistentStorage();
|
|
267
|
+
}
|
|
268
|
+
class SessionInfoManager extends SessionInfoManagerBase {
|
|
269
|
+
async get(sessionId) {
|
|
270
|
+
var _a;
|
|
271
|
+
const isLoggedIn = await this.storageUtility.getForUser(sessionId, "isLoggedIn", {
|
|
272
|
+
secure: true,
|
|
273
|
+
});
|
|
274
|
+
const webId = await this.storageUtility.getForUser(sessionId, "webId", {
|
|
275
|
+
secure: true,
|
|
276
|
+
});
|
|
277
|
+
const clientId = await this.storageUtility.getForUser(sessionId, "clientId", {
|
|
278
|
+
secure: false,
|
|
279
|
+
});
|
|
280
|
+
const clientSecret = await this.storageUtility.getForUser(sessionId, "clientSecret", {
|
|
281
|
+
secure: false,
|
|
282
|
+
});
|
|
283
|
+
const redirectUrl = await this.storageUtility.getForUser(sessionId, "redirectUrl", {
|
|
284
|
+
secure: false,
|
|
285
|
+
});
|
|
286
|
+
const refreshToken = await this.storageUtility.getForUser(sessionId, "refreshToken", {
|
|
287
|
+
secure: true,
|
|
288
|
+
});
|
|
289
|
+
const issuer = await this.storageUtility.getForUser(sessionId, "issuer", {
|
|
290
|
+
secure: false,
|
|
291
|
+
});
|
|
292
|
+
const tokenType = (_a = (await this.storageUtility.getForUser(sessionId, "tokenType", {
|
|
293
|
+
secure: false,
|
|
294
|
+
}))) !== null && _a !== void 0 ? _a : "DPoP";
|
|
295
|
+
if (!isSupportedTokenType(tokenType)) {
|
|
296
|
+
throw new Error(`Tokens of type [${tokenType}] are not supported.`);
|
|
297
|
+
}
|
|
298
|
+
if (clientId === undefined &&
|
|
299
|
+
isLoggedIn === undefined &&
|
|
300
|
+
webId === undefined &&
|
|
301
|
+
refreshToken === undefined) {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
return {
|
|
305
|
+
sessionId,
|
|
306
|
+
webId,
|
|
307
|
+
isLoggedIn: isLoggedIn === "true",
|
|
308
|
+
redirectUrl,
|
|
309
|
+
refreshToken,
|
|
310
|
+
issuer,
|
|
311
|
+
clientAppId: clientId,
|
|
312
|
+
clientAppSecret: clientSecret,
|
|
313
|
+
tokenType,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
async clear(sessionId) {
|
|
317
|
+
return clear(sessionId, this.storageUtility);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
class FallbackRedirectHandler {
|
|
322
|
+
async canHandle(redirectUrl) {
|
|
323
|
+
try {
|
|
324
|
+
new URL(redirectUrl);
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
catch (e) {
|
|
328
|
+
throw new Error(`[${redirectUrl}] is not a valid URL, and cannot be used as a redirect URL: ${e}`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
async handle(_redirectUrl) {
|
|
332
|
+
return getUnauthenticatedSession();
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
class AuthCodeRedirectHandler {
|
|
337
|
+
constructor(storageUtility, sessionInfoManager, issuerConfigFetcher, clientRegistrar, tokerRefresher) {
|
|
338
|
+
this.storageUtility = storageUtility;
|
|
339
|
+
this.sessionInfoManager = sessionInfoManager;
|
|
340
|
+
this.issuerConfigFetcher = issuerConfigFetcher;
|
|
341
|
+
this.clientRegistrar = clientRegistrar;
|
|
342
|
+
this.tokerRefresher = tokerRefresher;
|
|
343
|
+
}
|
|
344
|
+
async canHandle(redirectUrl) {
|
|
345
|
+
try {
|
|
346
|
+
const myUrl = new URL(redirectUrl);
|
|
347
|
+
return (myUrl.searchParams.get("code") !== null &&
|
|
348
|
+
myUrl.searchParams.get("state") !== null);
|
|
349
|
+
}
|
|
350
|
+
catch (e) {
|
|
351
|
+
throw new Error(`[${redirectUrl}] is not a valid URL, and cannot be used as a redirect URL: ${e}`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
async handle(redirectUrl, eventEmitter) {
|
|
355
|
+
if (!(await this.canHandle(redirectUrl))) {
|
|
356
|
+
throw new Error(`AuthCodeRedirectHandler cannot handle [${redirectUrl}]: it is missing one of [code, state].`);
|
|
357
|
+
}
|
|
358
|
+
const url = new URL(redirectUrl);
|
|
359
|
+
const oauthState = url.searchParams.get("state");
|
|
360
|
+
const storedSessionId = (await this.storageUtility.getForUser(oauthState, "sessionId", {
|
|
361
|
+
errorIfNull: true,
|
|
362
|
+
}));
|
|
363
|
+
const { issuerConfig, codeVerifier, redirectUrl: storedRedirectIri, dpop: isDpop, } = await loadOidcContextFromStorage(storedSessionId, this.storageUtility, this.issuerConfigFetcher);
|
|
364
|
+
const iss = url.searchParams.get("iss");
|
|
365
|
+
if (typeof iss === "string" && iss !== issuerConfig.issuer) {
|
|
366
|
+
throw new Error(`The value of the iss parameter (${iss}) does not match the issuer identifier of the authorization server (${issuerConfig.issuer}). See [rfc9207](https://www.rfc-editor.org/rfc/rfc9207.html#section-2.3-3.1.1)`);
|
|
367
|
+
}
|
|
368
|
+
if (codeVerifier === undefined) {
|
|
369
|
+
throw new Error(`The code verifier for session ${storedSessionId} is missing from storage.`);
|
|
370
|
+
}
|
|
371
|
+
if (storedRedirectIri === undefined) {
|
|
372
|
+
throw new Error(`The redirect URL for session ${storedSessionId} is missing from storage.`);
|
|
373
|
+
}
|
|
374
|
+
const client = await this.clientRegistrar.getClient({ sessionId: storedSessionId }, issuerConfig);
|
|
375
|
+
let tokens;
|
|
376
|
+
const tokenCreatedAt = Date.now();
|
|
377
|
+
if (isDpop) {
|
|
378
|
+
tokens = await getDpopToken(issuerConfig, client, {
|
|
379
|
+
grantType: "authorization_code",
|
|
380
|
+
code: url.searchParams.get("code"),
|
|
381
|
+
codeVerifier,
|
|
382
|
+
redirectUrl: storedRedirectIri,
|
|
383
|
+
});
|
|
384
|
+
window.localStorage.removeItem(`oidc.${oauthState}`);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
tokens = await getBearerToken(url.toString());
|
|
388
|
+
}
|
|
389
|
+
let refreshOptions;
|
|
390
|
+
if (tokens.refreshToken !== undefined) {
|
|
391
|
+
refreshOptions = {
|
|
392
|
+
sessionId: storedSessionId,
|
|
393
|
+
refreshToken: tokens.refreshToken,
|
|
394
|
+
tokenRefresher: this.tokerRefresher,
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
const authFetch = await buildAuthenticatedFetch(fetch$1, tokens.accessToken, {
|
|
398
|
+
dpopKey: tokens.dpopKey,
|
|
399
|
+
refreshOptions,
|
|
400
|
+
eventEmitter,
|
|
401
|
+
expiresIn: tokens.expiresIn,
|
|
402
|
+
});
|
|
403
|
+
await this.storageUtility.setForUser(storedSessionId, {
|
|
404
|
+
webId: tokens.webId,
|
|
405
|
+
isLoggedIn: "true",
|
|
406
|
+
}, { secure: true });
|
|
407
|
+
const sessionInfo = await this.sessionInfoManager.get(storedSessionId);
|
|
408
|
+
if (!sessionInfo) {
|
|
409
|
+
throw new Error(`Could not retrieve session: [${storedSessionId}].`);
|
|
410
|
+
}
|
|
411
|
+
return Object.assign(sessionInfo, {
|
|
412
|
+
fetch: authFetch,
|
|
413
|
+
getLogoutUrl: maybeBuildRpInitiatedLogout({
|
|
414
|
+
idTokenHint: tokens.idToken,
|
|
415
|
+
endSessionEndpoint: issuerConfig.endSessionEndpoint,
|
|
416
|
+
}),
|
|
417
|
+
expirationDate: typeof tokens.expiresIn === "number"
|
|
418
|
+
? tokenCreatedAt + tokens.expiresIn * 1000
|
|
419
|
+
: undefined,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
class AggregateRedirectHandler extends AggregateHandler {
|
|
425
|
+
constructor(redirectHandlers) {
|
|
426
|
+
super(redirectHandlers);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
class BrowserStorage {
|
|
431
|
+
get storage() {
|
|
432
|
+
return window.localStorage;
|
|
433
|
+
}
|
|
434
|
+
async get(key) {
|
|
435
|
+
return this.storage.getItem(key) || undefined;
|
|
436
|
+
}
|
|
437
|
+
async set(key, value) {
|
|
438
|
+
this.storage.setItem(key, value);
|
|
439
|
+
}
|
|
440
|
+
async delete(key) {
|
|
441
|
+
this.storage.removeItem(key);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
class Redirector {
|
|
446
|
+
redirect(redirectUrl, options) {
|
|
447
|
+
if (options && options.handleRedirect) {
|
|
448
|
+
options.handleRedirect(redirectUrl);
|
|
449
|
+
}
|
|
450
|
+
else if (options && options.redirectByReplacingState) {
|
|
451
|
+
window.history.replaceState({}, "", redirectUrl);
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
window.location.href = redirectUrl;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
class ClientRegistrar {
|
|
460
|
+
constructor(storageUtility) {
|
|
461
|
+
this.storageUtility = storageUtility;
|
|
462
|
+
}
|
|
463
|
+
async getClient(options, issuerConfig) {
|
|
464
|
+
const [storedClientId, storedClientSecret,] = await Promise.all([
|
|
465
|
+
this.storageUtility.getForUser(options.sessionId, "clientId", {
|
|
466
|
+
secure: false,
|
|
467
|
+
}),
|
|
468
|
+
this.storageUtility.getForUser(options.sessionId, "clientSecret", {
|
|
469
|
+
secure: false,
|
|
470
|
+
}),
|
|
471
|
+
]);
|
|
472
|
+
if (storedClientId) {
|
|
473
|
+
return {
|
|
474
|
+
clientId: storedClientId,
|
|
475
|
+
clientSecret: storedClientSecret,
|
|
476
|
+
clientType: "dynamic",
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
try {
|
|
480
|
+
const registeredClient = await registerClient(options, issuerConfig);
|
|
481
|
+
const infoToSave = {
|
|
482
|
+
clientId: registeredClient.clientId,
|
|
483
|
+
};
|
|
484
|
+
if (registeredClient.clientSecret) {
|
|
485
|
+
infoToSave.clientSecret = registeredClient.clientSecret;
|
|
486
|
+
}
|
|
487
|
+
if (registeredClient.idTokenSignedResponseAlg) {
|
|
488
|
+
infoToSave.idTokenSignedResponseAlg =
|
|
489
|
+
registeredClient.idTokenSignedResponseAlg;
|
|
490
|
+
}
|
|
491
|
+
await this.storageUtility.setForUser(options.sessionId, infoToSave, {
|
|
492
|
+
secure: false,
|
|
493
|
+
});
|
|
494
|
+
return registeredClient;
|
|
495
|
+
}
|
|
496
|
+
catch (error) {
|
|
497
|
+
throw new Error(`Client registration failed: [${error}]`);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
class ErrorOidcHandler {
|
|
503
|
+
async canHandle(redirectUrl) {
|
|
504
|
+
try {
|
|
505
|
+
return new URL(redirectUrl).searchParams.has("error");
|
|
506
|
+
}
|
|
507
|
+
catch (e) {
|
|
508
|
+
throw new Error(`[${redirectUrl}] is not a valid URL, and cannot be used as a redirect URL: ${e}`);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
async handle(redirectUrl, eventEmitter) {
|
|
512
|
+
if (eventEmitter !== undefined) {
|
|
513
|
+
const url = new URL(redirectUrl);
|
|
514
|
+
const errorUrl = url.searchParams.get("error");
|
|
515
|
+
const errorDescriptionUrl = url.searchParams.get("error_description");
|
|
516
|
+
eventEmitter.emit(EVENTS.ERROR, errorUrl, errorDescriptionUrl);
|
|
517
|
+
}
|
|
518
|
+
return getUnauthenticatedSession();
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
class TokenRefresher {
|
|
523
|
+
constructor(storageUtility, issuerConfigFetcher, clientRegistrar) {
|
|
524
|
+
this.storageUtility = storageUtility;
|
|
525
|
+
this.issuerConfigFetcher = issuerConfigFetcher;
|
|
526
|
+
this.clientRegistrar = clientRegistrar;
|
|
527
|
+
}
|
|
528
|
+
async refresh(sessionId, refreshToken, dpopKey, eventEmitter) {
|
|
529
|
+
const oidcContext = await loadOidcContextFromStorage(sessionId, this.storageUtility, this.issuerConfigFetcher);
|
|
530
|
+
const clientInfo = await this.clientRegistrar.getClient({ sessionId }, oidcContext.issuerConfig);
|
|
531
|
+
if (refreshToken === undefined) {
|
|
532
|
+
throw new Error(`Session [${sessionId}] has no refresh token to allow it to refresh its access token.`);
|
|
533
|
+
}
|
|
534
|
+
if (oidcContext.dpop && dpopKey === undefined) {
|
|
535
|
+
throw new Error(`For session [${sessionId}], the key bound to the DPoP access token must be provided to refresh said access token.`);
|
|
536
|
+
}
|
|
537
|
+
const tokenSet = await refresh(refreshToken, oidcContext.issuerConfig, clientInfo, dpopKey);
|
|
538
|
+
if (tokenSet.refreshToken !== undefined) {
|
|
539
|
+
eventEmitter === null || eventEmitter === void 0 ? void 0 : eventEmitter.emit(EVENTS.NEW_REFRESH_TOKEN, tokenSet.refreshToken);
|
|
540
|
+
await this.storageUtility.setForUser(sessionId, {
|
|
541
|
+
refreshToken: tokenSet.refreshToken,
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
return tokenSet;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function getClientAuthenticationWithDependencies(dependencies) {
|
|
549
|
+
const inMemoryStorage = new InMemoryStorage();
|
|
550
|
+
const secureStorage = dependencies.secureStorage || inMemoryStorage;
|
|
551
|
+
const insecureStorage = dependencies.insecureStorage || new BrowserStorage();
|
|
552
|
+
const storageUtility = new StorageUtilityBrowser(secureStorage, insecureStorage);
|
|
553
|
+
const issuerConfigFetcher = new IssuerConfigFetcher(storageUtility);
|
|
554
|
+
const clientRegistrar = new ClientRegistrar(storageUtility);
|
|
555
|
+
const sessionInfoManager = new SessionInfoManager(storageUtility);
|
|
556
|
+
const tokenRefresher = new TokenRefresher(storageUtility, issuerConfigFetcher, clientRegistrar);
|
|
557
|
+
const redirector = new Redirector();
|
|
558
|
+
const loginHandler = new OidcLoginHandler(storageUtility, new AuthorizationCodeWithPkceOidcHandler(storageUtility, redirector), issuerConfigFetcher, clientRegistrar);
|
|
559
|
+
const redirectHandler = new AggregateRedirectHandler([
|
|
560
|
+
new ErrorOidcHandler(),
|
|
561
|
+
new AuthCodeRedirectHandler(storageUtility, sessionInfoManager, issuerConfigFetcher, clientRegistrar, tokenRefresher),
|
|
562
|
+
new FallbackRedirectHandler(),
|
|
563
|
+
]);
|
|
564
|
+
return new ClientAuthentication(loginHandler, redirectHandler, new IWaterfallLogoutHandler(sessionInfoManager, redirector), sessionInfoManager, issuerConfigFetcher);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const KEY_CURRENT_SESSION = `${SOLID_CLIENT_AUTHN_KEY_PREFIX}currentSession`;
|
|
568
|
+
const KEY_CURRENT_URL = `${SOLID_CLIENT_AUTHN_KEY_PREFIX}currentUrl`;
|
|
569
|
+
|
|
570
|
+
async function silentlyAuthenticate(sessionId, clientAuthn, session) {
|
|
571
|
+
var _a;
|
|
572
|
+
const storedSessionInfo = await clientAuthn.validateCurrentSession(sessionId);
|
|
573
|
+
if (storedSessionInfo !== null) {
|
|
574
|
+
window.localStorage.setItem(KEY_CURRENT_URL, window.location.href);
|
|
575
|
+
await clientAuthn.login({
|
|
576
|
+
sessionId,
|
|
577
|
+
prompt: "none",
|
|
578
|
+
oidcIssuer: storedSessionInfo.issuer,
|
|
579
|
+
redirectUrl: storedSessionInfo.redirectUrl,
|
|
580
|
+
clientId: storedSessionInfo.clientAppId,
|
|
581
|
+
clientSecret: storedSessionInfo.clientAppSecret,
|
|
582
|
+
tokenType: (_a = storedSessionInfo.tokenType) !== null && _a !== void 0 ? _a : "DPoP",
|
|
583
|
+
}, session.events);
|
|
584
|
+
return true;
|
|
585
|
+
}
|
|
586
|
+
return false;
|
|
587
|
+
}
|
|
588
|
+
function isLoggedIn(sessionInfo) {
|
|
589
|
+
return !!(sessionInfo === null || sessionInfo === void 0 ? void 0 : sessionInfo.isLoggedIn);
|
|
590
|
+
}
|
|
591
|
+
class Session extends EventEmitter {
|
|
592
|
+
constructor(sessionOptions = {}, sessionId = undefined) {
|
|
593
|
+
super();
|
|
594
|
+
this.tokenRequestInProgress = false;
|
|
595
|
+
this.login = async (options) => {
|
|
596
|
+
var _a;
|
|
597
|
+
await this.clientAuthentication.login({
|
|
598
|
+
sessionId: this.info.sessionId,
|
|
599
|
+
...options,
|
|
600
|
+
tokenType: (_a = options.tokenType) !== null && _a !== void 0 ? _a : "DPoP",
|
|
601
|
+
}, this.events);
|
|
602
|
+
return new Promise(() => { });
|
|
603
|
+
};
|
|
604
|
+
this.fetch = (url, init) => this.clientAuthentication.fetch(url, init);
|
|
605
|
+
this.internalLogout = async (emitSignal, options) => {
|
|
606
|
+
window.localStorage.removeItem(KEY_CURRENT_SESSION);
|
|
607
|
+
await this.clientAuthentication.logout(this.info.sessionId, options);
|
|
608
|
+
this.info.isLoggedIn = false;
|
|
609
|
+
if (emitSignal) {
|
|
610
|
+
this.events.emit(EVENTS.LOGOUT);
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
this.logout = async (options) => this.internalLogout(true, options);
|
|
614
|
+
this.handleIncomingRedirect = async (inputOptions = {}) => {
|
|
615
|
+
var _a;
|
|
616
|
+
if (this.info.isLoggedIn) {
|
|
617
|
+
return this.info;
|
|
618
|
+
}
|
|
619
|
+
if (this.tokenRequestInProgress) {
|
|
620
|
+
return undefined;
|
|
621
|
+
}
|
|
622
|
+
const options = typeof inputOptions === "string" ? { url: inputOptions } : inputOptions;
|
|
623
|
+
const url = (_a = options.url) !== null && _a !== void 0 ? _a : window.location.href;
|
|
624
|
+
this.tokenRequestInProgress = true;
|
|
625
|
+
const sessionInfo = await this.clientAuthentication.handleIncomingRedirect(url, this.events);
|
|
626
|
+
if (isLoggedIn(sessionInfo)) {
|
|
627
|
+
this.setSessionInfo(sessionInfo);
|
|
628
|
+
const currentUrl = window.localStorage.getItem(KEY_CURRENT_URL);
|
|
629
|
+
if (currentUrl === null) {
|
|
630
|
+
this.events.emit(EVENTS.LOGIN);
|
|
631
|
+
}
|
|
632
|
+
else {
|
|
633
|
+
window.localStorage.removeItem(KEY_CURRENT_URL);
|
|
634
|
+
this.events.emit(EVENTS.SESSION_RESTORED, currentUrl);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
else if (options.restorePreviousSession === true) {
|
|
638
|
+
const storedSessionId = window.localStorage.getItem(KEY_CURRENT_SESSION);
|
|
639
|
+
if (storedSessionId !== null) {
|
|
640
|
+
const attemptedSilentAuthentication = await silentlyAuthenticate(storedSessionId, this.clientAuthentication, this);
|
|
641
|
+
if (attemptedSilentAuthentication) {
|
|
642
|
+
return new Promise(() => { });
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
this.tokenRequestInProgress = false;
|
|
647
|
+
return sessionInfo;
|
|
648
|
+
};
|
|
649
|
+
this.events = new Proxy(this, buildProxyHandler(Session.prototype, "events only implements ISessionEventListener"));
|
|
650
|
+
if (sessionOptions.clientAuthentication) {
|
|
651
|
+
this.clientAuthentication = sessionOptions.clientAuthentication;
|
|
652
|
+
}
|
|
653
|
+
else if (sessionOptions.secureStorage && sessionOptions.insecureStorage) {
|
|
654
|
+
this.clientAuthentication = getClientAuthenticationWithDependencies({
|
|
655
|
+
secureStorage: sessionOptions.secureStorage,
|
|
656
|
+
insecureStorage: sessionOptions.insecureStorage,
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
else {
|
|
660
|
+
this.clientAuthentication = getClientAuthenticationWithDependencies({});
|
|
661
|
+
}
|
|
662
|
+
if (sessionOptions.sessionInfo) {
|
|
663
|
+
this.info = {
|
|
664
|
+
sessionId: sessionOptions.sessionInfo.sessionId,
|
|
665
|
+
isLoggedIn: false,
|
|
666
|
+
webId: sessionOptions.sessionInfo.webId,
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
else {
|
|
670
|
+
this.info = {
|
|
671
|
+
sessionId: sessionId !== null && sessionId !== void 0 ? sessionId : v4(),
|
|
672
|
+
isLoggedIn: false,
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
this.events.on(EVENTS.LOGIN, () => window.localStorage.setItem(KEY_CURRENT_SESSION, this.info.sessionId));
|
|
676
|
+
this.events.on(EVENTS.SESSION_EXPIRED, () => this.internalLogout(false));
|
|
677
|
+
this.events.on(EVENTS.ERROR, () => this.internalLogout(false));
|
|
678
|
+
}
|
|
679
|
+
onLogin(callback) {
|
|
680
|
+
this.events.on(EVENTS.LOGIN, callback);
|
|
681
|
+
}
|
|
682
|
+
onLogout(callback) {
|
|
683
|
+
this.events.on(EVENTS.LOGOUT, callback);
|
|
684
|
+
}
|
|
685
|
+
onError(callback) {
|
|
686
|
+
this.events.on(EVENTS.ERROR, callback);
|
|
687
|
+
}
|
|
688
|
+
onSessionRestore(callback) {
|
|
689
|
+
this.events.on(EVENTS.SESSION_RESTORED, callback);
|
|
690
|
+
}
|
|
691
|
+
onSessionExpiration(callback) {
|
|
692
|
+
this.events.on(EVENTS.SESSION_EXPIRED, callback);
|
|
693
|
+
}
|
|
694
|
+
setSessionInfo(sessionInfo) {
|
|
695
|
+
this.info.isLoggedIn = sessionInfo.isLoggedIn;
|
|
696
|
+
this.info.webId = sessionInfo.webId;
|
|
697
|
+
this.info.sessionId = sessionInfo.sessionId;
|
|
698
|
+
this.info.expirationDate = sessionInfo.expirationDate;
|
|
699
|
+
this.events.on(EVENTS.SESSION_EXTENDED, (expiresIn) => {
|
|
700
|
+
this.info.expirationDate = Date.now() + expiresIn * 1000;
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
let defaultSession;
|
|
706
|
+
function getDefaultSession() {
|
|
707
|
+
if (typeof defaultSession === "undefined") {
|
|
708
|
+
defaultSession = new Session();
|
|
709
|
+
}
|
|
710
|
+
return defaultSession;
|
|
711
|
+
}
|
|
712
|
+
const fetch = (...args) => {
|
|
713
|
+
const session = getDefaultSession();
|
|
714
|
+
return session.fetch(...args);
|
|
715
|
+
};
|
|
716
|
+
const login = (...args) => {
|
|
717
|
+
const session = getDefaultSession();
|
|
718
|
+
return session.login(...args);
|
|
719
|
+
};
|
|
720
|
+
const logout = (...args) => {
|
|
721
|
+
const session = getDefaultSession();
|
|
722
|
+
return session.logout(...args);
|
|
723
|
+
};
|
|
724
|
+
const handleIncomingRedirect = (...args) => {
|
|
725
|
+
const session = getDefaultSession();
|
|
726
|
+
return session.handleIncomingRedirect(...args);
|
|
727
|
+
};
|
|
728
|
+
const onLogin = (...args) => {
|
|
729
|
+
const session = getDefaultSession();
|
|
730
|
+
return session.onLogin(...args);
|
|
731
|
+
};
|
|
732
|
+
const onLogout = (...args) => {
|
|
733
|
+
const session = getDefaultSession();
|
|
734
|
+
return session.onLogout(...args);
|
|
735
|
+
};
|
|
736
|
+
const onSessionRestore = (...args) => {
|
|
737
|
+
const session = getDefaultSession();
|
|
738
|
+
return session.onSessionRestore(...args);
|
|
739
|
+
};
|
|
740
|
+
const events = () => {
|
|
741
|
+
return getDefaultSession().events;
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
export { Session, events, fetch, getClientAuthenticationWithDependencies, getDefaultSession, handleIncomingRedirect, login, logout, onLogin, onLogout, onSessionRestore };
|
|
745
|
+
//# sourceMappingURL=index.mjs.map
|