From 975eef16671c30f9b256203e88e08d1c368ae715 Mon Sep 17 00:00:00 2001 From: Matej Sychra Date: Tue, 19 Dec 2023 13:28:31 +0100 Subject: [PATCH] updated Google OAuth (simple-oauth2) dependency/implementation --- lib/router.google.js | 28 ++++++++++++++++++++-------- package.json | 2 +- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/router.google.js b/lib/router.google.js index 6b0f26e91..d6facf79e 100644 --- a/lib/router.google.js +++ b/lib/router.google.js @@ -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 @@ -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) { @@ -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 @@ -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); + } 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) => { diff --git a/package.json b/package.json index 8ce23c849..676063812 100644 --- a/package.json +++ b/package.json @@ -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",