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(UserSession): switch "duration" to "expiration" in IOAuth2Options #847

Merged
merged 5 commits into from
May 20, 2021
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
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"
}
}
}