Skip to content
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
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fp = require('fastify-plugin')
const oauth2Module = require('simple-oauth2')

const promisify = require('util').promisify || require('es6-promisify').promisify
const callbackify = require('util').callbackify

function defaultGenerateStateFunction () {
return defaultState
Expand Down Expand Up @@ -73,10 +74,10 @@ const oauthPlugin = fp(function (fastify, options, next) {
}

const cbk = function (o, code, callback) {
return o.oauth2.authorizationCode.getToken({
return callbackify(o.oauth2.authorizationCode.getToken.bind(o.oauth2.authorizationCode, {
code: code,
redirect_uri: callbackUri
}, callback)
}))(callback)
}

function getAccessTokenFromAuthorizationCodeFlowCallbacked (request, callback) {
Expand All @@ -102,7 +103,7 @@ const oauthPlugin = fp(function (fastify, options, next) {

function getNewAccessTokenUsingRefreshTokenCallbacked (refreshToken, params, callback) {
const accessToken = fastify[name].oauth2.accessToken.create({ refresh_token: refreshToken })
accessToken.refresh(params, callback)
callbackify(accessToken.refresh.bind(accessToken, params))(callback)
}
const getNewAccessTokenUsingRefreshTokenPromisified = promisify(getNewAccessTokenUsingRefreshTokenCallbacked)

Expand All @@ -112,7 +113,6 @@ const oauthPlugin = fp(function (fastify, options, next) {
}
getNewAccessTokenUsingRefreshTokenCallbacked(refreshToken, params, callback)
}

const oauth2 = oauth2Module.create(credentials)

if (startRedirectPath) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"homepage": "https://github.com/fastify/fastify-oauth2#readme",
"devDependencies": {
"fastify": "^3.0.0-rc.1",
"nock": "^9.0.0",
"nock": "^12.0.3",
"pre-commit": "^1.2.2",
"simple-get": "^3.0.2",
"snazzy": "^8.0.0",
"standard": "^14.3.3",
"tap": "^12.0.0",
"tap": "^14.10.7",
"tsd": "^0.11.0"
},
"dependencies": {
Expand Down
12 changes: 10 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ function makeRequests (t, fastify) {
}

const githubScope = nock('https://github.com')
.post('/login/oauth/access_token', 'code=my-code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&grant_type=authorization_code&client_id=my-client-id&client_secret=my-secret')
.post('/login/oauth/access_token', 'grant_type=authorization_code&code=my-code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback', {
reqheaders: {
authorization: 'Basic bXktY2xpZW50LWlkOm15LXNlY3JldA=='
}
})
.reply(200, RESPONSE_BODY)
.post('/login/oauth/access_token', 'grant_type=refresh_token&refresh_token=my-refresh-token&client_id=my-client-id&client_secret=my-secret')
.post('/login/oauth/access_token', 'grant_type=refresh_token&refresh_token=my-refresh-token', {
reqheaders: {
authorization: 'Basic bXktY2xpZW50LWlkOm15LXNlY3JldA=='
}
})
.reply(200, RESPONSE_BODY_REFRESHED)

fastify.inject({
Expand Down