Skip to content

Commit cee4672

Browse files
authored
Merge pull request #1228 from Esri/b/fix-postMessage-auth-for-real
fix(arcgis-rest-request): postMessage auth works w/ credential
2 parents 428bbd8 + ae8159b commit cee4672

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

packages/arcgis-rest-request/src/ArcGISIdentityManager.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ import {
2424
import { NODEJS_DEFAULT_REFERER_HEADER } from "./index.js";
2525
import { AuthenticationManagerBase } from "./AuthenticationManagerBase.js";
2626

27+
/**
28+
* distinguish between an ICredential and IArcGISIdentityManagerOptions
29+
*/
30+
function isCredential(credential: any): credential is ICredential {
31+
return (
32+
typeof credential.userId === "string" ||
33+
typeof credential.expires === "number"
34+
);
35+
}
36+
2737
/**
2838
* Options for {@linkcode ArcGISIdentityManager.fromToken}.
2939
*/
@@ -904,7 +914,17 @@ export class ArcGISIdentityManager
904914
*/
905915
private static parentMessageHandler(event: any): ArcGISIdentityManager {
906916
if (event.data.type === "arcgis:auth:credential") {
907-
return new ArcGISIdentityManager(event.data.credential);
917+
const credential = event.data.credential as ICredential;
918+
// at 4.x - 4.5 we were passing .toJSON() instead of .toCredential()
919+
// so we attempt to handle either payload for backwards compatibility
920+
// but at the next breaking change we should only support an ICredential
921+
return isCredential(credential)
922+
? ArcGISIdentityManager.fromCredential(credential, {
923+
hasPortal: true,
924+
hasServer: false,
925+
server: credential.server
926+
} as IServerInfo)
927+
: new ArcGISIdentityManager(credential);
908928
}
909929
if (event.data.type === "arcgis:auth:error") {
910930
const err = new Error(event.data.error.message);
@@ -1318,10 +1338,10 @@ export class ArcGISIdentityManager
13181338
let msg = {};
13191339
if (isTokenValid) {
13201340
const credential = this.toCredential();
1321-
msg = {
1322-
type: "arcgis:auth:credential",
1323-
credential
1324-
};
1341+
// the following line allows us to conform to our spec without changing other depended-on functionality
1342+
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-auth/post-message-auth-spec.md#arcgisauthcredential
1343+
credential.server = credential.server.replace("/sharing/rest", "");
1344+
msg = { type: "arcgis:auth:credential", credential };
13251345
} else {
13261346
msg = {
13271347
type: "arcgis:auth:error",

packages/arcgis-rest-request/test/ArcGISIdentityManager.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,14 @@ describe("ArcGISIdentityManager", () => {
18111811
username: "jsmith"
18121812
};
18131813

1814+
const credential: ICredential = {
1815+
expires: TOMORROW.getTime(),
1816+
server: token.portal,
1817+
ssl: false,
1818+
token: token.token,
1819+
userId: token.username
1820+
};
1821+
18141822
it(".disablePostMessageAuth removes event listener", () => {
18151823
const removeSpy = spyOn(MockWindow, "removeEventListener");
18161824
const session = new ArcGISIdentityManager(token);
@@ -1958,7 +1966,7 @@ describe("ArcGISIdentityManager", () => {
19581966
postMessage(msg: any, origin: string) {
19591967
Win._fn({
19601968
origin: "https://origin.com",
1961-
data: { type: "arcgis:auth:credential", credential: token },
1969+
data: { type: "arcgis:auth:credential", credential },
19621970
source: Win.parent
19631971
});
19641972
}
@@ -1994,7 +2002,7 @@ describe("ArcGISIdentityManager", () => {
19942002
// fire a second we want to intercept
19952003
Win._fn({
19962004
origin: "https://origin.com",
1997-
data: { type: "arcgis:auth:credential", credential: token },
2005+
data: { type: "arcgis:auth:credential", credential },
19982006
source: Win.parent
19992007
});
20002008
}

0 commit comments

Comments
 (0)