Skip to content

Commit

Permalink
fix(UserSession): switch "duration" to "expiration" in IOAuth2Options (
Browse files Browse the repository at this point in the history
…#847)

* fix(UserSession): switch "duration" to "expiration" in IOAuth2Options

AFFECTS PACKAGES:
@esri/arcgis-rest-auth

ISSUES CLOSED: #843

* deprecated tag - only warn

since we're deprecating things, we want to allow the builds but warn.

* Update packages/arcgis-rest-auth/src/UserSession.ts

Co-authored-by: Tom Wayson <twayson@esri.com>

* do not mutate options

* missed options.duration

Co-authored-by: Tom Wayson <twayson@esri.com>
  • Loading branch information
gavinr and tomwayson authored May 20, 2021
1 parent 3f43679 commit 392f5bb
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 9 deletions.
31 changes: 23 additions & 8 deletions packages/arcgis-rest-auth/src/UserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,17 @@ export interface IOAuth2Options {

provider?: AuthenticationProvider;

/**
* The requested validity in minutes for a token. Defaults to 20160 (two weeks).
*/
expiration?: number;

/**
* Duration (in minutes) that a token will be valid. Defaults to 20160 (two weeks).
*
* @deprecated use 'expiration' instead
*/
duration?: number;
duration?: number;

/**
* Determines whether to open the authorization window in a new tab/window or in the current window.
Expand Down Expand Up @@ -297,11 +304,16 @@ export class UserSession implements IAuthenticationManager {
*/
/* istanbul ignore next */
public static beginOAuth2(options: IOAuth2Options, win: any = window) {

if(options.duration) {
console.log("DEPRECATED: 'duration' is deprecated - use 'expiration' instead");
}

const {
portal,
provider,
clientId,
duration,
expiration,
redirectUri,
popup,
popupWindowFeatures,
Expand All @@ -312,7 +324,7 @@ export class UserSession implements IAuthenticationManager {
...{
portal: "https://www.arcgis.com/sharing/rest",
provider: "arcgis",
duration: 20160,
expiration: 20160,
popup: true,
popupWindowFeatures:
"height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes",
Expand All @@ -323,11 +335,11 @@ export class UserSession implements IAuthenticationManager {
};
let url: string;
if (provider === "arcgis") {
url = `${portal}/oauth2/authorize?client_id=${clientId}&response_type=token&expiration=${duration}&redirect_uri=${encodeURIComponent(
url = `${portal}/oauth2/authorize?client_id=${clientId}&response_type=token&expiration=${options.duration || expiration}&redirect_uri=${encodeURIComponent(
redirectUri
)}&state=${state}&locale=${locale}`;
} else {
url = `${portal}/oauth2/social/authorize?client_id=${clientId}&socialLoginProviderName=${provider}&autoAccountCreateForSocial=true&response_type=token&expiration=${duration}&redirect_uri=${encodeURIComponent(
url = `${portal}/oauth2/social/authorize?client_id=${clientId}&socialLoginProviderName=${provider}&autoAccountCreateForSocial=true&response_type=token&expiration=${options.duration || expiration}&redirect_uri=${encodeURIComponent(
redirectUri
)}&state=${state}&locale=${locale}`;
}
Expand Down Expand Up @@ -528,13 +540,16 @@ export class UserSession implements IAuthenticationManager {
options: IOAuth2Options,
response: http.ServerResponse
) {
const { portal, clientId, duration, redirectUri }: IOAuth2Options = {
...{ portal: "https://arcgis.com/sharing/rest", duration: 20160 },
if(options.duration) {
console.log("DEPRECATED: 'duration' is deprecated - use 'expiration' instead");
}
const { portal, clientId, expiration, redirectUri }: IOAuth2Options = {
...{ portal: "https://arcgis.com/sharing/rest", expiration: 20160 },
...options,
};

response.writeHead(301, {
Location: `${portal}/oauth2/authorize?client_id=${clientId}&expiration=${duration}&response_type=code&redirect_uri=${encodeURIComponent(
Location: `${portal}/oauth2/authorize?client_id=${clientId}&expiration=${options.duration || expiration}&response_type=code&redirect_uri=${encodeURIComponent(
redirectUri
)}`,
});
Expand Down
92 changes: 92 additions & 0 deletions packages/arcgis-rest-auth/test/UserSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,52 @@ describe("UserSession", () => {
"https://www.arcgis.com/sharing/rest/oauth2/social/authorize?client_id=clientId123&socialLoginProviderName=google&autoAccountCreateForSocial=true&response_type=token&expiration=20160&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale="
);
});

it("should pass custom expiration", () => {
const MockWindow: any = {
location: {
href: "",
},
};

// https://github.com/palantir/tslint/issues/3056
void UserSession.beginOAuth2(
{
clientId: "clientId123",
redirectUri: "http://example-app.com/redirect",
popup: false,
expiration: 9000
},
MockWindow
);

expect(MockWindow.location.href).toBe(
"https://www.arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId123&response_type=token&expiration=9000&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale="
);
});

it("should pass custom duration (DEPRECATED)", () => {
const MockWindow: any = {
location: {
href: "",
},
};

// https://github.com/palantir/tslint/issues/3056
void UserSession.beginOAuth2(
{
clientId: "clientId123",
redirectUri: "http://example-app.com/redirect",
popup: false,
duration: 9001
},
MockWindow
);

expect(MockWindow.location.href).toBe(
"https://www.arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId123&response_type=token&expiration=9001&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale="
);
});
});

describe(".completeOAuth2()", () => {
Expand Down Expand Up @@ -1488,6 +1534,52 @@ describe("UserSession", () => {
MockResponse
);
});

it("should redirect the request to the authorization page with custom expiration", (done) => {
const spy = jasmine.createSpy("spy");
const MockResponse: any = {
writeHead: spy,
end() {
expect(spy.calls.mostRecent().args[0]).toBe(301);
expect(spy.calls.mostRecent().args[1].Location).toBe(
"https://arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId&expiration=10000&response_type=code&redirect_uri=https%3A%2F%2Fexample-app.com%2Fredirect-uri"
);
done();
},
};

UserSession.authorize(
{
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
expiration: 10000
},
MockResponse
);
});

it("should redirect the request to the authorization page with custom duration (DEPRECATED)", (done) => {
const spy = jasmine.createSpy("spy");
const MockResponse: any = {
writeHead: spy,
end() {
expect(spy.calls.mostRecent().args[0]).toBe(301);
expect(spy.calls.mostRecent().args[1].Location).toBe(
"https://arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId&expiration=10001&response_type=code&redirect_uri=https%3A%2F%2Fexample-app.com%2Fredirect-uri"
);
done();
},
};

UserSession.authorize(
{
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
duration: 10001
},
MockResponse
);
});
});

describe(".exchangeAuthorizationCode()", () => {
Expand Down
5 changes: 4 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"object-literal-sort-keys": false,
"interface-name": [true, "always-prefix"],
"no-string-literal": false,
"no-console": false
"no-console": false,
"deprecation": {
"severity": "warning"
}
}
}

0 comments on commit 392f5bb

Please sign in to comment.