Skip to content

Commit db543ab

Browse files
committed
if response contains error, then reject promise with custom SigninResponseError
1 parent 623f5e2 commit db543ab

9 files changed

Lines changed: 201 additions & 84 deletions

‎src/ResponseValidator.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default class ResponseValidator {
1616
this._metadataService = new MetadataServiceCtor(this._settings);
1717
this._userInfoService = new UserInfoServiceCtor(this._settings);
1818
}
19-
19+
2020
validateSigninResponse(state, response) {
2121
Log.info("ResponseValidator.validateSigninResponse");
2222

@@ -32,7 +32,7 @@ export default class ResponseValidator {
3232

3333
if (response.error) {
3434
Log.warn("Response was error", response.error);
35-
return Promise.resolve(response);
35+
return Promise.reject(response);
3636
}
3737

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

‎src/SigninResponse.js‎

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
import Log from './Log';
22
import UrlUtility from './UrlUtility';
3+
import SigninResponseError from './SigninResponseError';
34

45
export default class SigninResponse {
56
constructor(url) {
67

78
var values = UrlUtility.parseUrlFragment(url);
89

9-
this._state = values.state;
10-
1110
if (values.error){
12-
this._error = values.error;
13-
this._error_description = values.error_description;
14-
this._error_uri = values.error_uri;
11+
return new SigninResponseError(values);
1512
}
16-
else {
17-
this._id_token = values.id_token;
18-
this._session_state = values.session_state;
19-
this._access_token = values.access_token;
20-
this._token_type = values.token_type;
21-
this._scope = values.scope;
22-
23-
let expires_in = parseInt(values.expires_in);
24-
if (typeof expires_in === 'number' && expires_in > 0){
25-
let now = parseInt(Date.now() / 1000);
26-
this._expires_at = now + expires_in;
27-
}
13+
14+
this._state = values.state;
15+
this._id_token = values.id_token;
16+
this._session_state = values.session_state;
17+
this._access_token = values.access_token;
18+
this._token_type = values.token_type;
19+
this._scope = values.scope;
20+
21+
let expires_in = parseInt(values.expires_in);
22+
if (typeof expires_in === 'number' && expires_in > 0){
23+
let now = parseInt(Date.now() / 1000);
24+
this._expires_at = now + expires_in;
2825
}
2926
}
3027

@@ -35,16 +32,6 @@ export default class SigninResponse {
3532
this._state = value;
3633
}
3734

38-
get error(){
39-
return this._error;
40-
}
41-
get error_description(){
42-
return this._error_description;
43-
}
44-
get error_uri(){
45-
return this._error_uri;
46-
}
47-
4835
get id_token(){
4936
return this._id_token;
5037
}

‎src/SigninResponseError.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Log from './Log';
2+
import UrlUtility from './UrlUtility';
3+
4+
export default class SigninResponseError {
5+
constructor({error, error_description, error_uri, state}={}
6+
) {
7+
if (!error){
8+
Log.error("No error passed to SigninResponseError");
9+
throw new Error("error");
10+
}
11+
12+
this._error = error;
13+
this._error_description = error_description;
14+
this._error_uri = error_uri;
15+
this._state = state;
16+
17+
this._stack = (new Error()).stack;
18+
}
19+
20+
get message(){
21+
return this.error_description || this.error;
22+
}
23+
get name(){
24+
return "SigninResponseError";
25+
}
26+
get stack(){
27+
return this._stack;
28+
}
29+
30+
get error(){
31+
return this._error;
32+
}
33+
get error_description(){
34+
return this._error_description;
35+
}
36+
get error_uri(){
37+
return this._error_uri;
38+
}
39+
get state(){
40+
return this._state;
41+
}
42+
}

‎src/UrlUtility.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export default class UrlUtility {
2121
static parseUrlFragment(value, window = Window) {
2222
Log.info("UrlUtility.parseUrlFragment");
2323

24-
value = value || window.location.hash;
24+
if (typeof value !== 'string'){
25+
value = window.location.hash;
26+
}
2527

2628
var idx = value.lastIndexOf("#");
2729
if (idx >= 0) {
@@ -46,5 +48,7 @@ export default class UrlUtility {
4648
for (var prop in params) {
4749
return params;
4850
}
51+
52+
return {};
4953
}
5054
}

‎test/OidcClientService.spec.js‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import OidcClientService from '../src/OidcClientService';
22
import SigninRequest from '../src/SigninRequest';
33
import SigninResponse from '../src/SigninResponse';
4+
import SigninResponseError from '../src/SigninResponseError';
45
import SignoutRequest from '../src/SignoutRequest';
56
import Log from '../src/Log';
67

@@ -129,43 +130,34 @@ describe("OidcClientService", function() {
129130
describe("processSigninResponse", function() {
130131

131132
it("should return a promise", function() {
132-
subject.processSigninResponse("state=state&error=error").should.be.instanceof(Promise);
133+
subject.processSigninResponse("state=state").should.be.instanceof(Promise);
133134
});
134135

135136
it("should fail if no state on response", function(done) {
136137
stubStore.item = "state";
137-
subject.processSigninResponse("error=error").then(null, err => {
138+
subject.processSigninResponse("").then(null, err => {
138139
err.message.should.contain('state');
139140
done();
140141
});
141142
});
142143

143144
it("should fail if storage fails", function(done) {
144145
stubStore.error = "fail";
145-
subject.processSigninResponse("state=state&error=error").then(null, err => {
146+
subject.processSigninResponse("state=state").then(null, err => {
146147
err.message.should.contain('response');
147148
done();
148149
});
149150
});
150151

151152
it("should call validator", function(done) {
152153
stubStore.item = "state";
153-
subject.processSigninResponse("state=state&error=error").then(response => {
154+
subject.processSigninResponse("state=state").then(response => {
154155
stubValidator.state.should.equal('state');
155156
stubValidator.response.should.be.deep.equal(response);
156157
done();
157158
});
158159
});
159160

160-
it("should return SigninResponse", function(done) {
161-
stubStore.item = "state";
162-
163-
subject.processSigninResponse("state=state&error=error").then(response => {
164-
response.should.be.instanceof(SigninResponse);
165-
done();
166-
});
167-
});
168-
169161
});
170162

171163
describe("createSignoutRequest", function() {

‎test/ResponseValidator.spec.js‎

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@ import chai from 'chai';
77
chai.should();
88
let assert = chai.assert;
99

10-
describe("OidcClientService", function() {
10+
describe("ResponseValidator", function() {
1111
let settings;
1212
let subject;
1313
let stubMetadataService;
1414
let stubUserInfoService;
15+
16+
let state;
17+
let response;
1518

1619
beforeEach(function() {
1720
Log.setLogger(console);
1821
Log.level = Log.NONE;
22+
23+
state = {
24+
id:"state"
25+
};
26+
response = {
27+
state:'state'
28+
};
1929

2030
settings = {};
2131
stubMetadataService = new StubMetadataService();
@@ -24,17 +34,41 @@ describe("OidcClientService", function() {
2434
});
2535

2636
describe("constructor", function() {
27-
// it("should require a settings param", function() {
28-
// try {
29-
// new OidcClientService(undefined, stubStore, stubMetadataService);
30-
// }
31-
// catch (e) {
32-
// e.message.should.contain('settings');
33-
// return;
34-
// }
35-
// assert.fail();
36-
// });
3737

38+
it("should require a settings param", function() {
39+
try {
40+
new ResponseValidator(undefined, () => stubMetadataService, () => stubUserInfoService);
41+
}
42+
catch (e) {
43+
e.message.should.contain('settings');
44+
return;
45+
}
46+
assert.fail();
47+
});
48+
49+
});
50+
51+
describe("validateSigninResponse", function(done) {
52+
53+
it("should validate that the client state matches response state", function() {
54+
55+
subject.validateSigninResponse({ id: 1 }, { state: 2 }).then(null, err => {
56+
err.message.should.contain('match');
57+
done();
58+
});
59+
60+
});
61+
62+
it("should return error response", function(done) {
63+
64+
response.error = "test";
65+
subject.validateSigninResponse(state, response).then(null, err=>{
66+
response.error.should.equal("test");
67+
done();
68+
});
69+
70+
});
71+
3872
});
3973
});
4074

@@ -62,8 +96,8 @@ describe("OidcClientService", function() {
6296
// });
6397

6498

65-
class StubUserInfoService{
66-
getClaims(){
99+
class StubUserInfoService {
100+
getClaims() {
67101
return this.getClaimsResult;
68102
}
69103
}

‎test/SigninResponse.spec.js‎

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Log from '../src/Log';
22
import SigninResponse from '../src/SigninResponse';
3+
import SigninResponseError from '../src/SigninResponseError';
34

45
import chai from 'chai';
56
chai.should();
@@ -14,38 +15,16 @@ describe("SigninResponse", function() {
1415
subject.state.should.equal("foo");
1516
});
1617

17-
it("should read error", function() {
18-
let subject = new SigninResponse("error=foo");
19-
subject.error.should.equal("foo");
20-
});
21-
22-
it("should read error_description", function() {
23-
let subject = new SigninResponse("error=error&error_description=foo");
24-
subject.error_description.should.equal("foo");
18+
it("should return SigninResponse if no error", function() {
19+
let subject = new SigninResponse("");
20+
subject.should.be.instanceof(SigninResponse);
2521
});
2622

27-
it("should read error_uri", function() {
28-
let subject = new SigninResponse("error=error&error_uri=foo");
29-
subject.error_uri.should.equal("foo");
23+
it("should return SigninResponseError if error", function() {
24+
let subject = new SigninResponse("error=foo");
25+
subject.should.be.instanceof(SigninResponseError);
3026
});
3127

32-
it("should only read error info if error", function() {
33-
let subject = new SigninResponse("error_description=foo");
34-
expect(subject.error_description).to.be.undefined;
35-
36-
subject = new SigninResponse("error_uri=foo");
37-
expect(subject.error_uri).to.be.undefined;
38-
39-
subject = new SigninResponse("error=error&id_token=foo&session_state=foo&access_token=foo&token_type=foo&scope=foo&expires_in=10");
40-
expect(subject.id_token).to.be.undefined;
41-
expect(subject.session_state).to.be.undefined;
42-
expect(subject.access_token).to.be.undefined;
43-
expect(subject.token_type).to.be.undefined;
44-
expect(subject.scope).to.be.undefined;
45-
expect(subject.expires_in).to.be.undefined;
46-
expect(subject.expires_at).to.be.undefined;
47-
});
48-
4928
it("should read id_token", function() {
5029
let subject = new SigninResponse("id_token=foo");
5130
subject.id_token.should.equal("foo");

0 commit comments

Comments
 (0)