Skip to content

Commit

Permalink
#295: SSL property added to UserSession class. SSL calculated in comp…
Browse files Browse the repository at this point in the history
…leteOAuth2 and used in to/fromCredential.
  • Loading branch information
skitterm committed Sep 20, 2018
1 parent c85a677 commit a350f76
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
22 changes: 21 additions & 1 deletion packages/arcgis-rest-auth/src/UserSession.ts
Expand Up @@ -165,6 +165,11 @@ export interface IUserSessionOptions {
*/
portal?: string;

/**
* Whether requests should be made exlusively over HTTPS.
*/
ssl?: boolean;

/**
* ArcGIS Authentication is used by default. Specifying an alternative will take users directly to the corresponding provider's OAuth page.
*/
Expand Down Expand Up @@ -215,6 +220,11 @@ export class UserSession implements IAuthenticationManager {
*/
readonly portal: string;

/**
* Whether requests should be made exlusively over HTTPS.
*/
readonly ssl: boolean;

/**
* The authentication provider to use.
*/
Expand Down Expand Up @@ -301,6 +311,7 @@ export class UserSession implements IAuthenticationManager {
this._token = options.token;
this._tokenExpires = options.tokenExpires;
this.portal = options.portal || "https://www.arcgis.com/sharing/rest";
this.ssl = options.ssl;
this.provider = options.provider || "arcgis";
this.tokenDuration = options.tokenDuration || 20160;
this.redirectUri = options.redirectUri;
Expand Down Expand Up @@ -373,6 +384,7 @@ export class UserSession implements IAuthenticationManager {
new UserSession({
clientId,
portal,
ssl: oauthInfo.ssl,
token: oauthInfo.token,
tokenExpires: new Date(oauthInfo.expires),
username: oauthInfo.username
Expand Down Expand Up @@ -430,6 +442,7 @@ export class UserSession implements IAuthenticationManager {
return new UserSession({
clientId,
portal,
ssl: oauthInfo.ssl,
token: oauthInfo.token,
tokenExpires: oauthInfo.expires,
username: oauthInfo.username
Expand All @@ -456,10 +469,14 @@ export class UserSession implements IAuthenticationManager {
Date.now() + parseInt(match[2], 10) * 1000 - 60 * 1000
);
const username = decodeURIComponent(match[3]);
const ssl =
win.location.href.indexOf("&ssl=true") !== -1 ||
win.location.href.indexOf("#ssl=true") !== -1;

return completeSignIn(undefined, {
token,
expires,
ssl,
username
});
}
Expand Down Expand Up @@ -536,6 +553,7 @@ export class UserSession implements IAuthenticationManager {
token: options.token,
tokenExpires: new Date(options.tokenExpires),
portal: options.portal,
ssl: options.ssl,
tokenDuration: options.tokenDuration,
redirectUri: options.redirectUri,
refreshTokenTTL: options.refreshTokenTTL
Expand All @@ -557,6 +575,7 @@ export class UserSession implements IAuthenticationManager {
static fromCredential(credential: ICredential) {
return new UserSession({
portal: credential.server + `/sharing/rest`,
ssl: credential.ssl,
token: credential.token,
username: credential.userId,
tokenExpires: new Date(credential.expires)
Expand All @@ -576,7 +595,7 @@ export class UserSession implements IAuthenticationManager {
return {
expires: this.tokenExpires.getTime(),
server: this.portal,
ssl: true,
ssl: this.ssl,
token: this.token,
userId: this.username
};
Expand Down Expand Up @@ -644,6 +663,7 @@ export class UserSession implements IAuthenticationManager {
token: this.token,
tokenExpires: this.tokenExpires,
portal: this.portal,
ssl: this.ssl,
tokenDuration: this.tokenDuration,
redirectUri: this.redirectUri,
refreshTokenTTL: this.refreshTokenTTL
Expand Down
2 changes: 2 additions & 0 deletions packages/arcgis-rest-auth/src/fetch-token.ts
Expand Up @@ -13,13 +13,15 @@ interface IFetchTokenRawResponse {
expires_in: number;
username: string;
refresh_token?: string;
ssl?: boolean;
}

export interface IFetchTokenResponse {
token: string;
expires: Date;
username: string;
refreshToken?: string;
ssl?: boolean;
}

export function fetchToken(
Expand Down
19 changes: 14 additions & 5 deletions packages/arcgis-rest-auth/test/UserSession.test.ts
Expand Up @@ -19,6 +19,7 @@ describe("UserSession", () => {
const session = new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
ssl: false,
token: "token",
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
Expand All @@ -34,6 +35,7 @@ describe("UserSession", () => {
expect(session2.redirectUri).toEqual(
"https://example-app.com/redirect-uri"
);
expect(session2.ssl).toEqual(false);
expect(session2.token).toEqual("token");
expect(session2.tokenExpires).toEqual(TOMORROW);
expect(session2.refreshToken).toEqual("refreshToken");
Expand Down Expand Up @@ -525,6 +527,7 @@ describe("UserSession", () => {
.then(session => {
expect(session.token).toBe("token");
expect(session.username).toBe("c@sey");
expect(session.ssl).toBe(true);
expect(session.tokenExpires).toEqual(TOMORROW);
done();
})
Expand All @@ -543,7 +546,8 @@ describe("UserSession", () => {
JSON.stringify({
token: "token",
expires: TOMORROW,
username: "c@sey"
username: "c@sey",
ssl: true
})
);
});
Expand Down Expand Up @@ -652,7 +656,7 @@ describe("UserSession", () => {
const MockWindow = {
location: {
href:
"https://example-app.com/redirect-uri#access_token=token&expires_in=1209600&username=c%40sey&persist=true"
"https://example-app.com/redirect-uri#access_token=token&expires_in=1209600&username=c%40sey&ssl=true&persist=true"
},
get parent() {
return this;
Expand All @@ -670,6 +674,7 @@ describe("UserSession", () => {
expect(session.token).toBe("token");
expect(session.tokenExpires.getTime()).toBeGreaterThan(Date.now());
expect(session.username).toBe("c@sey");
expect(session.ssl).toBe(true);
});

it("should callback to create a new user session if finds a valid opener", done => {
Expand All @@ -683,6 +688,7 @@ describe("UserSession", () => {
const oauthInfo = JSON.parse(oauthInfoString);
expect(oauthInfo.token).toBe("token");
expect(oauthInfo.username).toBe("c@sey");
expect(oauthInfo.ssl).toBe(false);
expect(new Date(oauthInfo.expires).getTime()).toBeGreaterThan(
Date.now()
);
Expand Down Expand Up @@ -717,6 +723,7 @@ describe("UserSession", () => {
const oauthInfo = JSON.parse(oauthInfoString);
expect(oauthInfo.token).toBe("token");
expect(oauthInfo.username).toBe("c@sey");
expect(oauthInfo.ssl).toBe(true);
expect(new Date(oauthInfo.expires).getTime()).toBeGreaterThan(
Date.now()
);
Expand All @@ -727,7 +734,7 @@ describe("UserSession", () => {
},
location: {
href:
"https://example-app.com/redirect-uri#access_token=token&expires_in=1209600&username=c%40sey"
"https://example-app.com/redirect-uri#access_token=token&expires_in=1209600&username=c%40sey&ssl=true"
}
};

Expand Down Expand Up @@ -872,7 +879,7 @@ describe("UserSession", () => {
const MOCK_CREDENTIAL: ICredential = {
expires: TOMORROW.getTime(),
server: "https://www.arcgis.com",
ssl: true,
ssl: false,
token: "token",
userId: "jsmith"
};
Expand All @@ -882,6 +889,7 @@ describe("UserSession", () => {
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "token",
ssl: false,
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
refreshTokenExpires: TOMORROW,
Expand All @@ -893,7 +901,7 @@ describe("UserSession", () => {
const creds = session.toCredential();
expect(creds.userId).toEqual("jsmith");
expect(creds.server).toEqual("https://www.arcgis.com/sharing/rest");
expect(creds.ssl).toEqual(true);
expect(creds.ssl).toEqual(false);
expect(creds.token).toEqual("token");
expect(creds.expires).toEqual(TOMORROW.getTime());
});
Expand All @@ -902,6 +910,7 @@ describe("UserSession", () => {
const session = UserSession.fromCredential(MOCK_CREDENTIAL);
expect(session.username).toEqual("jsmith");
expect(session.portal).toEqual("https://www.arcgis.com/sharing/rest");
expect(session.ssl).toEqual(false);
expect(session.token).toEqual("token");
expect(session.tokenExpires).toEqual(new Date(TOMORROW));
});
Expand Down

0 comments on commit a350f76

Please sign in to comment.