/* 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 { IPPFxaAuthProviderSingleton } from "moz-src:///toolkit/components/ipprotection/fxa/IPPFxaAuthProvider.sys.mjs"; const lazy = {}; ChromeUtils.defineESModuleGetters(lazy, { IPProtectionService: "moz-src:///toolkit/components/ipprotection/IPProtectionService.sys.mjs", }); /** * FxA implementation of IPPAuthProvider that uses the direct token activation * flow by calling Guardian's POST /api/v1/fpn/activate endpoint with the FxA * Bearer token. */ class IPPFxaActivateAuthProviderSingleton extends IPPFxaAuthProviderSingleton { /** * @param {object} [signInWatcher] - Custom sign-in watcher. Defaults to IPPSignInWatcher. */ constructor(signInWatcher = null) { super( signInWatcher, IPPFxaActivateAuthProviderSingleton.#defaultEnrollAndEntitle ); } /** * @param {AbortSignal} [abortSignal=null] * @returns {Promise<{isEnrolledAndEntitled: boolean, entitlement?: object, error?: string}>} */ static async #defaultEnrollAndEntitle(abortSignal = null) { try { const { ok, entitlement, error } = await lazy.IPProtectionService.guardian.activate(abortSignal); if (!ok) { return { isEnrolledAndEntitled: false, error }; } return { isEnrolledAndEntitled: true, entitlement }; } catch (error) { return { isEnrolledAndEntitled: false, error: error?.message }; } } /** * @returns {Promise<{entitlement?: object, error?: string}>} */ async getEntitlement() { try { const { status, entitlement, error } = await lazy.IPProtectionService.guardian.fetchUserInfo(); if (error || !entitlement || status != 200) { return { error: error || `Status: ${status}` }; } return { entitlement }; } catch (error) { return { error: error.message }; } } } const IPPFxaActivateAuthProvider = new IPPFxaActivateAuthProviderSingleton(); export { IPPFxaActivateAuthProvider, IPPFxaActivateAuthProviderSingleton };