Skip to content

Commit

Permalink
fix issues with path being lowercased during federation (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickarlt authored May 23, 2019
1 parent 72181c2 commit 7788b21
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/arcgis-rest-auth/src/UserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,10 +651,9 @@ export class UserSession implements IAuthenticationManager {
// if a non-federated server was passed explicitly, it should be trusted.
if (options.server) {
// if the url includes more than '/arcgis/', trim the rest
const [serverRoot] = options.server
.toLowerCase()
.split(/\/rest(\/admin)?\/services\//);
this.trustedServers[serverRoot] = {
const root = this.getServerRootUrl(options.server);

this.trustedServers[root] = {
token: options.token,
expires: options.tokenExpires
};
Expand Down Expand Up @@ -776,6 +775,21 @@ export class UserSession implements IAuthenticationManager {
return Promise.reject(new ArcGISAuthError("Unable to refresh token."));
}

/**
* Determines the root of the ArcGIS Server or Portal for a given URL.
*
* @param url the URl to determine the root url for.
*/
public getServerRootUrl(url: string) {
const [root] = url.split(/\/rest(\/admin)?\/services\//);
const [match, protocol, domainAndPath] = root.match(/(https?:\/\/)(.+)/);
const [domain, ...path] = domainAndPath.split("/");

// only the domain is lowercased becasue in some cases an org id might be
// in the path which cannot be lowercased.
return `${protocol}${domain.toLowerCase()}/${path.join("/")}`;
}

/**
* Validates that a given URL is properly federated with our current `portal`.
* Attempts to use the internal `trustedServers` cache first.
Expand All @@ -786,7 +800,7 @@ export class UserSession implements IAuthenticationManager {
) {
// requests to /rest/services/ and /rest/admin/services/ are both valid
// Federated servers may have inconsistent casing, so lowerCase it
const [root] = url.toLowerCase().split(/\/rest(\/admin)?\/services\//);
const root = this.getServerRootUrl(url);
const existingToken = this.trustedServers[root];

if (
Expand Down
45 changes: 45 additions & 0 deletions packages/arcgis-rest-auth/test/UserSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,51 @@ describe("UserSession", () => {
});
});

describe("getServerRootUrl()", () => {
it("should lowercase domain names", () => {
const session = new UserSession({
clientId: "id",
token: "token",
tokenExpires: TOMORROW
});

const root = session.getServerRootUrl(
"https://PNP00035.esri.com/server/rest/services/Hosted/perimeters_dd83/FeatureServer"
);
expect(root).toEqual("https://pnp00035.esri.com/server");
});

it("should not lowercase path names", () => {
const session = new UserSession({
clientId: "id",
token: "token",
tokenExpires: TOMORROW
});

const root = session.getServerRootUrl(
"https://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis/rest/services/NB_Stereographic/VectorTileServer"
);
expect(root).toEqual(
"https://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis"
);
});

it("should respect the original https/http protocol", () => {
const session = new UserSession({
clientId: "id",
token: "token",
tokenExpires: TOMORROW
});

const root = session.getServerRootUrl(
"http://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis/rest/services/NB_Stereographic/VectorTileServer"
);
expect(root).toEqual(
"http://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis"
);
});
});

describe("non-federated server", () => {
it("shouldnt fetch a fresh token if the current one isnt expired.", done => {
const MOCK_USER_SESSION = new UserSession({
Expand Down

0 comments on commit 7788b21

Please sign in to comment.