Skip to content

Commit 33f03c5

Browse files
committed
rework properties on request/response objects
1 parent cfd9cb9 commit 33f03c5

8 files changed

Lines changed: 85 additions & 205 deletions

‎src/ErrorResponse.js‎

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,14 @@ export default class ErrorResponse {
99
throw new Error("error");
1010
}
1111

12-
this._error = error;
13-
this._error_description = error_description;
14-
this._error_uri = error_uri;
15-
this._state = state;
12+
this.message = error_description || error;
13+
this.name = "ErrorResponse";
14+
this.stack = (new Error()).stack;
1615

17-
this._stack = (new Error()).stack;
18-
}
19-
20-
get message(){
21-
return this.error_description || this.error;
22-
}
23-
get name(){
24-
return "ErrorResponse";
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;
16+
this.error = error;
17+
this.error_description = error_description;
18+
this.error_uri = error_uri;
19+
20+
this.state = state;
4121
}
4222
}

‎src/SigninRequest.js‎

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,66 @@ import Log from './Log';
22
import UrlUtility from './UrlUtility';
33
import State from './State';
44

5-
const OidcOptionalRequestParams = ["prompt", "display", "max_age", "ui_locales", "id_token_hint", "login_hint", "acr_values"];
6-
75
export default class SigninRequest {
86
constructor({
7+
// mandatory
98
url, client_id, redirect_uri, response_type, scope, data,
9+
// optional
1010
prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values
1111
}) {
12-
13-
if (!url){
12+
if (!url) {
1413
Log.error("No url passed to SigninRequest");
1514
throw new Error("url");
1615
}
17-
if (!client_id){
16+
if (!client_id) {
1817
Log.error("No client_id passed to SigninRequest");
1918
throw new Error("client_id");
2019
}
21-
if (!redirect_uri){
20+
if (!redirect_uri) {
2221
Log.error("No redirect_uri passed to SigninRequest");
2322
throw new Error("redirect_uri");
2423
}
25-
if (!response_type){
24+
if (!response_type) {
2625
Log.error("No response_type passed to SigninRequest");
2726
throw new Error("response_type");
2827
}
29-
if (!scope){
28+
if (!scope) {
3029
Log.error("No scope passed to SigninRequest");
3130
throw new Error("scope");
3231
}
32+
33+
let oidc = SigninRequest.isOidc(response_type);
34+
this.state = new State({ nonce: oidc, data });
35+
36+
url = UrlUtility.addQueryParam(url, "client_id", client_id);
37+
url = UrlUtility.addQueryParam(url, "redirect_uri", redirect_uri);
38+
url = UrlUtility.addQueryParam(url, "response_type", response_type);
39+
url = UrlUtility.addQueryParam(url, "scope", scope);
3340

34-
this._url = url;
35-
this._client_id = client_id;
36-
this._redirect_uri = redirect_uri;
37-
this._response_type = response_type;
38-
this._scope = scope;
39-
this._state = new State({nonce:this.isOidc, data});
40-
41-
// optional
42-
this._prompt = prompt;
43-
this._display = display;
44-
this._max_age = max_age;
45-
this._ui_locales = ui_locales;
46-
this._id_token_hint = id_token_hint;
47-
this._login_hint = login_hint;
48-
this._acr_values = acr_values;
49-
}
50-
51-
get state(){
52-
return this._state;
53-
}
54-
55-
get signinUrl(){
56-
if (!this._signinUrl){
57-
this._signinUrl = this._url;
58-
59-
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "client_id", this._client_id);
60-
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "redirect_uri", this._redirect_uri);
61-
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "response_type", this._response_type);
62-
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "scope", this._scope);
63-
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "state", this._state.id);
64-
65-
if (this.isOidc){
66-
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, "nonce", this._state.nonce);
41+
url = UrlUtility.addQueryParam(url, "state", this.state.id);
42+
if (oidc) {
43+
url = UrlUtility.addQueryParam(url, "nonce", this.state.nonce);
44+
}
45+
46+
var optional = { prompt, display, max_age, ui_locales, id_token_hint, login_hint, acr_values };
47+
for(let key in optional){
48+
if (optional[key]) {
49+
url = UrlUtility.addQueryParam(url, key, optional[key]);
6750
}
68-
69-
OidcOptionalRequestParams.forEach(key => {
70-
if (this["_" + key]){
71-
this._signinUrl = UrlUtility.addQueryParam(this._signinUrl, key, this["_" + key]);
72-
}
73-
});
7451
}
75-
76-
return this._signinUrl;
52+
53+
this.url = url;
7754
}
7855

79-
get isOidc() {
80-
var result = this._response_type.split(/\s+/g).filter(function(item) {
56+
static isOidc(response_type) {
57+
var result = response_type.split(/\s+/g).filter(function(item) {
8158
return item === "id_token";
8259
});
8360
return !!(result[0]);
8461
}
85-
86-
get isOAuth() {
87-
var result = this._response_type.split(/\s+/g).filter(function(item) {
62+
63+
static isOAuth(response_type) {
64+
var result = response_type.split(/\s+/g).filter(function(item) {
8865
return item === "token";
8966
});
9067
return !!(result[0]);

‎src/SigninResponse.js‎

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,25 @@ export default class SigninResponse {
1111
return new ErrorResponse(values);
1212
}
1313

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;
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;
2020

2121
let expires_in = parseInt(values.expires_in);
2222
if (typeof expires_in === 'number' && expires_in > 0){
2323
let now = parseInt(Date.now() / 1000);
24-
this._expires_at = now + expires_in;
24+
this.expires_at = now + expires_in;
2525
}
2626
}
2727

28-
get state(){
29-
return this._state;
30-
}
31-
set state(value){
32-
this._state = value;
33-
}
34-
35-
get id_token(){
36-
return this._id_token;
37-
}
38-
get session_state(){
39-
return this._session_state;
40-
}
41-
get access_token(){
42-
return this._access_token;
43-
}
44-
get token_type(){
45-
return this._token_type;
46-
}
47-
get scope(){
48-
return this._scope;
49-
}
5028
get expires_in(){
5129
if (this.expires_at){
5230
let now = parseInt(Date.now() / 1000);
5331
return this.expires_at - now;
5432
}
5533
return undefined;
5634
}
57-
get expires_at(){
58-
return this._expires_at;
59-
}
60-
61-
get profile(){
62-
return this._profile;
63-
}
64-
set profile(value){
65-
this._profile = value;
66-
}
6735
}

‎src/SignoutRequest.js‎

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,15 @@ export default class SignoutRequest {
99
throw new Error("url");
1010
}
1111

12-
this._url = url;
13-
this._id_token_hint = id_token_hint;
14-
this._post_logout_redirect_uri = post_logout_redirect_uri;
12+
this.state = new State({ data });
1513

16-
this._state = new State({data});
17-
}
18-
19-
get state(){
20-
return this._state;
21-
}
22-
23-
get signoutUrl() {
24-
if (!this._signoutUrl) {
25-
this._signoutUrl = this._url;
26-
this._signoutUrl = UrlUtility.addQueryParam(this._signoutUrl, "state", this._state.id);
27-
28-
if (this._id_token_hint) {
29-
this._signoutUrl = UrlUtility.addQueryParam(this._signoutUrl, "id_token_hint", this._id_token_hint);
30-
}
31-
32-
if (this._post_logout_redirect_uri) {
33-
this._signoutUrl = UrlUtility.addQueryParam(this._signoutUrl, "post_logout_redirect_uri", this._post_logout_redirect_uri);
34-
}
14+
url = UrlUtility.addQueryParam(url, "state", this.state.id);
15+
if (id_token_hint) {
16+
url = UrlUtility.addQueryParam(url, "id_token_hint", id_token_hint);
3517
}
36-
37-
return this._signoutUrl;
18+
if (post_logout_redirect_uri) {
19+
url = UrlUtility.addQueryParam(url, "post_logout_redirect_uri", post_logout_redirect_uri);
20+
}
21+
this.url = url;
3822
}
3923
}

‎src/SignoutResponse.js‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ export default class SignoutResponse {
1111
return new ErrorResponse(values);
1212
}
1313

14-
this._state = values.state;
15-
}
16-
17-
get state(){
18-
return this._state;
19-
}
20-
set state(value){
21-
this._state = value;
14+
this.state = values.state;
2215
}
2316
}

‎test/OidcClientService.spec.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ describe("OidcClientService", function() {
142142
p.then(request => {
143143
request.state.data.should.equal('foo');
144144

145-
var url = request.signinUrl;
145+
var url = request.url;
146146
url.should.contain("http://sts/authorize");
147147
url.should.contain("response_type=bar");
148148
url.should.contain("scope=baz");
@@ -249,7 +249,7 @@ describe("OidcClientService", function() {
249249

250250
p.then(request => {
251251
request.state.data.should.equal('foo');
252-
var url = request.signoutUrl;
252+
var url = request.url;
253253
url.should.contain("http://sts/signout");
254254
url.should.contain("post_logout_redirect_uri=bar");
255255
url.should.contain("id_token_hint=baz");

0 commit comments

Comments
 (0)