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

feat(auth): set up App Store modules #12374

Merged
merged 1 commit into from
Apr 22, 2022
Merged
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
2 changes: 1 addition & 1 deletion packages/fxa-auth-server/bin/key_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const jwtool = require('fxa-jwtool');
const { StatsD } = require('hot-shots');
const { Container } = require('typedi');
const { StripeHelper } = require('../lib/payments/stripe');
const { PlayBilling } = require('../lib/payments/google-play');
const { PlayBilling } = require('../lib/payments/iap/google-play');
const { CurrencyHelper } = require('../lib/payments/currencies');
const {
AuthLogger,
Expand Down
22 changes: 22 additions & 0 deletions packages/fxa-auth-server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,28 @@ const conf = convict({
env: 'PAYPAL_NVP_SIGNATURE',
},
},
appStore: {
credentials: {
doc: 'Map of AppStore Connect credentials by app bundle ID',
format: Object,
default: {
// Cannot use an actual bundleId (e.g. 'org.mozilla.ios.FirefoxVPN') as the key
// due to https://github.com/mozilla/node-convict/issues/250
org_mozilla_ios_FirefoxVPN: {
issuerId: 'issuer_id',
serverApiKey: 'key',
serverApiKeyId: 'key_id',
},
},
env: 'APP_STORE_CREDENTIALS',
},
sandbox: {
doc: 'Apple App Store Sandbox mode',
format: Boolean,
env: 'APP_STORE_SANDBOX',
default: true,
},
},
playApiServiceAccount: {
credentials: {
client_email: {
Expand Down
6 changes: 3 additions & 3 deletions packages/fxa-auth-server/lib/payments/capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { commaSeparatedListToArray } from './utils';
import error from '../error';
import { authEvents } from '../events';
import { AuthLogger, AuthRequest, ProfileClient } from '../types';
import { PlayBilling } from './google-play/play-billing';
import { SubscriptionPurchase } from './google-play/subscription-purchase';
import { PurchaseQueryError } from './google-play/types';
import { PlayBilling } from './iap/google-play/play-billing';
import { SubscriptionPurchase } from './iap/google-play/subscription-purchase';
import { PurchaseQueryError } from './iap/google-play/types';
import { StripeHelper } from './stripe';

function hex(blob: Buffer | string): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import {
AppStoreServerAPI,
Environment,
StatusResponse,
} from 'app-store-server-api';
import { Container } from 'typedi';

import { AppConfig, AuthLogger } from '../../../types';
import { AppStoreHelperError } from './types/errors';

export class AppStoreHelper {
private log: AuthLogger;
private appStoreServerApiClients: {
[key: string]: AppStoreServerAPI;
};
private credentialsByBundleId: any;
private environment: Environment;

constructor() {
this.log = Container.get(AuthLogger);
const {
subscriptions: { appStore },
} = Container.get(AppConfig);
this.credentialsByBundleId = {};
// Initialize App Store Server API client per bundle ID
this.environment = appStore.sandbox
? Environment.Sandbox
: Environment.Production;
this.appStoreServerApiClients = {};
for (const [bundleIdWithUnderscores, credentials] of Object.entries(
appStore.credentials
)) {
// Cannot use an actual bundleId (e.g. 'org.mozilla.ios.FirefoxVPN') as the key
// due to https://github.com/mozilla/node-convict/issues/250
const bundleId = bundleIdWithUnderscores.replace('_', '.');
this.credentialsByBundleId[bundleId] = credentials;
this.clientByBundleId(bundleId);
}
}

/**
* Returns an App Store Server API client by bundleId, initializing it first
* if needed.
*/
clientByBundleId(bundleId: string): AppStoreServerAPI {
if (this.appStoreServerApiClients.hasOwnProperty(bundleId)) {
return this.appStoreServerApiClients[bundleId];
}
if (!this.credentialsByBundleId.hasOwnProperty(bundleId)) {
const libraryError = new Error(
`No App Store credentials found for app with bundleId: ${bundleId}.`
);
libraryError.name = AppStoreHelperError.CREDENTIALS_NOT_FOUND;
throw libraryError;
}
const { serverApiKey, serverApiKeyId, issuerId } =
this.credentialsByBundleId[bundleId];
biancadanforth marked this conversation as resolved.
Show resolved Hide resolved
this.appStoreServerApiClients[bundleId] = new AppStoreServerAPI(
serverApiKey,
serverApiKeyId,
issuerId,
bundleId,
this.environment
);
return this.appStoreServerApiClients[bundleId];
}

async getSubscriptionStatuses(
bundleId: string,
originalTransactionId: string
): Promise<StatusResponse> {
const apiClient = this.clientByBundleId(bundleId);
return apiClient.getSubscriptionStatuses(originalTransactionId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { Firestore } from '@google-cloud/firestore';
import { Container } from 'typedi';

import { AppConfig, AuthFirestore, AuthLogger } from '../../../types';
import { AppStoreHelper } from './app-store-helper';
import { PurchaseManager } from './purchase-manager';

export class AppleIAP {
private firestore: Firestore;
private log: AuthLogger;
private prefix: string;

public purchaseManager: PurchaseManager;
constructor() {
this.log = Container.get(AuthLogger);
const appStoreHelper = new AppStoreHelper();

this.firestore = Container.get(AuthFirestore);
const { authFirestore } = Container.get(AppConfig);
this.prefix = `${authFirestore.prefix}iap-`;
const purchasesDbRef = this.firestore.collection(
`${this.prefix}app-store-purchases`
);
this.purchaseManager = new PurchaseManager(purchasesDbRef, appStoreHelper);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Error Codes representing an error that is temporary to Apple
* and should be retried again without changes.
* https://developer.apple.com/documentation/appstoreserverapi/error_codes
*/
export const APP_STORE_RETRY_ERRORS = [4040002, 4040004, 5000001, 4040006];

export class AppStoreRetryableError extends Error {
public errorCode: number;
public errorMessage: string;

constructor(errorCode: number, errorMessage: string) {
super(errorMessage);
this.name = 'AppStoreRetryableError';
this.errorCode = errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

export { AppleIAP } from './apple-iap';
Loading