From a878ab6ca48e8652777da4fc31bb8a5adb5c25ac Mon Sep 17 00:00:00 2001 From: Anton Lantukh Date: Wed, 21 Dec 2022 15:07:11 +0100 Subject: [PATCH] fix(auth): check the requiresSubscription value - new functions to check custom params for true / false values --- docs/features/video-protection.md | 2 +- src/services/config.service.ts | 3 +- src/utils/common.ts | 61 +------------------------------ src/utils/entitlements.ts | 4 +- 4 files changed, 7 insertions(+), 63 deletions(-) diff --git a/docs/features/video-protection.md b/docs/features/video-protection.md index 35086752e..7193e47f9 100644 --- a/docs/features/video-protection.md +++ b/docs/features/video-protection.md @@ -93,7 +93,7 @@ Note that there are many variations of these access models. ### Free content -It's possible to have free content. This is indicated with media parameter `free` (`requiresSubscription` param is deprecated). As a possible value for `free` param you can use 'true', 'yes' (lower- or uppercase) or any positive number. +It's possible to have free content. This is indicated with media parameter `free` (`requiresSubscription` param is deprecated, accepted values are 'false', 'no' and '0'). As a possible value for `free` param you can use 'true', 'yes' (lower- or uppercase) or '1' number. ### Users and entitlements diff --git a/src/services/config.service.ts b/src/services/config.service.ts index 5c781a75a..3d75c106d 100644 --- a/src/services/config.service.ts +++ b/src/services/config.service.ts @@ -2,6 +2,7 @@ import { array, boolean, mixed, number, object, SchemaOf, string, StringSchema } import i18next from 'i18next'; import type { Cleeng, InPlayer, Config, Content, Features, Menu, Styling } from '#types/Config'; +import { isTruthyCustomParamValue } from '#src/utils/common'; /** * Set config setup changes in both config.services.ts and config.d.ts @@ -115,7 +116,7 @@ const enrichConfig = (config: Config): Config => { config.integrations.inplayer = { clientId: config.custom?.['inplayer.clientId'] as string, assetId: Number(config.custom?.['inplayer.assetId']), - useSandbox: ['true', '1', 'yes'].indexOf((config.custom?.['inplayer.useSandbox'] as string)?.toLowerCase()) >= 0, + useSandbox: isTruthyCustomParamValue(config.custom?.['inplayer.useSandbox']), }; } diff --git a/src/utils/common.ts b/src/utils/common.ts index f2c844b22..9c3f02a49 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -83,66 +83,9 @@ export function getOverrideIP() { .trim(); } -/** - * Here we check that the value in the custom param can be considered to be True: - * 1. Boolean true value - * 2. Any number !== 0 - * 3. 'yes' or 'true' string in lower / upper case - */ -export const hasTrueValue = (value: undefined | null | string | number | boolean): boolean => { - // null, undefined or empty string - if (!value) { - return false; - } - - if (typeof value === 'boolean') { - return value; - } - - // 0 equals to false, other numbers equal to true - if (typeof value === 'number') { - return Boolean(value); - } - - if (!isNaN(Number(value))) { - return Number(value) > 0; - } - - const strValue = value.toLowerCase(); - - return strValue === 'true' || strValue === 'yes'; -}; - -/** - * @deprecated We should use true and hasTrueValue check for new properties - * Here we check that the value in the custom param can be considered to be False: - * 1. Boolean false value - * 2. Number === 0 - * 3. 'no' or 'false' string in lower / upper case - */ -export const hasFalseValue = (value: undefined | null | string | number | boolean): boolean => { - // null, undefined or empty string - if (!value) { - return false; - } - - if (typeof value === 'boolean') { - return !value; - } - - // 0 equals to false, other numbers equal to true - if (typeof value === 'number') { - return !value; - } - - if (!isNaN(Number(value))) { - return Number(value) === 0; - } - - const strValue = value.toLowerCase(); +export const isTruthyCustomParamValue = (value: unknown): boolean => ['true', '1', 'yes'].includes(String(value)?.toLowerCase()); - return strValue === 'false' || strValue === 'no'; -}; +export const isFalsyCustomParamValue = (value: unknown): boolean => ['false', '0', 'no'].includes(String(value)?.toLowerCase()); export function testId(value: string | undefined) { return IS_DEVELOPMENT_BUILD || IS_TEST_MODE || IS_PREVIEW_MODE ? value : undefined; diff --git a/src/utils/entitlements.ts b/src/utils/entitlements.ts index 69bc1b273..1b9d7a18e 100644 --- a/src/utils/entitlements.ts +++ b/src/utils/entitlements.ts @@ -1,7 +1,7 @@ import type { AccessModel } from '#types/Config'; import type { MediaOffer } from '#types/media'; import type { PlaylistItem } from '#types/playlist'; -import { hasFalseValue, hasTrueValue } from '#src/utils/common'; +import { isTruthyCustomParamValue, isFalsyCustomParamValue } from '#src/utils/common'; /** * The appearance of the lock icon, depending on the access model @@ -13,7 +13,7 @@ import { hasFalseValue, hasTrueValue } from '#src/utils/common'; * @returns */ export const isLocked = (accessModel: AccessModel, isLoggedIn: boolean, hasSubscription: boolean, playlistItem: PlaylistItem): boolean => { - const isItemFree = hasFalseValue(playlistItem?.requiresSubscription) || hasTrueValue(playlistItem?.free); + const isItemFree = isFalsyCustomParamValue(playlistItem?.requiresSubscription) || isTruthyCustomParamValue(playlistItem?.free); const mediaOffers = playlistItem?.mediaOffers; if (isItemFree) return false;