Skip to content

Commit

Permalink
Merge pull request #233 from Esri/bug/fix-portal-server-comparison
Browse files Browse the repository at this point in the history
fix(enterprise): ensure a brand new token can be generated for servers federated with ArcGIS Enterprise
  • Loading branch information
jgravois authored Jul 9, 2018
2 parents d15a6f5 + 299f3c0 commit ddd3d57
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/arcgis-rest-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@esri/arcgis-rest-request": "^1.2.1"
},
"devDependencies": {
"@esri/arcgis-rest-common-types": "^1.4.1",
"@esri/arcgis-rest-common-types": "^1.4.2",
"@esri/arcgis-rest-request": "^1.4.2"
},
"scripts": {
Expand Down
27 changes: 20 additions & 7 deletions packages/arcgis-rest-auth/src/UserSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,8 @@ export class UserSession implements IAuthenticationManager {
*/
getToken(url: string) {
if (
(/^https?:\/\/www\.arcgis\.com\/sharing\/rest\/?/.test(this.portal),
/^https?:\/\/\S+\.arcgis\.com.+/.test(url))
/^https?:\/\/www\.arcgis\.com\/sharing\/rest\/?/.test(this.portal) &&
/^https?:\/\/\S+\.arcgis\.com.+/.test(url)
) {
return this.getFreshToken();
} else if (new RegExp(this.portal).test(url)) {
Expand Down Expand Up @@ -686,11 +686,24 @@ export class UserSession implements IAuthenticationManager {
return response.authInfo.tokenServicesUrl;
})
.then((tokenServicesUrl: string) => {
return generateToken(tokenServicesUrl, {
token: this.token,
serverUrl: url,
expiration: this.tokenDuration
});
if (this.token) {
return generateToken(tokenServicesUrl, {
token: this.token,
serverUrl: url,
expiration: this.tokenDuration
});
// generate an entirely fresh token if necessary
} else {
return generateToken(tokenServicesUrl, {
username: this.username,
password: this.password,
expiration: this.tokenDuration
}).then((response: any) => {
this._token = response.token;
this._tokenExpires = new Date(response.expires);
return response;
});
}
})
.then(response => {
this.trustedServers[root] = {
Expand Down
42 changes: 41 additions & 1 deletion packages/arcgis-rest-auth/test/UserSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe("UserSession", () => {
});
});

it("should generate a token for an untrusted server", done => {
it("should generate a token for an untrusted, federated server", done => {
const session = new UserSession({
clientId: "id",
token: "token",
Expand Down Expand Up @@ -163,6 +163,46 @@ describe("UserSession", () => {
});
});

it("should generate a token for an untrusted, federated server with user credentials", done => {
const session = new UserSession({
username: "c@sey",
password: "jones",
portal: "https://gis.city.gov/sharing/rest"
});

fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
currentVersion: 10.51,
fullVersion: "10.5.1.120",
owningSystemUrl: "https://gis.city.gov",
authInfo: {
isTokenBasedSecurity: true,
tokenServicesUrl: "https://gis.city.gov/sharing/generateToken"
}
});

fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
owningSystemUrl: "http://gis.city.gov",
authInfo: {
tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
isTokenBasedSecurity: true
}
});

fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
token: "serverToken",
expires: TOMORROW
});

session
.getToken(
"https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
)
.then(token => {
expect(token).toBe("serverToken");
done();
});
});

it("should only make 1 token request to an untrusted portal for similar URLs", done => {
const session = new UserSession({
clientId: "id",
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-feature-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@esri/arcgis-rest-request": "^1.2.1"
},
"devDependencies": {
"@esri/arcgis-rest-common-types": "^1.4.1",
"@esri/arcgis-rest-common-types": "^1.4.2",
"@esri/arcgis-rest-request": "^1.4.2"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-geocoder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"devDependencies": {
"@esri/arcgis-rest-auth": "^1.4.2",
"@esri/arcgis-rest-common-types": "^1.4.1",
"@esri/arcgis-rest-common-types": "^1.4.2",
"@esri/arcgis-rest-request": "^1.4.2"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-groups/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"devDependencies": {
"@esri/arcgis-rest-auth": "^1.4.2",
"@esri/arcgis-rest-common-types": "^1.4.1",
"@esri/arcgis-rest-common-types": "^1.4.2",
"@esri/arcgis-rest-request": "^1.4.2"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-items/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"devDependencies": {
"@esri/arcgis-rest-auth": "^1.4.2",
"@esri/arcgis-rest-common-types": "^1.4.1",
"@esri/arcgis-rest-common-types": "^1.4.2",
"@esri/arcgis-rest-request": "^1.4.2"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-sharing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"devDependencies": {
"@esri/arcgis-rest-auth": "^1.4.2",
"@esri/arcgis-rest-common-types": "^1.4.1",
"@esri/arcgis-rest-common-types": "^1.4.2",
"@esri/arcgis-rest-request": "^1.4.2"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-users/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"devDependencies": {
"@esri/arcgis-rest-auth": "^1.4.2",
"@esri/arcgis-rest-common-types": "^1.4.1",
"@esri/arcgis-rest-common-types": "^1.4.2",
"@esri/arcgis-rest-request": "^1.4.2"
},
"scripts": {
Expand Down

0 comments on commit ddd3d57

Please sign in to comment.