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 issues with path being lowercased during federation #576

Merged
merged 1 commit into from
May 23, 2019
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
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