@osdk/oauth 0.1.0 → 0.1.1-main-20240503172030
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/CHANGELOG.md +7 -0
- package/build/js/index.browser.mjs +191 -0
- package/build/js/index.browser.mjs.map +1 -0
- package/build/js/index.cjs +2 -2
- package/build/js/index.cjs.map +1 -1
- package/build/js/index.mjs +2 -2
- package/build/js/index.mjs.map +1 -1
- package/build/types/createPublicOauthClient.d.ts.map +1 -1
- package/package.json +7 -6
- package/changelog/0.1.0/pr-156.v2.yml +0 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import delay from 'delay';
|
|
2
|
+
import { processAuthorizationCodeOAuth2Response, refreshTokenGrantRequest, processRevocationResponse, revocationRequest, isOAuth2Error, customFetch, authorizationCodeGrantRequest, validateAuthResponse, generateRandomState, generateRandomCodeVerifier, calculatePKCECodeChallenge } from 'oauth4webapi';
|
|
3
|
+
import invariant from 'tiny-invariant';
|
|
4
|
+
import { TypedEventTarget } from 'typescript-event-target';
|
|
5
|
+
|
|
6
|
+
// src/createPublicOauthClient.ts
|
|
7
|
+
function throwIfError(result) {
|
|
8
|
+
if (isOAuth2Error(result)) {
|
|
9
|
+
throw new Error(`Failed to get token: ${result.error}`);
|
|
10
|
+
}
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/createPublicOauthClient.ts
|
|
15
|
+
var storageKey = "asdfasdfdhjlkajhgj";
|
|
16
|
+
function saveLocal(x) {
|
|
17
|
+
localStorage.setItem(storageKey, JSON.stringify(x));
|
|
18
|
+
}
|
|
19
|
+
function removeLocal() {
|
|
20
|
+
localStorage.removeItem(storageKey);
|
|
21
|
+
}
|
|
22
|
+
function readLocal() {
|
|
23
|
+
return JSON.parse(localStorage.getItem(storageKey) ?? "{}");
|
|
24
|
+
}
|
|
25
|
+
function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginPage, postLoginPage = window.location.toString(), scopes = ["api:read-data", "api:write-data"], fetchFn = globalThis.fetch, ctxPath = "/multipass") {
|
|
26
|
+
const oauthHttpOptions = {
|
|
27
|
+
[customFetch]: fetchFn
|
|
28
|
+
};
|
|
29
|
+
const eventTarget = new TypedEventTarget();
|
|
30
|
+
const issuer = `${new URL(ctxPath, url)}`;
|
|
31
|
+
const as = {
|
|
32
|
+
token_endpoint: `${issuer}/api/oauth2/token`,
|
|
33
|
+
authorization_endpoint: `${issuer}/api/oauth2/authorize`,
|
|
34
|
+
revocation_endpoint: `${issuer}/api/oauth2/revoke_token`,
|
|
35
|
+
issuer
|
|
36
|
+
};
|
|
37
|
+
const client = {
|
|
38
|
+
client_id,
|
|
39
|
+
token_endpoint_auth_method: "none"
|
|
40
|
+
};
|
|
41
|
+
async function go(x) {
|
|
42
|
+
if (useHistory)
|
|
43
|
+
return window.history.replaceState({}, "", x);
|
|
44
|
+
else
|
|
45
|
+
window.location.assign(x);
|
|
46
|
+
await delay(1e3);
|
|
47
|
+
throw new Error("Unable to redirect");
|
|
48
|
+
}
|
|
49
|
+
function makeTokenAndSaveRefresh(resp, type) {
|
|
50
|
+
const {
|
|
51
|
+
refresh_token,
|
|
52
|
+
expires_in,
|
|
53
|
+
access_token
|
|
54
|
+
} = resp;
|
|
55
|
+
!(expires_in != null) ? process.env.NODE_ENV !== "production" ? invariant(false) : invariant(false) : void 0;
|
|
56
|
+
saveLocal({
|
|
57
|
+
refresh_token
|
|
58
|
+
});
|
|
59
|
+
token = {
|
|
60
|
+
refresh_token,
|
|
61
|
+
expires_in,
|
|
62
|
+
access_token,
|
|
63
|
+
expires_at: Date.now() + expires_in * 1e3
|
|
64
|
+
};
|
|
65
|
+
eventTarget.dispatchTypedEvent(type, new CustomEvent(type, {
|
|
66
|
+
detail: token
|
|
67
|
+
}));
|
|
68
|
+
return token;
|
|
69
|
+
}
|
|
70
|
+
async function maybeRefresh(expectRefreshToken) {
|
|
71
|
+
const {
|
|
72
|
+
refresh_token
|
|
73
|
+
} = readLocal();
|
|
74
|
+
if (!refresh_token) {
|
|
75
|
+
if (expectRefreshToken)
|
|
76
|
+
throw new Error("No refresh token found");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
return makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await refreshTokenGrantRequest(as, client, refresh_token, oauthHttpOptions))), "refresh");
|
|
81
|
+
} catch (e) {
|
|
82
|
+
if (process?.env?.NODE_ENV !== "production") {
|
|
83
|
+
console.warn("Failed to get OAuth2 refresh token. Removing refresh token", e);
|
|
84
|
+
}
|
|
85
|
+
removeLocal();
|
|
86
|
+
if (expectRefreshToken) {
|
|
87
|
+
throw new Error("Could not refresh token");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async function maybeHandleAuthReturn() {
|
|
92
|
+
const {
|
|
93
|
+
codeVerifier,
|
|
94
|
+
state,
|
|
95
|
+
oldUrl
|
|
96
|
+
} = readLocal();
|
|
97
|
+
if (!codeVerifier)
|
|
98
|
+
return;
|
|
99
|
+
try {
|
|
100
|
+
const ret2 = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await authorizationCodeGrantRequest(as, client, throwIfError(validateAuthResponse(as, client, new URL(window.location.href), state)), redirectUrl, codeVerifier, oauthHttpOptions))), "signIn");
|
|
101
|
+
go(oldUrl);
|
|
102
|
+
return ret2;
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (process?.env?.NODE_ENV !== "production") {
|
|
105
|
+
console.warn("Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow", e);
|
|
106
|
+
}
|
|
107
|
+
removeLocal();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async function initiateLoginRedirect() {
|
|
111
|
+
if (loginPage && window.location.href !== loginPage) {
|
|
112
|
+
saveLocal({
|
|
113
|
+
oldUrl: postLoginPage
|
|
114
|
+
});
|
|
115
|
+
return await go(loginPage);
|
|
116
|
+
}
|
|
117
|
+
const state = generateRandomState();
|
|
118
|
+
const codeVerifier = generateRandomCodeVerifier();
|
|
119
|
+
const oldUrl = readLocal().oldUrl ?? window.location.toString();
|
|
120
|
+
saveLocal({
|
|
121
|
+
codeVerifier,
|
|
122
|
+
state,
|
|
123
|
+
oldUrl
|
|
124
|
+
});
|
|
125
|
+
window.location.assign(`${as.authorization_endpoint}?${new URLSearchParams({
|
|
126
|
+
client_id,
|
|
127
|
+
response_type: "code",
|
|
128
|
+
state,
|
|
129
|
+
redirect_uri: redirectUrl,
|
|
130
|
+
code_challenge: await calculatePKCECodeChallenge(codeVerifier),
|
|
131
|
+
code_challenge_method: "S256",
|
|
132
|
+
scope: ["offline_access", ...scopes].join(" ")
|
|
133
|
+
})}`);
|
|
134
|
+
await delay(1e3);
|
|
135
|
+
throw new Error("Unable to redirect");
|
|
136
|
+
}
|
|
137
|
+
let refreshTimeout;
|
|
138
|
+
function rmTimeout() {
|
|
139
|
+
if (refreshTimeout)
|
|
140
|
+
clearTimeout(refreshTimeout);
|
|
141
|
+
}
|
|
142
|
+
function restartRefreshTimer(evt) {
|
|
143
|
+
rmTimeout();
|
|
144
|
+
refreshTimeout = setTimeout(refresh, evt.detail.expires_in * 1e3 - 60 * 1e3);
|
|
145
|
+
}
|
|
146
|
+
const refresh = maybeRefresh.bind(globalThis, true);
|
|
147
|
+
async function signOut() {
|
|
148
|
+
!token ? process.env.NODE_ENV !== "production" ? invariant(false, "not signed in") : invariant(false) : void 0;
|
|
149
|
+
const result = await processRevocationResponse(await revocationRequest(as, client, token.access_token, oauthHttpOptions));
|
|
150
|
+
rmTimeout();
|
|
151
|
+
removeLocal();
|
|
152
|
+
token = void 0;
|
|
153
|
+
throwIfError(result);
|
|
154
|
+
eventTarget.dispatchTypedEvent("signOut", new Event("signOut"));
|
|
155
|
+
}
|
|
156
|
+
let pendingSignIn;
|
|
157
|
+
async function signIn() {
|
|
158
|
+
if (pendingSignIn) {
|
|
159
|
+
return pendingSignIn;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
pendingSignIn = _signIn();
|
|
163
|
+
return await pendingSignIn;
|
|
164
|
+
} finally {
|
|
165
|
+
pendingSignIn = void 0;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async function _signIn() {
|
|
169
|
+
return token = await maybeRefresh() ?? await maybeHandleAuthReturn() ?? await initiateLoginRedirect();
|
|
170
|
+
}
|
|
171
|
+
eventTarget.addEventListener("signIn", restartRefreshTimer);
|
|
172
|
+
eventTarget.addEventListener("refresh", restartRefreshTimer);
|
|
173
|
+
let token;
|
|
174
|
+
const ret = Object.assign(async function ret2() {
|
|
175
|
+
if (!token || Date.now() >= token.expires_at) {
|
|
176
|
+
token = await signIn();
|
|
177
|
+
}
|
|
178
|
+
return token.access_token;
|
|
179
|
+
}, {
|
|
180
|
+
signIn,
|
|
181
|
+
refresh,
|
|
182
|
+
signOut,
|
|
183
|
+
addEventListener: eventTarget.addEventListener.bind(eventTarget),
|
|
184
|
+
removeEventListener: eventTarget.removeEventListener.bind(eventTarget)
|
|
185
|
+
});
|
|
186
|
+
return ret;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export { createPublicOauthClient };
|
|
190
|
+
//# sourceMappingURL=out.js.map
|
|
191
|
+
//# sourceMappingURL=index.browser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/createPublicOauthClient.ts","../../src/throwIfError.ts"],"names":["ret"],"mappings":";AAgBA,OAAO,WAAW;AAClB,SAAS,+BAA+B,4BAA4B,aAAa,4BAA4B,qBAAqB,wCAAwC,2BAA2B,0BAA0B,mBAAmB,4BAA4B;AAC9Q,OAAO,eAAe;AACtB,SAAS,wBAAwB;;;ACHjC,SAAS,qBAAqB;AACvB,SAAS,aAAa,QAAQ;AACnC,MAAI,cAAc,MAAM,GAAG;AACzB,UAAM,IAAI,MAAM,wBAAwB,OAAO,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QAAQ;AACxB,aAAO,MAAM;AAAA,IACf,UAAE;AACA,sBAAgB;AAAA,IAClB;AAAA,EACF;AAEA,iBAAe,UAAU;AAEvB,WAAO,QAAS,MAAM,aAAa,KAE7B,MAAM,sBAAsB,KAE9B,MAAM,sBAAsB;AAAA,EAClC;AACA,cAAY,iBAAiB,UAAU,mBAAmB;AAC1D,cAAY,iBAAiB,WAAW,mBAAmB;AAC3D,MAAI;AACJ,QAAM,MAAM,OAAO,OAAO,eAAeA,OAAM;AAC7C,QAAI,CAAC,SAAS,KAAK,IAAI,KAAK,MAAM,YAAY;AAC5C,cAAQ,MAAM,OAAO;AAAA,IACvB;AACA,WAAO,MAAM;AAAA,EACf,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA,kBAAkB,YAAY,iBAAiB,KAAK,WAAW;AAAA,IAC/D,qBAAqB,YAAY,oBAAoB,KAAK,WAAW;AAAA,EACvE,CAAC;AACD,SAAO;AACT","sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, processRevocationResponse, refreshTokenGrantRequest, revocationRequest, validateAuthResponse } from \"oauth4webapi\";\nimport invariant from \"tiny-invariant\";\nimport { TypedEventTarget } from \"typescript-event-target\";\nimport { throwIfError } from \"./throwIfError.js\";\nconst storageKey = \"asdfasdfdhjlkajhgj\";\nfunction saveLocal(x) {\n localStorage.setItem(storageKey, JSON.stringify(x));\n}\nfunction removeLocal() {\n localStorage.removeItem(storageKey);\n}\nfunction readLocal() {\n return JSON.parse(localStorage.getItem(storageKey) ?? \"{}\");\n}\n\n/**\n * @param client_id\n * @param url the base url of your foundry server\n * @param redirectUrl the url configured for redirect in the oauth configuration on the server\n * @param useHistory if true, when possible uses `history.replaceState()`, otherwise uses `window.location.assign()`\n * @param loginPage if you want a custom landing page prior to logging in, set this to that url\n * @param postLoginPage if you want to customize where you return to after a completed authentication cycle. defaults to the url when the page loaded\n * @param scopes\n * @param fetchFn\n * @param ctxPath\n * @returns {PublicOauthClient} which can be used as a token provider\n */\nexport function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginPage, postLoginPage = window.location.toString(), scopes = [\"api:read-data\", \"api:write-data\"], fetchFn = globalThis.fetch, ctxPath = \"/multipass\") {\n const oauthHttpOptions = {\n [customFetch]: fetchFn\n };\n const eventTarget = new TypedEventTarget();\n const issuer = `${new URL(ctxPath, url)}`;\n const as = {\n token_endpoint: `${issuer}/api/oauth2/token`,\n authorization_endpoint: `${issuer}/api/oauth2/authorize`,\n revocation_endpoint: `${issuer}/api/oauth2/revoke_token`,\n issuer\n };\n const client = {\n client_id,\n token_endpoint_auth_method: \"none\"\n };\n async function go(x) {\n if (useHistory) return window.history.replaceState({}, \"\", x);else window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n function makeTokenAndSaveRefresh(resp, type) {\n const {\n refresh_token,\n expires_in,\n access_token\n } = resp;\n !(expires_in != null) ? process.env.NODE_ENV !== \"production\" ? invariant(false) : invariant(false) : void 0;\n saveLocal({\n refresh_token\n });\n token = {\n refresh_token,\n expires_in,\n access_token,\n expires_at: Date.now() + expires_in * 1000\n };\n eventTarget.dispatchTypedEvent(type, new CustomEvent(type, {\n detail: token\n }));\n return token;\n }\n async function maybeRefresh(expectRefreshToken) {\n const {\n refresh_token\n } = readLocal();\n if (!refresh_token) {\n if (expectRefreshToken) throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we dont use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n return makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await refreshTokenGrantRequest(as, client, refresh_token, oauthHttpOptions))), \"refresh\");\n } catch (e) {\n if (process?.env?.NODE_ENV !== \"production\") {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 refresh token. Removing refresh token\", e);\n }\n removeLocal();\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const {\n codeVerifier,\n state,\n oldUrl\n } = readLocal();\n if (!codeVerifier) return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await authorizationCodeGrantRequest(as, client, throwIfError(validateAuthResponse(as, client, new URL(window.location.href), state)), redirectUrl, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n } catch (e) {\n if (process?.env?.NODE_ENV !== \"production\") {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow\", e);\n }\n removeLocal();\n }\n }\n async function initiateLoginRedirect() {\n if (loginPage && window.location.href !== loginPage) {\n saveLocal({\n oldUrl: postLoginPage\n });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal().oldUrl ?? window.location.toString();\n saveLocal({\n codeVerifier,\n state,\n oldUrl\n });\n window.location.assign(`${as.authorization_endpoint}?${new URLSearchParams({\n client_id,\n response_type: \"code\",\n state,\n redirect_uri: redirectUrl,\n code_challenge: await calculatePKCECodeChallenge(codeVerifier),\n code_challenge_method: \"S256\",\n scope: [\"offline_access\", ...scopes].join(\" \")\n })}`);\n\n // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n let refreshTimeout;\n function rmTimeout() {\n if (refreshTimeout) clearTimeout(refreshTimeout);\n }\n function restartRefreshTimer(evt) {\n rmTimeout();\n refreshTimeout = setTimeout(refresh, evt.detail.expires_in * 1000 - 60 * 1000);\n }\n const refresh = maybeRefresh.bind(globalThis, true);\n async function signOut() {\n !token ? process.env.NODE_ENV !== \"production\" ? invariant(false, \"not signed in\") : invariant(false) : void 0;\n const result = await processRevocationResponse(await revocationRequest(as, client, token.access_token, oauthHttpOptions));\n rmTimeout();\n\n // Clean up\n removeLocal();\n token = undefined;\n throwIfError(result);\n eventTarget.dispatchTypedEvent(\"signOut\", new Event(\"signOut\"));\n }\n let pendingSignIn;\n async function signIn() {\n if (pendingSignIn) {\n return pendingSignIn;\n }\n try {\n pendingSignIn = _signIn();\n return await pendingSignIn;\n } finally {\n pendingSignIn = undefined;\n }\n }\n /** Will throw if there is no token! */\n async function _signIn() {\n // 1. Check if we have a refresh token in local storage\n return token = (await maybeRefresh()\n // 2. If there is no refresh token we are likely trying to perform the callback\n ) ?? (await maybeHandleAuthReturn())\n // 3. If we haven't been able to load the token from one of the two above ways, we need to make the initial auth request\n ?? (await initiateLoginRedirect());\n }\n eventTarget.addEventListener(\"signIn\", restartRefreshTimer);\n eventTarget.addEventListener(\"refresh\", restartRefreshTimer);\n let token;\n const ret = Object.assign(async function ret() {\n if (!token || Date.now() >= token.expires_at) {\n token = await signIn();\n }\n return token.access_token;\n }, {\n signIn,\n refresh,\n signOut,\n addEventListener: eventTarget.addEventListener.bind(eventTarget),\n removeEventListener: eventTarget.removeEventListener.bind(eventTarget)\n });\n return ret;\n}","/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { isOAuth2Error } from \"oauth4webapi\";\nexport function throwIfError(result) {\n if (isOAuth2Error(result)) {\n throw new Error(`Failed to get token: ${result.error}`);\n }\n return result;\n}"]}
|
package/build/js/index.cjs
CHANGED
|
@@ -86,7 +86,7 @@ function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginP
|
|
|
86
86
|
try {
|
|
87
87
|
return makeTokenAndSaveRefresh(throwIfError(await oauth4webapi.processAuthorizationCodeOAuth2Response(as, client, await oauth4webapi.refreshTokenGrantRequest(as, client, refresh_token, oauthHttpOptions))), "refresh");
|
|
88
88
|
} catch (e) {
|
|
89
|
-
if (
|
|
89
|
+
if (process?.env?.NODE_ENV !== "production") {
|
|
90
90
|
console.warn("Failed to get OAuth2 refresh token. Removing refresh token", e);
|
|
91
91
|
}
|
|
92
92
|
removeLocal();
|
|
@@ -108,7 +108,7 @@ function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginP
|
|
|
108
108
|
go(oldUrl);
|
|
109
109
|
return ret2;
|
|
110
110
|
} catch (e) {
|
|
111
|
-
if (
|
|
111
|
+
if (process?.env?.NODE_ENV !== "production") {
|
|
112
112
|
console.warn("Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow", e);
|
|
113
113
|
}
|
|
114
114
|
removeLocal();
|
package/build/js/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/createPublicOauthClient.ts","../../src/throwIfError.ts"],"names":["ret"],"mappings":";AAgBA,OAAO,WAAW;AAClB,SAAS,+BAA+B,4BAA4B,aAAa,4BAA4B,qBAAqB,wCAAwC,2BAA2B,0BAA0B,mBAAmB,4BAA4B;AAC9Q,OAAO,eAAe;AACtB,SAAS,wBAAwB;;;ACHjC,SAAS,qBAAqB;AACvB,SAAS,aAAa,QAAQ;AACnC,MAAI,cAAc,MAAM,GAAG;AACzB,UAAM,IAAI,MAAM,wBAAwB,OAAO,KAAK,EAA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;AACA,sBAAgB;AAAA,IAClB;AAAA,EACF;AAEA,iBAAe,UAAU;AAEvB,WAAO,QAAS,MAAM,aAAa,KAE7B,MAAM,sBAAsB,KAE9B,MAAM,sBAAsB;AAAA,EAClC;AACA,cAAY,iBAAiB,UAAU,mBAAmB;AAC1D,cAAY,iBAAiB,WAAW,mBAAmB;AAC3D,MAAI;AACJ,QAAM,MAAM,OAAO,OAAO,eAAeA,OAAM;AAC7C,QAAI,CAAC,SAAS,KAAK,IAAI,KAAK,MAAM,YAAY;AAC5C,cAAQ,MAAM,OAAO;AAAA,IACvB;AACA,WAAO,MAAM;AAAA,EACf,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA,kBAAkB,YAAY,iBAAiB,KAAK,WAAW;AAAA,IAC/D,qBAAqB,YAAY,oBAAoB,KAAK,WAAW;AAAA,EACvE,CAAC;AACD,SAAO;AACT","sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, processRevocationResponse, refreshTokenGrantRequest, revocationRequest, validateAuthResponse } from \"oauth4webapi\";\nimport invariant from \"tiny-invariant\";\nimport { TypedEventTarget } from \"typescript-event-target\";\nimport { throwIfError } from \"./throwIfError.js\";\nconst storageKey = \"asdfasdfdhjlkajhgj\";\nfunction saveLocal(x) {\n localStorage.setItem(storageKey, JSON.stringify(x));\n}\nfunction removeLocal() {\n localStorage.removeItem(storageKey);\n}\nfunction readLocal() {\n return JSON.parse(localStorage.getItem(storageKey) ?? \"{}\");\n}\n\n/**\n * @param client_id\n * @param url the base url of your foundry server\n * @param redirectUrl the url configured for redirect in the oauth configuration on the server\n * @param useHistory if true, when possible uses `history.replaceState()`, otherwise uses `window.location.assign()`\n * @param loginPage if you want a custom landing page prior to logging in, set this to that url\n * @param postLoginPage if you want to customize where you return to after a completed authentication cycle. defaults to the url when the page loaded\n * @param scopes\n * @param fetchFn\n * @param ctxPath\n * @returns {PublicOauthClient} which can be used as a token provider\n */\nexport function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginPage, postLoginPage = window.location.toString(), scopes = [\"api:read-data\", \"api:write-data\"], fetchFn = globalThis.fetch, ctxPath = \"/multipass\") {\n const oauthHttpOptions = {\n [customFetch]: fetchFn\n };\n const eventTarget = new TypedEventTarget();\n const issuer = `${new URL(ctxPath, url)}`;\n const as = {\n token_endpoint: `${issuer}/api/oauth2/token`,\n authorization_endpoint: `${issuer}/api/oauth2/authorize`,\n revocation_endpoint: `${issuer}/api/oauth2/revoke_token`,\n issuer\n };\n const client = {\n client_id,\n token_endpoint_auth_method: \"none\"\n };\n async function go(x) {\n if (useHistory) return window.history.replaceState({}, \"\", x);else window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n function makeTokenAndSaveRefresh(resp, type) {\n const {\n refresh_token,\n expires_in,\n access_token\n } = resp;\n !(expires_in != null) ? process.env.NODE_ENV !== \"production\" ? invariant(false) : invariant(false) : void 0;\n saveLocal({\n refresh_token\n });\n token = {\n refresh_token,\n expires_in,\n access_token,\n expires_at: Date.now() + expires_in * 1000\n };\n eventTarget.dispatchTypedEvent(type, new CustomEvent(type, {\n detail: token\n }));\n return token;\n }\n async function maybeRefresh(expectRefreshToken) {\n const {\n refresh_token\n } = readLocal();\n if (!refresh_token) {\n if (expectRefreshToken) throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we dont use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n return makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await refreshTokenGrantRequest(as, client, refresh_token, oauthHttpOptions))), \"refresh\");\n } catch (e) {\n if (!__DEV__) {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 refresh token. Removing refresh token\", e);\n }\n removeLocal();\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const {\n codeVerifier,\n state,\n oldUrl\n } = readLocal();\n if (!codeVerifier) return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await authorizationCodeGrantRequest(as, client, throwIfError(validateAuthResponse(as, client, new URL(window.location.href), state)), redirectUrl, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n } catch (e) {\n if (__DEV__) {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow\", e);\n }\n removeLocal();\n }\n }\n async function initiateLoginRedirect() {\n if (loginPage && window.location.href !== loginPage) {\n saveLocal({\n oldUrl: postLoginPage\n });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal().oldUrl ?? window.location.toString();\n saveLocal({\n codeVerifier,\n state,\n oldUrl\n });\n window.location.assign(`${as.authorization_endpoint}?${new URLSearchParams({\n client_id,\n response_type: \"code\",\n state,\n redirect_uri: redirectUrl,\n code_challenge: await calculatePKCECodeChallenge(codeVerifier),\n code_challenge_method: \"S256\",\n scope: [\"offline_access\", ...scopes].join(\" \")\n })}`);\n\n // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n let refreshTimeout;\n function rmTimeout() {\n if (refreshTimeout) clearTimeout(refreshTimeout);\n }\n function restartRefreshTimer(evt) {\n rmTimeout();\n refreshTimeout = setTimeout(refresh, evt.detail.expires_in * 1000 - 60 * 1000);\n }\n const refresh = maybeRefresh.bind(globalThis, true);\n async function signOut() {\n !token ? process.env.NODE_ENV !== \"production\" ? invariant(false, \"not signed in\") : invariant(false) : void 0;\n const result = await processRevocationResponse(await revocationRequest(as, client, token.access_token, oauthHttpOptions));\n rmTimeout();\n\n // Clean up\n removeLocal();\n token = undefined;\n throwIfError(result);\n eventTarget.dispatchTypedEvent(\"signOut\", new Event(\"signOut\"));\n }\n let pendingSignIn;\n async function signIn() {\n if (pendingSignIn) {\n return pendingSignIn;\n }\n try {\n pendingSignIn = _signIn();\n return await pendingSignIn;\n } finally {\n pendingSignIn = undefined;\n }\n }\n /** Will throw if there is no token! */\n async function _signIn() {\n // 1. Check if we have a refresh token in local storage\n return token = (await maybeRefresh()\n // 2. If there is no refresh token we are likely trying to perform the callback\n ) ?? (await maybeHandleAuthReturn())\n // 3. If we haven't been able to load the token from one of the two above ways, we need to make the initial auth request\n ?? (await initiateLoginRedirect());\n }\n eventTarget.addEventListener(\"signIn\", restartRefreshTimer);\n eventTarget.addEventListener(\"refresh\", restartRefreshTimer);\n let token;\n const ret = Object.assign(async function ret() {\n if (!token || Date.now() >= token.expires_at) {\n token = await signIn();\n }\n return token.access_token;\n }, {\n signIn,\n refresh,\n signOut,\n addEventListener: eventTarget.addEventListener.bind(eventTarget),\n removeEventListener: eventTarget.removeEventListener.bind(eventTarget)\n });\n return ret;\n}","/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { isOAuth2Error } from \"oauth4webapi\";\nexport function throwIfError(result) {\n if (isOAuth2Error(result)) {\n throw new Error(`Failed to get token: ${result.error}`);\n }\n return result;\n}"]}
|
|
1
|
+
{"version":3,"sources":["../../src/createPublicOauthClient.ts","../../src/throwIfError.ts"],"names":["ret"],"mappings":";AAgBA,OAAO,WAAW;AAClB,SAAS,+BAA+B,4BAA4B,aAAa,4BAA4B,qBAAqB,wCAAwC,2BAA2B,0BAA0B,mBAAmB,4BAA4B;AAC9Q,OAAO,eAAe;AACtB,SAAS,wBAAwB;;;ACHjC,SAAS,qBAAqB;AACvB,SAAS,aAAa,QAAQ;AACnC,MAAI,cAAc,MAAM,GAAG;AACzB,UAAM,IAAI,MAAM,wBAAwB,OAAO,KAAK,EAAE;AAAA,EACxD;AACA,SAAO;AACT;;;ADDA,IAAM,aAAa;AACnB,SAAS,UAAU,GAAG;AACpB,eAAa,QAAQ,YAAY,KAAK,UAAU,CAAC,CAAC;AACpD;AACA,SAAS,cAAc;AACrB,eAAa,WAAW,UAAU;AACpC;AACA,SAAS,YAAY;AACnB,SAAO,KAAK,MAAM,aAAa,QAAQ,UAAU,KAAK,IAAI;AAC5D;AAcO,SAAS,wBAAwB,WAAW,KAAK,aAAa,YAAY,WAAW,gBAAgB,OAAO,SAAS,SAAS,GAAG,SAAS,CAAC,iBAAiB,gBAAgB,GAAG,UAAU,WAAW,OAAO,UAAU,cAAc;AACxO,QAAM,mBAAmB;AAAA,IACvB,CAAC,WAAW,GAAG;AAAA,EACjB;AACA,QAAM,cAAc,IAAI,iBAAiB;AACzC,QAAM,SAAS,GAAG,IAAI,IAAI,SAAS,GAAG,CAAC;AACvC,QAAM,KAAK;AAAA,IACT,gBAAgB,GAAG,MAAM;AAAA,IACzB,wBAAwB,GAAG,MAAM;AAAA,IACjC,qBAAqB,GAAG,MAAM;AAAA,IAC9B;AAAA,EACF;AACA,QAAM,SAAS;AAAA,IACb;AAAA,IACA,4BAA4B;AAAA,EAC9B;AACA,iBAAe,GAAG,GAAG;AACnB,QAAI;AAAY,aAAO,O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K,YAAY,IAAI;AAClD,iBAAe,UAAU;AACvB,KAAC,QAAQ,QAAQ,IAAI,aAAa,eAAe,UAAU,OAAO,eAAe,IAAI,UAAU,KAAK,IAAI;AACxG,UAAM,SAAS,MAAM,0BAA0B,MAAM,kBAAkB,IAAI,QAAQ,MAAM,cAAc,gBAAgB,CAAC;AACxH,cAAU;AAGV,gBAAY;AACZ,YAAQ;AACR,iBAAa,MAAM;AACnB,gBAAY,mBAAmB,WAAW,IAAI,MAAM,SAAS,CAAC;AAAA,EAChE;AACA,MAAI;AACJ,iBAAe,SAAS;AACtB,QAAI,eAAe;AACjB,aAAO;AAAA,IACT;AACA,QAAI;AACF,sBAAgB,QAAQ;AACxB,aAAO,MAAM;AAAA,IACf,UAAE;AACA,sBAAgB;AAAA,IAClB;AAAA,EACF;AAEA,iBAAe,UAAU;AAEvB,WAAO,QAAS,MAAM,aAAa,KAE7B,MAAM,sBAAsB,KAE9B,MAAM,sBAAsB;AAAA,EAClC;AACA,cAAY,iBAAiB,UAAU,mBAAmB;AAC1D,cAAY,iBAAiB,WAAW,mBAAmB;AAC3D,MAAI;AACJ,QAAM,MAAM,OAAO,OAAO,eAAeA,OAAM;AAC7C,QAAI,CAAC,SAAS,KAAK,IAAI,KAAK,MAAM,YAAY;AAC5C,cAAQ,MAAM,OAAO;AAAA,IACvB;AACA,WAAO,MAAM;AAAA,EACf,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA,kBAAkB,YAAY,iBAAiB,KAAK,WAAW;AAAA,IAC/D,qBAAqB,YAAY,oBAAoB,KAAK,WAAW;AAAA,EACvE,CAAC;AACD,SAAO;AACT","sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, processRevocationResponse, refreshTokenGrantRequest, revocationRequest, validateAuthResponse } from \"oauth4webapi\";\nimport invariant from \"tiny-invariant\";\nimport { TypedEventTarget } from \"typescript-event-target\";\nimport { throwIfError } from \"./throwIfError.js\";\nconst storageKey = \"asdfasdfdhjlkajhgj\";\nfunction saveLocal(x) {\n localStorage.setItem(storageKey, JSON.stringify(x));\n}\nfunction removeLocal() {\n localStorage.removeItem(storageKey);\n}\nfunction readLocal() {\n return JSON.parse(localStorage.getItem(storageKey) ?? \"{}\");\n}\n\n/**\n * @param client_id\n * @param url the base url of your foundry server\n * @param redirectUrl the url configured for redirect in the oauth configuration on the server\n * @param useHistory if true, when possible uses `history.replaceState()`, otherwise uses `window.location.assign()`\n * @param loginPage if you want a custom landing page prior to logging in, set this to that url\n * @param postLoginPage if you want to customize where you return to after a completed authentication cycle. defaults to the url when the page loaded\n * @param scopes\n * @param fetchFn\n * @param ctxPath\n * @returns {PublicOauthClient} which can be used as a token provider\n */\nexport function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginPage, postLoginPage = window.location.toString(), scopes = [\"api:read-data\", \"api:write-data\"], fetchFn = globalThis.fetch, ctxPath = \"/multipass\") {\n const oauthHttpOptions = {\n [customFetch]: fetchFn\n };\n const eventTarget = new TypedEventTarget();\n const issuer = `${new URL(ctxPath, url)}`;\n const as = {\n token_endpoint: `${issuer}/api/oauth2/token`,\n authorization_endpoint: `${issuer}/api/oauth2/authorize`,\n revocation_endpoint: `${issuer}/api/oauth2/revoke_token`,\n issuer\n };\n const client = {\n client_id,\n token_endpoint_auth_method: \"none\"\n };\n async function go(x) {\n if (useHistory) return window.history.replaceState({}, \"\", x);else window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n function makeTokenAndSaveRefresh(resp, type) {\n const {\n refresh_token,\n expires_in,\n access_token\n } = resp;\n !(expires_in != null) ? process.env.NODE_ENV !== \"production\" ? invariant(false) : invariant(false) : void 0;\n saveLocal({\n refresh_token\n });\n token = {\n refresh_token,\n expires_in,\n access_token,\n expires_at: Date.now() + expires_in * 1000\n };\n eventTarget.dispatchTypedEvent(type, new CustomEvent(type, {\n detail: token\n }));\n return token;\n }\n async function maybeRefresh(expectRefreshToken) {\n const {\n refresh_token\n } = readLocal();\n if (!refresh_token) {\n if (expectRefreshToken) throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we dont use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n return makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await refreshTokenGrantRequest(as, client, refresh_token, oauthHttpOptions))), \"refresh\");\n } catch (e) {\n if (process?.env?.NODE_ENV !== \"production\") {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 refresh token. Removing refresh token\", e);\n }\n removeLocal();\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const {\n codeVerifier,\n state,\n oldUrl\n } = readLocal();\n if (!codeVerifier) return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await authorizationCodeGrantRequest(as, client, throwIfError(validateAuthResponse(as, client, new URL(window.location.href), state)), redirectUrl, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n } catch (e) {\n if (process?.env?.NODE_ENV !== \"production\") {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow\", e);\n }\n removeLocal();\n }\n }\n async function initiateLoginRedirect() {\n if (loginPage && window.location.href !== loginPage) {\n saveLocal({\n oldUrl: postLoginPage\n });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal().oldUrl ?? window.location.toString();\n saveLocal({\n codeVerifier,\n state,\n oldUrl\n });\n window.location.assign(`${as.authorization_endpoint}?${new URLSearchParams({\n client_id,\n response_type: \"code\",\n state,\n redirect_uri: redirectUrl,\n code_challenge: await calculatePKCECodeChallenge(codeVerifier),\n code_challenge_method: \"S256\",\n scope: [\"offline_access\", ...scopes].join(\" \")\n })}`);\n\n // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n let refreshTimeout;\n function rmTimeout() {\n if (refreshTimeout) clearTimeout(refreshTimeout);\n }\n function restartRefreshTimer(evt) {\n rmTimeout();\n refreshTimeout = setTimeout(refresh, evt.detail.expires_in * 1000 - 60 * 1000);\n }\n const refresh = maybeRefresh.bind(globalThis, true);\n async function signOut() {\n !token ? process.env.NODE_ENV !== \"production\" ? invariant(false, \"not signed in\") : invariant(false) : void 0;\n const result = await processRevocationResponse(await revocationRequest(as, client, token.access_token, oauthHttpOptions));\n rmTimeout();\n\n // Clean up\n removeLocal();\n token = undefined;\n throwIfError(result);\n eventTarget.dispatchTypedEvent(\"signOut\", new Event(\"signOut\"));\n }\n let pendingSignIn;\n async function signIn() {\n if (pendingSignIn) {\n return pendingSignIn;\n }\n try {\n pendingSignIn = _signIn();\n return await pendingSignIn;\n } finally {\n pendingSignIn = undefined;\n }\n }\n /** Will throw if there is no token! */\n async function _signIn() {\n // 1. Check if we have a refresh token in local storage\n return token = (await maybeRefresh()\n // 2. If there is no refresh token we are likely trying to perform the callback\n ) ?? (await maybeHandleAuthReturn())\n // 3. If we haven't been able to load the token from one of the two above ways, we need to make the initial auth request\n ?? (await initiateLoginRedirect());\n }\n eventTarget.addEventListener(\"signIn\", restartRefreshTimer);\n eventTarget.addEventListener(\"refresh\", restartRefreshTimer);\n let token;\n const ret = Object.assign(async function ret() {\n if (!token || Date.now() >= token.expires_at) {\n token = await signIn();\n }\n return token.access_token;\n }, {\n signIn,\n refresh,\n signOut,\n addEventListener: eventTarget.addEventListener.bind(eventTarget),\n removeEventListener: eventTarget.removeEventListener.bind(eventTarget)\n });\n return ret;\n}","/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { isOAuth2Error } from \"oauth4webapi\";\nexport function throwIfError(result) {\n if (isOAuth2Error(result)) {\n throw new Error(`Failed to get token: ${result.error}`);\n }\n return result;\n}"]}
|
package/build/js/index.mjs
CHANGED
|
@@ -79,7 +79,7 @@ function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginP
|
|
|
79
79
|
try {
|
|
80
80
|
return makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await refreshTokenGrantRequest(as, client, refresh_token, oauthHttpOptions))), "refresh");
|
|
81
81
|
} catch (e) {
|
|
82
|
-
if (
|
|
82
|
+
if (process?.env?.NODE_ENV !== "production") {
|
|
83
83
|
console.warn("Failed to get OAuth2 refresh token. Removing refresh token", e);
|
|
84
84
|
}
|
|
85
85
|
removeLocal();
|
|
@@ -101,7 +101,7 @@ function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginP
|
|
|
101
101
|
go(oldUrl);
|
|
102
102
|
return ret2;
|
|
103
103
|
} catch (e) {
|
|
104
|
-
if (
|
|
104
|
+
if (process?.env?.NODE_ENV !== "production") {
|
|
105
105
|
console.warn("Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow", e);
|
|
106
106
|
}
|
|
107
107
|
removeLocal();
|
package/build/js/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/createPublicOauthClient.ts","../../src/throwIfError.ts"],"names":["ret"],"mappings":";AAgBA,OAAO,WAAW;AAClB,SAAS,+BAA+B,4BAA4B,aAAa,4BAA4B,qBAAqB,wCAAwC,2BAA2B,0BAA0B,mBAAmB,4BAA4B;AAC9Q,OAAO,eAAe;AACtB,SAAS,wBAAwB;;;ACHjC,SAAS,qBAAqB;AACvB,SAAS,aAAa,QAAQ;AACnC,MAAI,cAAc,MAAM,GAAG;AACzB,UAAM,IAAI,MAAM,wBAAwB,OAAO,KAAK,EAAE;AAAA,EACxD;AACA,SAAO;AACT;;;ADDA,IAAM,aAAa;AACnB,SAAS,UAAU,GAAG;AACpB,eAAa,QAAQ,YAAY,KAAK,UAAU,CAAC,CAAC;AACpD;AACA,SAAS,cAAc;AACrB,eAAa,WAAW,UAAU;AACpC;AACA,SAAS,YAAY;AACnB,SAAO,KAAK,MAAM,aAAa,QAAQ,UAAU,KAAK,IAAI;AAC5D;AAcO,SAAS,wBAAwB,WAAW,KAAK,aAAa,YAAY,WAAW,gBAAgB,OAAO,SAAS,SAAS,GAAG,SAAS,CAAC,iBAAiB,gBAAgB,GAAG,UAAU,WAAW,OAAO,UAAU,cAAc;AACxO,QAAM,mBAAmB;AAAA,IACvB,CAAC,WAAW,GAAG;AAAA,EACjB;AACA,QAAM,cAAc,IAAI,iBAAiB;AACzC,QAAM,SAAS,GAAG,IAAI,IAAI,SAAS,GAAG,CAAC;AACvC,QAAM,KAAK;AAAA,IACT,gBAAgB,GAAG,MAAM;AAAA,IACzB,wBAAwB,GAAG,MAAM;AAAA,IACjC,qBAAqB,GAAG,MAAM;AAAA,IAC9B;AAAA,EACF;AACA,QAAM,SAAS;AAAA,IACb;AAAA,IACA,4BAA4B;AAAA,EAC9B;AACA,iBAAe,GAAG,GAAG;AACnB,QAAI;AAAY,aAAO,OAAO,QAAQ,aAAa,CAAC,GAAG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IAAI,aAAa,eAAe,UAAU,OAAO,eAAe,IAAI,UAAU,KAAK,IAAI;AACxG,UAAM,SAAS,MAAM,0BAA0B,MAAM,kBAAkB,IAAI,QAAQ,MAAM,cAAc,gBAAgB,CAAC;AACxH,cAAU;AAGV,gBAAY;AACZ,YAAQ;AACR,iBAAa,MAAM;AACnB,gBAAY,mBAAmB,WAAW,IAAI,MAAM,SAAS,CAAC;AAAA,EAChE;AACA,MAAI;AACJ,iBAAe,SAAS;AACtB,QAAI,eAAe;AACjB,aAAO;AAAA,IACT;AACA,QAAI;AACF,sBAAgB,QAAQ;AACxB,aAAO,MAAM;AAAA,IACf,UAAE;AACA,sBAAgB;AAAA,IAClB;AAAA,EACF;AAEA,iBAAe,UAAU;AAEvB,WAAO,QAAS,MAAM,aAAa,KAE7B,MAAM,sBAAsB,KAE9B,MAAM,sBAAsB;AAAA,EAClC;AACA,cAAY,iBAAiB,UAAU,mBAAmB;AAC1D,cAAY,iBAAiB,WAAW,mBAAmB;AAC3D,MAAI;AACJ,QAAM,MAAM,OAAO,OAAO,eAAeA,OAAM;AAC7C,QAAI,CAAC,SAAS,KAAK,IAAI,KAAK,MAAM,YAAY;AAC5C,cAAQ,MAAM,OAAO;AAAA,IACvB;AACA,WAAO,MAAM;AAAA,EACf,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA,kBAAkB,YAAY,iBAAiB,KAAK,WAAW;AAAA,IAC/D,qBAAqB,YAAY,oBAAoB,KAAK,WAAW;AAAA,EACvE,CAAC;AACD,SAAO;AACT","sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, processRevocationResponse, refreshTokenGrantRequest, revocationRequest, validateAuthResponse } from \"oauth4webapi\";\nimport invariant from \"tiny-invariant\";\nimport { TypedEventTarget } from \"typescript-event-target\";\nimport { throwIfError } from \"./throwIfError.js\";\nconst storageKey = \"asdfasdfdhjlkajhgj\";\nfunction saveLocal(x) {\n localStorage.setItem(storageKey, JSON.stringify(x));\n}\nfunction removeLocal() {\n localStorage.removeItem(storageKey);\n}\nfunction readLocal() {\n return JSON.parse(localStorage.getItem(storageKey) ?? \"{}\");\n}\n\n/**\n * @param client_id\n * @param url the base url of your foundry server\n * @param redirectUrl the url configured for redirect in the oauth configuration on the server\n * @param useHistory if true, when possible uses `history.replaceState()`, otherwise uses `window.location.assign()`\n * @param loginPage if you want a custom landing page prior to logging in, set this to that url\n * @param postLoginPage if you want to customize where you return to after a completed authentication cycle. defaults to the url when the page loaded\n * @param scopes\n * @param fetchFn\n * @param ctxPath\n * @returns {PublicOauthClient} which can be used as a token provider\n */\nexport function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginPage, postLoginPage = window.location.toString(), scopes = [\"api:read-data\", \"api:write-data\"], fetchFn = globalThis.fetch, ctxPath = \"/multipass\") {\n const oauthHttpOptions = {\n [customFetch]: fetchFn\n };\n const eventTarget = new TypedEventTarget();\n const issuer = `${new URL(ctxPath, url)}`;\n const as = {\n token_endpoint: `${issuer}/api/oauth2/token`,\n authorization_endpoint: `${issuer}/api/oauth2/authorize`,\n revocation_endpoint: `${issuer}/api/oauth2/revoke_token`,\n issuer\n };\n const client = {\n client_id,\n token_endpoint_auth_method: \"none\"\n };\n async function go(x) {\n if (useHistory) return window.history.replaceState({}, \"\", x);else window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n function makeTokenAndSaveRefresh(resp, type) {\n const {\n refresh_token,\n expires_in,\n access_token\n } = resp;\n !(expires_in != null) ? process.env.NODE_ENV !== \"production\" ? invariant(false) : invariant(false) : void 0;\n saveLocal({\n refresh_token\n });\n token = {\n refresh_token,\n expires_in,\n access_token,\n expires_at: Date.now() + expires_in * 1000\n };\n eventTarget.dispatchTypedEvent(type, new CustomEvent(type, {\n detail: token\n }));\n return token;\n }\n async function maybeRefresh(expectRefreshToken) {\n const {\n refresh_token\n } = readLocal();\n if (!refresh_token) {\n if (expectRefreshToken) throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we dont use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n return makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await refreshTokenGrantRequest(as, client, refresh_token, oauthHttpOptions))), \"refresh\");\n } catch (e) {\n if (!__DEV__) {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 refresh token. Removing refresh token\", e);\n }\n removeLocal();\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const {\n codeVerifier,\n state,\n oldUrl\n } = readLocal();\n if (!codeVerifier) return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await authorizationCodeGrantRequest(as, client, throwIfError(validateAuthResponse(as, client, new URL(window.location.href), state)), redirectUrl, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n } catch (e) {\n if (__DEV__) {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow\", e);\n }\n removeLocal();\n }\n }\n async function initiateLoginRedirect() {\n if (loginPage && window.location.href !== loginPage) {\n saveLocal({\n oldUrl: postLoginPage\n });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal().oldUrl ?? window.location.toString();\n saveLocal({\n codeVerifier,\n state,\n oldUrl\n });\n window.location.assign(`${as.authorization_endpoint}?${new URLSearchParams({\n client_id,\n response_type: \"code\",\n state,\n redirect_uri: redirectUrl,\n code_challenge: await calculatePKCECodeChallenge(codeVerifier),\n code_challenge_method: \"S256\",\n scope: [\"offline_access\", ...scopes].join(\" \")\n })}`);\n\n // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n let refreshTimeout;\n function rmTimeout() {\n if (refreshTimeout) clearTimeout(refreshTimeout);\n }\n function restartRefreshTimer(evt) {\n rmTimeout();\n refreshTimeout = setTimeout(refresh, evt.detail.expires_in * 1000 - 60 * 1000);\n }\n const refresh = maybeRefresh.bind(globalThis, true);\n async function signOut() {\n !token ? process.env.NODE_ENV !== \"production\" ? invariant(false, \"not signed in\") : invariant(false) : void 0;\n const result = await processRevocationResponse(await revocationRequest(as, client, token.access_token, oauthHttpOptions));\n rmTimeout();\n\n // Clean up\n removeLocal();\n token = undefined;\n throwIfError(result);\n eventTarget.dispatchTypedEvent(\"signOut\", new Event(\"signOut\"));\n }\n let pendingSignIn;\n async function signIn() {\n if (pendingSignIn) {\n return pendingSignIn;\n }\n try {\n pendingSignIn = _signIn();\n return await pendingSignIn;\n } finally {\n pendingSignIn = undefined;\n }\n }\n /** Will throw if there is no token! */\n async function _signIn() {\n // 1. Check if we have a refresh token in local storage\n return token = (await maybeRefresh()\n // 2. If there is no refresh token we are likely trying to perform the callback\n ) ?? (await maybeHandleAuthReturn())\n // 3. If we haven't been able to load the token from one of the two above ways, we need to make the initial auth request\n ?? (await initiateLoginRedirect());\n }\n eventTarget.addEventListener(\"signIn\", restartRefreshTimer);\n eventTarget.addEventListener(\"refresh\", restartRefreshTimer);\n let token;\n const ret = Object.assign(async function ret() {\n if (!token || Date.now() >= token.expires_at) {\n token = await signIn();\n }\n return token.access_token;\n }, {\n signIn,\n refresh,\n signOut,\n addEventListener: eventTarget.addEventListener.bind(eventTarget),\n removeEventListener: eventTarget.removeEventListener.bind(eventTarget)\n });\n return ret;\n}","/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { isOAuth2Error } from \"oauth4webapi\";\nexport function throwIfError(result) {\n if (isOAuth2Error(result)) {\n throw new Error(`Failed to get token: ${result.error}`);\n }\n return result;\n}"]}
|
|
1
|
+
{"version":3,"sources":["../../src/createPublicOauthClient.ts","../../src/throwIfError.ts"],"names":["ret"],"mappings":";AAgBA,OAAO,WAAW;AAClB,SAAS,+BAA+B,4BAA4B,aAAa,4BAA4B,qBAAqB,wCAAwC,2BAA2B,0BAA0B,mBAAmB,4BAA4B;AAC9Q,OAAO,eAAe;AACtB,SAAS,wBAAwB;;;ACHjC,SAAS,qBAAqB;AACvB,SAAS,aAAa,QAAQ;AACnC,MAAI,cAAc,MAAM,GAAG;AACzB,UAAM,IAAI,MAAM,wBAAwB,OAAO,KAAK,EAAE;AAAA,EACxD;AACA,SAAO;AACT;;;ADDA,IAAM,aAAa;AACnB,SAAS,UAAU,GAAG;AACpB,eAAa,QAAQ,YAAY,KAAK,UAAU,CAAC,CAAC;AACpD;AACA,SAAS,cAAc;AACrB,eAAa,WAAW,UAAU;AACpC;AACA,SAAS,YAAY;AACnB,SAAO,KAAK,MAAM,aAAa,QAAQ,UAAU,KAAK,IAAI;AAC5D;AAcO,SAAS,wBAAwB,WAAW,KAAK,aAAa,YAAY,WAAW,gBAAgB,OAAO,SAAS,SAAS,GAAG,SAAS,CAAC,iBAAiB,gBAAgB,GAAG,UAAU,WAAW,OAAO,UAAU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A,WAAO,MAAM;AAAA,EACf,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA,kBAAkB,YAAY,iBAAiB,KAAK,WAAW;AAAA,IAC/D,qBAAqB,YAAY,oBAAoB,KAAK,WAAW;AAAA,EACvE,CAAC;AACD,SAAO;AACT","sourcesContent":["/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, processRevocationResponse, refreshTokenGrantRequest, revocationRequest, validateAuthResponse } from \"oauth4webapi\";\nimport invariant from \"tiny-invariant\";\nimport { TypedEventTarget } from \"typescript-event-target\";\nimport { throwIfError } from \"./throwIfError.js\";\nconst storageKey = \"asdfasdfdhjlkajhgj\";\nfunction saveLocal(x) {\n localStorage.setItem(storageKey, JSON.stringify(x));\n}\nfunction removeLocal() {\n localStorage.removeItem(storageKey);\n}\nfunction readLocal() {\n return JSON.parse(localStorage.getItem(storageKey) ?? \"{}\");\n}\n\n/**\n * @param client_id\n * @param url the base url of your foundry server\n * @param redirectUrl the url configured for redirect in the oauth configuration on the server\n * @param useHistory if true, when possible uses `history.replaceState()`, otherwise uses `window.location.assign()`\n * @param loginPage if you want a custom landing page prior to logging in, set this to that url\n * @param postLoginPage if you want to customize where you return to after a completed authentication cycle. defaults to the url when the page loaded\n * @param scopes\n * @param fetchFn\n * @param ctxPath\n * @returns {PublicOauthClient} which can be used as a token provider\n */\nexport function createPublicOauthClient(client_id, url, redirectUrl, useHistory, loginPage, postLoginPage = window.location.toString(), scopes = [\"api:read-data\", \"api:write-data\"], fetchFn = globalThis.fetch, ctxPath = \"/multipass\") {\n const oauthHttpOptions = {\n [customFetch]: fetchFn\n };\n const eventTarget = new TypedEventTarget();\n const issuer = `${new URL(ctxPath, url)}`;\n const as = {\n token_endpoint: `${issuer}/api/oauth2/token`,\n authorization_endpoint: `${issuer}/api/oauth2/authorize`,\n revocation_endpoint: `${issuer}/api/oauth2/revoke_token`,\n issuer\n };\n const client = {\n client_id,\n token_endpoint_auth_method: \"none\"\n };\n async function go(x) {\n if (useHistory) return window.history.replaceState({}, \"\", x);else window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n function makeTokenAndSaveRefresh(resp, type) {\n const {\n refresh_token,\n expires_in,\n access_token\n } = resp;\n !(expires_in != null) ? process.env.NODE_ENV !== \"production\" ? invariant(false) : invariant(false) : void 0;\n saveLocal({\n refresh_token\n });\n token = {\n refresh_token,\n expires_in,\n access_token,\n expires_at: Date.now() + expires_in * 1000\n };\n eventTarget.dispatchTypedEvent(type, new CustomEvent(type, {\n detail: token\n }));\n return token;\n }\n async function maybeRefresh(expectRefreshToken) {\n const {\n refresh_token\n } = readLocal();\n if (!refresh_token) {\n if (expectRefreshToken) throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we dont use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n return makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await refreshTokenGrantRequest(as, client, refresh_token, oauthHttpOptions))), \"refresh\");\n } catch (e) {\n if (process?.env?.NODE_ENV !== \"production\") {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 refresh token. Removing refresh token\", e);\n }\n removeLocal();\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const {\n codeVerifier,\n state,\n oldUrl\n } = readLocal();\n if (!codeVerifier) return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(as, client, await authorizationCodeGrantRequest(as, client, throwIfError(validateAuthResponse(as, client, new URL(window.location.href), state)), redirectUrl, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n } catch (e) {\n if (process?.env?.NODE_ENV !== \"production\") {\n // eslint-disable-next-line no-console\n console.warn(\"Failed to get OAuth2 token using PCKE, removing PCKE and starting a new auth flow\", e);\n }\n removeLocal();\n }\n }\n async function initiateLoginRedirect() {\n if (loginPage && window.location.href !== loginPage) {\n saveLocal({\n oldUrl: postLoginPage\n });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal().oldUrl ?? window.location.toString();\n saveLocal({\n codeVerifier,\n state,\n oldUrl\n });\n window.location.assign(`${as.authorization_endpoint}?${new URLSearchParams({\n client_id,\n response_type: \"code\",\n state,\n redirect_uri: redirectUrl,\n code_challenge: await calculatePKCECodeChallenge(codeVerifier),\n code_challenge_method: \"S256\",\n scope: [\"offline_access\", ...scopes].join(\" \")\n })}`);\n\n // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n let refreshTimeout;\n function rmTimeout() {\n if (refreshTimeout) clearTimeout(refreshTimeout);\n }\n function restartRefreshTimer(evt) {\n rmTimeout();\n refreshTimeout = setTimeout(refresh, evt.detail.expires_in * 1000 - 60 * 1000);\n }\n const refresh = maybeRefresh.bind(globalThis, true);\n async function signOut() {\n !token ? process.env.NODE_ENV !== \"production\" ? invariant(false, \"not signed in\") : invariant(false) : void 0;\n const result = await processRevocationResponse(await revocationRequest(as, client, token.access_token, oauthHttpOptions));\n rmTimeout();\n\n // Clean up\n removeLocal();\n token = undefined;\n throwIfError(result);\n eventTarget.dispatchTypedEvent(\"signOut\", new Event(\"signOut\"));\n }\n let pendingSignIn;\n async function signIn() {\n if (pendingSignIn) {\n return pendingSignIn;\n }\n try {\n pendingSignIn = _signIn();\n return await pendingSignIn;\n } finally {\n pendingSignIn = undefined;\n }\n }\n /** Will throw if there is no token! */\n async function _signIn() {\n // 1. Check if we have a refresh token in local storage\n return token = (await maybeRefresh()\n // 2. If there is no refresh token we are likely trying to perform the callback\n ) ?? (await maybeHandleAuthReturn())\n // 3. If we haven't been able to load the token from one of the two above ways, we need to make the initial auth request\n ?? (await initiateLoginRedirect());\n }\n eventTarget.addEventListener(\"signIn\", restartRefreshTimer);\n eventTarget.addEventListener(\"refresh\", restartRefreshTimer);\n let token;\n const ret = Object.assign(async function ret() {\n if (!token || Date.now() >= token.expires_at) {\n token = await signIn();\n }\n return token.access_token;\n }, {\n signIn,\n refresh,\n signOut,\n addEventListener: eventTarget.addEventListener.bind(eventTarget),\n removeEventListener: eventTarget.removeEventListener.bind(eventTarget)\n });\n return ret;\n}","/*\n * Copyright 2024 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { isOAuth2Error } from \"oauth4webapi\";\nexport function throwIfError(result) {\n if (isOAuth2Error(result)) {\n throw new Error(`Failed to get token: ${result.error}`);\n }\n return result;\n}"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createPublicOauthClient.d.ts","sourceRoot":"","sources":["../../src/createPublicOauthClient.ts"],"names":[],"mappings":"AAqCA,OAAO,KAAK,EAAU,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.d.ts","sourceRoot":"","sources":["../../src/createPublicOauthClient.ts"],"names":[],"mappings":"AAqCA,OAAO,KAAK,EAAU,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAmDxE;;;;;;;;;;;GAWG;AACH,wBAAgB,uBAAuB,CACrC,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,EACnB,UAAU,CAAC,EAAE,OAAO,EACpB,SAAS,CAAC,EAAE,MAAM,EAClB,aAAa,GAAE,MAAmC,EAClD,MAAM,GAAE,MAAM,EAAwC,EACtD,OAAO,GAAE,OAAO,UAAU,CAAC,KAAwB,EACnD,OAAO,GAAE,MAAqB,GAC7B,iBAAiB,CA0OnB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@osdk/oauth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1-main-20240503172030",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./build/types/index.d.ts",
|
|
12
|
+
"browser": "./build/js/index.browser.mjs",
|
|
12
13
|
"import": "./build/js/index.mjs",
|
|
13
14
|
"require": "./build/js/index.cjs"
|
|
14
15
|
},
|
|
15
16
|
"./*": {
|
|
16
17
|
"types": "./build/types/public/*.d.ts",
|
|
18
|
+
"browser": "./build/js/public/*.browser.mjs",
|
|
17
19
|
"import": "./build/js/public/*.mjs",
|
|
18
20
|
"require": "./build/js/public/*.cjs"
|
|
19
21
|
}
|
|
@@ -22,13 +24,13 @@
|
|
|
22
24
|
"delay": "^6.0.0",
|
|
23
25
|
"oauth4webapi": "^2.10.3",
|
|
24
26
|
"tiny-invariant": "^1.3.1",
|
|
25
|
-
"typescript-event-target": "^1.1.
|
|
27
|
+
"typescript-event-target": "^1.1.1"
|
|
26
28
|
},
|
|
27
29
|
"devDependencies": {
|
|
28
30
|
"jest-extended": "^4.0.2",
|
|
29
31
|
"ts-expect": "^1.3.0",
|
|
30
|
-
"type-fest": "^4.
|
|
31
|
-
"typescript": "^5.4.
|
|
32
|
+
"type-fest": "^4.17.0",
|
|
33
|
+
"typescript": "^5.4.5"
|
|
32
34
|
},
|
|
33
35
|
"publishConfig": {
|
|
34
36
|
"access": "public"
|
|
@@ -36,8 +38,7 @@
|
|
|
36
38
|
"files": [
|
|
37
39
|
"build/types",
|
|
38
40
|
"build/js",
|
|
39
|
-
"
|
|
40
|
-
"CHANGELOG_OLD.md",
|
|
41
|
+
"CHANGELOG.md",
|
|
41
42
|
"package.json",
|
|
42
43
|
"templates",
|
|
43
44
|
"*.d.ts"
|