Skip to content

Commit

Permalink
Merge pull request #2 from avmaxim/bugfix/apple-auth-url-fails
Browse files Browse the repository at this point in the history
Add "secretsInTokenUri" param for providers who require client_id, client_secret and grant_type in a body of token request
  • Loading branch information
avmaxim authored Oct 20, 2021
2 parents 0866769 + 524e612 commit 5289ef2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface FastifyOAuth2Options {
credentials: Credentials;
callbackUri: string;
callbackUriParams?: Object;
secretsInTokenUri?: boolean;
generateStateFunction?: Function;
checkStateFunction?: Function;
startRedirectPath: string;
Expand Down
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const oauthPlugin = fp(function (fastify, options, next) {

const name = options.name
const credentials = options.credentials
const secretsInTokenUri = options.secretsInTokenUri || false
const callbackUri = options.callbackUri
const callbackUriParams = options.callbackUriParams || {}
const scope = options.scope
Expand Down Expand Up @@ -84,10 +85,18 @@ const oauthPlugin = fp(function (fastify, options, next) {
}

const cbk = function (o, code, callback) {
return callbackify(o.oauth2.authorizationCode.getToken.bind(o.oauth2.authorizationCode, {
code: code,
redirect_uri: callbackUri
}))(callback)
const getTokenParams = {
code,
redirect_uri: callbackUri,
...(secretsInTokenUri && {
client_id: credentials.client.id,
client_secret: credentials.client.secret,
grant_type: 'authorization_code'
})
}
return callbackify(o.oauth2.authorizationCode.getToken.bind(
o.oauth2.authorizationCode, { ...getTokenParams }
))(callback)
}

function getAccessTokenFromAuthorizationCodeFlowCallbacked (request, callback) {
Expand All @@ -99,6 +108,7 @@ const oauthPlugin = fp(function (fastify, options, next) {
callback(err)
return
}

cbk(fastify[name], code, callback)
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-oauth2",
"version": "4.2.4",
"version": "4.2.5",
"description": "Perform login using oauth2 protocol",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 5289ef2

Please sign in to comment.