Skip to content

fix: add token property to IAuthenticationManager #1212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fetchMock from "fetch-mock";
import { getGeography } from "../src/getGeography.js";

const MOCK_AUTH = {
token: "token",
getToken() {
return Promise.resolve("token");
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fetchMock from "fetch-mock";
import { queryDemographicData } from "../src/queryDemographicData.js";

const MOCK_AUTH = {
token: "token",
getToken() {
return Promise.resolve("token");
},
Expand Down
23 changes: 9 additions & 14 deletions packages/arcgis-rest-geocoding/test/bulk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ const addresses = [
}
];

const MOCK_AUTH = {
token: "token",
getToken() {
return Promise.resolve("token");
},
portal: "https://mapsdev.arcgis.com"
};

describe("geocode", () => {
afterEach(() => {
fetchMock.restore();
});

it("should make a bulk geocoding request, even with an unmatchable record", (done) => {
fetchMock.once("*", GeocodeAddresses);

const MOCK_AUTH = {
getToken() {
return Promise.resolve("token");
},
portal: "https://mapsdev.arcgis.com"
};

bulkGeocode({ addresses, authentication: MOCK_AUTH })
.then((response) => {
expect(fetchMock.called()).toEqual(true);
Expand Down Expand Up @@ -207,13 +209,6 @@ describe("geocode", () => {
it("should support rawResponse", (done) => {
fetchMock.once("*", GeocodeAddresses);

const MOCK_AUTH = {
getToken() {
return Promise.resolve("token");
},
portal: "https://mapsdev.arcgis.com"
};

bulkGeocode({ addresses, authentication: MOCK_AUTH, rawResponse: true })
.then((response: any) => {
expect(fetchMock.called()).toEqual(true);
Expand Down
1 change: 1 addition & 0 deletions packages/arcgis-rest-portal/test/groups/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe("groups", () => {

describe("authenticted methods", () => {
const MOCK_AUTH = {
token: "token",
getToken() {
return Promise.resolve("fake-token");
},
Expand Down
1 change: 1 addition & 0 deletions packages/arcgis-rest-portal/test/groups/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe("groups", () => {

describe("authenticted methods", () => {
const MOCK_AUTH = {
token: "token",
getToken() {
return Promise.resolve("fake-token");
},
Expand Down
1 change: 1 addition & 0 deletions packages/arcgis-rest-portal/test/users/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UserSearchResponse } from "../mocks/users/user-search.js";

describe("users", () => {
const MOCK_AUTH = {
token: "token",
getToken() {
return Promise.resolve("fake-token");
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe("determineUsername", () => {
const requestOptions = {
username: "c@sey",
authentication: {
token: "fake",
portal: "https://bar.com/arcgis/sharing/rest",
username: "bob",
getUsername: function () {
Expand All @@ -34,6 +35,7 @@ describe("determineUsername", () => {
it("should fallback to the username in the requestOptions authentication", () => {
const requestOptions = {
authentication: {
token: "fake",
portal: "https://bar.com/arcgis/sharing/rest",
username: "bob",
getUsername: function () {
Expand All @@ -52,6 +54,7 @@ describe("determineUsername", () => {
it("should fallback to getUsername() in the requestOptions authentication", () => {
const requestOptions = {
authentication: {
token: "fake",
portal: "https://bar.com/arcgis/sharing/rest",
getUsername: function () {
return Promise.resolve("jsmith");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("portal", () => {
describe("getPortalSettings", () => {
// setup an authmgr to use in all these tests
const MOCK_AUTH = {
token: "fake-token",
getToken() {
return Promise.resolve("fake-token");
},
Expand Down
2 changes: 2 additions & 0 deletions packages/arcgis-rest-portal/test/util/get-portal-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("getPortalUrl", () => {
it("should use the portal from authorization if passed", () => {
const requestOptions = {
authentication: {
token: "token",
portal: "https://foo.com/arcgis/sharing/rest",
getToken() {
return Promise.resolve("fake");
Expand All @@ -24,6 +25,7 @@ describe("getPortalUrl", () => {
it("should use the portal in the requestOptions if passed", () => {
const requestOptions = {
authentication: {
token: "token",
portal: "https://foo.com/arcgis/sharing/rest",
getToken() {
return Promise.resolve("fake");
Expand Down
30 changes: 9 additions & 21 deletions packages/arcgis-rest-portal/test/util/portal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ import {
SubscriptionInfoResponse
} from "./../mocks/portal/response.js";

// setup an authmgr to use in all these tests
const MOCK_AUTH = {
token: "fake-token",
getToken() {
return Promise.resolve("fake-token");
},
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};

describe("portal", () => {
afterEach(() => {
fetchMock.restore();
});
describe("getPortal", () => {
// setup an authmgr to use in all these tests
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};
const MOCK_REQOPTS = {
authentication: MOCK_AUTH
};
Expand Down Expand Up @@ -60,13 +62,6 @@ describe("portal", () => {
});
});
describe("getSelf", () => {
// setup an authmgr to use in all these tests
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};
const MOCK_REQOPTS = {
authentication: MOCK_AUTH
};
Expand All @@ -90,13 +85,6 @@ describe("portal", () => {
});

describe("getSubscriptionInfo", () => {
// setup an authmgr to use in all these tests
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};
const MOCK_REQOPTS = {
authentication: MOCK_AUTH
};
Expand Down
7 changes: 7 additions & 0 deletions packages/arcgis-rest-request/src/ApiKeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ export class ApiKeyManager

private key: string;

/**
* The current API key as a string.
*/
get token() {
return this.key;
}

/**
* The preferred method for creating an instance of `ApiKeyManager`.
*/
Expand Down
13 changes: 10 additions & 3 deletions packages/arcgis-rest-request/src/ApplicationCredentialsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,17 @@ export class ApplicationCredentialsManager
public portal: string;
private clientId: string;
private clientSecret: string;
private token: string;
private _token: string;
private expires: Date;
private duration: number;

/**
* The current token. This may be invalid, expired or `undefined`. To guarantee a valid token, use the {@linkcode ApplicationCredentialsManager.getToken} method instead.
*/
public get token() {
return this._token;
}

/**
* Preferred method for creating an `ApplicationCredentialsManager`
*/
Expand All @@ -89,7 +96,7 @@ export class ApplicationCredentialsManager
super(options);
this.clientId = options.clientId;
this.clientSecret = options.clientSecret;
this.token = options.token;
this._token = options.token;
this.expires = options.expires;
this.portal = options.portal || "https://www.arcgis.com/sharing/rest";
this.duration = options.duration || 7200;
Expand Down Expand Up @@ -127,7 +134,7 @@ export class ApplicationCredentialsManager
return fetchToken(`${this.portal}/oauth2/token/`, options)
.then((response) => {
this._pendingTokenRequest = null;
this.token = response.token;
this._token = response.token;
this.expires = response.expires;
return response.token;
})
Expand Down
1 change: 1 addition & 0 deletions packages/arcgis-rest-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export function internalRequest(

authentication = {
portal: "https://www.arcgis.com/sharing/rest",
token: rawToken,
getToken: () => {
return Promise.resolve(rawToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface IAuthenticationManager {
*/
portal: string;

/**
* Getter for consistency between ArcGISIdentityManager and older ArcGIS REST JS verisons. This token may be invalid, expired or `undefined`. To guarentee a valid token, use the {@linkcode IAuthenticationManager.getToken} method instead.
*/
get token(): string;

/**
* Returns the proper token for a given URL and request options.
* @param url The requested URL.
Expand Down
10 changes: 10 additions & 0 deletions packages/arcgis-rest-request/test/ApiKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,15 @@ describe("ApiKeyManager", () => {
fail(e);
});
});

describe(".token", () => {
it("should return the token", () => {
const session = ApiKeyManager.fromKey({
key: "token"
});

expect(session.token).toEqual("token");
});
});
});
});
36 changes: 8 additions & 28 deletions packages/arcgis-rest-request/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ import {
isNode
} from "../../../scripts/test-helpers.js";

const MOCK_AUTH = {
token: "token",
portal: "https://www.arcgis.com/sharing/rest",
getToken() {
return Promise.resolve("token");
}
};

describe("request()", () => {
afterEach(() => {
fetchMock.restore();
Expand Down Expand Up @@ -159,13 +167,6 @@ describe("request()", () => {
it("should use the `authentication` option to authenticate a request", (done) => {
fetchMock.once("*", WebMapAsText);

const MOCK_AUTH = {
portal: "https://www.arcgis.com/sharing/rest",
getToken() {
return Promise.resolve("token");
}
};

request(
"https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data",
{
Expand All @@ -189,13 +190,6 @@ describe("request()", () => {
it("should hide token in POST body in browser environments otherwise it should hide token in `X-ESRI_AUTHORIZATION` header in Node", (done) => {
fetchMock.once("*", SharingRestInfo);

const MOCK_AUTH = {
portal: "https://www.arcgis.com/sharing/rest",
getToken() {
return Promise.resolve("token");
}
};

request("https://www.arcgis.com/sharing/rest/info", {
authentication: MOCK_AUTH,
httpMethod: "GET",
Expand Down Expand Up @@ -271,13 +265,6 @@ describe("request()", () => {
fetchMock.once("*", SharingRestInfo);
const restInfoUrl = "https://www.arcgis.com/sharing/rest/info";

const MOCK_AUTH = {
portal: "https://www.arcgis.com/sharing/rest",
getToken() {
return Promise.resolve("token");
}
};

request(restInfoUrl, {
authentication: MOCK_AUTH,
httpMethod: "GET",
Expand Down Expand Up @@ -459,13 +446,6 @@ describe("request()", () => {
return;
});

const MOCK_AUTH = {
portal: "https://www.arcgis.com/sharing/rest",
getToken() {
return Promise.resolve("token");
}
};

setDefaultRequestOptions({
authentication: MOCK_AUTH
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ describe("ArcGISRequestError", () => {

describe("retry", () => {
const MockAuth: {
token: string;
portal: string;
getToken: any;
retryHandler: IRetryAuthError;
} = {
token: "token",
portal: "https://www.arcgis.com/sharing/rest",
getToken() {
return Promise.resolve("token");
Expand Down
Loading