@@ -24,6 +24,16 @@ import {
24
24
import { NODEJS_DEFAULT_REFERER_HEADER } from "./index.js" ;
25
25
import { AuthenticationManagerBase } from "./AuthenticationManagerBase.js" ;
26
26
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
+
27
37
/**
28
38
* Options for {@linkcode ArcGISIdentityManager.fromToken}.
29
39
*/
@@ -904,7 +914,17 @@ export class ArcGISIdentityManager
904
914
*/
905
915
private static parentMessageHandler ( event : any ) : ArcGISIdentityManager {
906
916
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 ) ;
908
928
}
909
929
if ( event . data . type === "arcgis:auth:error" ) {
910
930
const err = new Error ( event . data . error . message ) ;
@@ -1318,10 +1338,10 @@ export class ArcGISIdentityManager
1318
1338
let msg = { } ;
1319
1339
if ( isTokenValid ) {
1320
1340
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 } ;
1325
1345
} else {
1326
1346
msg = {
1327
1347
type : "arcgis:auth:error" ,
0 commit comments