@civic/auth 0.0.1-beta.18 → 0.0.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -26
- package/dist/chunk-5NUJ7LFF.mjs +17 -0
- package/dist/chunk-5NUJ7LFF.mjs.map +1 -0
- package/dist/chunk-KS7ERXGZ.js +481 -0
- package/dist/chunk-KS7ERXGZ.js.map +1 -0
- package/dist/chunk-NINRO7GS.js +209 -0
- package/dist/chunk-NINRO7GS.js.map +1 -0
- package/dist/chunk-NXBKSUKI.mjs +481 -0
- package/dist/chunk-NXBKSUKI.mjs.map +1 -0
- package/dist/chunk-T7HUHQ3J.mjs +209 -0
- package/dist/chunk-T7HUHQ3J.mjs.map +1 -0
- package/dist/chunk-WZLC5B4C.js +17 -0
- package/dist/chunk-WZLC5B4C.js.map +1 -0
- package/dist/index-DoDoIY_K.d.mts +79 -0
- package/dist/index-DoDoIY_K.d.ts +79 -0
- package/dist/index.css +70 -63
- package/dist/index.css.map +1 -1
- package/dist/index.d.mts +1 -3
- package/dist/index.d.ts +1 -3
- package/dist/nextjs.d.mts +11 -10
- package/dist/nextjs.d.ts +11 -10
- package/dist/nextjs.js +173 -62
- package/dist/nextjs.js.map +1 -1
- package/dist/nextjs.mjs +171 -60
- package/dist/nextjs.mjs.map +1 -1
- package/dist/react.d.mts +65 -39
- package/dist/react.d.ts +65 -39
- package/dist/react.js +212 -433
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +235 -456
- package/dist/react.mjs.map +1 -1
- package/dist/server.d.mts +12 -13
- package/dist/server.d.ts +12 -13
- package/dist/server.js +186 -3
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +192 -9
- package/dist/server.mjs.map +1 -1
- package/package.json +4 -4
- package/dist/chunk-5XL2ST72.mjs +0 -226
- package/dist/chunk-5XL2ST72.mjs.map +0 -1
- package/dist/chunk-G3P5TIO2.mjs +0 -708
- package/dist/chunk-G3P5TIO2.mjs.map +0 -1
- package/dist/chunk-RF23Q4V6.js +0 -708
- package/dist/chunk-RF23Q4V6.js.map +0 -1
- package/dist/chunk-SEKF2WZX.js +0 -226
- package/dist/chunk-SEKF2WZX.js.map +0 -1
- package/dist/index-DTimUlkB.d.ts +0 -17
- package/dist/index-DvjkKpkk.d.mts +0 -17
- package/dist/types-HdCjGldB.d.mts +0 -58
- package/dist/types-HdCjGldB.d.ts +0 -58
- package/dist/types-b4c1koXj.d.mts +0 -19
- package/dist/types-b4c1koXj.d.ts +0 -19
package/dist/chunk-G3P5TIO2.mjs
DELETED
|
@@ -1,708 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__async,
|
|
3
|
-
__spreadProps,
|
|
4
|
-
__spreadValues
|
|
5
|
-
} from "./chunk-RGHW4PYM.mjs";
|
|
6
|
-
|
|
7
|
-
// src/shared/storage.ts
|
|
8
|
-
var DEFAULT_COOKIE_DURATION = 60 * 15;
|
|
9
|
-
var CookieStorage = class {
|
|
10
|
-
constructor(settings = {}) {
|
|
11
|
-
var _a, _b, _c, _d, _e;
|
|
12
|
-
this.settings = {
|
|
13
|
-
httpOnly: (_a = settings.httpOnly) != null ? _a : true,
|
|
14
|
-
secure: (_b = settings.secure) != null ? _b : true,
|
|
15
|
-
// the callback request comes the auth server
|
|
16
|
-
// 'lax' ensures the code_verifier cookie is sent with the request
|
|
17
|
-
sameSite: (_c = settings.sameSite) != null ? _c : "lax",
|
|
18
|
-
expires: (_d = settings.expires) != null ? _d : new Date(Date.now() + 1e3 * DEFAULT_COOKIE_DURATION),
|
|
19
|
-
path: (_e = settings.path) != null ? _e : "/"
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// src/constants.ts
|
|
25
|
-
var DEFAULT_SCOPES = [
|
|
26
|
-
"openid",
|
|
27
|
-
"profile",
|
|
28
|
-
"email",
|
|
29
|
-
"forwardedTokens",
|
|
30
|
-
"offline_access"
|
|
31
|
-
];
|
|
32
|
-
var IFRAME_ID = "civic-auth-iframe";
|
|
33
|
-
var AUTH_SERVER = "https://auth-dev.civic.com/oauth";
|
|
34
|
-
var DEFAULT_OAUTH_GET_PARAMS = ["code", "state", "iss"];
|
|
35
|
-
var TOKEN_EXCHANGE_TRIGGER_TEXT = "sameDomainCodeExchangeRequired";
|
|
36
|
-
|
|
37
|
-
// src/shared/types.ts
|
|
38
|
-
var OAuthTokens = /* @__PURE__ */ ((OAuthTokens2) => {
|
|
39
|
-
OAuthTokens2["ID_TOKEN"] = "id_token";
|
|
40
|
-
OAuthTokens2["ACCESS_TOKEN"] = "access_token";
|
|
41
|
-
OAuthTokens2["REFRESH_TOKEN"] = "refresh_token";
|
|
42
|
-
return OAuthTokens2;
|
|
43
|
-
})(OAuthTokens || {});
|
|
44
|
-
|
|
45
|
-
// src/shared/util.ts
|
|
46
|
-
import { OAuth2Client } from "oslo/oauth2";
|
|
47
|
-
|
|
48
|
-
// src/lib/oauth.ts
|
|
49
|
-
import { v4 as uuid } from "uuid";
|
|
50
|
-
var getIssuerVariations = (issuer) => {
|
|
51
|
-
const issuerWithoutSlash = issuer.endsWith("/") ? issuer.slice(0, issuer.length - 1) : issuer;
|
|
52
|
-
const issuerWithSlash = `${issuerWithoutSlash}/`;
|
|
53
|
-
return [issuerWithoutSlash, issuerWithSlash];
|
|
54
|
-
};
|
|
55
|
-
var addSlashIfNeeded = (url) => url.endsWith("/") ? url : `${url}/`;
|
|
56
|
-
var getOauthEndpoints = (oauthServer) => __async(void 0, null, function* () {
|
|
57
|
-
const openIdConfigResponse = yield fetch(
|
|
58
|
-
`${addSlashIfNeeded(oauthServer)}.well-known/openid-configuration`
|
|
59
|
-
);
|
|
60
|
-
const openIdConfig = yield openIdConfigResponse.json();
|
|
61
|
-
return {
|
|
62
|
-
jwks: openIdConfig.jwks_uri,
|
|
63
|
-
auth: openIdConfig.authorization_endpoint,
|
|
64
|
-
token: openIdConfig.token_endpoint,
|
|
65
|
-
userinfo: openIdConfig.userinfo_endpoint
|
|
66
|
-
};
|
|
67
|
-
});
|
|
68
|
-
var generateState = (displayMode) => {
|
|
69
|
-
const jsonString = JSON.stringify({
|
|
70
|
-
uuid: uuid(),
|
|
71
|
-
displayMode
|
|
72
|
-
});
|
|
73
|
-
return btoa(jsonString);
|
|
74
|
-
};
|
|
75
|
-
var displayModeFromState = (state, sessionDisplayMode) => {
|
|
76
|
-
try {
|
|
77
|
-
const jsonString = atob(state);
|
|
78
|
-
return JSON.parse(jsonString).displayMode;
|
|
79
|
-
} catch (e) {
|
|
80
|
-
console.error("Failed to parse displayMode from state:", state);
|
|
81
|
-
return sessionDisplayMode;
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
// src/shared/util.ts
|
|
86
|
-
import * as jose from "jose";
|
|
87
|
-
|
|
88
|
-
// src/utils.ts
|
|
89
|
-
import { clsx } from "clsx";
|
|
90
|
-
import { twMerge } from "tailwind-merge";
|
|
91
|
-
var cn = (...inputs) => {
|
|
92
|
-
return twMerge(clsx(inputs));
|
|
93
|
-
};
|
|
94
|
-
var withoutUndefined = (obj) => {
|
|
95
|
-
const result = {};
|
|
96
|
-
for (const key in obj) {
|
|
97
|
-
if (obj[key] !== void 0) {
|
|
98
|
-
result[key] = obj[key];
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return result;
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
// src/lib/jwt.ts
|
|
105
|
-
var convertForwardedTokenFormat = (inputTokens) => Object.fromEntries(
|
|
106
|
-
Object.entries(inputTokens).map(([source, tokens]) => [
|
|
107
|
-
source,
|
|
108
|
-
{
|
|
109
|
-
idToken: tokens == null ? void 0 : tokens.id_token,
|
|
110
|
-
accessToken: tokens == null ? void 0 : tokens.access_token,
|
|
111
|
-
refreshToken: tokens == null ? void 0 : tokens.refresh_token
|
|
112
|
-
}
|
|
113
|
-
])
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
// src/shared/UserSession.ts
|
|
117
|
-
var GenericUserSession = class {
|
|
118
|
-
constructor(storage) {
|
|
119
|
-
this.storage = storage;
|
|
120
|
-
}
|
|
121
|
-
get() {
|
|
122
|
-
const user = this.storage.get("user" /* USER */);
|
|
123
|
-
return user ? JSON.parse(user) : null;
|
|
124
|
-
}
|
|
125
|
-
set(user) {
|
|
126
|
-
const forwardedTokens = (user == null ? void 0 : user.forwardedTokens) ? convertForwardedTokenFormat(user == null ? void 0 : user.forwardedTokens) : null;
|
|
127
|
-
const value = user ? JSON.stringify(__spreadProps(__spreadValues({}, user), { forwardedTokens })) : "";
|
|
128
|
-
this.storage.set("user" /* USER */, value);
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
// src/shared/util.ts
|
|
133
|
-
function deriveCodeChallenge(codeVerifier, method = "S256") {
|
|
134
|
-
return __async(this, null, function* () {
|
|
135
|
-
if (method === "Plain") {
|
|
136
|
-
console.warn("Using insecure plain code challenge method");
|
|
137
|
-
return codeVerifier;
|
|
138
|
-
}
|
|
139
|
-
const encoder = new TextEncoder();
|
|
140
|
-
const data = encoder.encode(codeVerifier);
|
|
141
|
-
const digest = yield crypto.subtle.digest("SHA-256", data);
|
|
142
|
-
return btoa(String.fromCharCode(...new Uint8Array(digest))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
function getEndpointsWithOverrides(_0) {
|
|
146
|
-
return __async(this, arguments, function* (oauthServer, endpointOverrides = {}) {
|
|
147
|
-
const endpoints = yield getOauthEndpoints(oauthServer);
|
|
148
|
-
return __spreadValues(__spreadValues({}, endpoints), endpointOverrides);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
function generateOauthLoginUrl(config) {
|
|
152
|
-
return __async(this, null, function* () {
|
|
153
|
-
const endpoints = yield getEndpointsWithOverrides(
|
|
154
|
-
config.oauthServer,
|
|
155
|
-
config.endpointOverrides
|
|
156
|
-
);
|
|
157
|
-
const oauth2Client = buildOauth2Client(
|
|
158
|
-
config.clientId,
|
|
159
|
-
config.redirectUrl,
|
|
160
|
-
endpoints
|
|
161
|
-
);
|
|
162
|
-
const challenge = yield config.pkceConsumer.getCodeChallenge();
|
|
163
|
-
const oAuthUrl = yield oauth2Client.createAuthorizationURL({
|
|
164
|
-
state: config.state,
|
|
165
|
-
scopes: config.scopes
|
|
166
|
-
});
|
|
167
|
-
oAuthUrl.searchParams.append("code_challenge", challenge);
|
|
168
|
-
oAuthUrl.searchParams.append("code_challenge_method", "S256");
|
|
169
|
-
if (config.nonce) {
|
|
170
|
-
oAuthUrl.searchParams.append("nonce", config.nonce);
|
|
171
|
-
}
|
|
172
|
-
oAuthUrl.searchParams.append("prompt", "consent");
|
|
173
|
-
console.log("Generated OAuth URL", oAuthUrl.toString());
|
|
174
|
-
return oAuthUrl;
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
function generateOauthLogoutUrl(config) {
|
|
178
|
-
return __async(this, null, function* () {
|
|
179
|
-
return new URL("http://localhost");
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
function buildOauth2Client(clientId, redirectUri, endpoints) {
|
|
183
|
-
return new OAuth2Client(clientId, endpoints.auth, endpoints.token, {
|
|
184
|
-
redirectURI: redirectUri
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
function exchangeTokens(code, state, pkceProducer, oauth2Client, oauthServer, endpoints) {
|
|
188
|
-
return __async(this, null, function* () {
|
|
189
|
-
const codeVerifier = yield pkceProducer.getCodeVerifier();
|
|
190
|
-
if (!codeVerifier) throw new Error("Code verifier not found in state");
|
|
191
|
-
const tokens = yield oauth2Client.validateAuthorizationCode(code, {
|
|
192
|
-
codeVerifier
|
|
193
|
-
});
|
|
194
|
-
try {
|
|
195
|
-
yield validateOauth2Tokens(tokens, endpoints, oauth2Client, oauthServer);
|
|
196
|
-
} catch (error) {
|
|
197
|
-
console.error("tokenExchange error", { error, tokens });
|
|
198
|
-
throw new Error(
|
|
199
|
-
`OIDC tokens validation failed: ${error.message}`
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
return tokens;
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
function storeTokens(storage, tokens) {
|
|
206
|
-
storage.set("id_token" /* ID_TOKEN */, tokens.id_token);
|
|
207
|
-
storage.set("access_token" /* ACCESS_TOKEN */, tokens.access_token);
|
|
208
|
-
if (tokens.refresh_token)
|
|
209
|
-
storage.set("refresh_token" /* REFRESH_TOKEN */, tokens.refresh_token);
|
|
210
|
-
}
|
|
211
|
-
function clearTokens(storage) {
|
|
212
|
-
Object.values(OAuthTokens).forEach((cookie) => {
|
|
213
|
-
storage.set(cookie, "");
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
function clearUser(storage) {
|
|
217
|
-
const userSession = new GenericUserSession(storage);
|
|
218
|
-
userSession.set(null);
|
|
219
|
-
}
|
|
220
|
-
function retrieveTokens(storage) {
|
|
221
|
-
const idToken = storage.get("id_token" /* ID_TOKEN */);
|
|
222
|
-
const accessToken = storage.get("access_token" /* ACCESS_TOKEN */);
|
|
223
|
-
const refreshToken = storage.get("refresh_token" /* REFRESH_TOKEN */);
|
|
224
|
-
if (!idToken || !accessToken) return null;
|
|
225
|
-
return {
|
|
226
|
-
id_token: idToken,
|
|
227
|
-
access_token: accessToken,
|
|
228
|
-
refresh_token: refreshToken != null ? refreshToken : void 0
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
function validateOauth2Tokens(tokens, endpoints, oauth2Client, issuer) {
|
|
232
|
-
return __async(this, null, function* () {
|
|
233
|
-
const JWKS = jose.createRemoteJWKSet(new URL(endpoints.jwks));
|
|
234
|
-
const idTokenResponse = yield jose.jwtVerify(
|
|
235
|
-
tokens.id_token,
|
|
236
|
-
JWKS,
|
|
237
|
-
{
|
|
238
|
-
issuer: getIssuerVariations(issuer),
|
|
239
|
-
audience: oauth2Client.clientId
|
|
240
|
-
}
|
|
241
|
-
);
|
|
242
|
-
const accessTokenResponse = yield jose.jwtVerify(
|
|
243
|
-
tokens.access_token,
|
|
244
|
-
JWKS,
|
|
245
|
-
{
|
|
246
|
-
issuer: getIssuerVariations(issuer)
|
|
247
|
-
}
|
|
248
|
-
);
|
|
249
|
-
return withoutUndefined({
|
|
250
|
-
id_token: idTokenResponse.payload,
|
|
251
|
-
access_token: accessTokenResponse.payload,
|
|
252
|
-
refresh_token: tokens.refresh_token
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// src/services/PKCE.ts
|
|
258
|
-
import { generateCodeVerifier } from "oslo/oauth2";
|
|
259
|
-
|
|
260
|
-
// src/browser/storage.ts
|
|
261
|
-
var LocalStorageAdapter = class {
|
|
262
|
-
get(key) {
|
|
263
|
-
return localStorage.getItem(key) || "";
|
|
264
|
-
}
|
|
265
|
-
set(key, value) {
|
|
266
|
-
localStorage.setItem(key, value);
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
// src/services/PKCE.ts
|
|
271
|
-
var ConfidentialClientPKCEConsumer = class {
|
|
272
|
-
constructor(pkceChallengeEndpoint) {
|
|
273
|
-
this.pkceChallengeEndpoint = pkceChallengeEndpoint;
|
|
274
|
-
}
|
|
275
|
-
getCodeChallenge() {
|
|
276
|
-
return __async(this, null, function* () {
|
|
277
|
-
const response = yield fetch(this.pkceChallengeEndpoint);
|
|
278
|
-
const data = yield response.json();
|
|
279
|
-
return data.challenge;
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
var GenericPublicClientPKCEProducer = class {
|
|
284
|
-
constructor(storage) {
|
|
285
|
-
this.storage = storage;
|
|
286
|
-
}
|
|
287
|
-
// if there is already a verifier, return it,
|
|
288
|
-
// If not, create a new one and store it
|
|
289
|
-
getCodeChallenge() {
|
|
290
|
-
return __async(this, null, function* () {
|
|
291
|
-
const verifier = generateCodeVerifier();
|
|
292
|
-
this.storage.set("code_verifier", verifier);
|
|
293
|
-
return deriveCodeChallenge(verifier);
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
// if there is already a verifier, return it,
|
|
297
|
-
getCodeVerifier() {
|
|
298
|
-
return __async(this, null, function* () {
|
|
299
|
-
return this.storage.get("code_verifier");
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
var BrowserPublicClientPKCEProducer = class extends GenericPublicClientPKCEProducer {
|
|
304
|
-
constructor() {
|
|
305
|
-
super(new LocalStorageAdapter());
|
|
306
|
-
}
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
// src/services/AuthenticationService.ts
|
|
310
|
-
import { OAuth2Client as OAuth2Client2 } from "oslo/oauth2";
|
|
311
|
-
|
|
312
|
-
// src/lib/windowUtil.ts
|
|
313
|
-
var isWindowInIframe = (window2) => {
|
|
314
|
-
var _a;
|
|
315
|
-
if (typeof window2 !== "undefined") {
|
|
316
|
-
try {
|
|
317
|
-
if (((_a = window2 == null ? void 0 : window2.frameElement) == null ? void 0 : _a.id) === "civic-auth-iframe") {
|
|
318
|
-
return true;
|
|
319
|
-
}
|
|
320
|
-
} catch (_e) {
|
|
321
|
-
return false;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
return false;
|
|
325
|
-
};
|
|
326
|
-
var removeParamsWithoutReload = (paramsToRemove) => {
|
|
327
|
-
const url = new URL(window.location.href);
|
|
328
|
-
paramsToRemove.forEach((param) => {
|
|
329
|
-
url.searchParams.delete(param);
|
|
330
|
-
});
|
|
331
|
-
window.history.replaceState({}, "", url);
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
// src/services/AuthenticationService.ts
|
|
335
|
-
var BrowserAuthenticationInitiator = class {
|
|
336
|
-
constructor(config) {
|
|
337
|
-
this.config = config;
|
|
338
|
-
}
|
|
339
|
-
// Use the config (Client ID, scopes OAuth Server, Endpoints, PKCEConsumer) to generate a new login url
|
|
340
|
-
// and then use the display mode to decide how to send the user there
|
|
341
|
-
signIn(iframeRef) {
|
|
342
|
-
return __async(this, null, function* () {
|
|
343
|
-
const url = yield generateOauthLoginUrl(this.config);
|
|
344
|
-
if (this.config.displayMode === "iframe") {
|
|
345
|
-
if (!iframeRef)
|
|
346
|
-
throw new Error("iframeRef is required for displayMode 'iframe'");
|
|
347
|
-
iframeRef.setAttribute("src", url.toString());
|
|
348
|
-
}
|
|
349
|
-
if (this.config.displayMode === "redirect") {
|
|
350
|
-
window.location.href = url.toString();
|
|
351
|
-
}
|
|
352
|
-
if (this.config.displayMode === "new_tab") {
|
|
353
|
-
window.open(url.toString(), "_blank");
|
|
354
|
-
}
|
|
355
|
-
return url;
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
signOut() {
|
|
359
|
-
return __async(this, null, function* () {
|
|
360
|
-
const localStorage2 = new LocalStorageAdapter();
|
|
361
|
-
clearTokens(localStorage2);
|
|
362
|
-
clearUser(localStorage2);
|
|
363
|
-
const url = yield generateOauthLogoutUrl(this.config);
|
|
364
|
-
return url;
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
};
|
|
368
|
-
var GenericAuthenticationInitiator = class {
|
|
369
|
-
constructor(config) {
|
|
370
|
-
this.config = config;
|
|
371
|
-
}
|
|
372
|
-
// Use the config (Client ID, scopes OAuth Server, Endpoints, PKCEConsumer) to generate a new login url
|
|
373
|
-
// and simply return the url
|
|
374
|
-
signIn() {
|
|
375
|
-
return __async(this, null, function* () {
|
|
376
|
-
return generateOauthLoginUrl(this.config);
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
signOut() {
|
|
380
|
-
return __async(this, null, function* () {
|
|
381
|
-
return generateOauthLogoutUrl(this.config);
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
};
|
|
385
|
-
var BrowserAuthenticationService = class _BrowserAuthenticationService extends BrowserAuthenticationInitiator {
|
|
386
|
-
// TODO WIP - perhaps we want to keep resolver and initiator separate here
|
|
387
|
-
constructor(config, pkceProducer = new BrowserPublicClientPKCEProducer()) {
|
|
388
|
-
super(__spreadProps(__spreadValues({}, config), {
|
|
389
|
-
state: generateState(config.displayMode),
|
|
390
|
-
// Store and retrieve the PKCE challenge in local storage
|
|
391
|
-
pkceConsumer: pkceProducer
|
|
392
|
-
}));
|
|
393
|
-
this.pkceProducer = pkceProducer;
|
|
394
|
-
}
|
|
395
|
-
// TODO too much code duplication here between the browser and the server variant.
|
|
396
|
-
// Suggestion for refactor: Standardise the config for AuthenticationResolvers and create a one-shot
|
|
397
|
-
// function for generating an oauth2client from it
|
|
398
|
-
init() {
|
|
399
|
-
return __async(this, null, function* () {
|
|
400
|
-
this.endpoints = yield getEndpointsWithOverrides(
|
|
401
|
-
this.config.oauthServer,
|
|
402
|
-
this.config.endpointOverrides
|
|
403
|
-
);
|
|
404
|
-
this.oauth2client = new OAuth2Client2(
|
|
405
|
-
this.config.clientId,
|
|
406
|
-
this.endpoints.auth,
|
|
407
|
-
this.endpoints.token,
|
|
408
|
-
{
|
|
409
|
-
redirectURI: this.config.redirectUrl
|
|
410
|
-
}
|
|
411
|
-
);
|
|
412
|
-
return this;
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
// Two responsibilities:
|
|
416
|
-
// 1. resolve the auth code to get the tokens (should use library code)
|
|
417
|
-
// 2. store the tokens in local storage
|
|
418
|
-
tokenExchange(code, state) {
|
|
419
|
-
return __async(this, null, function* () {
|
|
420
|
-
if (!this.oauth2client) yield this.init();
|
|
421
|
-
const codeVerifier = yield this.pkceProducer.getCodeVerifier();
|
|
422
|
-
if (!codeVerifier) throw new Error("Code verifier not found in storage");
|
|
423
|
-
const tokens = yield exchangeTokens(
|
|
424
|
-
code,
|
|
425
|
-
state,
|
|
426
|
-
this.pkceProducer,
|
|
427
|
-
this.oauth2client,
|
|
428
|
-
// clean up types here to avoid the ! operator
|
|
429
|
-
this.config.oauthServer,
|
|
430
|
-
this.endpoints
|
|
431
|
-
// clean up types here to avoid the ! operator
|
|
432
|
-
);
|
|
433
|
-
storeTokens(new LocalStorageAdapter(), tokens);
|
|
434
|
-
const parsedDisplayMode = displayModeFromState(
|
|
435
|
-
state,
|
|
436
|
-
this.config.displayMode
|
|
437
|
-
);
|
|
438
|
-
if (parsedDisplayMode === "new_tab") {
|
|
439
|
-
window.close();
|
|
440
|
-
} else if (parsedDisplayMode === "redirect") {
|
|
441
|
-
removeParamsWithoutReload(DEFAULT_OAUTH_GET_PARAMS);
|
|
442
|
-
}
|
|
443
|
-
return tokens;
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
// Get the session data from local storage
|
|
447
|
-
getSessionData() {
|
|
448
|
-
return __async(this, null, function* () {
|
|
449
|
-
const storageData = retrieveTokens(new LocalStorageAdapter());
|
|
450
|
-
if (!storageData) return null;
|
|
451
|
-
return {
|
|
452
|
-
authenticated: !!storageData.id_token,
|
|
453
|
-
idToken: storageData.id_token,
|
|
454
|
-
accessToken: storageData.access_token,
|
|
455
|
-
refreshToken: storageData.refresh_token
|
|
456
|
-
};
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
validateExistingSession() {
|
|
460
|
-
return __async(this, null, function* () {
|
|
461
|
-
try {
|
|
462
|
-
const sessionData = yield this.getSessionData();
|
|
463
|
-
if (!(sessionData == null ? void 0 : sessionData.idToken) || !sessionData.accessToken) {
|
|
464
|
-
const unAuthenticatedSession = __spreadProps(__spreadValues({}, sessionData), { authenticated: false });
|
|
465
|
-
clearTokens(new LocalStorageAdapter());
|
|
466
|
-
return unAuthenticatedSession;
|
|
467
|
-
}
|
|
468
|
-
if (!this.endpoints || !this.oauth2client) yield this.init();
|
|
469
|
-
yield validateOauth2Tokens(
|
|
470
|
-
{
|
|
471
|
-
access_token: sessionData.accessToken,
|
|
472
|
-
id_token: sessionData.idToken,
|
|
473
|
-
refresh_token: sessionData.refreshToken
|
|
474
|
-
},
|
|
475
|
-
this.endpoints,
|
|
476
|
-
this.oauth2client,
|
|
477
|
-
this.config.oauthServer
|
|
478
|
-
);
|
|
479
|
-
return sessionData;
|
|
480
|
-
} catch (error) {
|
|
481
|
-
console.warn("Failed to validate existing tokens", error);
|
|
482
|
-
const unAuthenticatedSession = {
|
|
483
|
-
authenticated: false
|
|
484
|
-
};
|
|
485
|
-
clearTokens(new LocalStorageAdapter());
|
|
486
|
-
return unAuthenticatedSession;
|
|
487
|
-
}
|
|
488
|
-
});
|
|
489
|
-
}
|
|
490
|
-
static build(config) {
|
|
491
|
-
return __async(this, null, function* () {
|
|
492
|
-
const resolver = new _BrowserAuthenticationService(config);
|
|
493
|
-
yield resolver.init();
|
|
494
|
-
return resolver;
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
// src/server/ServerAuthenticationResolver.ts
|
|
500
|
-
import { OAuth2Client as OAuth2Client3 } from "oslo/oauth2";
|
|
501
|
-
var ServerAuthenticationResolver = class _ServerAuthenticationResolver {
|
|
502
|
-
constructor(authConfig, storage, endpointOverrides) {
|
|
503
|
-
this.authConfig = authConfig;
|
|
504
|
-
this.storage = storage;
|
|
505
|
-
this.endpointOverrides = endpointOverrides;
|
|
506
|
-
this.pkceProducer = new GenericPublicClientPKCEProducer(storage);
|
|
507
|
-
}
|
|
508
|
-
validateExistingSession() {
|
|
509
|
-
throw new Error("Method not implemented.");
|
|
510
|
-
}
|
|
511
|
-
init() {
|
|
512
|
-
return __async(this, null, function* () {
|
|
513
|
-
this.endpoints = yield getEndpointsWithOverrides(
|
|
514
|
-
this.authConfig.oauthServer,
|
|
515
|
-
this.endpointOverrides
|
|
516
|
-
);
|
|
517
|
-
this.oauth2client = new OAuth2Client3(
|
|
518
|
-
this.authConfig.clientId,
|
|
519
|
-
this.endpoints.auth,
|
|
520
|
-
this.endpoints.token,
|
|
521
|
-
{
|
|
522
|
-
redirectURI: this.authConfig.redirectUrl
|
|
523
|
-
}
|
|
524
|
-
);
|
|
525
|
-
return this;
|
|
526
|
-
});
|
|
527
|
-
}
|
|
528
|
-
tokenExchange(code, state) {
|
|
529
|
-
return __async(this, null, function* () {
|
|
530
|
-
if (!this.oauth2client) yield this.init();
|
|
531
|
-
const codeVerifier = yield this.pkceProducer.getCodeVerifier();
|
|
532
|
-
if (!codeVerifier) throw new Error("Code verifier not found in storage");
|
|
533
|
-
const tokens = yield exchangeTokens(
|
|
534
|
-
code,
|
|
535
|
-
state,
|
|
536
|
-
this.pkceProducer,
|
|
537
|
-
this.oauth2client,
|
|
538
|
-
// clean up types here to avoid the ! operator
|
|
539
|
-
this.authConfig.oauthServer,
|
|
540
|
-
this.endpoints
|
|
541
|
-
// clean up types here to avoid the ! operator
|
|
542
|
-
);
|
|
543
|
-
storeTokens(this.storage, tokens);
|
|
544
|
-
return tokens;
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
getSessionData() {
|
|
548
|
-
return __async(this, null, function* () {
|
|
549
|
-
const storageData = retrieveTokens(this.storage);
|
|
550
|
-
if (!storageData) return null;
|
|
551
|
-
return {
|
|
552
|
-
authenticated: !!storageData.id_token,
|
|
553
|
-
idToken: storageData.id_token,
|
|
554
|
-
accessToken: storageData.access_token,
|
|
555
|
-
refreshToken: storageData.refresh_token
|
|
556
|
-
};
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
static build(authConfig, storage, endpointOverrides) {
|
|
560
|
-
return __async(this, null, function* () {
|
|
561
|
-
const resolver = new _ServerAuthenticationResolver(
|
|
562
|
-
authConfig,
|
|
563
|
-
storage,
|
|
564
|
-
endpointOverrides
|
|
565
|
-
);
|
|
566
|
-
yield resolver.init();
|
|
567
|
-
return resolver;
|
|
568
|
-
});
|
|
569
|
-
}
|
|
570
|
-
};
|
|
571
|
-
|
|
572
|
-
// src/server/login.ts
|
|
573
|
-
function resolveOAuthAccessCode(code, state, storage, config) {
|
|
574
|
-
return __async(this, null, function* () {
|
|
575
|
-
var _a;
|
|
576
|
-
const authSessionService = yield ServerAuthenticationResolver.build(
|
|
577
|
-
__spreadProps(__spreadValues({}, config), {
|
|
578
|
-
oauthServer: (_a = config.oauthServer) != null ? _a : AUTH_SERVER
|
|
579
|
-
}),
|
|
580
|
-
storage,
|
|
581
|
-
config.endpointOverrides
|
|
582
|
-
);
|
|
583
|
-
return authSessionService.tokenExchange(code, state);
|
|
584
|
-
});
|
|
585
|
-
}
|
|
586
|
-
function isLoggedIn(storage) {
|
|
587
|
-
return !!storage.get("id_token");
|
|
588
|
-
}
|
|
589
|
-
function buildLoginUrl(config, storage) {
|
|
590
|
-
return __async(this, null, function* () {
|
|
591
|
-
var _a, _b, _c;
|
|
592
|
-
const state = (_a = config.state) != null ? _a : Math.random().toString(36).substring(2);
|
|
593
|
-
const scopes = (_b = config.scopes) != null ? _b : DEFAULT_SCOPES;
|
|
594
|
-
const pkceProducer = new GenericPublicClientPKCEProducer(storage);
|
|
595
|
-
const authInitiator = new GenericAuthenticationInitiator(__spreadProps(__spreadValues({}, config), {
|
|
596
|
-
state,
|
|
597
|
-
scopes,
|
|
598
|
-
oauthServer: (_c = config.oauthServer) != null ? _c : AUTH_SERVER,
|
|
599
|
-
// When retrieving the PKCE challenge on the server-side, we produce it and store it in the session
|
|
600
|
-
pkceConsumer: pkceProducer
|
|
601
|
-
}));
|
|
602
|
-
return authInitiator.signIn();
|
|
603
|
-
});
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
// src/shared/session.ts
|
|
607
|
-
import { parseJWT } from "oslo/jwt";
|
|
608
|
-
function getUser(storage) {
|
|
609
|
-
return __async(this, null, function* () {
|
|
610
|
-
var _a, _b;
|
|
611
|
-
const tokens = retrieveTokens(storage);
|
|
612
|
-
if (!tokens) return null;
|
|
613
|
-
return (_b = (_a = parseJWT(tokens.id_token)) == null ? void 0 : _a.payload) != null ? _b : null;
|
|
614
|
-
});
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
// src/shared/GenericAuthenticationRefresher.ts
|
|
618
|
-
import { OAuth2Client as OAuth2Client4 } from "oslo/oauth2";
|
|
619
|
-
var GenericAuthenticationRefresher = class _GenericAuthenticationRefresher {
|
|
620
|
-
constructor(authConfig, storage, endpointOverrides) {
|
|
621
|
-
this.authConfig = authConfig;
|
|
622
|
-
this.storage = storage;
|
|
623
|
-
this.endpointOverrides = endpointOverrides;
|
|
624
|
-
}
|
|
625
|
-
init() {
|
|
626
|
-
return __async(this, null, function* () {
|
|
627
|
-
this.endpoints = yield getEndpointsWithOverrides(
|
|
628
|
-
this.authConfig.oauthServer,
|
|
629
|
-
this.endpointOverrides
|
|
630
|
-
);
|
|
631
|
-
this.oauth2client = new OAuth2Client4(
|
|
632
|
-
this.authConfig.clientId,
|
|
633
|
-
this.endpoints.auth,
|
|
634
|
-
this.endpoints.token,
|
|
635
|
-
{
|
|
636
|
-
redirectURI: this.authConfig.redirectUrl
|
|
637
|
-
}
|
|
638
|
-
);
|
|
639
|
-
return this;
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
-
static build(authConfig, storage, endpointOverrides) {
|
|
643
|
-
return __async(this, null, function* () {
|
|
644
|
-
const refresher = new _GenericAuthenticationRefresher(
|
|
645
|
-
authConfig,
|
|
646
|
-
storage,
|
|
647
|
-
endpointOverrides
|
|
648
|
-
);
|
|
649
|
-
yield refresher.init();
|
|
650
|
-
return refresher;
|
|
651
|
-
});
|
|
652
|
-
}
|
|
653
|
-
refreshTokens() {
|
|
654
|
-
return __async(this, null, function* () {
|
|
655
|
-
if (!this.oauth2client) yield this.init();
|
|
656
|
-
const tokens = retrieveTokens(this.storage);
|
|
657
|
-
if (!(tokens == null ? void 0 : tokens.refresh_token)) throw new Error("No refresh token available");
|
|
658
|
-
const oauth2Client = this.oauth2client;
|
|
659
|
-
const refreshedTokens = yield oauth2Client.refreshAccessToken(
|
|
660
|
-
tokens.refresh_token
|
|
661
|
-
);
|
|
662
|
-
storeTokens(this.storage, refreshedTokens);
|
|
663
|
-
return tokens;
|
|
664
|
-
});
|
|
665
|
-
}
|
|
666
|
-
};
|
|
667
|
-
|
|
668
|
-
// src/server/refresh.ts
|
|
669
|
-
function refreshTokens(storage, config) {
|
|
670
|
-
return __async(this, null, function* () {
|
|
671
|
-
var _a;
|
|
672
|
-
const refresher = yield GenericAuthenticationRefresher.build(
|
|
673
|
-
__spreadProps(__spreadValues({}, config), {
|
|
674
|
-
oauthServer: (_a = config.oauthServer) != null ? _a : AUTH_SERVER
|
|
675
|
-
}),
|
|
676
|
-
storage,
|
|
677
|
-
config.endpointOverrides
|
|
678
|
-
);
|
|
679
|
-
return refresher.refreshTokens();
|
|
680
|
-
});
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
export {
|
|
684
|
-
convertForwardedTokenFormat,
|
|
685
|
-
GenericUserSession,
|
|
686
|
-
DEFAULT_SCOPES,
|
|
687
|
-
IFRAME_ID,
|
|
688
|
-
TOKEN_EXCHANGE_TRIGGER_TEXT,
|
|
689
|
-
isWindowInIframe,
|
|
690
|
-
generateState,
|
|
691
|
-
cn,
|
|
692
|
-
withoutUndefined,
|
|
693
|
-
clearTokens,
|
|
694
|
-
retrieveTokens,
|
|
695
|
-
LocalStorageAdapter,
|
|
696
|
-
ConfidentialClientPKCEConsumer,
|
|
697
|
-
GenericPublicClientPKCEProducer,
|
|
698
|
-
BrowserPublicClientPKCEProducer,
|
|
699
|
-
BrowserAuthenticationInitiator,
|
|
700
|
-
BrowserAuthenticationService,
|
|
701
|
-
getUser,
|
|
702
|
-
CookieStorage,
|
|
703
|
-
resolveOAuthAccessCode,
|
|
704
|
-
isLoggedIn,
|
|
705
|
-
buildLoginUrl,
|
|
706
|
-
refreshTokens
|
|
707
|
-
};
|
|
708
|
-
//# sourceMappingURL=chunk-G3P5TIO2.mjs.map
|