Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: create a pass object for the google api (#5) #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to also get it from process.env - we can have it there with JSON.stringify. In fact it's there in the env file I shared with you

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