Skip to content

Commit

Permalink
fix(auth): check the requiresSubscription value
Browse files Browse the repository at this point in the history
- new functions to check custom params for true / false values
  • Loading branch information
AntonLantukh committed Dec 21, 2022
1 parent 368b140 commit a878ab6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 63 deletions.
2 changes: 1 addition & 1 deletion docs/features/video-protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']),
};
}

Expand Down
61 changes: 2 additions & 59 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/entitlements.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down

0 comments on commit a878ab6

Please sign in to comment.