Skip to content

Commit

Permalink
Merge pull request #14038 from mozilla/FXA-5833
Browse files Browse the repository at this point in the history
chore(auth): allow profile scope on App Store registration route
  • Loading branch information
biancadanforth authored Aug 31, 2022
2 parents 31fa8e3 + c2c4ea0 commit 3d55231
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
21 changes: 15 additions & 6 deletions packages/fxa-auth-server/lib/routes/subscriptions/apple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* 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 { ServerRoute } from '@hapi/hapi';
import isA from 'joi';
import { DecodedNotificationPayload } from 'app-store-server-api';
import { OAUTH_SCOPE_SUBSCRIPTIONS_IAP } from 'fxa-shared/oauth/constants';
import ScopeSet from 'fxa-shared/oauth/scopes';
import isA from 'joi';
import { Container } from 'typedi';

import SUBSCRIPTIONS_DOCS from '../../../docs/swagger/subscriptions-api';
Expand All @@ -14,7 +15,6 @@ import { AppleIAP } from '../../payments/iap/apple-app-store/apple-iap';
import { PurchaseUpdateError } from '../../payments/iap/apple-app-store/types/errors';
import { IAPConfig } from '../../payments/iap/iap-config';
import { AuthLogger, AuthRequest } from '../../types';
import { handleAuthScoped } from './utils';

export class AppleIapHandler {
private log: AuthLogger;
Expand Down Expand Up @@ -94,9 +94,18 @@ export class AppleIapHandler {
*/
public async registerOriginalTransactionId(request: AuthRequest) {
this.log.begin('appleIap.registerOriginalTransactionId', request);
const { uid } = handleAuthScoped(request.auth, [
const { auth } = request;
const scopes = [
OAUTH_SCOPE_SUBSCRIPTIONS_IAP,
]);
// FIXME: Remove this scope and use `handleAuthScoped` instead of below logic
// once VPN migration is complete (FXA-5848).
'profile:subscriptions',
];
const scope = ScopeSet.fromArray(auth.credentials.scope);
if (!scopes.some((requiredScope) => scope.contains(requiredScope))) {
throw error.invalidScopes();
}
const { user: uid } = auth.credentials;

const { appName } = request.params;
const { originalTransactionId } = request.payload as any;
Expand All @@ -110,7 +119,7 @@ export class AppleIapHandler {
purchase = await this.appStore.purchaseManager.registerToUserAccount(
bundleId,
originalTransactionId,
uid
uid as string
);
} catch (err) {
switch (err.name) {
Expand All @@ -128,7 +137,7 @@ export class AppleIapHandler {
);
}
}
await this.capabilityService.iapUpdate(uid, purchase);
await this.capabilityService.iapUpdate(uid as string, purchase);
return { transactionIdValid: true };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ describe('AppleIapHandler', () => {
assert.deepEqual(result, { transactionIdValid: true });
});

it('accepts a "profile" scope for auth', async () => {
request.auth.credentials.scope = ['profile'];
appleIap.purchaseManager = {
registerToUserAccount: sinon.fake.resolves({}),
};
iapConfig.getBundleId = sinon.fake.resolves('testPackage');
await appleIapHandler.registerOriginalTransactionId(request);
});

it('throws on invalid package', async () => {
appleIap.purchaseManager = {
registerToUserAccount: sinon.fake.resolves({}),
Expand Down

0 comments on commit 3d55231

Please sign in to comment.