@osdk/oauth 1.1.0-beta.0 → 1.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/build/browser/__tests__/createPublicOauthClient.test.d.ts +2 -0
- package/build/browser/__tests__/createPublicOauthClient.test.d.ts.map +1 -0
- package/build/browser/__tests__/createPublicOauthClient.test.js +56 -0
- package/build/browser/__tests__/createPublicOauthClient.test.js.map +1 -0
- package/build/browser/createPublicOauthClient.d.ts +48 -10
- package/build/browser/createPublicOauthClient.d.ts.map +1 -1
- package/build/browser/createPublicOauthClient.js +19 -23
- package/build/browser/createPublicOauthClient.js.map +1 -1
- package/build/browser/utils.d.ts +6 -0
- package/build/browser/utils.d.ts.map +1 -0
- package/build/browser/utils.js +44 -0
- package/build/browser/utils.js.map +1 -0
- package/build/esm/__tests__/createPublicOauthClient.test.d.ts +2 -0
- package/build/esm/__tests__/createPublicOauthClient.test.d.ts.map +1 -0
- package/build/esm/__tests__/createPublicOauthClient.test.js +56 -0
- package/build/esm/__tests__/createPublicOauthClient.test.js.map +1 -0
- package/build/esm/createPublicOauthClient.d.ts +48 -10
- package/build/esm/createPublicOauthClient.d.ts.map +1 -1
- package/build/esm/createPublicOauthClient.js +19 -23
- package/build/esm/createPublicOauthClient.js.map +1 -1
- package/build/esm/utils.d.ts +6 -0
- package/build/esm/utils.d.ts.map +1 -0
- package/build/esm/utils.js +44 -0
- package/build/esm/utils.js.map +1 -0
- package/package.json +6 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @osdk/oauth
|
|
2
2
|
|
|
3
|
+
## 1.1.0-beta.2
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 33590c7: Add tests for createPublicOauthClient overload
|
|
8
|
+
- e00bb29: Overload createPublicOauthClient to support object of options
|
|
9
|
+
|
|
10
|
+
## 1.1.0-beta.1
|
|
11
|
+
|
|
12
|
+
### Minor Changes
|
|
13
|
+
|
|
14
|
+
- 25fd9f0: Updating package.json to have better entry points to support react native bundlers.
|
|
15
|
+
|
|
3
16
|
## 1.1.0-beta.0
|
|
4
17
|
|
|
5
18
|
### Minor Changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/createPublicOauthClient.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { createPublicOauthClient } from "@osdk/oauth";
|
|
17
|
+
import * as oauthModule from "@osdk/oauth";
|
|
18
|
+
import { describe, expect, it, vi } from "vitest";
|
|
19
|
+
import * as utilsModule from "../utils.js";
|
|
20
|
+
const originalCreatePublicOauthClient = oauthModule.createPublicOauthClient;
|
|
21
|
+
const FOUNDRY_CLIENT_ID = "example-foundry-client-id";
|
|
22
|
+
const FOUNDRY_URL = "http://example-foundry-url.com";
|
|
23
|
+
const REDIRECT_URI = "http://localhost:8080/auth/callback";
|
|
24
|
+
vi.stubGlobal("window", {
|
|
25
|
+
location: {
|
|
26
|
+
toString: vi.fn().mockReturnValue("http://example.com")
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
describe("createPublicOauthClient", () => {
|
|
30
|
+
it("should return the same processed options for both client creation methods", async () => {
|
|
31
|
+
const mockProcessOptionsAndAssignDefaults = vi.fn();
|
|
32
|
+
// Mock processOptionsAndAssignDefaults to call the mock function
|
|
33
|
+
vi.spyOn(utilsModule, "processOptionsAndAssignDefaults").mockImplementation((...args) => {
|
|
34
|
+
mockProcessOptionsAndAssignDefaults(...args);
|
|
35
|
+
return utilsModule.processOptionsAndAssignDefaults(...args);
|
|
36
|
+
});
|
|
37
|
+
// Mock createPublicOauthClient to call both the mock and the original function
|
|
38
|
+
vi.spyOn(oauthModule, "createPublicOauthClient").mockImplementation((...args) => {
|
|
39
|
+
const [_client_id, ...rest] = args;
|
|
40
|
+
mockProcessOptionsAndAssignDefaults(...rest);
|
|
41
|
+
return originalCreatePublicOauthClient(...args);
|
|
42
|
+
});
|
|
43
|
+
const authClient = createPublicOauthClient(FOUNDRY_CLIENT_ID, FOUNDRY_URL, REDIRECT_URI, true, undefined, undefined, undefined, fetch, undefined);
|
|
44
|
+
expect(authClient).toBeDefined();
|
|
45
|
+
expect(mockProcessOptionsAndAssignDefaults).toHaveBeenCalledTimes(1);
|
|
46
|
+
const authClientWithOptions = createPublicOauthClient(FOUNDRY_CLIENT_ID, FOUNDRY_URL, REDIRECT_URI, {
|
|
47
|
+
useHistory: true,
|
|
48
|
+
fetchFn: fetch
|
|
49
|
+
});
|
|
50
|
+
expect(authClientWithOptions).toBeDefined();
|
|
51
|
+
expect(mockProcessOptionsAndAssignDefaults).toHaveBeenCalledTimes(2);
|
|
52
|
+
const [result1, result2] = mockProcessOptionsAndAssignDefaults.mock.results;
|
|
53
|
+
expect(result1.value).toEqual(result2.value);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
//# sourceMappingURL=createPublicOauthClient.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.test.js","names":["createPublicOauthClient","oauthModule","describe","expect","it","vi","utilsModule","originalCreatePublicOauthClient","FOUNDRY_CLIENT_ID","FOUNDRY_URL","REDIRECT_URI","stubGlobal","location","toString","fn","mockReturnValue","mockProcessOptionsAndAssignDefaults","spyOn","mockImplementation","args","processOptionsAndAssignDefaults","_client_id","rest","authClient","undefined","fetch","toBeDefined","toHaveBeenCalledTimes","authClientWithOptions","useHistory","fetchFn","result1","result2","mock","results","value","toEqual"],"sources":["createPublicOauthClient.test.js"],"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 */\nimport { createPublicOauthClient } from \"@osdk/oauth\";\nimport * as oauthModule from \"@osdk/oauth\";\nimport { describe, expect, it, vi } from \"vitest\";\nimport * as utilsModule from \"../utils.js\";\nconst originalCreatePublicOauthClient = oauthModule.createPublicOauthClient;\nconst FOUNDRY_CLIENT_ID = \"example-foundry-client-id\";\nconst FOUNDRY_URL = \"http://example-foundry-url.com\";\nconst REDIRECT_URI = \"http://localhost:8080/auth/callback\";\nvi.stubGlobal(\"window\", {\n location: {\n toString: vi.fn().mockReturnValue(\"http://example.com\"),\n },\n});\ndescribe(\"createPublicOauthClient\", () => {\n it(\"should return the same processed options for both client creation methods\", async () => {\n const mockProcessOptionsAndAssignDefaults = vi.fn();\n // Mock processOptionsAndAssignDefaults to call the mock function\n vi.spyOn(utilsModule, \"processOptionsAndAssignDefaults\").mockImplementation((...args) => {\n mockProcessOptionsAndAssignDefaults(...args);\n return utilsModule.processOptionsAndAssignDefaults(...args);\n });\n // Mock createPublicOauthClient to call both the mock and the original function\n vi.spyOn(oauthModule, \"createPublicOauthClient\").mockImplementation((...args) => {\n const [_client_id, ...rest] = args;\n mockProcessOptionsAndAssignDefaults(...rest);\n return originalCreatePublicOauthClient(...args);\n });\n const authClient = createPublicOauthClient(FOUNDRY_CLIENT_ID, FOUNDRY_URL, REDIRECT_URI, true, undefined, undefined, undefined, fetch, undefined);\n expect(authClient).toBeDefined();\n expect(mockProcessOptionsAndAssignDefaults).toHaveBeenCalledTimes(1);\n const authClientWithOptions = createPublicOauthClient(FOUNDRY_CLIENT_ID, FOUNDRY_URL, REDIRECT_URI, {\n useHistory: true,\n fetchFn: fetch,\n });\n expect(authClientWithOptions).toBeDefined();\n expect(mockProcessOptionsAndAssignDefaults).toHaveBeenCalledTimes(2);\n const [result1, result2] = mockProcessOptionsAndAssignDefaults.mock.results;\n expect(result1.value).toEqual(result2.value);\n });\n});\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,uBAAuB,QAAQ,aAAa;AACrD,OAAO,KAAKC,WAAW,MAAM,aAAa;AAC1C,SAASC,QAAQ,EAAEC,MAAM,EAAEC,EAAE,EAAEC,EAAE,QAAQ,QAAQ;AACjD,OAAO,KAAKC,WAAW,MAAM,aAAa;AAC1C,MAAMC,+BAA+B,GAAGN,WAAW,CAACD,uBAAuB;AAC3E,MAAMQ,iBAAiB,GAAG,2BAA2B;AACrD,MAAMC,WAAW,GAAG,gCAAgC;AACpD,MAAMC,YAAY,GAAG,qCAAqC;AAC1DL,EAAE,CAACM,UAAU,CAAC,QAAQ,EAAE;EACpBC,QAAQ,EAAE;IACNC,QAAQ,EAAER,EAAE,CAACS,EAAE,CAAC,CAAC,CAACC,eAAe,CAAC,oBAAoB;EAC1D;AACJ,CAAC,CAAC;AACFb,QAAQ,CAAC,yBAAyB,EAAE,MAAM;EACtCE,EAAE,CAAC,2EAA2E,EAAE,YAAY;IACxF,MAAMY,mCAAmC,GAAGX,EAAE,CAACS,EAAE,CAAC,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AAO,CAACG,KAAK,CAAC;EAChD,CAAC,CAAC;AACN,CAAC,CAAC","ignoreList":[]}
|
|
@@ -1,15 +1,53 @@
|
|
|
1
1
|
import type { PublicOauthClient } from "./PublicOauthClient.js";
|
|
2
|
+
export interface PublicOauthClientOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If true, uses `history.replaceState()`, otherwise uses `window.location.assign()` (defaults to true)
|
|
5
|
+
*/
|
|
6
|
+
useHistory?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Custom landing page URL prior to logging in
|
|
9
|
+
*/
|
|
10
|
+
loginPage?: string;
|
|
11
|
+
/**
|
|
12
|
+
* URL to return to after completed authentication cycle (defaults to `window.location.toString()`)
|
|
13
|
+
*/
|
|
14
|
+
postLoginPage?: string;
|
|
15
|
+
/**
|
|
16
|
+
* * @param {string[]} [scopes=[]] - OAuth scopes to request. If not provided, defaults to `["api:read-data", "api:write-data"]`
|
|
17
|
+
*/
|
|
18
|
+
scopes?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* Custom fetch function to use for requests (defaults to `globalThis.fetch`)
|
|
21
|
+
*/
|
|
22
|
+
fetchFn?: typeof globalThis.fetch;
|
|
23
|
+
/**
|
|
24
|
+
* Context path for the authorization server (defaults to "/multipass")
|
|
25
|
+
*/
|
|
26
|
+
ctxPath?: string;
|
|
27
|
+
}
|
|
2
28
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @param
|
|
6
|
-
* @param
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
* @
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
29
|
+
* Creates a PublicOauthClient for authentication.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} clientId - The client_id from the OAuth configuration on the server
|
|
32
|
+
* @param {string} url - The base URL of your Foundry server
|
|
33
|
+
* @param {string} redirectUrl - The URL configured for redirect in the OAuth configuration on the server
|
|
34
|
+
* @param {PublicOauthClientOptions} options - Additional options for the client
|
|
35
|
+
* @returns {PublicOauthClient} A client that can be used as a token provider
|
|
36
|
+
*/
|
|
37
|
+
export declare function createPublicOauthClient(clientId: string, url: string, redirectUrl: string, options?: PublicOauthClientOptions): PublicOauthClient;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a PublicOauthClient for authentication.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} clientId - The client_id from the OAuth configuration on the server
|
|
42
|
+
* @param {string} url - The base URL of your Foundry server
|
|
43
|
+
* @param {string} redirectUrl - The URL configured for redirect in the OAuth configuration on the server
|
|
44
|
+
* @param {boolean} useHistory - If true, uses `history.replaceState()`, otherwise uses `window.location.assign()` (defaults to true)
|
|
45
|
+
* @param {string} loginPage - Custom landing page URL prior to logging in
|
|
46
|
+
* @param {string} postLoginPage - URL to return to after completed authentication cycle (defaults to `window.location.toString()`)
|
|
47
|
+
* @param {string[]} scopes - OAuth scopes to request. If not provided, defaults to `["api:read-data", "api:write-data"]`
|
|
48
|
+
* @param {typeof globalThis.fetch} fetchFn - Custom fetch function to use for requests (defaults to `globalThis.fetch`)
|
|
49
|
+
* @param {string} ctxPath - Context path for the authorization server (defaults to "/multipass")
|
|
50
|
+
* @returns {PublicOauthClient} A client that can be used as a token provider
|
|
13
51
|
*/
|
|
14
52
|
export declare function createPublicOauthClient(client_id: string, url: string, redirectUrl: string, useHistory?: boolean, loginPage?: string, postLoginPage?: string, scopes?: string[], fetchFn?: typeof globalThis.fetch, ctxPath?: string): PublicOauthClient;
|
|
15
53
|
//# sourceMappingURL=createPublicOauthClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createPublicOauthClient.d.ts","sourceRoot":"","sources":["../../src/createPublicOauthClient.ts"],"names":[],"mappings":"AAmCA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.d.ts","sourceRoot":"","sources":["../../src/createPublicOauthClient.ts"],"names":[],"mappings":"AAmCA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAYhE,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IAErB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAElB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAElC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,wBAAwB,GACjC,iBAAiB,CAAC;AAErB;;;;;;;;;;;;;GAaG;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,CAAC,EAAE,MAAM,EACtB,MAAM,CAAC,EAAE,MAAM,EAAE,EACjB,OAAO,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,EACjC,OAAO,CAAC,EAAE,MAAM,GACf,iBAAiB,CAAC"}
|
|
@@ -17,19 +17,16 @@ import delay from "delay";
|
|
|
17
17
|
import { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, refreshTokenGrantRequest, validateAuthResponse } from "oauth4webapi";
|
|
18
18
|
import { common, createAuthorizationServer, readLocal, removeLocal, saveLocal } from "./common.js";
|
|
19
19
|
import { throwIfError } from "./throwIfError.js";
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
* @returns {PublicOauthClient} which can be used as a token provider
|
|
31
|
-
*/
|
|
32
|
-
export function createPublicOauthClient(client_id, url, redirectUrl, useHistory = true, loginPage, postLoginPage = window.location.toString(), scopes = [], fetchFn = globalThis.fetch, ctxPath = "/multipass") {
|
|
20
|
+
import { processOptionsAndAssignDefaults } from "./utils.js";
|
|
21
|
+
export function createPublicOauthClient(client_id, url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath) {
|
|
22
|
+
({
|
|
23
|
+
useHistory,
|
|
24
|
+
loginPage,
|
|
25
|
+
postLoginPage,
|
|
26
|
+
scopes,
|
|
27
|
+
fetchFn,
|
|
28
|
+
ctxPath
|
|
29
|
+
} = processOptionsAndAssignDefaults(url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath));
|
|
33
30
|
const client = {
|
|
34
31
|
client_id,
|
|
35
32
|
token_endpoint_auth_method: "none"
|
|
@@ -38,9 +35,6 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
38
35
|
const oauthHttpOptions = {
|
|
39
36
|
[customFetch]: fetchFn
|
|
40
37
|
};
|
|
41
|
-
if (scopes.length === 0) {
|
|
42
|
-
scopes = ["api:read-data", "api:write-data"];
|
|
43
|
-
}
|
|
44
38
|
const {
|
|
45
39
|
makeTokenAndSaveRefresh,
|
|
46
40
|
getToken
|
|
@@ -53,11 +47,12 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
53
47
|
// 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
|
|
54
48
|
?? (await initiateLoginRedirect());
|
|
55
49
|
}, oauthHttpOptions, maybeRefresh.bind(globalThis, true));
|
|
56
|
-
|
|
50
|
+
// as an arrow function, `useHistory` is known to be a boolean
|
|
51
|
+
const go = async x => {
|
|
57
52
|
if (useHistory) return window.history.replaceState({}, "", x);else window.location.assign(x);
|
|
58
53
|
await delay(1000);
|
|
59
54
|
throw new Error("Unable to redirect");
|
|
60
|
-
}
|
|
55
|
+
};
|
|
61
56
|
async function maybeRefresh(expectRefreshToken) {
|
|
62
57
|
const {
|
|
63
58
|
refresh_token
|
|
@@ -70,7 +65,7 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
70
65
|
// note, we don't use processRefreshTokenResponse because the mp oauth implementation
|
|
71
66
|
// doesn't do refresh tokens quite right and this lets us skip the id check
|
|
72
67
|
const result = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await refreshTokenGrantRequest(authServer, client, refresh_token, oauthHttpOptions))), "refresh");
|
|
73
|
-
if (result && window.location.pathname === new URL(
|
|
68
|
+
if (result && window.location.pathname === new URL(redirect_uri).pathname) {
|
|
74
69
|
const {
|
|
75
70
|
oldUrl
|
|
76
71
|
} = readLocal(client);
|
|
@@ -96,7 +91,7 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
96
91
|
} = readLocal(client);
|
|
97
92
|
if (!codeVerifier) return;
|
|
98
93
|
try {
|
|
99
|
-
const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await authorizationCodeGrantRequest(authServer, client, throwIfError(validateAuthResponse(authServer, client, new URL(window.location.href), state)),
|
|
94
|
+
const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await authorizationCodeGrantRequest(authServer, client, throwIfError(validateAuthResponse(authServer, client, new URL(window.location.href), state)), redirect_uri, codeVerifier, oauthHttpOptions))), "signIn");
|
|
100
95
|
go(oldUrl);
|
|
101
96
|
return ret;
|
|
102
97
|
} catch (e) {
|
|
@@ -107,7 +102,8 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
107
102
|
removeLocal(client);
|
|
108
103
|
}
|
|
109
104
|
}
|
|
110
|
-
|
|
105
|
+
// As an arrow function, `scopes` and `postLoginPage` are known at compile time
|
|
106
|
+
const initiateLoginRedirect = async () => {
|
|
111
107
|
if (loginPage && window.location.href !== loginPage && window.location.pathname !== loginPage) {
|
|
112
108
|
saveLocal(client, {
|
|
113
109
|
oldUrl: postLoginPage
|
|
@@ -126,7 +122,7 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
126
122
|
client_id,
|
|
127
123
|
response_type: "code",
|
|
128
124
|
state,
|
|
129
|
-
redirect_uri
|
|
125
|
+
redirect_uri,
|
|
130
126
|
code_challenge: await calculatePKCECodeChallenge(codeVerifier),
|
|
131
127
|
code_challenge_method: "S256",
|
|
132
128
|
scope: ["offline_access", ...scopes].join(" ")
|
|
@@ -134,7 +130,7 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
134
130
|
// Give time for redirect to happen
|
|
135
131
|
await delay(1000);
|
|
136
132
|
throw new Error("Unable to redirect");
|
|
137
|
-
}
|
|
133
|
+
};
|
|
138
134
|
return getToken;
|
|
139
135
|
}
|
|
140
136
|
//# sourceMappingURL=createPublicOauthClient.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createPublicOauthClient.js","names":["delay","authorizationCodeGrantRequest","calculatePKCECodeChallenge","customFetch","generateRandomCodeVerifier","generateRandomState","processAuthorizationCodeOAuth2Response","refreshTokenGrantRequest","validateAuthResponse","common","createAuthorizationServer","readLocal","removeLocal","saveLocal","throwIfError","createPublicOauthClient","client_id","url","redirectUrl","useHistory","loginPage","postLoginPage","window","location","toString","scopes","fetchFn","globalThis","fetch","ctxPath","client","token_endpoint_auth_method","authServer","oauthHttpOptions","length","makeTokenAndSaveRefresh","getToken","_signIn","maybeRefresh","maybeHandleAuthReturn","initiateLoginRedirect","bind","go","x","history","replaceState","assign","Error","expectRefreshToken","refresh_token","result","pathname","URL","oldUrl","e","process","env","NODE_ENV","console","warn","codeVerifier","state","ret","href","authorization_endpoint","URLSearchParams","response_type","redirect_uri","code_challenge","code_challenge_method","scope","join"],"sources":["createPublicOauthClient.js"],"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 */\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, refreshTokenGrantRequest, validateAuthResponse, } from \"oauth4webapi\";\nimport { common, createAuthorizationServer, readLocal, removeLocal, saveLocal, } from \"./common.js\";\nimport { throwIfError } from \"./throwIfError.js\";\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 = true, loginPage, postLoginPage = window.location.toString(), scopes = [], fetchFn = globalThis.fetch, ctxPath = \"/multipass\") {\n const client = { client_id, token_endpoint_auth_method: \"none\" };\n const authServer = createAuthorizationServer(ctxPath, url);\n const oauthHttpOptions = { [customFetch]: fetchFn };\n if (scopes.length === 0) {\n scopes = [\"api:read-data\", \"api:write-data\"];\n }\n const { makeTokenAndSaveRefresh, getToken } = common(client, authServer, _signIn, oauthHttpOptions, maybeRefresh.bind(globalThis, true));\n async function go(x) {\n if (useHistory)\n return window.history.replaceState({}, \"\", x);\n else\n window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n async function maybeRefresh(expectRefreshToken) {\n const { refresh_token } = readLocal(client);\n if (!refresh_token) {\n if (expectRefreshToken)\n throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we don't use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n const result = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await refreshTokenGrantRequest(authServer, client, refresh_token, oauthHttpOptions))), \"refresh\");\n if (result && window.location.pathname === new URL(redirectUrl).pathname) {\n const { oldUrl } = readLocal(client);\n go(oldUrl ?? \"/\");\n }\n return result;\n }\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(client);\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const { codeVerifier, state, oldUrl } = readLocal(client);\n if (!codeVerifier)\n return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await authorizationCodeGrantRequest(authServer, client, throwIfError(validateAuthResponse(authServer, client, new URL(window.location.href), state)), redirectUrl, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n }\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 PKCE, removing PKCE and starting a new auth flow\", e);\n }\n removeLocal(client);\n }\n }\n async function initiateLoginRedirect() {\n if (loginPage\n && window.location.href !== loginPage\n && window.location.pathname !== loginPage) {\n saveLocal(client, { oldUrl: postLoginPage });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal(client).oldUrl ?? window.location.toString();\n saveLocal(client, { codeVerifier, state, oldUrl });\n window.location.assign(`${authServer\n .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 // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\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 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 return getToken;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,KAAK,MAAM,OAAO;AACzB,SAASC,6BAA6B,EAAEC,0BAA0B,EAAEC,WAAW,EAAEC,0BAA0B,EAAEC,mBAAmB,EAAEC,sCAAsC,EAAEC,wBAAwB,EAAEC,oBAAoB,QAAS,cAAc;AAC/O,SAASC,MAAM,EAAEC,yBAAyB,EAAEC,SAAS,EAAEC,WAAW,EAAEC,SAAS,QAAS,aAAa;AACnG,SAASC,YAAY,QAAQ,mBAAmB;AAChD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,uBAAuBA,CAACC,SAAS,EAAEC,GAAG,EAAEC,WAAW,EAAEC,UAAU,GAAG,IAAI,EAAEC,SAAS,EAAEC,aAAa,GAAGC,MAAM,CAACC,QAAQ,CAACC,QAAQ,CAAC,CAAC,EAAEC,MAAM,GAAG,EAAE,EAAEC,OAAO,GAAGC,UAAU,CAACC,KAAK,EAAEC,OAAO,GAAG,YAAY,EAAE;EAC5M,MAAMC,MAAM,GAAG;IAAEd,SAAS;IAAEe,0BAA0B,EAAE;EAAO,CAA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ACL;IACA,MAAMvE,KAAK,CAAC,IAAI,CAAC;IACjB,MAAM,IAAI+C,KAAK,CAAC,oBAAoB,CAAC;EACzC;EAUA,OAAOX,QAAQ;AACnB","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.js","names":["delay","authorizationCodeGrantRequest","calculatePKCECodeChallenge","customFetch","generateRandomCodeVerifier","generateRandomState","processAuthorizationCodeOAuth2Response","refreshTokenGrantRequest","validateAuthResponse","common","createAuthorizationServer","readLocal","removeLocal","saveLocal","throwIfError","processOptionsAndAssignDefaults","createPublicOauthClient","client_id","url","redirect_uri","useHistory","loginPage","postLoginPage","scopes","fetchFn","ctxPath","client","token_endpoint_auth_method","authServer","oauthHttpOptions","makeTokenAndSaveRefresh","getToken","_signIn","maybeRefresh","maybeHandleAuthReturn","initiateLoginRedirect","bind","globalThis","go","x","window","history","replaceState","location","assign","Error","expectRefreshToken","refresh_token","result","pathname","URL","oldUrl","e","process","env","NODE_ENV","console","warn","codeVerifier","state","ret","href","toString","authorization_endpoint","URLSearchParams","response_type","code_challenge","code_challenge_method","scope","join"],"sources":["createPublicOauthClient.js"],"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 */\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, refreshTokenGrantRequest, validateAuthResponse, } from \"oauth4webapi\";\nimport { common, createAuthorizationServer, readLocal, removeLocal, saveLocal, } from \"./common.js\";\nimport { throwIfError } from \"./throwIfError.js\";\nimport { processOptionsAndAssignDefaults } from \"./utils.js\";\nexport function createPublicOauthClient(client_id, url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath) {\n ({\n useHistory,\n loginPage,\n postLoginPage,\n scopes,\n fetchFn,\n ctxPath,\n } = processOptionsAndAssignDefaults(url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath));\n const client = { client_id, token_endpoint_auth_method: \"none\" };\n const authServer = createAuthorizationServer(ctxPath, url);\n const oauthHttpOptions = { [customFetch]: fetchFn };\n const { makeTokenAndSaveRefresh, getToken } = common(client, authServer, _signIn, oauthHttpOptions, maybeRefresh.bind(globalThis, true));\n // as an arrow function, `useHistory` is known to be a boolean\n const go = async (x) => {\n if (useHistory)\n return window.history.replaceState({}, \"\", x);\n else\n window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n };\n async function maybeRefresh(expectRefreshToken) {\n const { refresh_token } = readLocal(client);\n if (!refresh_token) {\n if (expectRefreshToken)\n throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we don't use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n const result = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await refreshTokenGrantRequest(authServer, client, refresh_token, oauthHttpOptions))), \"refresh\");\n if (result && window.location.pathname === new URL(redirect_uri).pathname) {\n const { oldUrl } = readLocal(client);\n go(oldUrl ?? \"/\");\n }\n return result;\n }\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(client);\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const { codeVerifier, state, oldUrl } = readLocal(client);\n if (!codeVerifier)\n return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await authorizationCodeGrantRequest(authServer, client, throwIfError(validateAuthResponse(authServer, client, new URL(window.location.href), state)), redirect_uri, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n }\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 PKCE, removing PKCE and starting a new auth flow\", e);\n }\n removeLocal(client);\n }\n }\n // As an arrow function, `scopes` and `postLoginPage` are known at compile time\n const initiateLoginRedirect = async () => {\n if (loginPage\n && window.location.href !== loginPage\n && window.location.pathname !== loginPage) {\n saveLocal(client, { oldUrl: postLoginPage });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal(client).oldUrl ?? window.location.toString();\n saveLocal(client, { codeVerifier, state, oldUrl });\n window.location.assign(`${authServer\n .authorization_endpoint}?${new URLSearchParams({\n client_id,\n response_type: \"code\",\n state,\n redirect_uri,\n code_challenge: await calculatePKCECodeChallenge(codeVerifier),\n code_challenge_method: \"S256\",\n scope: [\"offline_access\", ...scopes].join(\" \"),\n })}`);\n // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\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 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 return getToken;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,KAAK,MAAM,OAAO;AACzB,SAASC,6BAA6B,EAAEC,0BAA0B,EAAEC,WAAW,EAAEC,0BAA0B,EAAEC,mBAAmB,EAAEC,sCAAsC,EAAEC,wBAAwB,EAAEC,oBAAoB,QAAS,cAAc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mB,QAAQ,CAAC,CAAC;IACrEjD,SAAS,CAACa,MAAM,EAAE;MAAEgC,YAAY;MAAEC,KAAK;MAAER;IAAO,CAAC,CAAC;IAClDX,MAAM,CAACG,QAAQ,CAACC,MAAM,CAAC,GAAGhB,UAAU,CAC/BmC,sBAAsB,IAAI,IAAIC,eAAe,CAAC;MAC/C/C,SAAS;MACTgD,aAAa,EAAE,MAAM;MACrBN,KAAK;MACLxC,YAAY;MACZ+C,cAAc,EAAE,MAAMhE,0BAA0B,CAACwD,YAAY,CAAC;MAC9DS,qBAAqB,EAAE,MAAM;MAC7BC,KAAK,EAAE,CAAC,gBAAgB,EAAE,GAAG7C,MAAM,CAAC,CAAC8C,IAAI,CAAC,GAAG;IACjD,CAAC,CAAC,EAAE,CAAC;IACL;IACA,MAAMrE,KAAK,CAAC,IAAI,CAAC;IACjB,MAAM,IAAI6C,KAAK,CAAC,oBAAoB,CAAC;EACzC,CAAC;EAUD,OAAOd,QAAQ;AACnB","ignoreList":[]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { PublicOauthClientOptions } from "./createPublicOauthClient.js";
|
|
2
|
+
export type ProcessedPublicOauthClientOptions = Omit<Required<PublicOauthClientOptions>, "loginPage"> & {
|
|
3
|
+
loginPage?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function processOptionsAndAssignDefaults(url: string, redirect_uri: string, useHistory?: boolean | PublicOauthClientOptions, loginPage?: string, postLoginPage?: string, scopes?: string[], fetchFn?: typeof globalThis.fetch, ctxPath?: string): ProcessedPublicOauthClientOptions;
|
|
6
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAE7E,MAAM,MAAM,iCAAiC,GACzC,IAAI,CAAC,QAAQ,CAAC,wBAAwB,CAAC,EAAE,WAAW,CAAC,GACrD;IACA,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEJ,wBAAgB,+BAA+B,CAC7C,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,MAAM,EACpB,UAAU,CAAC,EAAE,OAAO,GAAG,wBAAwB,EAC/C,SAAS,CAAC,EAAE,MAAM,EAClB,aAAa,CAAC,EAAE,MAAM,EACtB,MAAM,CAAC,EAAE,MAAM,EAAE,EACjB,OAAO,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,EACjC,OAAO,CAAC,EAAE,MAAM,GACf,iCAAiC,CAgCnC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import invariant from "tiny-invariant";
|
|
17
|
+
export function processOptionsAndAssignDefaults(url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath) {
|
|
18
|
+
let options = {};
|
|
19
|
+
if (typeof useHistory === "object") {
|
|
20
|
+
!(!loginPage && !postLoginPage && !scopes && !fetchFn && !ctxPath) ? process.env.NODE_ENV !== "production" ? invariant(false, "If useHistory is an object, other options should not be provided") : invariant(false) : void 0;
|
|
21
|
+
options = useHistory;
|
|
22
|
+
} else {
|
|
23
|
+
options = {
|
|
24
|
+
useHistory,
|
|
25
|
+
loginPage,
|
|
26
|
+
postLoginPage,
|
|
27
|
+
scopes,
|
|
28
|
+
fetchFn,
|
|
29
|
+
ctxPath
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
!url ? process.env.NODE_ENV !== "production" ? invariant(false, "url is required") : invariant(false) : void 0;
|
|
33
|
+
!redirect_uri ? process.env.NODE_ENV !== "production" ? invariant(false, "redirectUrl is required") : invariant(false) : void 0;
|
|
34
|
+
// Assign defaults
|
|
35
|
+
return {
|
|
36
|
+
useHistory: options.useHistory ?? true,
|
|
37
|
+
loginPage: options.loginPage,
|
|
38
|
+
postLoginPage: options.postLoginPage || window.location.toString(),
|
|
39
|
+
scopes: options.scopes ?? ["api:read-data", "api:write-data"],
|
|
40
|
+
fetchFn: options.fetchFn ?? globalThis.fetch,
|
|
41
|
+
ctxPath: options.ctxPath ?? "/multipass"
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":["invariant","processOptionsAndAssignDefaults","url","redirect_uri","useHistory","loginPage","postLoginPage","scopes","fetchFn","ctxPath","options","process","env","NODE_ENV","window","location","toString","globalThis","fetch"],"sources":["utils.js"],"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 */\nimport invariant from \"tiny-invariant\";\nexport function processOptionsAndAssignDefaults(url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath) {\n let options = {};\n if (typeof useHistory === \"object\") {\n invariant(!loginPage && !postLoginPage && !scopes && !fetchFn && !ctxPath, \"If useHistory is an object, other options should not be provided\");\n options = useHistory;\n }\n else {\n options = {\n useHistory,\n loginPage,\n postLoginPage,\n scopes,\n fetchFn,\n ctxPath,\n };\n }\n invariant(url, \"url is required\");\n invariant(redirect_uri, \"redirectUrl is required\");\n // Assign defaults\n return {\n useHistory: options.useHistory ?? true,\n loginPage: options.loginPage,\n postLoginPage: options.postLoginPage || window.location.toString(),\n scopes: options.scopes ?? [\"api:read-data\", \"api:write-data\"],\n fetchFn: options.fetchFn ?? globalThis.fetch,\n ctxPath: options.ctxPath ?? \"/multipass\",\n };\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,SAAS,MAAM,gBAAgB;AACtC,OAAO,SAASC,+BAA+BA,CAACC,GAAG,EAAEC,YAAY,EAAEC,UAAU,EAAEC,SAAS,EAAEC,aAAa,EAAEC,MAAM,EAAEC,OAAO,EAAEC,OAAO,EAAE;EAC/H,IAAIC,OAAO,GAAG,CAAC,CAAC;EAChB,IAAI,OAAON,UAAU,KAAK,QAAQ,EAAE;IAChC,EAAU,CAACC,SAAS,IAAI,CAACC,aAAa,IAAI,CAACC,MAAM,IAAI,CAACC,OAAO,IAAI,CAACC,OAAO,IAAAE,OAAA,CAAAC,GAAA,CAAAC,QAAA,oBAAzEb,SAAS,QAAkE,kEAAkE,IAA7IA,SAAS;IACTU,OAAO,GAAGN,UAAU;EACxB,CAAC,MACI;IACDM,OAAO,GAAG;MACNN,UAAU;MACVC,SAAS;MACTC,aAAa;MACbC,MAAM;MACNC,OAAO;MACPC;IACJ,CAAC;EACL;EACA,CAAUP,GAAG,GAAAS,OAAA,CAAAC,GAAA,CAAAC,QAAA,oBAAbb,SAAS,QAAM,iBAAiB,IAAhCA,SAAS;EACT,CAAUG,YAAY,GAAAQ,OAAA,CAAAC,GAAA,CAAAC,QAAA,oBAAtBb,SAAS,QAAe,yBAAyB,IAAjDA,SAAS;EACT;EACA,OAAO;IACHI,UAAU,EAAEM,OAAO,CAACN,UAAU,IAAI,IAAI;IACtCC,SAAS,EAAEK,OAAO,CAACL,SAAS;IAC5BC,aAAa,EAAEI,OAAO,CAACJ,aAAa,IAAIQ,MAAM,CAACC,QAAQ,CAACC,QAAQ,CAAC,CAAC;IAClET,MAAM,EAAEG,OAAO,CAACH,MAAM,IAAI,CAAC,eAAe,EAAE,gBAAgB,CAAC;IAC7DC,OAAO,EAAEE,OAAO,CAACF,OAAO,IAAIS,UAAU,CAACC,KAAK;IAC5CT,OAAO,EAAEC,OAAO,CAACD,OAAO,IAAI;EAChC,CAAC;AACL","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/createPublicOauthClient.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { createPublicOauthClient } from "@osdk/oauth";
|
|
17
|
+
import * as oauthModule from "@osdk/oauth";
|
|
18
|
+
import { describe, expect, it, vi } from "vitest";
|
|
19
|
+
import * as utilsModule from "../utils.js";
|
|
20
|
+
const originalCreatePublicOauthClient = oauthModule.createPublicOauthClient;
|
|
21
|
+
const FOUNDRY_CLIENT_ID = "example-foundry-client-id";
|
|
22
|
+
const FOUNDRY_URL = "http://example-foundry-url.com";
|
|
23
|
+
const REDIRECT_URI = "http://localhost:8080/auth/callback";
|
|
24
|
+
vi.stubGlobal("window", {
|
|
25
|
+
location: {
|
|
26
|
+
toString: vi.fn().mockReturnValue("http://example.com")
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
describe("createPublicOauthClient", () => {
|
|
30
|
+
it("should return the same processed options for both client creation methods", async () => {
|
|
31
|
+
const mockProcessOptionsAndAssignDefaults = vi.fn();
|
|
32
|
+
// Mock processOptionsAndAssignDefaults to call the mock function
|
|
33
|
+
vi.spyOn(utilsModule, "processOptionsAndAssignDefaults").mockImplementation((...args) => {
|
|
34
|
+
mockProcessOptionsAndAssignDefaults(...args);
|
|
35
|
+
return utilsModule.processOptionsAndAssignDefaults(...args);
|
|
36
|
+
});
|
|
37
|
+
// Mock createPublicOauthClient to call both the mock and the original function
|
|
38
|
+
vi.spyOn(oauthModule, "createPublicOauthClient").mockImplementation((...args) => {
|
|
39
|
+
const [_client_id, ...rest] = args;
|
|
40
|
+
mockProcessOptionsAndAssignDefaults(...rest);
|
|
41
|
+
return originalCreatePublicOauthClient(...args);
|
|
42
|
+
});
|
|
43
|
+
const authClient = createPublicOauthClient(FOUNDRY_CLIENT_ID, FOUNDRY_URL, REDIRECT_URI, true, undefined, undefined, undefined, fetch, undefined);
|
|
44
|
+
expect(authClient).toBeDefined();
|
|
45
|
+
expect(mockProcessOptionsAndAssignDefaults).toHaveBeenCalledTimes(1);
|
|
46
|
+
const authClientWithOptions = createPublicOauthClient(FOUNDRY_CLIENT_ID, FOUNDRY_URL, REDIRECT_URI, {
|
|
47
|
+
useHistory: true,
|
|
48
|
+
fetchFn: fetch
|
|
49
|
+
});
|
|
50
|
+
expect(authClientWithOptions).toBeDefined();
|
|
51
|
+
expect(mockProcessOptionsAndAssignDefaults).toHaveBeenCalledTimes(2);
|
|
52
|
+
const [result1, result2] = mockProcessOptionsAndAssignDefaults.mock.results;
|
|
53
|
+
expect(result1.value).toEqual(result2.value);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
//# sourceMappingURL=createPublicOauthClient.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.test.js","names":["createPublicOauthClient","oauthModule","describe","expect","it","vi","utilsModule","originalCreatePublicOauthClient","FOUNDRY_CLIENT_ID","FOUNDRY_URL","REDIRECT_URI","stubGlobal","location","toString","fn","mockReturnValue","mockProcessOptionsAndAssignDefaults","spyOn","mockImplementation","args","processOptionsAndAssignDefaults","_client_id","rest","authClient","undefined","fetch","toBeDefined","toHaveBeenCalledTimes","authClientWithOptions","useHistory","fetchFn","result1","result2","mock","results","value","toEqual"],"sources":["createPublicOauthClient.test.js"],"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 */\nimport { createPublicOauthClient } from \"@osdk/oauth\";\nimport * as oauthModule from \"@osdk/oauth\";\nimport { describe, expect, it, vi } from \"vitest\";\nimport * as utilsModule from \"../utils.js\";\nconst originalCreatePublicOauthClient = oauthModule.createPublicOauthClient;\nconst FOUNDRY_CLIENT_ID = \"example-foundry-client-id\";\nconst FOUNDRY_URL = \"http://example-foundry-url.com\";\nconst REDIRECT_URI = \"http://localhost:8080/auth/callback\";\nvi.stubGlobal(\"window\", {\n location: {\n toString: vi.fn().mockReturnValue(\"http://example.com\"),\n },\n});\ndescribe(\"createPublicOauthClient\", () => {\n it(\"should return the same processed options for both client creation methods\", async () => {\n const mockProcessOptionsAndAssignDefaults = vi.fn();\n // Mock processOptionsAndAssignDefaults to call the mock function\n vi.spyOn(utilsModule, \"processOptionsAndAssignDefaults\").mockImplementation((...args) => {\n mockProcessOptionsAndAssignDefaults(...args);\n return utilsModule.processOptionsAndAssignDefaults(...args);\n });\n // Mock createPublicOauthClient to call both the mock and the original function\n vi.spyOn(oauthModule, \"createPublicOauthClient\").mockImplementation((...args) => {\n const [_client_id, ...rest] = args;\n mockProcessOptionsAndAssignDefaults(...rest);\n return originalCreatePublicOauthClient(...args);\n });\n const authClient = createPublicOauthClient(FOUNDRY_CLIENT_ID, FOUNDRY_URL, REDIRECT_URI, true, undefined, undefined, undefined, fetch, undefined);\n expect(authClient).toBeDefined();\n expect(mockProcessOptionsAndAssignDefaults).toHaveBeenCalledTimes(1);\n const authClientWithOptions = createPublicOauthClient(FOUNDRY_CLIENT_ID, FOUNDRY_URL, REDIRECT_URI, {\n useHistory: true,\n fetchFn: fetch,\n });\n expect(authClientWithOptions).toBeDefined();\n expect(mockProcessOptionsAndAssignDefaults).toHaveBeenCalledTimes(2);\n const [result1, result2] = mockProcessOptionsAndAssignDefaults.mock.results;\n expect(result1.value).toEqual(result2.value);\n });\n});\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,uBAAuB,QAAQ,aAAa;AACrD,OAAO,KAAKC,WAAW,MAAM,aAAa;AAC1C,SAASC,QAAQ,EAAEC,MAAM,EAAEC,EAAE,EAAEC,EAAE,QAAQ,QAAQ;AACjD,OAAO,KAAKC,WAAW,MAAM,aAAa;AAC1C,MAAMC,+BAA+B,GAAGN,WAAW,CAACD,uBAAuB;AAC3E,MAAMQ,iBAAiB,GAAG,2BAA2B;AACrD,MAAMC,WAAW,GAAG,gCAAgC;AACpD,MAAMC,YAAY,GAAG,qCAAqC;AAC1DL,EAA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CAAC;IACFtB,MAAM,CAACyB,qBAAqB,CAAC,CAACF,WAAW,CAAC,CAAC;IAC3CvB,MAAM,CAACa,mCAAmC,CAAC,CAACW,qBAAqB,CAAC,CAAC,CAAC;IACpE,MAAM,CAACI,OAAO,EAAEC,OAAO,CAAC,GAAGhB,mCAAmC,CAACiB,IAAI,CAACC,OAAO;IAC3E/B,MAAM,CAAC4B,OAAO,CAACI,KAAK,CAAC,CAACC,OAAO,CAACJ,OAAO,CAACG,KAAK,CAAC;EAChD,CAAC,CAAC;AACN,CAAC,CAAC","ignoreList":[]}
|
|
@@ -1,15 +1,53 @@
|
|
|
1
1
|
import type { PublicOauthClient } from "./PublicOauthClient.js";
|
|
2
|
+
export interface PublicOauthClientOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If true, uses `history.replaceState()`, otherwise uses `window.location.assign()` (defaults to true)
|
|
5
|
+
*/
|
|
6
|
+
useHistory?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Custom landing page URL prior to logging in
|
|
9
|
+
*/
|
|
10
|
+
loginPage?: string;
|
|
11
|
+
/**
|
|
12
|
+
* URL to return to after completed authentication cycle (defaults to `window.location.toString()`)
|
|
13
|
+
*/
|
|
14
|
+
postLoginPage?: string;
|
|
15
|
+
/**
|
|
16
|
+
* * @param {string[]} [scopes=[]] - OAuth scopes to request. If not provided, defaults to `["api:read-data", "api:write-data"]`
|
|
17
|
+
*/
|
|
18
|
+
scopes?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* Custom fetch function to use for requests (defaults to `globalThis.fetch`)
|
|
21
|
+
*/
|
|
22
|
+
fetchFn?: typeof globalThis.fetch;
|
|
23
|
+
/**
|
|
24
|
+
* Context path for the authorization server (defaults to "/multipass")
|
|
25
|
+
*/
|
|
26
|
+
ctxPath?: string;
|
|
27
|
+
}
|
|
2
28
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @param
|
|
6
|
-
* @param
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
* @
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
29
|
+
* Creates a PublicOauthClient for authentication.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} clientId - The client_id from the OAuth configuration on the server
|
|
32
|
+
* @param {string} url - The base URL of your Foundry server
|
|
33
|
+
* @param {string} redirectUrl - The URL configured for redirect in the OAuth configuration on the server
|
|
34
|
+
* @param {PublicOauthClientOptions} options - Additional options for the client
|
|
35
|
+
* @returns {PublicOauthClient} A client that can be used as a token provider
|
|
36
|
+
*/
|
|
37
|
+
export declare function createPublicOauthClient(clientId: string, url: string, redirectUrl: string, options?: PublicOauthClientOptions): PublicOauthClient;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a PublicOauthClient for authentication.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} clientId - The client_id from the OAuth configuration on the server
|
|
42
|
+
* @param {string} url - The base URL of your Foundry server
|
|
43
|
+
* @param {string} redirectUrl - The URL configured for redirect in the OAuth configuration on the server
|
|
44
|
+
* @param {boolean} useHistory - If true, uses `history.replaceState()`, otherwise uses `window.location.assign()` (defaults to true)
|
|
45
|
+
* @param {string} loginPage - Custom landing page URL prior to logging in
|
|
46
|
+
* @param {string} postLoginPage - URL to return to after completed authentication cycle (defaults to `window.location.toString()`)
|
|
47
|
+
* @param {string[]} scopes - OAuth scopes to request. If not provided, defaults to `["api:read-data", "api:write-data"]`
|
|
48
|
+
* @param {typeof globalThis.fetch} fetchFn - Custom fetch function to use for requests (defaults to `globalThis.fetch`)
|
|
49
|
+
* @param {string} ctxPath - Context path for the authorization server (defaults to "/multipass")
|
|
50
|
+
* @returns {PublicOauthClient} A client that can be used as a token provider
|
|
13
51
|
*/
|
|
14
52
|
export declare function createPublicOauthClient(client_id: string, url: string, redirectUrl: string, useHistory?: boolean, loginPage?: string, postLoginPage?: string, scopes?: string[], fetchFn?: typeof globalThis.fetch, ctxPath?: string): PublicOauthClient;
|
|
15
53
|
//# sourceMappingURL=createPublicOauthClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createPublicOauthClient.d.ts","sourceRoot":"","sources":["../../src/createPublicOauthClient.ts"],"names":[],"mappings":"AAmCA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.d.ts","sourceRoot":"","sources":["../../src/createPublicOauthClient.ts"],"names":[],"mappings":"AAmCA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAYhE,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IAErB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAElB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAElC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,wBAAwB,GACjC,iBAAiB,CAAC;AAErB;;;;;;;;;;;;;GAaG;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,CAAC,EAAE,MAAM,EACtB,MAAM,CAAC,EAAE,MAAM,EAAE,EACjB,OAAO,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,EACjC,OAAO,CAAC,EAAE,MAAM,GACf,iBAAiB,CAAC"}
|
|
@@ -17,19 +17,16 @@ import delay from "delay";
|
|
|
17
17
|
import { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, refreshTokenGrantRequest, validateAuthResponse } from "oauth4webapi";
|
|
18
18
|
import { common, createAuthorizationServer, readLocal, removeLocal, saveLocal } from "./common.js";
|
|
19
19
|
import { throwIfError } from "./throwIfError.js";
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
* @returns {PublicOauthClient} which can be used as a token provider
|
|
31
|
-
*/
|
|
32
|
-
export function createPublicOauthClient(client_id, url, redirectUrl, useHistory = true, loginPage, postLoginPage = window.location.toString(), scopes = [], fetchFn = globalThis.fetch, ctxPath = "/multipass") {
|
|
20
|
+
import { processOptionsAndAssignDefaults } from "./utils.js";
|
|
21
|
+
export function createPublicOauthClient(client_id, url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath) {
|
|
22
|
+
({
|
|
23
|
+
useHistory,
|
|
24
|
+
loginPage,
|
|
25
|
+
postLoginPage,
|
|
26
|
+
scopes,
|
|
27
|
+
fetchFn,
|
|
28
|
+
ctxPath
|
|
29
|
+
} = processOptionsAndAssignDefaults(url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath));
|
|
33
30
|
const client = {
|
|
34
31
|
client_id,
|
|
35
32
|
token_endpoint_auth_method: "none"
|
|
@@ -38,9 +35,6 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
38
35
|
const oauthHttpOptions = {
|
|
39
36
|
[customFetch]: fetchFn
|
|
40
37
|
};
|
|
41
|
-
if (scopes.length === 0) {
|
|
42
|
-
scopes = ["api:read-data", "api:write-data"];
|
|
43
|
-
}
|
|
44
38
|
const {
|
|
45
39
|
makeTokenAndSaveRefresh,
|
|
46
40
|
getToken
|
|
@@ -53,11 +47,12 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
53
47
|
// 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
|
|
54
48
|
?? (await initiateLoginRedirect());
|
|
55
49
|
}, oauthHttpOptions, maybeRefresh.bind(globalThis, true));
|
|
56
|
-
|
|
50
|
+
// as an arrow function, `useHistory` is known to be a boolean
|
|
51
|
+
const go = async x => {
|
|
57
52
|
if (useHistory) return window.history.replaceState({}, "", x);else window.location.assign(x);
|
|
58
53
|
await delay(1000);
|
|
59
54
|
throw new Error("Unable to redirect");
|
|
60
|
-
}
|
|
55
|
+
};
|
|
61
56
|
async function maybeRefresh(expectRefreshToken) {
|
|
62
57
|
const {
|
|
63
58
|
refresh_token
|
|
@@ -70,7 +65,7 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
70
65
|
// note, we don't use processRefreshTokenResponse because the mp oauth implementation
|
|
71
66
|
// doesn't do refresh tokens quite right and this lets us skip the id check
|
|
72
67
|
const result = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await refreshTokenGrantRequest(authServer, client, refresh_token, oauthHttpOptions))), "refresh");
|
|
73
|
-
if (result && window.location.pathname === new URL(
|
|
68
|
+
if (result && window.location.pathname === new URL(redirect_uri).pathname) {
|
|
74
69
|
const {
|
|
75
70
|
oldUrl
|
|
76
71
|
} = readLocal(client);
|
|
@@ -96,7 +91,7 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
96
91
|
} = readLocal(client);
|
|
97
92
|
if (!codeVerifier) return;
|
|
98
93
|
try {
|
|
99
|
-
const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await authorizationCodeGrantRequest(authServer, client, throwIfError(validateAuthResponse(authServer, client, new URL(window.location.href), state)),
|
|
94
|
+
const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await authorizationCodeGrantRequest(authServer, client, throwIfError(validateAuthResponse(authServer, client, new URL(window.location.href), state)), redirect_uri, codeVerifier, oauthHttpOptions))), "signIn");
|
|
100
95
|
go(oldUrl);
|
|
101
96
|
return ret;
|
|
102
97
|
} catch (e) {
|
|
@@ -107,7 +102,8 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
107
102
|
removeLocal(client);
|
|
108
103
|
}
|
|
109
104
|
}
|
|
110
|
-
|
|
105
|
+
// As an arrow function, `scopes` and `postLoginPage` are known at compile time
|
|
106
|
+
const initiateLoginRedirect = async () => {
|
|
111
107
|
if (loginPage && window.location.href !== loginPage && window.location.pathname !== loginPage) {
|
|
112
108
|
saveLocal(client, {
|
|
113
109
|
oldUrl: postLoginPage
|
|
@@ -126,7 +122,7 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
126
122
|
client_id,
|
|
127
123
|
response_type: "code",
|
|
128
124
|
state,
|
|
129
|
-
redirect_uri
|
|
125
|
+
redirect_uri,
|
|
130
126
|
code_challenge: await calculatePKCECodeChallenge(codeVerifier),
|
|
131
127
|
code_challenge_method: "S256",
|
|
132
128
|
scope: ["offline_access", ...scopes].join(" ")
|
|
@@ -134,7 +130,7 @@ export function createPublicOauthClient(client_id, url, redirectUrl, useHistory
|
|
|
134
130
|
// Give time for redirect to happen
|
|
135
131
|
await delay(1000);
|
|
136
132
|
throw new Error("Unable to redirect");
|
|
137
|
-
}
|
|
133
|
+
};
|
|
138
134
|
return getToken;
|
|
139
135
|
}
|
|
140
136
|
//# sourceMappingURL=createPublicOauthClient.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createPublicOauthClient.js","names":["delay","authorizationCodeGrantRequest","calculatePKCECodeChallenge","customFetch","generateRandomCodeVerifier","generateRandomState","processAuthorizationCodeOAuth2Response","refreshTokenGrantRequest","validateAuthResponse","common","createAuthorizationServer","readLocal","removeLocal","saveLocal","throwIfError","createPublicOauthClient","client_id","url","redirectUrl","useHistory","loginPage","postLoginPage","window","location","toString","scopes","fetchFn","globalThis","fetch","ctxPath","client","token_endpoint_auth_method","authServer","oauthHttpOptions","length","makeTokenAndSaveRefresh","getToken","_signIn","maybeRefresh","maybeHandleAuthReturn","initiateLoginRedirect","bind","go","x","history","replaceState","assign","Error","expectRefreshToken","refresh_token","result","pathname","URL","oldUrl","e","process","env","NODE_ENV","console","warn","codeVerifier","state","ret","href","authorization_endpoint","URLSearchParams","response_type","redirect_uri","code_challenge","code_challenge_method","scope","join"],"sources":["createPublicOauthClient.js"],"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 */\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, refreshTokenGrantRequest, validateAuthResponse, } from \"oauth4webapi\";\nimport { common, createAuthorizationServer, readLocal, removeLocal, saveLocal, } from \"./common.js\";\nimport { throwIfError } from \"./throwIfError.js\";\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 = true, loginPage, postLoginPage = window.location.toString(), scopes = [], fetchFn = globalThis.fetch, ctxPath = \"/multipass\") {\n const client = { client_id, token_endpoint_auth_method: \"none\" };\n const authServer = createAuthorizationServer(ctxPath, url);\n const oauthHttpOptions = { [customFetch]: fetchFn };\n if (scopes.length === 0) {\n scopes = [\"api:read-data\", \"api:write-data\"];\n }\n const { makeTokenAndSaveRefresh, getToken } = common(client, authServer, _signIn, oauthHttpOptions, maybeRefresh.bind(globalThis, true));\n async function go(x) {\n if (useHistory)\n return window.history.replaceState({}, \"\", x);\n else\n window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n }\n async function maybeRefresh(expectRefreshToken) {\n const { refresh_token } = readLocal(client);\n if (!refresh_token) {\n if (expectRefreshToken)\n throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we don't use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n const result = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await refreshTokenGrantRequest(authServer, client, refresh_token, oauthHttpOptions))), \"refresh\");\n if (result && window.location.pathname === new URL(redirectUrl).pathname) {\n const { oldUrl } = readLocal(client);\n go(oldUrl ?? \"/\");\n }\n return result;\n }\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(client);\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const { codeVerifier, state, oldUrl } = readLocal(client);\n if (!codeVerifier)\n return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await authorizationCodeGrantRequest(authServer, client, throwIfError(validateAuthResponse(authServer, client, new URL(window.location.href), state)), redirectUrl, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n }\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 PKCE, removing PKCE and starting a new auth flow\", e);\n }\n removeLocal(client);\n }\n }\n async function initiateLoginRedirect() {\n if (loginPage\n && window.location.href !== loginPage\n && window.location.pathname !== loginPage) {\n saveLocal(client, { oldUrl: postLoginPage });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal(client).oldUrl ?? window.location.toString();\n saveLocal(client, { codeVerifier, state, oldUrl });\n window.location.assign(`${authServer\n .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 // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\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 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 return getToken;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,KAAK,MAAM,OAAO;AACzB,SAASC,6BAA6B,EAAEC,0BAA0B,EAAEC,WAAW,EAAEC,0BAA0B,EAAEC,mBAAmB,EAAEC,sCAAsC,EAAEC,wBAAwB,EAAEC,oBAAoB,QAAS,cAAc;AAC/O,SAASC,MAAM,EAAEC,yBAAyB,EAAEC,SAAS,EAAEC,WAAW,EAAEC,SAAS,QAAS,aAAa;AACnG,SAASC,YAAY,QAAQ,mBAAmB;AAChD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,uBAAuBA,CAACC,SAAS,EAAEC,G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;MAC/CjD,SAAS;MACTkD,aAAa,EAAE,MAAM;MACrBL,KAAK;MACLM,YAAY,EAAEjD,WAAW;MACzBkD,cAAc,EAAE,MAAMlE,0BAA0B,CAAC0D,YAAY,CAAC;MAC9DS,qBAAqB,EAAE,MAAM;MAC7BC,KAAK,EAAE,CAAC,gBAAgB,EAAE,GAAG7C,MAAM,CAAC,CAAC8C,IAAI,CAAC,GAAG;IACjD,CAAC,CAAC,EAAE,CAAC;IACL;IACA,MAAMvE,KAAK,CAAC,IAAI,CAAC;IACjB,MAAM,IAAI+C,KAAK,CAAC,oBAAoB,CAAC;EACzC;EAUA,OAAOX,QAAQ;AACnB","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"createPublicOauthClient.js","names":["delay","authorizationCodeGrantRequest","calculatePKCECodeChallenge","customFetch","generateRandomCodeVerifier","generateRandomState","processAuthorizationCodeOAuth2Response","refreshTokenGrantRequest","validateAuthResponse","common","createAuthorizationServer","readLocal","removeLocal","saveLocal","throwIfError","processOptionsAndAssignDefaults","createPublicOauthClient","client_id","url","redirect_uri","useHistory","loginPage","postLoginPage","scopes","fetchFn","ctxPath","client","token_endpoint_auth_method","authServer","oauthHttpOptions","makeTokenAndSaveRefresh","getToken","_signIn","maybeRefresh","maybeHandleAuthReturn","initiateLoginRedirect","bind","globalThis","go","x","window","history","replaceState","location","assign","Error","expectRefreshToken","refresh_token","result","pathname","URL","oldUrl","e","process","env","NODE_ENV","console","warn","codeVerifier","state","ret","href","toString","authorization_endpoint","URLSearchParams","response_type","code_challenge","code_challenge_method","scope","join"],"sources":["createPublicOauthClient.js"],"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 */\nimport delay from \"delay\";\nimport { authorizationCodeGrantRequest, calculatePKCECodeChallenge, customFetch, generateRandomCodeVerifier, generateRandomState, processAuthorizationCodeOAuth2Response, refreshTokenGrantRequest, validateAuthResponse, } from \"oauth4webapi\";\nimport { common, createAuthorizationServer, readLocal, removeLocal, saveLocal, } from \"./common.js\";\nimport { throwIfError } from \"./throwIfError.js\";\nimport { processOptionsAndAssignDefaults } from \"./utils.js\";\nexport function createPublicOauthClient(client_id, url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath) {\n ({\n useHistory,\n loginPage,\n postLoginPage,\n scopes,\n fetchFn,\n ctxPath,\n } = processOptionsAndAssignDefaults(url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath));\n const client = { client_id, token_endpoint_auth_method: \"none\" };\n const authServer = createAuthorizationServer(ctxPath, url);\n const oauthHttpOptions = { [customFetch]: fetchFn };\n const { makeTokenAndSaveRefresh, getToken } = common(client, authServer, _signIn, oauthHttpOptions, maybeRefresh.bind(globalThis, true));\n // as an arrow function, `useHistory` is known to be a boolean\n const go = async (x) => {\n if (useHistory)\n return window.history.replaceState({}, \"\", x);\n else\n window.location.assign(x);\n await delay(1000);\n throw new Error(\"Unable to redirect\");\n };\n async function maybeRefresh(expectRefreshToken) {\n const { refresh_token } = readLocal(client);\n if (!refresh_token) {\n if (expectRefreshToken)\n throw new Error(\"No refresh token found\");\n return;\n }\n try {\n // note, we don't use processRefreshTokenResponse because the mp oauth implementation\n // doesn't do refresh tokens quite right and this lets us skip the id check\n const result = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await refreshTokenGrantRequest(authServer, client, refresh_token, oauthHttpOptions))), \"refresh\");\n if (result && window.location.pathname === new URL(redirect_uri).pathname) {\n const { oldUrl } = readLocal(client);\n go(oldUrl ?? \"/\");\n }\n return result;\n }\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(client);\n if (expectRefreshToken) {\n throw new Error(\"Could not refresh token\");\n }\n }\n }\n async function maybeHandleAuthReturn() {\n const { codeVerifier, state, oldUrl } = readLocal(client);\n if (!codeVerifier)\n return;\n try {\n const ret = makeTokenAndSaveRefresh(throwIfError(await processAuthorizationCodeOAuth2Response(authServer, client, await authorizationCodeGrantRequest(authServer, client, throwIfError(validateAuthResponse(authServer, client, new URL(window.location.href), state)), redirect_uri, codeVerifier, oauthHttpOptions))), \"signIn\");\n go(oldUrl);\n return ret;\n }\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 PKCE, removing PKCE and starting a new auth flow\", e);\n }\n removeLocal(client);\n }\n }\n // As an arrow function, `scopes` and `postLoginPage` are known at compile time\n const initiateLoginRedirect = async () => {\n if (loginPage\n && window.location.href !== loginPage\n && window.location.pathname !== loginPage) {\n saveLocal(client, { oldUrl: postLoginPage });\n return await go(loginPage);\n }\n const state = generateRandomState();\n const codeVerifier = generateRandomCodeVerifier();\n const oldUrl = readLocal(client).oldUrl ?? window.location.toString();\n saveLocal(client, { codeVerifier, state, oldUrl });\n window.location.assign(`${authServer\n .authorization_endpoint}?${new URLSearchParams({\n client_id,\n response_type: \"code\",\n state,\n redirect_uri,\n code_challenge: await calculatePKCECodeChallenge(codeVerifier),\n code_challenge_method: \"S256\",\n scope: [\"offline_access\", ...scopes].join(\" \"),\n })}`);\n // Give time for redirect to happen\n await delay(1000);\n throw new Error(\"Unable to redirect\");\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 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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AAEyB,MAAM,EAAE7B;MAAc,CAAC,CAAC;MAC5C,OAAO,MAAMgB,EAAE,CAACjB,SAAS,CAAC;IAC9B;IACA,MAAMsC,KAAK,GAAGtD,mBAAmB,CAAC,CAAC;IACnC,MAAMqD,YAAY,GAAGtD,0BAA0B,CAAC,CAAC;IACjD,MAAM+C,MAAM,GAAGxC,SAAS,CAACe,MAAM,CAAC,CAACyB,MAAM,IAAIX,MAAM,CAACG,QAAQ,CAACmB,QAAQ,CAAC,CAAC;IACrEjD,SAAS,CAACa,MAAM,EAAE;MAAEgC,YAAY;MAAEC,KAAK;MAAER;IAAO,CAAC,CAAC;IAClDX,MAAM,CAACG,QAAQ,CAACC,MAAM,CAAC,GAAGhB,UAAU,CAC/BmC,sBAAsB,IAAI,IAAIC,eAAe,CAAC;MAC/C/C,SAAS;MACTgD,aAAa,EAAE,MAAM;MACrBN,KAAK;MACLxC,YAAY;MACZ+C,cAAc,EAAE,MAAMhE,0BAA0B,CAACwD,YAAY,CAAC;MAC9DS,qBAAqB,EAAE,MAAM;MAC7BC,KAAK,EAAE,CAAC,gBAAgB,EAAE,GAAG7C,MAAM,CAAC,CAAC8C,IAAI,CAAC,GAAG;IACjD,CAAC,CAAC,EAAE,CAAC;IACL;IACA,MAAMrE,KAAK,CAAC,IAAI,CAAC;IACjB,MAAM,IAAI6C,KAAK,CAAC,oBAAoB,CAAC;EACzC,CAAC;EAUD,OAAOd,QAAQ;AACnB","ignoreList":[]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { PublicOauthClientOptions } from "./createPublicOauthClient.js";
|
|
2
|
+
export type ProcessedPublicOauthClientOptions = Omit<Required<PublicOauthClientOptions>, "loginPage"> & {
|
|
3
|
+
loginPage?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare function processOptionsAndAssignDefaults(url: string, redirect_uri: string, useHistory?: boolean | PublicOauthClientOptions, loginPage?: string, postLoginPage?: string, scopes?: string[], fetchFn?: typeof globalThis.fetch, ctxPath?: string): ProcessedPublicOauthClientOptions;
|
|
6
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAE7E,MAAM,MAAM,iCAAiC,GACzC,IAAI,CAAC,QAAQ,CAAC,wBAAwB,CAAC,EAAE,WAAW,CAAC,GACrD;IACA,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEJ,wBAAgB,+BAA+B,CAC7C,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,MAAM,EACpB,UAAU,CAAC,EAAE,OAAO,GAAG,wBAAwB,EAC/C,SAAS,CAAC,EAAE,MAAM,EAClB,aAAa,CAAC,EAAE,MAAM,EACtB,MAAM,CAAC,EAAE,MAAM,EAAE,EACjB,OAAO,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,EACjC,OAAO,CAAC,EAAE,MAAM,GACf,iCAAiC,CAgCnC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import invariant from "tiny-invariant";
|
|
17
|
+
export function processOptionsAndAssignDefaults(url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath) {
|
|
18
|
+
let options = {};
|
|
19
|
+
if (typeof useHistory === "object") {
|
|
20
|
+
!(!loginPage && !postLoginPage && !scopes && !fetchFn && !ctxPath) ? process.env.NODE_ENV !== "production" ? invariant(false, "If useHistory is an object, other options should not be provided") : invariant(false) : void 0;
|
|
21
|
+
options = useHistory;
|
|
22
|
+
} else {
|
|
23
|
+
options = {
|
|
24
|
+
useHistory,
|
|
25
|
+
loginPage,
|
|
26
|
+
postLoginPage,
|
|
27
|
+
scopes,
|
|
28
|
+
fetchFn,
|
|
29
|
+
ctxPath
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
!url ? process.env.NODE_ENV !== "production" ? invariant(false, "url is required") : invariant(false) : void 0;
|
|
33
|
+
!redirect_uri ? process.env.NODE_ENV !== "production" ? invariant(false, "redirectUrl is required") : invariant(false) : void 0;
|
|
34
|
+
// Assign defaults
|
|
35
|
+
return {
|
|
36
|
+
useHistory: options.useHistory ?? true,
|
|
37
|
+
loginPage: options.loginPage,
|
|
38
|
+
postLoginPage: options.postLoginPage || window.location.toString(),
|
|
39
|
+
scopes: options.scopes ?? ["api:read-data", "api:write-data"],
|
|
40
|
+
fetchFn: options.fetchFn ?? globalThis.fetch,
|
|
41
|
+
ctxPath: options.ctxPath ?? "/multipass"
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":["invariant","processOptionsAndAssignDefaults","url","redirect_uri","useHistory","loginPage","postLoginPage","scopes","fetchFn","ctxPath","options","process","env","NODE_ENV","window","location","toString","globalThis","fetch"],"sources":["utils.js"],"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 */\nimport invariant from \"tiny-invariant\";\nexport function processOptionsAndAssignDefaults(url, redirect_uri, useHistory, loginPage, postLoginPage, scopes, fetchFn, ctxPath) {\n let options = {};\n if (typeof useHistory === \"object\") {\n invariant(!loginPage && !postLoginPage && !scopes && !fetchFn && !ctxPath, \"If useHistory is an object, other options should not be provided\");\n options = useHistory;\n }\n else {\n options = {\n useHistory,\n loginPage,\n postLoginPage,\n scopes,\n fetchFn,\n ctxPath,\n };\n }\n invariant(url, \"url is required\");\n invariant(redirect_uri, \"redirectUrl is required\");\n // Assign defaults\n return {\n useHistory: options.useHistory ?? true,\n loginPage: options.loginPage,\n postLoginPage: options.postLoginPage || window.location.toString(),\n scopes: options.scopes ?? [\"api:read-data\", \"api:write-data\"],\n fetchFn: options.fetchFn ?? globalThis.fetch,\n ctxPath: options.ctxPath ?? \"/multipass\",\n };\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,SAAS,MAAM,gBAAgB;AACtC,OAAO,SAASC,+BAA+BA,CAACC,GAAG,EAAEC,YAAY,EAAEC,UAAU,EAAEC,SAAS,EAAEC,aAAa,EAAEC,MAAM,EAAEC,OAAO,EAAEC,OAAO,EAAE;EAC/H,IAAIC,OAAO,GAAG,CAAC,CAAC;EAChB,IAAI,OAAON,UAAU,KAAK,QAAQ,EAAE;IAChC,EAAU,CAACC,SAAS,IAAI,CAACC,aAAa,IAAI,CAACC,MAAM,IAAI,CAACC,OAAO,IAAI,CAACC,OAAO,IAAAE,OAAA,CAAAC,GAAA,CAAAC,QAAA,oBAAzEb,SAAS,QAAkE,kEAAkE,IAA7IA,SAAS;IACTU,OAAO,GAAGN,UAAU;EACxB,CAAC,MACI;IACDM,OAAO,GAAG;MACNN,UAAU;MACVC,SAAS;MACTC,aAAa;MACbC,MAAM;MACNC,OAAO;MACPC;IACJ,CAAC;EACL;EACA,CAAUP,GAAG,GAAAS,OAAA,CAAAC,GAAA,CAAAC,QAAA,oBAAbb,SAAS,QAAM,iBAAiB,IAAhCA,SAAS;EACT,CAAUG,YAAY,GAAAQ,OAAA,CAAAC,GAAA,CAAAC,QAAA,oBAAtBb,SAAS,QAAe,yBAAyB,IAAjDA,SAAS;EACT;EACA,OAAO;IACHI,UAAU,EAAEM,OAAO,CAACN,UAAU,IAAI,IAAI;IACtCC,SAAS,EAAEK,OAAO,CAACL,SAAS;IAC5BC,aAAa,EAAEI,OAAO,CAACJ,aAAa,IAAIQ,MAAM,CAACC,QAAQ,CAACC,QAAQ,CAAC,CAAC;IAClET,MAAM,EAAEG,OAAO,CAACH,MAAM,IAAI,CAAC,eAAe,EAAE,gBAAgB,CAAC;IAC7DC,OAAO,EAAEE,OAAO,CAACF,OAAO,IAAIS,UAAU,CAACC,KAAK;IAC5CT,OAAO,EAAEC,OAAO,CAACD,OAAO,IAAI;EAChC,CAAC;AACL","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@osdk/oauth",
|
|
3
|
-
"version": "1.1.0-beta.
|
|
3
|
+
"version": "1.1.0-beta.2",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"browser": "./build/browser/index.js",
|
|
12
|
-
"import": "./build/esm/index.js"
|
|
12
|
+
"import": "./build/esm/index.js",
|
|
13
|
+
"default": "./build/browser/index.js"
|
|
13
14
|
},
|
|
14
15
|
"./*": {
|
|
15
16
|
"browser": "./build/browser/public/*.js",
|
|
16
|
-
"import": "./build/esm/public/*.js"
|
|
17
|
+
"import": "./build/esm/public/*.js",
|
|
18
|
+
"default": "./build/browser/public/*.js"
|
|
17
19
|
}
|
|
18
20
|
},
|
|
19
21
|
"dependencies": {
|
|
@@ -24,7 +26,7 @@
|
|
|
24
26
|
},
|
|
25
27
|
"devDependencies": {
|
|
26
28
|
"jest-extended": "^4.0.2",
|
|
27
|
-
"typescript": "
|
|
29
|
+
"typescript": "~5.5.4",
|
|
28
30
|
"@osdk/monorepo.api-extractor": "~0.0.0",
|
|
29
31
|
"@osdk/monorepo.tsconfig": "~0.0.0",
|
|
30
32
|
"@osdk/monorepo.tsup": "~0.0.0"
|