Skip to content

Commit 69307b4

Browse files
committed
change how error responses are handled
1 parent b4a85d0 commit 69307b4

7 files changed

Lines changed: 126 additions & 84 deletions

File tree

‎src/OidcClient.js‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import Log from './Log';
55
import OidcClientSettings from './OidcClientSettings';
66
import MetadataService from './MetadataService';
7+
import ErrorResponse from './ErrorResponse';
78
import SigninRequest from './SigninRequest';
89
import SigninResponse from './SigninResponse';
910
import SignoutRequest from './SignoutRequest';
@@ -44,8 +45,6 @@ export default class OidcClient {
4445
) {
4546
Log.info("OidcClient.createSigninRequest");
4647

47-
stateStore = stateStore || this._stateStore;
48-
4948
let client_id = this._settings.client_id;
5049
response_type = response_type || this._settings.response_type;
5150
scope = scope || this._settings.scope;
@@ -72,6 +71,7 @@ export default class OidcClient {
7271
});
7372

7473
var state = request.state;
74+
stateStore = stateStore || this._stateStore;
7575

7676
return stateStore.set(state.id, state.toStorageString()).then(() => {
7777
return request;
@@ -82,17 +82,16 @@ export default class OidcClient {
8282
processSigninResponse(url, stateStore) {
8383
Log.info("OidcClient.processSigninResponse");
8484

85-
stateStore = stateStore || this._stateStore;
86-
8785
var response = new SigninResponse(url);
86+
8887
if (!response.state) {
8988
Log.error("No state in response");
9089
return Promise.reject(new Error("No state in response"));
9190
}
9291

93-
var stateKey = response.state;
94-
95-
return stateStore.remove(stateKey).then(storedStateString => {
92+
stateStore = stateStore || this._stateStore;
93+
94+
return stateStore.remove(response.state).then(storedStateString => {
9695
if (!storedStateString) {
9796
Log.error("No matching state found in storage");
9897
throw new Error("No matching state found in storage");
@@ -110,8 +109,6 @@ export default class OidcClient {
110109
) {
111110
Log.info("OidcClient.createSignoutRequest");
112111

113-
stateStore = stateStore || this._stateStore;
114-
115112
post_logout_redirect_uri = post_logout_redirect_uri || this._settings.post_logout_redirect_uri;
116113

117114
return this._metadataService.getEndSessionEndpoint().then(url => {
@@ -127,6 +124,8 @@ export default class OidcClient {
127124
var state = request.state;
128125
if (state) {
129126
Log.info("Signout request has state to persist");
127+
128+
stateStore = stateStore || this._stateStore;
130129
stateStore.set(state.id, state.toStorageString());
131130
}
132131

@@ -137,16 +136,22 @@ export default class OidcClient {
137136
processSignoutResponse(url, stateStore) {
138137
Log.info("OidcClient.processSignoutResponse");
139138

140-
stateStore = stateStore || this._stateStore;
141-
142139
var response = new SignoutResponse(url);
143140
if (!response.state) {
144-
Log.error("No state in response");
145-
return Promise.reject(new Error("No state in response"));
141+
Log.info("No state in response");
142+
143+
if (response.error) {
144+
Log.warn("Response was error", response.error);
145+
return Promise.reject(new ErrorResponse(response));
146+
}
147+
148+
return Promise.resolve(response);
146149
}
147150

148151
var stateKey = response.state;
149152

153+
stateStore = stateStore || this._stateStore;
154+
150155
return stateStore.remove(stateKey).then(storedStateString => {
151156
if (!storedStateString) {
152157
Log.error("No matching state found in storage");

‎src/ResponseValidator.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import Log from './Log';
55
import MetadataService from './MetadataService';
66
import UserInfoService from './UserInfoService';
7+
import ErrorResponse from './ErrorResponse';
78
import JwtUtil from './JwtUtil';
89

910
const ProtocolClaims = ["nonce", "at_hash", "iat", "nbf", "exp", "aud", "iss", "c_hash"];
@@ -53,9 +54,9 @@ export default class ResponseValidator {
5354

5455
if (response.error) {
5556
Log.warn("Response was error", response.error);
56-
return Promise.reject(response);
57+
return Promise.reject(new ErrorResponse(response));
5758
}
58-
59+
5960
return Promise.resolve(response);
6061
}
6162

@@ -75,7 +76,7 @@ export default class ResponseValidator {
7576

7677
if (response.error) {
7778
Log.warn("Response was error", response.error);
78-
return Promise.reject(response);
79+
return Promise.reject(new ErrorResponse(response));
7980
}
8081

8182
if (state.nonce && !response.id_token) {

‎src/SigninResponse.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import Log from './Log';
54
import UrlUtility from './UrlUtility';
6-
import ErrorResponse from './ErrorResponse';
75

86
const OidcScope = "openid";
97

@@ -12,10 +10,10 @@ export default class SigninResponse {
1210

1311
var values = UrlUtility.parseUrlFragment(url, "#");
1412

15-
if (values.error) {
16-
return new ErrorResponse(values);
17-
}
18-
13+
this.error = values.error;
14+
this.error_description = values.error_description;
15+
this.error_uri = values.error_uri;
16+
1917
this.state = values.state;
2018
this.id_token = values.id_token;
2119
this.session_state = values.session_state;

‎src/SignoutResponse.js‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import Log from './Log';
54
import UrlUtility from './UrlUtility';
6-
import ErrorResponse from './ErrorResponse';
75

86
export default class SignoutResponse {
97
constructor(url) {
10-
8+
119
var values = UrlUtility.parseUrlFragment(url, "?");
12-
13-
if (values.error){
14-
return new ErrorResponse(values);
15-
}
16-
10+
11+
this.error = values.error;
12+
this.error_description = values.error_description;
13+
this.error_uri = values.error_uri;
14+
1715
this.state = values.state;
1816
}
1917
}

‎test/unit/OidcClient.spec.js‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,26 @@ describe("OidcClient", function () {
268268
it("should store state", function (done) {
269269
stubMetadataService.getEndSessionEndpointResult = Promise.resolve("http://sts/signout");
270270

271-
var p = subject.createSignoutRequest();
271+
var p = subject.createSignoutRequest({
272+
data:"foo"
273+
});
272274

273275
p.then(request => {
274276
stubStore.item.should.be.ok;
275277
done();
276278
});
277279
});
278280

281+
it("should not generate state if no data", function (done) {
282+
stubMetadataService.getEndSessionEndpointResult = Promise.resolve("http://sts/signout");
283+
284+
var p = subject.createSignoutRequest();
285+
286+
p.then(request => {
287+
assert.isUndefined(stubStore.item);
288+
done();
289+
});
290+
});
279291
});
280292

281293
describe("processSignoutResponse", function () {
@@ -284,10 +296,16 @@ describe("OidcClient", function () {
284296
subject.processSignoutResponse("state=state").should.be.instanceof(Promise);
285297
});
286298

287-
it("should fail if no state on response", function (done) {
288-
stubStore.item = "state";
289-
subject.processSignoutResponse("").then(null, err => {
290-
err.message.should.contain('state');
299+
it("should return result if no state on response", function (done) {
300+
subject.processSignoutResponse("").then(response => {
301+
response.should.be.ok;
302+
done();
303+
});
304+
});
305+
306+
it("should return error", function (done) {
307+
subject.processSignoutResponse("error=foo").then(null, err => {
308+
err.error.should.equal("foo");
291309
done();
292310
});
293311
});
@@ -310,6 +328,17 @@ describe("OidcClient", function () {
310328
done();
311329
});
312330
});
331+
332+
it("should call validator with state even if error in response", function (done) {
333+
334+
stubStore.item = new State({ id: '1', data:"bar" }).toStorageString();
335+
336+
subject.processSignoutResponse("state=1&error=foo").then(response => {
337+
stubValidator.signoutState.id.should.equal('1');
338+
stubValidator.signoutResponse.should.be.deep.equal(response);
339+
done();
340+
});
341+
});
313342

314343
});
315344

0 commit comments

Comments
 (0)