Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Refresh Token Flow #19

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/model/meteor-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const saveAuthorizationCode = bind(function saveAuthCode (code, client, u
redirectUri,
scope: code.scope,
client: {
id: client.client_id
id: client.clientId
},
user: {
id: user.id
Expand Down Expand Up @@ -139,5 +139,14 @@ export const saveRefreshToken = bind(function (token, clientId, expires, user) {
* @private used by OAuthMeteorModel.prototype.getRefreshToken
*/
export const getRefreshToken = bind(function (refreshToken) {
return collections.RefreshTokens.findOne({ refreshToken })
return collections.AccessTokens.findOne({ refreshToken })
})

export const revokeToken = bind(function (token) {
const docCount = collections.AccessTokens.find({ refreshToken: token.refreshToken }).count()
if (docCount === 0) {
return true
}

return collections.AccessTokens.remove({ refreshToken: token.refreshToken }) === docCount
})
11 changes: 10 additions & 1 deletion lib/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
saveAuthorizationCode,
saveRefreshToken,
saveToken,
getAccessToken
getAccessToken,
revokeToken
} from './meteor-model'

/**
Expand Down Expand Up @@ -174,6 +175,14 @@ class OAuthMeteorModel {
this.log('grantTypeAllowed (clientId:', clientId, ', grantType:', grantType + ')')
return ['authorization_code', 'refresh_token'].includes(grantType)
}

/**
* revokeToken(refreshToken) is required and should return true
*/
async revokeToken (refreshToken) {
this.log(`revokeToken (refreshToken: ${refreshToken})`)
return revokeToken(refreshToken)
}
}

export { OAuthMeteorModel }
3 changes: 2 additions & 1 deletion lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { validateParams } from './validation/validateParams'
import { requiredAuthorizeGetParams } from './validation/requiredAuthorizeGetParams'
import { requiredAuthorizePostParams } from './validation/requiredAuthorizePostParams'
import { requiredAccessTokenPostParams } from './validation/requiredAccessTokenPostParams'
import { requiredRefreshTokenPostParams } from './validation/requiredRefreshTokenPostParams'
import { UserValidation } from './validation/UserValidation'
import { OptionsSchema } from './validation/OptionsSchema'

Expand Down Expand Up @@ -407,7 +408,7 @@ const initRoutes = (self, { accessTokenUrl = '/oauth/token', authorizeUrl = '/oa
// - validate authorization code
// - issue accessToken and refreshToken
route('post', accessTokenUrl, async function (req, res /*, next */) {
if (!validateParams(req.body, requiredAccessTokenPostParams, self.debug)) {
if (!validateParams(req.body, req.body?.refresh_token ? requiredRefreshTokenPostParams : requiredAccessTokenPostParams, self.debug)) {
return errorHandler(res, {
status: 400,
error: 'invalid_request',
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/isModelInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const modelNames = [
'saveAuthorizationCode',
'saveRefreshToken',
'saveToken',
'getAccessToken'
'getAccessToken',
'revokeToken'
]

/**
Expand All @@ -28,6 +29,7 @@ const modelNames = [
* - 'saveRefreshToken',
* - 'saveToken',
* - 'getAccessToken'
* - 'revokeToken'
* @param model {Object} the model implementation
* @return {boolean} true if valid, otherwise false
*/
Expand Down
11 changes: 11 additions & 0 deletions lib/validation/requiredRefreshTokenPostParams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Match } from 'meteor/check'
import { nonEmptyString } from './nonEmptyString'

const isNonEmptyString = Match.Where(nonEmptyString)

export const requiredRefreshTokenPostParams = {
grant_type: isNonEmptyString,
refresh_token: isNonEmptyString,
client_id: Match.Maybe(String),
client_secret: Match.Maybe(String)
}
Loading