Skip to content
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

Fix #1035 #1093

Merged
merged 4 commits into from
Apr 27, 2023
Merged
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
70 changes: 43 additions & 27 deletions packages/arcgis-rest-request/src/fetch-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ITokenRequestOptions } from "./utils/ITokenRequestOptions.js";

const FIVE_MINUTES_IN_MILLISECONDS = 5 * 60 * 1000;

interface IFetchTokenRawResponse {
interface IoAuthTokenResponse {
access_token: string;
expires_in: number;
username: string;
Expand All @@ -16,11 +16,16 @@ interface IFetchTokenRawResponse {
refresh_token_expires_in?: number;
}

interface IGenerateTokenRawResponse {
token: string;
expires: number;
}

export interface IFetchTokenResponse {
token: string;
expires: Date;
username: string;
ssl: boolean;
ssl?: boolean;
refreshToken?: string;
refreshTokenExpires?: Date;
}
Expand All @@ -34,32 +39,43 @@ export function fetchToken(
// we generate a response, so we can't return the raw response
options.rawResponse = false;

return request(url, options).then((response: IFetchTokenRawResponse) => {
const r: IFetchTokenResponse = {
token: response.access_token,
username: response.username,
expires: new Date(
// convert seconds in response to milliseconds and add the value to the current time to calculate a static expiration timestamp
// we subtract 5 minutes here to make sure that we refresh the token early if the user makes requests
Date.now() + response.expires_in * 1000 - FIVE_MINUTES_IN_MILLISECONDS
),
ssl: response.ssl === true
};
return request(url, options).then(
(response: IGenerateTokenRawResponse | IoAuthTokenResponse) => {
// Typescript uses the "in" keyword to determine we have a generateToken response or an oauth token response
if ("token" in response && "expires" in response) {
return {
token: response.token,
username: requestOptions.params.username,
expires: new Date(response.expires)
};
}

if (response.refresh_token) {
r.refreshToken = response.refresh_token;
}
const portalTokenResponse: IFetchTokenResponse = {
token: response.access_token,
username: response.username,
expires: new Date(
// convert seconds in response to milliseconds and add the value to the current time to calculate a static expiration timestamp
// we subtract 5 minutes here to make sure that we refresh the token early if the user makes requests
Date.now() + response.expires_in * 1000 - FIVE_MINUTES_IN_MILLISECONDS
),
ssl: response.ssl === true
};

if (response.refresh_token_expires_in) {
r.refreshTokenExpires = new Date(
// convert seconds in response to milliseconds and add the value to the current time to calculate a static expiration timestamp
// we subtract 5 minutes here to make sure that we refresh the token early if the user makes requests
Date.now() +
response.refresh_token_expires_in * 1000 -
FIVE_MINUTES_IN_MILLISECONDS
);
}
if (response.refresh_token) {
portalTokenResponse.refreshToken = response.refresh_token;
}

return r;
});
if (response.refresh_token_expires_in) {
portalTokenResponse.refreshTokenExpires = new Date(
// convert seconds in response to milliseconds and add the value to the current time to calculate a static expiration timestamp
// we subtract 5 minutes here to make sure that we refresh the token early if the user makes requests
Date.now() +
response.refresh_token_expires_in * 1000 -
FIVE_MINUTES_IN_MILLISECONDS
);
}

return portalTokenResponse;
}
);
}
33 changes: 32 additions & 1 deletion packages/arcgis-rest-request/test/fetchToken.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import fetchMock from "fetch-mock";
import fetchMock, { MockCall } from "fetch-mock";
import { fetchToken } from "../src/index.js";

const TOKEN_URL = "https://www.arcgis.com/sharing/rest/oauth2/token";
Expand Down Expand Up @@ -106,4 +106,35 @@ describe("fetchToken()", () => {
fail(e);
});
});

it("should generate a token for an instance of ArcGIS Server", () => {
const SERVER_URL =
"https://my-arcgis-server.com/arcgis/tokens/generateToken";
const expiresDate = Date.now();

fetchMock.postOnce(SERVER_URL, {
token: "token",
expires: expiresDate
});

return fetchToken(SERVER_URL, {
params: {
username: "Casey",
password: "password"
}
})
.then((response) => {
const [url, options] = fetchMock.lastCall(SERVER_URL) as MockCall;
expect(url).toEqual(SERVER_URL);
expect(options?.body).toContain("f=json");
expect(options?.body).toContain("username=Casey");
expect(options?.body).toContain("password=password");
expect(response.token).toEqual("token");
expect(response.expires).toEqual(new Date(expiresDate));
expect(response.username).toEqual("Casey");
})
.catch((e) => {
fail(e);
});
});
});