Skip to content

Commit e6a819d

Browse files
committed
add signin request code
1 parent eb73219 commit e6a819d

10 files changed

Lines changed: 445 additions & 112 deletions

‎src/OidcClientService.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,27 @@ export default class OidcClientService {
1919
return this._settings;
2020
}
2121

22-
createSigninRequest(request) {
22+
createSigninRequest({response_type, scope, redirect_uri, state}={}) {
2323
Log.info("createSigninRequest");
24+
25+
let client_id = this._settings.client_id;
26+
response_type = response_type || this._settings.response_type;
27+
scope = scope || this._settings.scope;
28+
redirect_uri = redirect_uri || this._settings.redirect_uri;
29+
30+
return this._metadataService.getAuthorizationEndpoint().then(url => {
31+
Log.info("Received authorization endpoint", url);
32+
33+
return new SigninRequest(url,
34+
client_id,
35+
redirect_uri,
36+
response_type,
37+
scope,
38+
state);
39+
}, err => {
40+
Log.error("Failed to create signin request", err);
41+
throw new Error("Failed to create signin request");
42+
});
2443
}
2544

2645
processSigninResponse(){
@@ -45,6 +64,10 @@ export default class OidcClientService {
4564
});
4665
}
4766

67+
processSignoutResponse(){
68+
Log.info("processSignoutResponse");
69+
}
70+
4871
// OidcClient.prototype.createTokenRequestAsync = function () {
4972
// log("OidcClient.createTokenRequestAsync");
5073

‎src/OidcClientSettings.js‎

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,96 @@
1+
import Log from './Log';
2+
13
const OidcMetadataUrlPath = '.well-known/openid-configuration';
4+
const DefaultResponseType = "id_token";
5+
const DefaultScope = "openid";
26

37
export default class OidcClientSettings {
48
constructor(settings) {
59
if (!settings) {
610
Log.error("No settings passed to OidcClientSettings");
711
throw new Error("settings");
812
}
13+
if (!settings.client_id) {
14+
Log.error("No client_id on settings passed to OidcClientSettings");
15+
throw new Error("client_id");
16+
}
917

18+
// metadata related
1019
this._authority = settings.authority;
11-
this._metadataUrl = settings.metadataUrl;
20+
this._metadataUrl = settings.metadataUrl;
1221
this._metadata = settings.metadata;
1322
this._signingKeys = settings.signingKeys;
14-
23+
24+
// client related
25+
this._client_id = settings.client_id;
26+
this._response_type = settings.response_type || DefaultResponseType;
27+
this._scope = settings.scope || DefaultScope;
28+
this._redirect_uri = settings.redirect_uri;
1529
this._post_logout_redirect_uri = settings.post_logout_redirect_uri;
16-
17-
if (settings.filterProtocolClaims === undefined){
30+
31+
// behavior flags
32+
if (settings.filterProtocolClaims === undefined) {
1833
this._filterProtocolClaims = true;
1934
}
2035
else {
2136
this._filterProtocolClaims = !!settings.filterProtocolClaims;
2237
}
2338
}
39+
40+
// config values
41+
// client
42+
get client_id() {
43+
return this._client_id;
44+
}
45+
get response_type() {
46+
return this._response_type;
47+
}
48+
get scope() {
49+
return this._scope;
50+
}
51+
get redirect_uri() {
52+
return this._redirect_uri;
53+
}
54+
get post_logout_redirect_uri() {
55+
return this._post_logout_redirect_uri;
56+
}
2457

58+
// metadata
2559
get authority() {
2660
return this._authority;
2761
}
28-
2962
get metadataUrl() {
3063
if (!this._metadataUrl) {
3164
this._metadataUrl = this.authority;
32-
65+
3366
if (this._metadataUrl && this._metadataUrl.indexOf(OidcMetadataUrlPath) < 0) {
3467
if (this._metadataUrl[this._metadataUrl.length - 1] !== '/') {
3568
this._metadataUrl += '/';
3669
}
3770
this._metadataUrl += OidcMetadataUrlPath;
3871
}
3972
}
40-
73+
4174
return this._metadataUrl;
4275
}
4376

77+
// behavior
78+
get filterProtocolClaims() {
79+
return this._filterProtocolClaims;
80+
}
81+
82+
// settable/cachable values
4483
get metadata() {
4584
return this._metadata;
4685
}
47-
set metadata(value){
86+
set metadata(value) {
4887
this._metadata = value;
4988
}
50-
89+
5190
get signingKeys() {
5291
return this._signingKeys;
5392
}
54-
set signingKeys(value){
93+
set signingKeys(value) {
5594
this._signingKeys = value;
5695
}
57-
58-
get filterProtocolClaims(){
59-
return this._filterProtocolClaims;
60-
}
61-
get post_logout_redirect_uri(){
62-
return this._post_logout_redirect_uri;
63-
}
6496
}

‎src/SigninRequest.js‎

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
1+
import Log from './Log';
2+
import UrlUtility from './UrlUtility';
3+
import State from './State';
4+
15
export default class SigninRequest {
2-
constructor(settings){
3-
this._settings = settings;
6+
constructor(url, client_id, redirect_uri, response_type, scope, state, StateCtor = State) {
7+
8+
if (!url){
9+
Log.error("No url passed to SigninRequest");
10+
throw new Error("url");
11+
}
12+
if (!client_id){
13+
Log.error("No client_id passed to SigninRequest");
14+
throw new Error("client_id");
15+
}
16+
if (!redirect_uri){
17+
Log.error("No redirect_uri passed to SigninRequest");
18+
throw new Error("redirect_uri");
19+
}
20+
if (!response_type){
21+
Log.error("No response_type passed to SigninRequest");
22+
throw new Error("response_type");
23+
}
24+
if (!scope){
25+
Log.error("No scope passed to SigninRequest");
26+
throw new Error("scope");
27+
}
28+
29+
this._url = url;
30+
this._client_id = client_id;
31+
this._redirect_uri = redirect_uri;
32+
this._response_type = response_type;
33+
this._scope = scope;
34+
this._state = new StateCtor(state);
435
}
536

6-
// get isOidc() {
7-
// if (this._settings.response_type) {
8-
// var result = this._settings.response_type.split(/\s+/g).filter(function(item) {
9-
// return item === "id_token";
10-
// });
11-
// return !!(result[0]);
12-
// }
13-
// return false;
14-
// }
15-
16-
// get isOAuth() {
17-
// if (this._settings.response_type) {
18-
// var result = this._settings.response_type.split(/\s+/g).filter(function(item) {
19-
// return item === "token";
20-
// });
21-
// return !!(result[0]);
22-
// }
23-
// return false;
24-
// }
37+
get signinUrl(){
38+
if (!this._signinUrl){
39+
this._signinUrl = this._url;
40+
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "state", this._state.toUriString());
41+
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "client_id", this._client_id);
42+
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "redirect_uri", this._redirect_uri);
43+
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "response_type", this._response_type);
44+
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "scope", this._scope);
45+
}
46+
47+
return this._signinUrl;
48+
}
49+
50+
get state(){
51+
return this._state.toClientStorageString();
52+
}
2553
}

‎src/SignoutRequest.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Log from './Log';
12
import UrlUtility from './UrlUtility';
23
import State from './State';
34

‎src/State.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export default class State {
1010

1111
this._crypto = crypto;
1212
}
13+
14+
get id() {
15+
return this._data.nonce;
16+
}
1317

1418
toUriString(){
1519
return encodeURIComponent(JSON.stringify(this._data));

0 commit comments

Comments
 (0)