Skip to content

Commit

Permalink
feat: create a pass object for the google api (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
aahna-ashina committed May 28, 2022
1 parent c584023 commit 0283fce
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
3 changes: 3 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ yarn-error.log*
.env.development.local
.env.test.local
.env.production.local

# Google service account key file
key.json
5 changes: 3 additions & 2 deletions server/pages/api/downloadPass.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
Expand Down
51 changes: 42 additions & 9 deletions server/utils/GoogleAuthUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '<issuer ID>'
Expand All @@ -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;
}
}
}
}
9 changes: 8 additions & 1 deletion server/utils/Passes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Platform } from "../interfaces"
import { GoogleAuthUtils } from "./GoogleAuthUtils"

export class Passes {

Expand All @@ -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
}
}

/**
Expand Down

0 comments on commit 0283fce

Please sign in to comment.