Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Commit

Permalink
feat: add configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
eliias committed Apr 7, 2016
1 parent a99708f commit f983e34
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
17 changes: 12 additions & 5 deletions src/grant-types/refresh-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export default function({
tokenModel,
tokenField,
accessTokenTTL,
refreshTokenTTL
}) {
refreshTokenTTL,
destroyTokenAfterUse = true
}) {
const {
refresh_token,
scope} = req.body
scope
} = req.body

if (!_.isString(refresh_token) || refresh_token.length === 0) {
return Promise.reject(new HTTPError(OAUTH_INVALID_REFRESH_TOKEN))
Expand Down Expand Up @@ -51,7 +53,7 @@ export default function({
ownerId: owner.id
})

return Promise
const pair = Promise
.all([accessToken, refreshToken])
.then(tokens => {
const [accessToken, refreshToken] = tokens
Expand All @@ -63,7 +65,12 @@ export default function({
user_id: owner.id
}
})
.then(() => token.destroy())

if (destroyTokenAfterUse) {
return pair.then(() => token.destroy())
}

return pair
})
})
}
18 changes: 13 additions & 5 deletions src/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ function authorize({userModel, clientModel, tokenModel, codeModel}) {
client_id,
redirect_uri,
scope,
state} = Object.assign({}, req.query, req.body)
state
} = Object.assign({}, req.query, req.body)

if (!client_id || client_id.length === 0) {
return Promise.reject(new OAuthError(OAUTH_CLIENT_ID_MISSING))
Expand Down Expand Up @@ -65,13 +66,19 @@ function authorize({userModel, clientModel, tokenModel, codeModel}) {
}
}

function token({userModel, clientModel, tokenModel, codeModel}) {
function token({
userModel,
clientModel,
tokenModel,
codeModel,
accessTokenTTL = 1,
refreshTokenTTL = 14,
destroyTokenAfterUse = false
}) {
// TODO Must be set by plugin config or defaults!
const usernameField = 'email'
const passwordField = 'password'
const tokenField = 'token'
const accessTokenTTL = 1 // hours
const refreshTokenTTL = 14 // days

return function(req, res) {
const {grant_type} = req.body
Expand Down Expand Up @@ -108,7 +115,8 @@ function token({userModel, clientModel, tokenModel, codeModel}) {
userModel,
tokenField,
accessTokenTTL,
refreshTokenTTL
refreshTokenTTL,
destroyTokenAfterUse
})
}

Expand Down

0 comments on commit f983e34

Please sign in to comment.