Skip to content

Commit 76c29b0

Browse files
committed
add state class
1 parent 8f34081 commit 76c29b0

13 files changed

Lines changed: 320 additions & 3 deletions

‎src/Crypto.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default class Crypto{
2+
static hash(value){
3+
return value;
4+
}
5+
}

‎src/OidcClientService.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,22 @@ export default class OidcClientService {
2727
Log.info("processSigninResponse");
2828
}
2929

30-
createSignoutRequest(request){
30+
createSignoutRequest({id_token_hint, state, post_logout_redirect_uri}){
3131
Log.info("createSignoutRequest");
3232

33+
post_logout_redirect_uri = post_logout_redirect_uri || this._settings.post_logout_redirect_uri;
3334

35+
return this._metadataService.getEndSessionEndpoint(url => {
36+
Log.info("Received end session endpoint", url);
37+
38+
return new SignoutRequest(url, {
39+
id_token_hint,
40+
post_logout_redirect_uri,
41+
state});
42+
}, err => {
43+
Log.error("Failed to create signout request", err);
44+
throw new Error("Failed to create signout request");
45+
});
3446
}
3547

3648
// OidcClient.prototype.createTokenRequestAsync = function () {

‎src/OidcClientSettings.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export default class OidcClientSettings {
1212
this._metadata = settings.metadata;
1313
this._signingKeys = settings.signingKeys;
1414

15+
this._post_logout_redirect_uri = settings.post_logout_redirect_uri;
16+
1517
if (settings.filterProtocolClaims === undefined){
1618
this._filterProtocolClaims = true;
1719
}
@@ -56,4 +58,7 @@ export default class OidcClientSettings {
5658
get filterProtocolClaims(){
5759
return this._filterProtocolClaims;
5860
}
61+
get post_logout_redirect_uri(){
62+
return this._post_logout_redirect_uri;
63+
}
5964
}

‎src/SignoutRequest.js‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
1+
import UrlUtility from './UrlUtility';
2+
import State from './State';
3+
14
export default class SignoutRequest {
2-
}
5+
constructor(url, {id_token_hint, post_logout_redirect_uri, state}={}, StateCtor = State){
6+
if (!url){
7+
Log.error("No url passed to SignoutRequest");
8+
throw new Error("url");
9+
}
10+
11+
this._url = url;
12+
this._id_token_hint = id_token_hint;
13+
this._post_logout_redirect_uri = post_logout_redirect_uri;
14+
15+
this._state = new StateCtor(state);
16+
}
17+
18+
get signoutUrl(){
19+
if (!this._signoutUrl){
20+
this._signoutUrl = this._url;
21+
this._signoutUrl = UrlUtility.addQueryParam(this._signoutUrl, "state", this._state.toUriString());
22+
23+
if (this._id_token_hint){
24+
this._signoutUrl = UrlUtility.addQueryParam(this._signoutUrl, "id_token_hint", this._id_token_hint);
25+
}
26+
27+
if (this._post_logout_redirect_uri){
28+
this._signoutUrl = UrlUtility.addQueryParam(this._signoutUrl, "post_logout_redirect_uri", this._post_logout_redirect_uri);
29+
}
30+
}
31+
32+
return this._signoutUrl;
33+
}
34+
35+
get state(){
36+
return this._state;
37+
}
38+
}

‎src/State.js‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Log from './Log';
2+
import random from './random';
3+
import Crypto from './Crypto';
4+
5+
export default class State {
6+
constructor(state, crypto = Crypto){
7+
8+
this._data = {nonce:random()};
9+
this._data.state = state;
10+
11+
this._crypto = crypto;
12+
}
13+
14+
toUriString(){
15+
return encodeURIComponent(JSON.stringify(this._data));
16+
}
17+
18+
toClientStorageString(){
19+
return JSON.stringify({
20+
hash: this._crypto.hash(this.toUriString()),
21+
nonce: this._data.nonce
22+
});
23+
}
24+
25+
static verify(clientStorageString, stateString, crypto = Crypto){
26+
Log.info("State.verify");
27+
28+
var clientStorage = JSON.parse(clientStorageString);
29+
30+
if (clientStorage.hash === crypto.hash(stateString)){
31+
Log.info("hash comparison successful");
32+
33+
var json = decodeURIComponent(stateString);
34+
var data = JSON.parse(json);
35+
36+
if (data.nonce === clientStorage.nonce) {
37+
Log.info("nonce comparison successful");
38+
39+
return data.state || true;
40+
}
41+
else{
42+
Log.warn("nonce comparison failed");
43+
}
44+
}
45+
else {
46+
Log.warn("hash comparison failed");
47+
}
48+
49+
return false;
50+
}
51+
}

‎src/UrlUtility.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default class UrlUtility {
2+
static addQueryParam(url, name, value) {
3+
if (url.indexOf('?') < 0){
4+
url += "?";
5+
}
6+
7+
if (url[url.length - 1] !== "?"){
8+
url += "&";
9+
}
10+
11+
url += encodeURIComponent(name);
12+
url += "=";
13+
url += encodeURIComponent(value);
14+
15+
return url;
16+
}
17+
}

‎src/UserInfoService.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import JsonService from './JsonService';
22
import MetadataService from './MetadataService';
33
import Log from './Log';
44

5-
const ProtocolClaims = ["nonce", "at_hash", "iat", "nbf", "exp", "aud", "iss"];
5+
const ProtocolClaims = ["nonce", "at_hash", "iat", "nbf", "exp", "aud", "iss", "c_hash"];
66

77
export default class UserInfoService {
88
constructor(settings, JsonServiceCtor = JsonService, MetadataServiceCtor = MetadataService) {

‎src/random.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// NOTICE: the code in this file originally developed by Microsoft
2+
// source: https://github.com/AzureAD/azure-activedirectory-library-for-js/blob/master/lib/adal.js#L1029
3+
// original license: https://github.com/AzureAD/azure-activedirectory-library-for-js/blob/master/LICENSE.txt
4+
export default function random() {
5+
var guidHolder = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx';
6+
var hex = '0123456789abcdef';
7+
var r = 0;
8+
var guidResponse = "";
9+
for (var i = 0; i < guidHolder.length; i++) {
10+
if (guidHolder[i] !== '-' && guidHolder[i] !== '4') {
11+
// each x and y needs to be random
12+
r = Math.random() * 16 | 0;
13+
}
14+
15+
if (guidHolder[i] === 'x') {
16+
guidResponse += hex[r];
17+
} else if (guidHolder[i] === 'y') {
18+
// clock-seq-and-reserved first hex is filtered and remaining hex values are random
19+
r &= 0x3; // bit and with 0011 to set pos 2 to zero ?0??
20+
r |= 0x8; // set pos 3 to 1 as 1???
21+
guidResponse += hex[r];
22+
} else {
23+
guidResponse += guidHolder[i];
24+
}
25+
}
26+
return guidResponse;
27+
}

‎test/OidcClientSettings.spec.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,13 @@ describe("OidcClientSettings", function() {
105105
subject.filterProtocolClaims.should.equal(false);
106106
});
107107
});
108+
109+
describe("post_logout_redirect_uri", function() {
110+
it("should return value from initial settings", function(){
111+
let subject = new OidcClientSettings({
112+
post_logout_redirect_uri:"http://app/loggedout"
113+
});
114+
subject.post_logout_redirect_uri.should.equal("http://app/loggedout");
115+
});
116+
});
108117
});

