@civic/auth 0.0.1-beta.22 → 0.0.1-beta.24
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/chunk-2OZJONNO.js +172 -0
- package/dist/chunk-2OZJONNO.js.map +1 -0
- package/dist/chunk-3YV5NEM4.js +173 -0
- package/dist/chunk-3YV5NEM4.js.map +1 -0
- package/dist/chunk-5UQQYXCX.js.map +1 -1
- package/dist/chunk-5XL2ST72.mjs +226 -0
- package/dist/chunk-5XL2ST72.mjs.map +1 -0
- package/dist/chunk-6RFRDWIP.js.map +1 -1
- package/dist/chunk-6RJHOVY6.mjs +712 -0
- package/dist/chunk-6RJHOVY6.mjs.map +1 -0
- package/dist/chunk-74J7HX36.mjs +712 -0
- package/dist/chunk-74J7HX36.mjs.map +1 -0
- package/dist/chunk-7K3QN2AT.js.map +1 -1
- package/dist/chunk-A43GY6C3.mjs +173 -0
- package/dist/chunk-A43GY6C3.mjs.map +1 -0
- package/dist/{chunk-NLRREFOX.mjs → chunk-AMCR45Y5.mjs} +2 -2
- package/dist/chunk-AMCR45Y5.mjs.map +1 -0
- package/dist/chunk-COWYPS3A.js +712 -0
- package/dist/chunk-COWYPS3A.js.map +1 -0
- package/dist/chunk-CRTRMMJ7.js.map +1 -1
- package/dist/chunk-CTVJJBBA.js.map +1 -1
- package/dist/chunk-ELO3M4DA.js +175 -0
- package/dist/chunk-ELO3M4DA.js.map +1 -0
- package/dist/chunk-FZ4INHWE.mjs +707 -0
- package/dist/chunk-FZ4INHWE.mjs.map +1 -0
- package/dist/chunk-G3P5TIO2.mjs +708 -0
- package/dist/chunk-G3P5TIO2.mjs.map +1 -0
- package/dist/chunk-JCLIMTK5.js +712 -0
- package/dist/chunk-JCLIMTK5.js.map +1 -0
- package/dist/chunk-JDZPCA3P.js.map +1 -1
- package/dist/chunk-NTJWPNOZ.mjs +175 -0
- package/dist/chunk-NTJWPNOZ.mjs.map +1 -0
- package/dist/chunk-ON4OH5OM.mjs +172 -0
- package/dist/chunk-ON4OH5OM.mjs.map +1 -0
- package/dist/chunk-QHE3SPKQ.js +712 -0
- package/dist/chunk-QHE3SPKQ.js.map +1 -0
- package/dist/chunk-RCFPLIWS.js +707 -0
- package/dist/chunk-RCFPLIWS.js.map +1 -0
- package/dist/chunk-RF23Q4V6.js +708 -0
- package/dist/chunk-RF23Q4V6.js.map +1 -0
- package/dist/chunk-SEKF2WZX.js +226 -0
- package/dist/chunk-SEKF2WZX.js.map +1 -0
- package/dist/chunk-SN7YDQQH.js.map +1 -1
- package/dist/chunk-WXSUVTI4.mjs +712 -0
- package/dist/chunk-WXSUVTI4.mjs.map +1 -0
- package/dist/{chunk-FHRZSX3C.js → chunk-XNSHSKGI.js} +2 -2
- package/dist/chunk-XNSHSKGI.js.map +1 -0
- package/dist/index-DTimUlkB.d.ts +17 -0
- package/dist/index-DvjkKpkk.d.mts +17 -0
- package/dist/index.css +10 -2
- package/dist/index.css.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/nextjs/client.css +10 -2
- package/dist/nextjs/client.css.map +1 -1
- package/dist/nextjs/client.js +16 -8
- package/dist/nextjs/client.js.map +1 -1
- package/dist/nextjs/client.mjs +13 -5
- package/dist/nextjs/client.mjs.map +1 -1
- package/dist/nextjs.d.mts +2 -4
- package/dist/nextjs.d.ts +2 -4
- package/dist/nextjs.js +9 -9
- package/dist/nextjs.js.map +1 -1
- package/dist/nextjs.mjs +1 -1
- package/dist/react.js +7 -7
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +2 -2
- package/dist/server.js.map +1 -1
- package/dist/types-b4c1koXj.d.mts +19 -0
- package/dist/types-b4c1koXj.d.ts +19 -0
- package/package.json +14 -14
- package/dist/chunk-FHRZSX3C.js.map +0 -1
- package/dist/chunk-NLRREFOX.mjs.map +0 -1
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkCRTRMMJ7js = require('./chunk-CRTRMMJ7.js');
|
|
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
|
+
var _oauth2 = require('oslo/oauth2');
|
|
47
|
+
|
|
48
|
+
// src/lib/oauth.ts
|
|
49
|
+
var _uuid = require('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) => _chunkCRTRMMJ7js.__async.call(void 0, 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.v4.call(void 0, ),
|
|
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
|
+
var _jose = require('jose'); var jose = _interopRequireWildcard(_jose);
|
|
87
|
+
|
|
88
|
+
// src/utils.ts
|
|
89
|
+
var _clsx = require('clsx');
|
|
90
|
+
var _tailwindmerge = require('tailwind-merge');
|
|
91
|
+
var cn = (...inputs) => {
|
|
92
|
+
return _tailwindmerge.twMerge.call(void 0, _clsx.clsx.call(void 0, 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(_chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, this, arguments, function* (oauthServer, endpointOverrides = {}) {
|
|
147
|
+
const endpoints = yield getOauthEndpoints(oauthServer);
|
|
148
|
+
return _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, endpoints), endpointOverrides);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
function generateOauthLoginUrl(config) {
|
|
152
|
+
return _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
179
|
+
return new URL("http://localhost");
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
function buildOauth2Client(clientId, redirectUri, endpoints) {
|
|
183
|
+
return new (0, _oauth2.OAuth2Client)(clientId, endpoints.auth, endpoints.token, {
|
|
184
|
+
redirectURI: redirectUri
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
function exchangeTokens(code, state, pkceProducer, oauth2Client, oauthServer, endpoints) {
|
|
188
|
+
return _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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
|
+
|
|
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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
291
|
+
const verifier = _oauth2.generateCodeVerifier.call(void 0, );
|
|
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 _chunkCRTRMMJ7js.__async.call(void 0, 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
|
+
|
|
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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
376
|
+
return generateOauthLoginUrl(this.config);
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
signOut() {
|
|
380
|
+
return _chunkCRTRMMJ7js.__async.call(void 0, 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(_chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, 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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
400
|
+
this.endpoints = yield getEndpointsWithOverrides(
|
|
401
|
+
this.config.oauthServer,
|
|
402
|
+
this.config.endpointOverrides
|
|
403
|
+
);
|
|
404
|
+
this.oauth2client = new (0, _oauth2.OAuth2Client)(
|
|
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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
461
|
+
try {
|
|
462
|
+
const sessionData = yield this.getSessionData();
|
|
463
|
+
if (!(sessionData == null ? void 0 : sessionData.idToken) || !sessionData.accessToken) {
|
|
464
|
+
const unAuthenticatedSession = _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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
|
+
|
|
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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
513
|
+
this.endpoints = yield getEndpointsWithOverrides(
|
|
514
|
+
this.authConfig.oauthServer,
|
|
515
|
+
this.endpointOverrides
|
|
516
|
+
);
|
|
517
|
+
this.oauth2client = new (0, _oauth2.OAuth2Client)(
|
|
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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
575
|
+
var _a;
|
|
576
|
+
const authSessionService = yield ServerAuthenticationResolver.build(
|
|
577
|
+
_chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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(_chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, 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
|
+
var _jwt = require('oslo/jwt');
|
|
608
|
+
function getUser(storage) {
|
|
609
|
+
return _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
610
|
+
var _a, _b;
|
|
611
|
+
const tokens = retrieveTokens(storage);
|
|
612
|
+
if (!tokens) return null;
|
|
613
|
+
return (_b = (_a = _jwt.parseJWT.call(void 0, tokens.id_token)) == null ? void 0 : _a.payload) != null ? _b : null;
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// src/shared/GenericAuthenticationRefresher.ts
|
|
618
|
+
|
|
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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
627
|
+
this.endpoints = yield getEndpointsWithOverrides(
|
|
628
|
+
this.authConfig.oauthServer,
|
|
629
|
+
this.endpointOverrides
|
|
630
|
+
);
|
|
631
|
+
this.oauth2client = new (0, _oauth2.OAuth2Client)(
|
|
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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, 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 _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () {
|
|
671
|
+
var _a;
|
|
672
|
+
const refresher = yield GenericAuthenticationRefresher.build(
|
|
673
|
+
_chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, 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
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
exports.convertForwardedTokenFormat = convertForwardedTokenFormat; exports.GenericUserSession = GenericUserSession; exports.DEFAULT_SCOPES = DEFAULT_SCOPES; exports.IFRAME_ID = IFRAME_ID; exports.TOKEN_EXCHANGE_TRIGGER_TEXT = TOKEN_EXCHANGE_TRIGGER_TEXT; exports.isWindowInIframe = isWindowInIframe; exports.generateState = generateState; exports.cn = cn; exports.withoutUndefined = withoutUndefined; exports.clearTokens = clearTokens; exports.retrieveTokens = retrieveTokens; exports.LocalStorageAdapter = LocalStorageAdapter; exports.ConfidentialClientPKCEConsumer = ConfidentialClientPKCEConsumer; exports.GenericPublicClientPKCEProducer = GenericPublicClientPKCEProducer; exports.BrowserPublicClientPKCEProducer = BrowserPublicClientPKCEProducer; exports.BrowserAuthenticationInitiator = BrowserAuthenticationInitiator; exports.BrowserAuthenticationService = BrowserAuthenticationService; exports.getUser = getUser; exports.CookieStorage = CookieStorage; exports.resolveOAuthAccessCode = resolveOAuthAccessCode; exports.isLoggedIn = isLoggedIn; exports.buildLoginUrl = buildLoginUrl; exports.refreshTokens = refreshTokens;
|
|
708
|
+
//# sourceMappingURL=chunk-RF23Q4V6.js.map
|