diff --git a/server/.gitignore b/server/.gitignore index 20fccdd..f060e60 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -28,3 +28,6 @@ yarn-error.log* .env.development.local .env.test.local .env.production.local + +# Google service account key file +key.json diff --git a/server/pages/api/downloadPass.ts b/server/pages/api/downloadPass.ts index 41d2080..0fe9620 100644 --- a/server/pages/api/downloadPass.ts +++ b/server/pages/api/downloadPass.ts @@ -1,5 +1,6 @@ import { NextApiRequest, NextApiResponse } from "next" -import { ApplePass } from "../../interfaces" +import { ApplePass, Platform } from "../../interfaces" +import { Passes } from "../../utils/Passes" // req = HTTP incoming message, res = HTTP server response export default function handler(req: NextApiRequest, res: NextApiResponse) { @@ -27,7 +28,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) { // TODO // Populate the pass template - // TODO + Passes.downloadPass(123456, Platform.Google) // Serve the pass download to the user // TODO diff --git a/server/utils/GoogleAuthUtils.ts b/server/utils/GoogleAuthUtils.ts index 2e29bca..67c774e 100644 --- a/server/utils/GoogleAuthUtils.ts +++ b/server/utils/GoogleAuthUtils.ts @@ -3,11 +3,8 @@ 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' + static async createPassAndToken() { + console.log('createPassAndToken') // Issuer ID obtained from Google Pay Business Console. const issuerId = process.env.GOOGLE_WALLET_ISSUER_ID || '' @@ -21,8 +18,44 @@ export class GoogleAuthUtils { // 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 - } + // The content of the service account key file obtained from Google Cloud Console. + const serviceAccountCredentialsBase64 = process.env.GOOGLE_APPLICATION_CREDENTIALS || 'Base64 encoded' + console.log('serviceAccountCredentialsBase64:\n', serviceAccountCredentialsBase64) + + // Decode from Base64 + const serviceAccountCredentials = Buffer.from(serviceAccountCredentialsBase64, "base64").toString() + console.log('serviceAccountCredentials:\n', serviceAccountCredentials) + + // Convert service account credentials from string to JSON + // const serviceAccountCredentialsJSON = JSON.parse(serviceAccountCredentials) + const serviceAccountCredentialsFromFile = require('../key.json') + const serviceAccountCredentialsJSON = JSON.parse(serviceAccountCredentialsFromFile) + console.log('serviceAccountCredentialsJSON:\n', serviceAccountCredentialsJSON) + + const httpClient = new GoogleAuth({ + credentials: serviceAccountCredentialsJSON, + scopes: 'https://www.googleapis.com/auth/wallet_object.issuer' + }) + + const objectPayload = require('../../template-versions/google/1/generic-pass.json') + objectPayload.id = objectId + objectPayload.classId = classId + console.log('objectPayload:\n', objectPayload) + + // Create a pass object + const objectUrl = 'https://walletobjects.googleapis.com/walletobjects/v1/genericObject/' + let objectResponse; + try { + objectResponse = await httpClient.request({url: objectUrl + objectPayload.id, method: 'GET'}); + console.log('existing object', objectPayload.id); + } catch (err : any) { + if (err.response && (err.response.status === 404)) { + objectResponse = await httpClient.request({url: objectUrl, method: 'POST', data: objectPayload}); + console.log('new object', objectPayload.id); + } else { + console.error(err); + throw err; + } + } + } } diff --git a/server/utils/Passes.ts b/server/utils/Passes.ts index 9243776..6da2a4b 100644 --- a/server/utils/Passes.ts +++ b/server/utils/Passes.ts @@ -1,4 +1,5 @@ import { Platform } from "../interfaces" +import { GoogleAuthUtils } from "./GoogleAuthUtils" export class Passes { @@ -11,7 +12,13 @@ export class Passes { console.log('passportID:', passportID) console.log('platform:', platform) - // TODO + if (platform == Platform.Google) { + console.log('platform == Platform.Google') + GoogleAuthUtils.createPassAndToken() + } else if (platform == Platform.Apple) { + console.log('platform == Platform.Apple') + // TODO + } } /**