‎test/SignoutRequest.spec.js‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Log from '../src/Log';
2+
import SignoutRequest from '../src/SignoutRequest';
3+
4+
import StubState from './StubState';
5+
6+
import chai from 'chai';
7+
chai.should();
8+
let assert = chai.assert;
9+
10+
describe("SignoutRequest", function() {
11+
12+
let stubState = new StubState("state");
13+
let StubStateCtor = () => stubState;
14+
15+
16+
describe("constructor", function() {
17+
18+
it("should require a url param", function() {
19+
try {
20+
new SignoutRequest();
21+
}
22+
catch (e) {
23+
return;
24+
}
25+
assert.fail();
26+
});
27+
28+
});
29+
30+
describe("signoutUrl", function() {
31+
32+
it("should include url", function() {
33+
let subject = new SignoutRequest("http://sts/signout");
34+
subject.signoutUrl.indexOf("http://sts/signout").should.equal(0);
35+
});
36+
37+
it("should include state", function() {
38+
let subject = new SignoutRequest("http://sts/signout", {state:"test"}, StubStateCtor);
39+
subject.signoutUrl.should.contain("state=state");
40+
});
41+
42+
it("should include id_token_hint", function() {
43+
let subject = new SignoutRequest("http://sts/signout", {id_token_hint:"test"});
44+
subject.signoutUrl.should.contain("id_token_hint=test");
45+
});
46+
47+
it("should include post_logout_redirect_uri", function() {
48+
let subject = new SignoutRequest("http://sts/signout", {post_logout_redirect_uri:"test"});
49+
subject.signoutUrl.should.contain("post_logout_redirect_uri=test");
50+
});
51+
52+
it("should include id_token_hint, post_logout_redirect_uri, and state", function() {
53+
let subject = new SignoutRequest("http://sts/signout", {
54+
id_token_hint : 'token',
55+
post_logout_redirect_uri:"uri",
56+
state:"xoxo"
57+
}, StubStateCtor);
58+
59+
var url = subject.signoutUrl;
60+
url.indexOf('http://sts/signout?').should.equal(0);
61+
url.should.contain("id_token_hint=token");
62+
url.should.contain("post_logout_redirect_uri=uri");
63+
url.should.contain("state=state");
64+
});
65+
66+
});
67+
68+
});

0 commit comments

Comments
 (0)