Skip to content

Conversation

@mjsalinger
Copy link
Contributor

Adds client, user, and scope as arguments to generateAccessToken and generateRefreshToken. This allows such things as returning the same access token on a reauth if the token is not expired, as many oauth implementations do.

maxtruxa added a commit to maxtruxa/node-oauth2-server that referenced this pull request May 27, 2016
@nunofgs
Copy link
Collaborator

nunofgs commented Oct 13, 2016

@mjsalinger issuing the same access token in the "refresh token" grant does not seem to be allowed by the OAuth RFC:

The authorization server authenticates the client and validates the refresh token, and if valid, issues a new access token (and, optionally, a new refresh token).

Can you provide more detail on how you're using these parameters?

I believe you would need to also pass these down to the abstract-grant in order to get that to work.

@mjsalinger
Copy link
Contributor Author

I don't think it's needed in abstract grant, just for tokens. The use case I'm dealing with is primarily the mobile use case, where a single user may need access from multiple devices. If each device has a single access token and single refresh token, then you end up having to specifically link the refresh and access token. The general practice I've seen is that only one access token is issued per user/clientId combo, and a unique, single use refresh token is issued for each token endpoint access. Once the access token expires, each client will need to use it's refresh token to obtain a new access token. The first device to refresh will get a newly generated access token, then each subsequent device will also need to refresh to obtain the access token. So it does follow the spec in for the device, they are issued a new access token, since the point of a refresh token is to refresh an expired token.

This, by the way, is the way Google OAuth handles it, and I've seen it in other implementations as well.

@pi-kei
Copy link

pi-kei commented Oct 21, 2016

Generate token params is a good thing but these params also should be passed anywhere where generateAccessToken() or generateRefreshToken() called.

@mjsalinger mjsalinger added this to the 3.0.0-b3 milestone Oct 22, 2016
Copy link
Member

@maxtruxa maxtruxa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing an error rather than falling back to generating a token when model#generateAccessToken/model#generateRefreshToken return a falsy value would be safer in my opinion.

return Promise.try(this.model.generateAccessToken);
return Promise.try(promisify(this.model.generateAccessToken, 3), [client, user, scope])
.then(function(accessToken) {
return accessToken || tokenUtil.generateRandomToken();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's a good idea to fall back to the default generateRandomToken?
I would consider it an error if model#generateAccessToken is implemented but returns a falsy value.

return Promise.try(this.model.generateRefreshToken);
return Promise.try(promisify(this.model.generateRefreshToken, 3), [client, user, scope])
.then(function(refreshToken) {
return refreshToken || tokenUtil.generateRandomToken();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's a good idea to fall back to the default generateRandomToken?
I would consider it an error if model#generateRefreshToken is implemented but returns a falsy value.

@maxtruxa
Copy link
Member

How about adding client/user/scope to model#generateAuthorizationCode as well?

This would require some restructuring of AuthorizeHandler#handle but would enable the use of JWTs as authorization codes.
If it's too much of a change for now I can submit a PR later on.

@mjsalinger mjsalinger force-pushed the Generate-Token-Params branch from 6c32df5 to 26f5fba Compare October 25, 2016 10:39
@mjsalinger mjsalinger force-pushed the Generate-Token-Params branch from 26f5fba to e018193 Compare October 25, 2016 10:48
@mjsalinger
Copy link
Contributor Author

@maxtruxa I'm not sure throwing an error is appropriate - the idea is if the called function does not generate a token, the library will do it for you as a fallback, which I think is reasonable behavior - as long as the value is falsy. The model function could still throw or callback an error...

+1 on adding client/user/scope to generateAuthorizationCode, but let's put that out to a separate PR - I'll create an issue around that.

Made some changes to clean up the code a little, and rebased.

@maxtruxa
Copy link
Member

That's very true 👍

@mjsalinger mjsalinger merged commit fcf78d1 into oauthjs:master Oct 25, 2016
@mjsalinger mjsalinger deleted the Generate-Token-Params branch October 25, 2016 11:09
@mjsalinger mjsalinger restored the Generate-Token-Params branch November 9, 2016 11:09
@mjsalinger mjsalinger deleted the Generate-Token-Params branch November 9, 2016 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants