From 9c169bb031249362e2ea0327052daa106f7b592c Mon Sep 17 00:00:00 2001 From: aahna-ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Mon, 23 May 2022 16:53:13 +0800 Subject: [PATCH] feat: google wallet api authentication and authorization (#11) Add utils class for google authentication --- server/.env.local.sample | 5 +++++ server/utils/GoogleAuthUtils.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 server/utils/GoogleAuthUtils.ts diff --git a/server/.env.local.sample b/server/.env.local.sample index b602a2b..8aa1746 100644 --- a/server/.env.local.sample +++ b/server/.env.local.sample @@ -1,2 +1,7 @@ BASIC_AUTH_USERNAME=username BASIC_AUTH_PASSWORD=password + +GOOGLE_APPLICATION_CREDENTIALS= +GOOGLE_WALLET_ISSUER_ID= +GOOGLE_WALLET_CLASS_ID= +GOOGLE_WALLET_USER_ID= diff --git a/server/utils/GoogleAuthUtils.ts b/server/utils/GoogleAuthUtils.ts new file mode 100644 index 0000000..2e29bca --- /dev/null +++ b/server/utils/GoogleAuthUtils.ts @@ -0,0 +1,28 @@ +const { GoogleAuth } = require('google-auth-library') +const jwt = require('jsonwebtoken') + +export class GoogleAuthUtils { + + static retrieveCredentials() { + console.log('retrieveCredentials') + + // Path to service account key file obtained from Google CLoud Console. + const serviceAccountFile = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json' + + // Issuer ID obtained from Google Pay Business Console. + const issuerId = process.env.GOOGLE_WALLET_ISSUER_ID || '' + + // Developer defined ID for the wallet class. + const classId = process.env.GOOGLE_WALLET_CLASS_ID || 'test-generic-class-id' + + // Developer defined ID for the user, eg an email address. + const userId = process.env.GOOGLE_WALLET_USER_ID || 'test@example.com' + + // ID for the wallet object, must be in the form `issuerId.userId` where userId is alphanumeric. + const objectId = `${issuerId}.${userId.replace(/[^\w.-]/g, '_')}-${classId}` + + const credentials = require(serviceAccountFile) + + return credentials + } +}