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
29af928e
Commit
29af928e
authored
Feb 09, 2017
by
Robert Knight
Committed by
GitHub
Feb 09, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #220 from hypothesis/cache-access-token-promise-not-access-token
Cache access token promise not access token
parents
8620bf98
e15f5740
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
13 deletions
+35
-13
oauth-auth.js
src/sidebar/oauth-auth.js
+10
-13
oauth-auth-test.js
src/sidebar/test/oauth-auth-test.js
+25
-0
No files found.
src/sidebar/oauth-auth.js
View file @
29af928e
...
...
@@ -13,7 +13,7 @@ var resolve = require('./util/url-util').resolve;
// @ngInject
function
auth
(
$http
,
settings
)
{
var
cachedToken
;
var
accessTokenPromise
;
var
tokenUrl
=
resolve
(
'token'
,
settings
.
apiUrl
);
// Exchange the JWT grant token for an access token.
...
...
@@ -36,26 +36,23 @@ function auth($http, settings) {
}
function
tokenGetter
()
{
if
(
cachedToken
)
{
return
Promise
.
resolve
(
cachedToken
.
token
);
}
else
{
if
(
!
accessTokenPromise
)
{
var
grantToken
;
if
(
Array
.
isArray
(
settings
.
services
)
&&
settings
.
services
.
length
>
0
)
{
grantToken
=
settings
.
services
[
0
].
grantToken
;
}
if
(
!
grantToken
)
{
return
Promise
.
resolve
(
null
);
if
(
grantToken
)
{
accessTokenPromise
=
exchangeToken
(
grantToken
).
then
(
function
(
tokenInfo
)
{
return
tokenInfo
.
access_token
;
});
}
else
{
accessTokenPromise
=
Promise
.
resolve
(
null
);
}
return
exchangeToken
(
grantToken
).
then
(
function
(
tokenInfo
)
{
cachedToken
=
{
token
:
tokenInfo
.
access_token
,
};
return
cachedToken
.
token
;
});
}
return
accessTokenPromise
;
}
// clearCache() isn't implemented (or needed) yet for OAuth.
...
...
src/sidebar/test/oauth-auth-test.js
View file @
29af928e
...
...
@@ -75,6 +75,31 @@ describe('oauth auth', function () {
});
});
// If an access token request has already been made but is still in
// flight when tokenGetter() is called again, then it should just return
// the pending Promise for the first request again (and not send a second
// concurrent HTTP request).
it
(
'should not make two concurrent access token requests'
,
function
()
{
// Make $http.post() return a pending Promise (simulates an in-flight
// HTTP request).
fakeHttp
.
post
.
returns
(
new
Promise
(
function
()
{}));
// The first time tokenGetter() is called it sends the access token HTTP
// request and returns a Promise for the access token.
var
firstAccessTokenPromise
=
auth
.
tokenGetter
();
// No matter how many times it's called while there's an HTTP request
// in-flight, tokenGetter() never sends a second concurrent HTTP request.
auth
.
tokenGetter
();
auth
.
tokenGetter
();
// It just keeps on returning the same Promise for the access token.
var
accessTokenPromise
=
auth
.
tokenGetter
();
assert
.
strictEqual
(
accessTokenPromise
,
firstAccessTokenPromise
);
assert
.
equal
(
fakeHttp
.
post
.
callCount
,
1
);
});
it
(
'should return null if no grant token was provided'
,
function
()
{
var
auth
=
authService
(
fakeHttp
,
{
services
:
[{
authority
:
'publisher.org'
}],
...
...
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