-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Notify when corrective action is required * Get EULA status from account API. Remove deprecated cookie functions. * Fix double notification * Use backend API to check and accept EULA * Attempt device auth refresh first * Always set access token as bearer token * More logging for login
- Loading branch information
Showing
25 changed files
with
307 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import axios from 'axios'; | ||
import { Logger } from 'pino'; | ||
import { EULA_AGREEMENTS_ENDPOINT, REQUIRED_EULAS, STORE_HOMEPAGE } from './common/constants.js'; | ||
import logger from './common/logger.js'; | ||
import { getAccountAuth } from './common/device-auths.js'; | ||
import { config } from './common/config/setup.js'; | ||
import { generateLoginRedirect } from './purchase.js'; | ||
import { sendNotification } from './notify.js'; | ||
import { NotificationReason } from './interfaces/notification-reason.js'; | ||
|
||
export interface EulaVersion { | ||
key: string; | ||
version: number; | ||
locale: string; | ||
} | ||
|
||
export interface EulaAgreementResponse { | ||
key: string; | ||
version: number; | ||
revision: number; | ||
title: string; | ||
body: string; | ||
locale: string; | ||
createdTimestamp: string; | ||
lastModifiedTimestamp: string; | ||
status: string; | ||
description: string; | ||
custom: boolean; | ||
url: string; | ||
wasDeclined: boolean; | ||
operatorId: string; | ||
notes: string; | ||
hasResponse: boolean; | ||
} | ||
|
||
export class EulaManager { | ||
private accountId: string; | ||
|
||
private accessToken: string; | ||
|
||
private email: string; | ||
|
||
private L: Logger; | ||
|
||
constructor(email: string) { | ||
this.L = logger.child({ user: email }); | ||
const deviceAuth = getAccountAuth(email); | ||
if (!deviceAuth) throw new Error('Device auth not found'); | ||
this.accountId = deviceAuth.account_id; | ||
this.accessToken = deviceAuth.access_token; | ||
this.email = email; | ||
} | ||
|
||
public async checkEulaStatus(): Promise<void> { | ||
this.L.debug('Checking EULA status'); | ||
const pendingEulas = await this.fetchPendingEulas(); | ||
|
||
if (pendingEulas.length) { | ||
if (config.notifyEula) { | ||
this.L.error('User needs to log in an accept an updated EULA'); | ||
const actionUrl = generateLoginRedirect(STORE_HOMEPAGE); | ||
await sendNotification(this.email, NotificationReason.PRIVACY_POLICY_ACCEPTANCE, actionUrl); | ||
throw new Error(`${this.email} needs to accept an updated EULA`); | ||
} else { | ||
this.L.info({ pendingEulas }, 'Accepting EULAs'); | ||
await this.acceptEulas(pendingEulas); | ||
} | ||
} | ||
} | ||
|
||
private async fetchPendingEulas() { | ||
const eulaStatuses: (EulaVersion | undefined)[] = await Promise.all( | ||
REQUIRED_EULAS.map(async (key) => { | ||
const url = `${EULA_AGREEMENTS_ENDPOINT}/${key}/account/${this.accountId}`; | ||
this.L.trace({ url }, 'Checking EULA status'); | ||
const response = await axios.get<EulaAgreementResponse | undefined>(url, { | ||
headers: { Authorization: `Bearer ${this.accessToken}` }, | ||
}); | ||
if (!response.data) return undefined; | ||
this.L.debug({ key }, 'EULA is not accepted'); | ||
return { | ||
key, | ||
version: response.data.version, | ||
locale: response.data.locale, | ||
}; | ||
}), | ||
); | ||
const pendingEulas = eulaStatuses.filter((eula): eula is EulaVersion => eula !== undefined); | ||
this.L.trace({ pendingEulas }, 'Pending EULAs'); | ||
return pendingEulas; | ||
} | ||
|
||
private async acceptEulas(eulaVersions: EulaVersion[]): Promise<void> { | ||
await Promise.all( | ||
eulaVersions.map(async (eulaVersion) => { | ||
const url = `${EULA_AGREEMENTS_ENDPOINT}/${eulaVersion.key}/version/${eulaVersion.version}/account/${this.accountId}/accept`; | ||
this.L.trace({ url }, 'Accepting EULA'); | ||
await axios.post(url, undefined, { | ||
params: { locale: eulaVersion.locale }, | ||
headers: { Authorization: `Bearer ${this.accessToken}` }, | ||
}); | ||
this.L.debug({ key: eulaVersion.key }, 'EULA accepted'); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
export enum NotificationReason { | ||
LOGIN = 'LOGIN', | ||
PURCHASE = 'PURCHASE', | ||
CREATE_ACCOUNT = 'CREATE_ACCOUNT', | ||
TEST = 'TEST', | ||
PURCHASE_ERROR = 'PURCHASE ERROR', | ||
PRIVACY_POLICY_ACCEPTANCE = 'PRIVACY_POLICY_ACCEPTANCE', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.