Skip to content

Commit f231ec9

Browse files
committed
add scopes, expires_in, expired
1 parent ab98b56 commit f231ec9

2 files changed

Lines changed: 77 additions & 22 deletions

File tree

‎src/SigninResponse.js‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,44 @@ import ErrorResponse from './ErrorResponse';
44

55
export default class SigninResponse {
66
constructor(url) {
7-
7+
88
var values = UrlUtility.parseUrlFragment(url, "#");
9-
10-
if (values.error){
9+
10+
if (values.error) {
1111
return new ErrorResponse(values);
1212
}
13-
13+
1414
this.state = values.state;
1515
this.id_token = values.id_token;
1616
this.session_state = values.session_state;
1717
this.access_token = values.access_token;
1818
this.token_type = values.token_type;
1919
this.scope = values.scope;
20-
20+
2121
let expires_in = parseInt(values.expires_in);
22-
if (typeof expires_in === 'number' && expires_in > 0){
22+
if (typeof expires_in === 'number' && expires_in > 0) {
2323
let now = parseInt(Date.now() / 1000);
2424
this.expires_at = now + expires_in;
2525
}
2626
}
27-
28-
get expires_in(){
29-
if (this.expires_at){
27+
28+
get expires_in() {
29+
if (this.expires_at) {
3030
let now = parseInt(Date.now() / 1000);
3131
return this.expires_at - now;
3232
}
3333
return undefined;
3434
}
35-
}
35+
36+
get expired() {
37+
let expires_in = this.expires_in;
38+
if (expires_in !== undefined) {
39+
return expires_in <= 0;
40+
}
41+
return undefined;
42+
}
43+
44+
get scopes() {
45+
return (this.scope || "").split(" ");
46+
}
47+
}

‎test/unit/SigninResponse.spec.js‎

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,105 @@ describe("SigninResponse", function() {
1414
let subject = new SigninResponse("state=foo");
1515
subject.state.should.equal("foo");
1616
});
17-
17+
1818
it("should return SigninResponse if no error", function() {
1919
let subject = new SigninResponse("");
2020
subject.should.be.instanceof(SigninResponse);
2121
});
22-
22+
2323
it("should return ErrorResponse if error", function() {
2424
let subject = new SigninResponse("error=foo");
2525
subject.should.be.instanceof(ErrorResponse);
2626
});
27-
27+
2828
it("should read id_token", function() {
2929
let subject = new SigninResponse("id_token=foo");
3030
subject.id_token.should.equal("foo");
3131
});
32-
32+
3333
it("should read session_state", function() {
3434
let subject = new SigninResponse("session_state=foo");
3535
subject.session_state.should.equal("foo");
3636
});
37-
37+
3838
it("should read access_token", function() {
3939
let subject = new SigninResponse("access_token=foo");
4040
subject.access_token.should.equal("foo");
4141
});
42-
42+
4343
it("should read token_type", function() {
4444
let subject = new SigninResponse("token_type=foo");
4545
subject.token_type.should.equal("foo");
4646
});
47-
47+
4848
it("should read scope", function() {
4949
let subject = new SigninResponse("scope=foo");
5050
subject.scope.should.equal("foo");
5151
});
52-
52+
5353
it("should read expires_in", function() {
5454
let subject = new SigninResponse("expires_in=10");
5555
subject.expires_in.should.equal(10);
5656
});
57-
57+
5858
it("should calculate expires_at", function() {
5959
let subject = new SigninResponse("expires_in=10");
60-
subject.expires_at.should.equal(parseInt((Date.now()/1000) + 10));
60+
subject.expires_at.should.equal(parseInt((Date.now() / 1000) + 10));
6161
});
62-
62+
6363
it("should not read invalid expires_in", function() {
6464
let subject = new SigninResponse("expires_in=foo");
6565
expect(subject.expires_in).to.be.undefined;
6666
expect(subject.expires_at).to.be.undefined;
67-
67+
6868
subject = new SigninResponse("expires_in=-10");
6969
expect(subject.expires_in).to.be.undefined;
7070
expect(subject.expires_at).to.be.undefined;
7171
});
7272

7373
});
7474

75+
describe("scopes", function() {
76+
it("should return list of scope", function() {
77+
let subject = new SigninResponse("scope=foo");
78+
subject.scopes.should.deep.equal(["foo"]);
79+
80+
subject = new SigninResponse("scope=foo%20bar");
81+
subject.scopes.should.deep.equal(["foo", "bar"]);
82+
83+
subject = new SigninResponse("scope=foo%20bar%20baz");
84+
subject.scopes.should.deep.equal(["foo", "bar", "baz"]);
85+
});
86+
});
87+
88+
describe("expires_in", function() {
89+
it("should calculate how much time left", function() {
90+
Date.now = function() {
91+
return 1000 * 1000; // ms
92+
}
93+
let subject = new SigninResponse("expires_in=100");
94+
subject.expires_in.should.equal(100);
95+
96+
Date.now = function() {
97+
return 1050 * 1000;
98+
}
99+
subject.expires_in.should.equal(50);
100+
});
101+
});
102+
103+
describe("expired", function() {
104+
it("should calculate how much time left", function() {
105+
Date.now = function() {
106+
return 1000 * 1000; // ms
107+
}
108+
let subject = new SigninResponse("expires_in=100");
109+
subject.expired.should.be.false;
110+
111+
Date.now = function() {
112+
return 1100 * 1000; // ms
113+
}
114+
subject.expired.should.be.true;
115+
});
116+
});
117+
75118
});

0 commit comments

Comments
 (0)