Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
8620bf98
Commit
8620bf98
authored
Feb 08, 2017
by
Robert Knight
Committed by
GitHub
Feb 08, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #219 from hypothesis/dont-reuse-oauth-grant-tokens
Don't reuse OAuth grant tokens
parents
7f75f29e
d0bfcce7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
30 deletions
+12
-30
oauth-auth.js
src/sidebar/oauth-auth.js
+12
-13
oauth-auth-test.js
src/sidebar/test/oauth-auth-test.js
+0
-17
No files found.
src/sidebar/oauth-auth.js
View file @
8620bf98
...
...
@@ -16,11 +16,6 @@ function auth($http, settings) {
var
cachedToken
;
var
tokenUrl
=
resolve
(
'token'
,
settings
.
apiUrl
);
var
grantToken
;
if
(
Array
.
isArray
(
settings
.
services
)
&&
settings
.
services
.
length
>
0
)
{
grantToken
=
settings
.
services
[
0
].
grantToken
;
}
// Exchange the JWT grant token for an access token.
// See https://tools.ietf.org/html/rfc7523#section-4
function
exchangeToken
(
grantToken
)
{
...
...
@@ -41,21 +36,25 @@ function auth($http, settings) {
}
function
tokenGetter
()
{
// performance.now() is used instead of Date.now() because it is
// monotonically increasing.
if
(
cachedToken
&&
cachedToken
.
expiresAt
>
performance
.
now
())
{
if
(
cachedToken
)
{
return
Promise
.
resolve
(
cachedToken
.
token
);
}
else
if
(
grantToken
)
{
var
refreshStart
=
performance
.
now
();
}
else
{
var
grantToken
;
if
(
Array
.
isArray
(
settings
.
services
)
&&
settings
.
services
.
length
>
0
)
{
grantToken
=
settings
.
services
[
0
].
grantToken
;
}
if
(
!
grantToken
)
{
return
Promise
.
resolve
(
null
);
}
return
exchangeToken
(
grantToken
).
then
(
function
(
tokenInfo
)
{
cachedToken
=
{
token
:
tokenInfo
.
access_token
,
expiresAt
:
refreshStart
+
tokenInfo
.
expires_in
*
1000
,
};
return
cachedToken
.
token
;
});
}
else
{
return
Promise
.
resolve
(
null
);
}
}
...
...
src/sidebar/test/oauth-auth-test.js
View file @
8620bf98
...
...
@@ -84,22 +84,5 @@ describe('oauth auth', function () {
assert
.
equal
(
token
,
null
);
});
});
it
(
'should refresh the access token if it has expired'
,
function
()
{
return
auth
.
tokenGetter
().
then
(
function
()
{
var
now
=
performance
.
now
();
nowStub
.
returns
(
now
+
DEFAULT_TOKEN_EXPIRES_IN_SECS
*
1000
+
100
);
fakeHttp
.
post
.
returns
(
Promise
.
resolve
({
status
:
200
,
data
:
{
access_token
:
'a-different-access-token'
,
expires_in
:
DEFAULT_TOKEN_EXPIRES_IN_SECS
,
},
}));
return
auth
.
tokenGetter
();
}).
then
(
function
(
token
)
{
assert
.
equal
(
token
,
'a-different-access-token'
);
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment