Skip to content

Commit

Permalink
updated Google OAuth (simple-oauth2) dependency/implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
suculent committed Dec 19, 2023
1 parent 57d03de commit 975eef1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
28 changes: 20 additions & 8 deletions lib/router.google.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const app_config = Globals.app_config(); // public_url and public_url but it is

const google_ocfg = Globals.google_ocfg();

const oauth2 = require('simple-oauth2').create({
const oAuthConfig = {
client: {
id: process.env.GOOGLE_OAUTH_ID,
secret: process.env.GOOGLE_OAUTH_SECRET
Expand All @@ -32,7 +32,9 @@ const oauth2 = require('simple-oauth2').create({
tokenHost: 'https://www.googleapis.com',
tokenPath: '/oauth2/v4/token'
}
});
};

const { AuthorizationCode } = require('simple-oauth2');

module.exports = function (app) {

Expand Down Expand Up @@ -136,7 +138,10 @@ module.exports = function (app) {
require("crypto").randomBytes(48, (_err, buffer) => {
var token = buffer.toString('hex');
redis_client.v4.set("oa:google:" + token, 60); // auto-expires in 1 minute; TODO: verify
const authorizationUri = oauth2.authorizationCode.authorizeURL({

const client = new AuthorizationCode(oAuthConfig);

const authorizationUri = client.authorizeURL({
redirect_uri: google_ocfg.web.redirect_uris[0],
scope: 'email',
state: sha256(token) // returned upon auth provider call back
Expand All @@ -158,13 +163,20 @@ module.exports = function (app) {
}
}

const options = {
code,
redirect_uri: google_ocfg.web.redirect_uris[0]
const tokenParams = {
code: code,
redirect_uri: google_ocfg.web.redirect_uris[0],
scope: 'email',
};

const result = await oauth2.authorizationCode.getToken(options);
const accessToken = oauth2.accessToken.create(result);
let accessToken;

try {
accessToken = await client.getToken(tokenParams);

Check notice on line 175 in lib/router.google.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/router.google.js#L175

'client' is not defined.
} catch (error) {
console.log('Access Token Error', error.message);
}

const gat_url = 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' + accessToken.token.access_token;

https.get(gat_url, (res3) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"sha256": "^0.2.0",
"shell-escape": "^0.2.0",
"sillyname": "^0.1.0",
"simple-oauth2": "^4.3.0",
"simple-oauth2": "^5.0.0",
"slack-notify": "^2.0.6",
"socket.io": "^4.7.2",
"socket.io-client": "^4.7.2",
Expand Down

0 comments on commit 975eef1

Please sign in to comment.