Created
July 28, 2021 14:42
-
-
Save rodrigobranas/a123c2a2ce58b6348f27588eb9aab22c to your computer and use it in GitHub Desktop.
OAuth2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const flow = { | |
google: { | |
auth: { | |
url: 'https://accounts.google.com/o/oauth2/v2/auth', | |
querystring: (params) => `scope=${params.scope}&prompt=${params.prompt}&response_type=${params.response_type}&client_id=${params.client_id}&redirect_uri=${params.redirect_uri}`, | |
method: 'get', | |
params: ['scope', 'prompt', 'response_type', 'client_id', 'redirect_uri'] | |
}, | |
token: { | |
url: 'https://oauth2.googleapis.com/token', | |
method: 'post', | |
headers: { | |
'Content-type': 'application/json', | |
}, | |
params: ['code', 'client_id', 'client_secret', 'redirect_uri', 'grant_type'] | |
}, | |
userInfo: { | |
url: 'https://www.googleapis.com/oauth2/v2/userinfo', | |
querystring: (access_token) => `access_token=${access_token}`, | |
method: 'get', | |
headers: (id_token) => ({ 'Authorization': `Bearer ${id_token}` }), | |
params: ['access_token', 'id_token'] | |
}, | |
revoke: { | |
url: 'https://oauth2.googleapis.com/revoke', | |
querystring: (access_token) => `token=${access_token}`, | |
method: 'post', | |
params: ['access_token'] | |
} | |
}, | |
github: { | |
auth: { | |
url: 'https://github.com/login/oauth/authorize', | |
querystring: (params) => `scope=${params.scope}&client_id=${params.client_id}&redirect_uri=${params.redirect_uri}`, | |
method: 'get', | |
params: ['scope', 'client_id', 'redirect_uri'] | |
}, | |
token: { | |
url: 'https://github.com/login/oauth/access_token', | |
method: 'post', | |
headers: { | |
'Accept': 'application/json', | |
'Content-type': 'application/json', | |
}, | |
params: ['code', 'client_id', 'client_secret'] | |
}, | |
userInfo: { | |
url: 'https://api.github.com/user', | |
method: 'get', | |
headers: (access_token) => ({ 'Authorization': `Bearer ${access_token}` }), | |
params: ['access_token'] | |
} | |
} | |
}; | |
exports.generateAuthUrl = function (provider, obj) { | |
const url = flow[provider].auth.url; | |
return `${url}?${flow[provider].auth.querystring(obj)}`; | |
}; | |
exports.generateToken = async function (provider, obj) { | |
const options = { | |
url: flow[provider].token.url, | |
method: flow[provider].token.method, | |
headers: flow[provider].token.headers, | |
data: obj | |
}; | |
const response = await axios(options); | |
return response.data; | |
}; | |
exports.getUserInfo = async function (provider, obj) { | |
let options; | |
if (provider === 'google') { | |
options = { | |
url: `${flow[provider].userInfo.url}?${flow[provider].userInfo.querystring(obj.access_token)}`, | |
method: flow[provider].userInfo.method, | |
headers: flow[provider].userInfo.headers(obj.id_token) | |
} | |
} | |
if (provider === 'github') { | |
options = { | |
url: `${flow[provider].userInfo.url}`, | |
method: flow[provider].userInfo.method, | |
headers: flow[provider].userInfo.headers(obj.access_token) | |
} | |
} | |
const response = await axios(options); | |
return response.data; | |
}; | |
exports.revoke = async function (provider, obj) { | |
const options = { | |
url: `${flow[provider].revoke.url}?${flow[provider].revoke.querystring(obj.access_token)}`, | |
method: flow[provider].revoke.method | |
} | |
const response = await axios(options); | |
return response.data; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment