Skip to content

Commit

Permalink
feat(project): jwp signging service
Browse files Browse the repository at this point in the history
  • Loading branch information
kiremitrov123 committed Mar 14, 2023
1 parent 88f88a9 commit 9b1030d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 49 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"deploy:github": "node ./scripts/deploy-github.js"
},
"dependencies": {
"@inplayer-org/inplayer.js": "^3.13.5",
"@inplayer-org/inplayer.js": "^3.13.7",
"classnames": "^2.3.1",
"date-fns": "^2.28.0",
"dompurify": "^2.3.8",
Expand Down Expand Up @@ -142,4 +142,4 @@
"flat": "^5.0.1",
"json5": "^2.2.2"
}
}
}
24 changes: 15 additions & 9 deletions src/hooks/usePlaylistItemCallback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMediaToken } from '#src/services/entitlement.service';
import { getMediaSignedToken, getMediaToken } from '#src/services/entitlement.service';
import { useAccountStore } from '#src/stores/AccountStore';
import { useConfigStore } from '#src/stores/ConfigStore';
import { getMediaById } from '#src/services/api.service';
Expand All @@ -8,7 +8,7 @@ import { addQueryParams } from '#src/utils/formatting';

export const usePlaylistItemCallback = (startDateTime?: string | null, endDateTime?: string | null) => {
const auth = useAccountStore(({ auth }) => auth);
const signingConfig = useConfigStore((state) => state.config?.contentSigningService);
const config = useConfigStore((state) => state.config);

const applyLiveStreamOffset = (item: PlaylistItem) => {
if (!startDateTime) return item;
Expand All @@ -31,15 +31,21 @@ export const usePlaylistItemCallback = (startDateTime?: string | null, endDateTi

return useEventCallback(async (item: PlaylistItem) => {
const jwt = auth?.jwt;
const host = signingConfig?.host;
const drmPolicyId = signingConfig?.drmPolicyId;
const host = config?.contentSigningService?.host;
const drmPolicyId = config?.contentProtection?.drm?.defaultPolicyId ?? config?.contentSigningService?.drmPolicyId;
const urlSigning = config?.custom?.urlSigning; // custom param whether we want to use url signing
const signingEnabled = !!host;

if (!signingEnabled) return applyLiveStreamOffset(item);

// if signing is enabled, we need to sign the media item first. Assuming that the media item given to the player
// isn't signed.
const token = await getMediaToken(host, item.mediaid, jwt, {}, drmPolicyId);
let token = '';
if (urlSigning) {
// if drmPolicyId is present in config, it is being handled on backend before retreiving the token
token = await getMediaSignedToken(config?.id, item.mediaid);
} else {
if (!signingEnabled) return applyLiveStreamOffset(item);
// if signing is enabled, we need to sign the media item first. Assuming that the media item given to the player
// isn't signed.
token = await getMediaToken(host, item.mediaid, jwt, {}, drmPolicyId);
}

const signedMediaItem = await getMediaById(item.mediaid, token, drmPolicyId);

Expand Down
8 changes: 8 additions & 0 deletions src/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ const configSchema: SchemaOf<Config> = object({
host: string().required() as StringSchema<string>,
drmPolicyId: string().notRequired(),
}),
contentProtection: object()
.shape({
accessModel: string().oneOf(['free', 'authfree', 'authvod', 'svod']).notRequired(),
drm: object({
defaultPolicyId: string(),
}).notRequired(),
})
.notRequired(),
}).defined();

const loadConfig = async (configLocation: string) => {
Expand Down
11 changes: 11 additions & 0 deletions src/services/entitlement.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import InPlayer from '@inplayer-org/inplayer.js';

import type { GetMediaParams } from '#types/media';
import type { GetPlaylistParams } from '#types/playlist';

Expand All @@ -21,6 +23,15 @@ export const getMediaToken = async (host: string, id: string, jwt?: string, para
return data.token;
};

export const getMediaSignedToken = async (configId: string = '', mediaId: string) => {
try {
const { data } = await InPlayer.Asset.getSignedMediaToken(configId, mediaId);
return data.token;
} catch {
throw new Error('Unauthorized');
}
};

export const getPublicToken = async (
host: string,
type: EntitlementType,
Expand Down
7 changes: 4 additions & 3 deletions src/utils/configLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const calculateAccessModel = (config: Config): AccessModel => {
};

export async function loadAndValidateConfig(configSource: string | undefined) {
configSource = formatSourceLocation(configSource);
const configLocation = formatSourceLocation(configSource);

// Explicitly set default config here as a local variable,
// otherwise if it's a module level const, the merge below causes changes to nested properties
Expand All @@ -69,12 +69,13 @@ export async function loadAndValidateConfig(configSource: string | undefined) {
features: {},
};

if (!configSource) {
if (!configLocation) {
useConfigStore.setState({ config: defaultConfig });
throw new Error('Config not defined');
}

let config = await loadConfig(configSource);
let config = await loadConfig(configLocation);
config.id = configSource;
config.assets = config.assets || {};

// make sure the banner always defaults to the JWP banner when not defined in the config
Expand Down
10 changes: 10 additions & 0 deletions types/Config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@ export type Config = {
features?: Features;
custom?: Record<string, unknown>;
contentSigningService?: ContentSigningConfig;
contentProtection?: ContentProtection;
};

export type ContentSigningConfig = {
host: string;
drmPolicyId?: string;
};

export type ContentProtection = {
accessModel: 'free' | 'authfree' | 'authvod' | 'svod';
drm?: Drm;
};

export type Drm = {
defaultPolicyId: string;
};

export type ContentType = 'playlist' | 'continue_watching' | 'favorites';

export type Content = {
Expand Down
63 changes: 28 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1557,13 +1557,13 @@
resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45"
integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==

"@inplayer-org/inplayer.js@^3.13.5":
version "3.13.5"
resolved "https://registry.yarnpkg.com/@inplayer-org/inplayer.js/-/inplayer.js-3.13.5.tgz#b180cbbe72b92dd4bed02412a6256ceb8f0ac25f"
integrity sha512-Aa9TplN0MhdjayHSUsEM/4i6A8W7ow6NzdSufr9xn1wzy0kgmH+jFDq++eFArFn0fZ66zfDHAl0H9dS8RLQUgA==
"@inplayer-org/inplayer.js@^3.13.7":
version "3.13.7"
resolved "https://registry.yarnpkg.com/@inplayer-org/inplayer.js/-/inplayer.js-3.13.7.tgz#8a3ebb52ffc90622c6be7618d9d767c4c7125167"
integrity sha512-7xx1/HDd2/UTZGW7MWoqdgbgkVA63iSJDecAn2Mho5JpdD35OfQMT514otNKkw0nNbDU5/Lj0cTnEsfN1a1QxQ==
dependencies:
aws-iot-device-sdk "^2.2.6"
axios "^0.19.2"
axios "^0.21.2"
core-js "^3.8.2"
fingerprintjs2 "^2.1.2"
lodash "^4.17.20"
Expand Down Expand Up @@ -2667,14 +2667,7 @@ axe-core@4.4.1:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.1.tgz#7dbdc25989298f9ad006645cd396782443757413"
integrity sha512-gd1kmb21kwNuWr6BQz8fv6GNECPBnUasepcoLbekws23NVBLODdsClRZ+bQ8+9Uomf3Sm3+Vwn0oYG9NvwnJCw==

axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
dependencies:
follow-redirects "1.5.10"

axios@^0.21.4:
axios@^0.21.2, axios@^0.21.4:
version "0.21.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
Expand Down Expand Up @@ -3432,9 +3425,9 @@ core-js@^3.19.2:
integrity sha512-1t+2a/d2lppW1gkLXx3pKPVGbBdxXAkqztvWb1EJ8oF8O2gIGiytzflNiFEehYwVK/t2ryUsGBoOFFvNx95mbg==

core-js@^3.8.2:
version "3.27.2"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.27.2.tgz#85b35453a424abdcacb97474797815f4d62ebbf7"
integrity sha512-9ashVQskuh5AZEZ1JdQWp1GqSoC1e1G87MzRqg2gIfVAQ7Qn9K+uFj8EcniUFA4P2NLZfV+TOlX1SzoKfo+s7w==
version "3.29.0"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.29.0.tgz#0273e142b67761058bcde5615c503c7406b572d6"
integrity sha512-VG23vuEisJNkGl6XQmFJd3rEG/so/CNatqeE+7uZAwTSwFeB/qaO0be8xZYUNWprJ/GIwL8aMt9cj1kvbpTZhg==

core-util-is@~1.0.0:
version "1.0.3"
Expand Down Expand Up @@ -3674,13 +3667,6 @@ debug@4.3.4, debug@^4.3.2, debug@^4.3.4:
dependencies:
ms "2.1.2"

debug@=3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

debug@^2.2.0, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -4626,13 +4612,6 @@ fn-args@^4.0.0:
resolved "https://registry.yarnpkg.com/fn-args/-/fn-args-4.0.0.tgz#2e912f7a74c5e9ef25060bd60127d6a8680237a5"
integrity sha512-M9XSagc92ejQhi+7kjpFPAO59xKbGRsbOg/9dfwSj84DfzB0pj+Q81DVD1pKr084Xf2oICwUNI0pCvGORmD9zg==

follow-redirects@1.5.10:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"

follow-redirects@^1.14.0:
version "1.14.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
Expand Down Expand Up @@ -6479,7 +6458,12 @@ minimist@1.2.6:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==

minimist@^1.1.0, minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6:
minimist@^1.1.0:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6:
version "1.2.7"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.7.tgz#daa1c4d91f507390437c6a8bc01078e7000c4d18"
integrity sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==
Expand Down Expand Up @@ -7523,9 +7507,9 @@ qj@~2.0.0:
integrity sha512-8466vlnAF/piI42tzMBUfhaAWn2yBNPOLSSbA2YBlEh+S8CxBXbAO1AwuDReGKYX6LlsK19wBL9cpXZGlgsXxA==

qs@^6.9.4:
version "6.11.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.0.tgz#fd0d963446f7a65e1367e01abd85429453f0c37a"
integrity sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==
version "6.11.1"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.1.tgz#6c29dff97f0c0060765911ba65cbc9764186109f"
integrity sha512-0wsrzgTz/kAVIeuxSjnpGC56rzYtr6JT/2BwEvMaPhFIoYa1aGO8LbzuU1R0uUYQkLpWBTOj0l/CLAJB64J6nQ==
dependencies:
side-channel "^1.0.4"

Expand Down Expand Up @@ -7734,7 +7718,7 @@ read-pkg@^5.2.0:
parse-json "^5.0.0"
type-fest "^0.6.0"

readable-stream@3, readable-stream@^3.0.0, readable-stream@^3.0.2, readable-stream@^3.1.1, readable-stream@^3.4.0, readable-stream@^3.6.0:
readable-stream@3, readable-stream@^3.0.0, readable-stream@^3.0.2, readable-stream@^3.1.1, readable-stream@^3.4.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.0.tgz#337bbda3adc0706bd3e024426a286d4b4b2c9198"
integrity sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==
Expand All @@ -7756,6 +7740,15 @@ readable-stream@^2.0.0, readable-stream@^2.0.1, readable-stream@^2.0.5, readable
string_decoder "~1.1.1"
util-deprecate "~1.0.1"

readable-stream@^3.6.0:
version "3.6.2"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.2.tgz#56a9b36ea965c00c5a93ef31eb111a0f11056967"
integrity sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==
dependencies:
inherits "^2.0.3"
string_decoder "^1.1.1"
util-deprecate "^1.0.1"

readdirp@^3.5.0, readdirp@~3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.6.0.tgz#74a370bd857116e245b29cc97340cd431a02a6c7"
Expand Down

0 comments on commit 9b1030d

Please sign in to comment.