Skip to content

Commit

Permalink
fix(auth): check the free value
Browse files Browse the repository at this point in the history
- accept only true (+ case variations) as a set value
  • Loading branch information
“Anton committed Jul 13, 2022
1 parent 17b5b89 commit e4b233c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ const loadConfig = async (configLocation: string) => {
const response = await fetch(configLocation, {
headers: {
Accept: 'application/json',
'Access-Control-Allow-Origin': '*',
},
mode: 'cors',
method: 'GET',
});

Expand Down
30 changes: 30 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,33 @@ export function getOverrideIP() {
?.split('=')[1]
.trim();
}

/**
* True value:
* 1. Boolean true value
* 2. Any number !== 0
* 3. 'yes' or 'true' string
*/
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 (!Number.isNaN(value)) {
return Number(value) > 0;
}

const strValue = value.toLowerCase();

return strValue === 'true' || strValue === 'yes';
};
4 changes: 3 additions & 1 deletion src/utils/entitlements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { AccessModel } from '../../types/Config';
import type { MediaOffer } from '../../types/media';
import type { PlaylistItem } from '../../types/playlist';

import { hasTrueValue } from '#src/utils/common';

/**
* The appearance of the lock icon, depending on the access model
*
Expand All @@ -12,7 +14,7 @@ import type { PlaylistItem } from '../../types/playlist';
* @returns
*/
export const isLocked = (accessModel: AccessModel, isLoggedIn: boolean, hasSubscription: boolean, playlistItem: PlaylistItem): boolean => {
const isItemFree = playlistItem?.requiresSubscription === 'false' || !!playlistItem?.free;
const isItemFree = !hasTrueValue(playlistItem?.requiresSubscription) || hasTrueValue(playlistItem?.free?.toLowerCase());
const mediaOffers = playlistItem?.mediaOffers;

if (isItemFree) return false;
Expand Down

0 comments on commit e4b233c

Please sign in to comment.