SPEKE Sample Code
This is a sample code to acquire encryption keys from Axinom DRM Key Service using SPEKE v2 protocol.
Setup​
The following CPIX request will be used in all samples below.
const requestCPIX = `
<cpix:CPIX contentId="abc123" version="2.3" xmlns:cpix="urn:dashif:org:cpix" xmlns:pskc="urn:ietf:params:xml:ns:keyprov:pskc">
<cpix:ContentKeyList>
<cpix:ContentKey explicitIV="OFj2IjCsPJFfMAxmQxLGPw==" kid="98ee5596-cd3e-a20d-163a-e382420c6eff" commonEncryptionScheme="cbcs"/>
<cpix:ContentKey explicitIV="L6jzdXrXAFbCJGBuMrrKrG==" kid="53abdba2-f210-43cb-bc90-f18f9a890a02" commonEncryptionScheme="cbcs"/>
</cpix:ContentKeyList>
<cpix:DRMSystemList>
<cpix:DRMSystem kid="98ee5596-cd3e-a20d-163a-e382420c6eff" systemId="94ce86fb-07ff-4f43-adb8-93d2fa968ca2">
<cpix:HLSSignalingData playlist="media"/>
<cpix:HLSSignalingData playlist="master"/>
</cpix:DRMSystem>
<cpix:DRMSystem kid="53abdba2-f210-43cb-bc90-f18f9a890a02" systemId="94ce86fb-07ff-4f43-adb8-93d2fa968ca2">
<cpix:HLSSignalingData playlist="media"/>
<cpix:HLSSignalingData playlist="master"/>
</cpix:DRMSystem>
<cpix:DRMSystem kid="98ee5596-cd3e-a20d-163a-e382420c6eff" systemId="9a04f079-9840-4286-ab92-e65be0885f95">
<cpix:PSSH/>
<cpix:ContentProtectionData/>
<cpix:HLSSignalingData playlist="media"/>
<cpix:HLSSignalingData playlist="master"/>
<cpix:SmoothStreamingProtectionHeaderData/>
</cpix:DRMSystem>
<cpix:DRMSystem kid="53abdba2-f210-43cb-bc90-f18f9a890a02" systemId="9a04f079-9840-4286-ab92-e65be0885f95">
<cpix:PSSH/>
<cpix:ContentProtectionData/>
<cpix:HLSSignalingData playlist="media"/>
<cpix:HLSSignalingData playlist="master"/>
<cpix:SmoothStreamingProtectionHeaderData/>
</cpix:DRMSystem>
<cpix:DRMSystem kid="98ee5596-cd3e-a20d-163a-e382420c6eff" systemId="edef8ba9-79d6-4ace-a3c8-27dcd51d21ed">
<cpix:PSSH/>
<cpix:ContentProtectionData/>
<cpix:HLSSignalingData playlist="media"/>
<cpix:HLSSignalingData playlist="master"/>
</cpix:DRMSystem>
<cpix:DRMSystem kid="53abdba2-f210-43cb-bc90-f18f9a890a02" systemId="edef8ba9-79d6-4ace-a3c8-27dcd51d21ed">
<cpix:PSSH/>
<cpix:ContentProtectionData/>
<cpix:HLSSignalingData playlist="media"/>
<cpix:HLSSignalingData playlist="master"/>
</cpix:DRMSystem>
</cpix:DRMSystemList>
<cpix:ContentKeyUsageRuleList>
<cpix:ContentKeyUsageRule kid="98ee5596-cd3e-a20d-163a-e382420c6eff" intendedTrackType="VIDEO">
<cpix:VideoFilter />
</cpix:ContentKeyUsageRule>
<cpix:ContentKeyUsageRule kid="53abdba2-f210-43cb-bc90-f18f9a890a02" intendedTrackType="AUDIO">
<cpix:AudioFilter />
</cpix:ContentKeyUsageRule>
</cpix:ContentKeyUsageRuleList>
</cpix:CPIX>
`;
JavaScript / NodeJS​
This example uses superagent to send HTTP requests.
const superagent = require('superagent');
async function invokeSpeke() {
const responseCPIX = await executeSpeke(requestCPIX, 2);
console.log(responseCPIX);
}
/**
* Execute a SPEKE request against the Axinom DRM Key Service
* @param requestCPIX - request in CPIX format
* @param version - SPEKE protocol version, can be 1 or 2; default 2
* @returns response in CPIX format
*/
async function executeSpeke(requestCPIX : string, version: number = 2) : Promise<string> {
if(version !== 1 && version !== 2) throw new Error('Invalid version - supported versions are only 1 or 2');
const MOSAIC_KEY_SERVICE_ENDPOINT = 'https://key-server-management.axprod.net/api/Speke' + (version === 2 ? 'V2' : '');
const MOSAIC_KEY_TENANT_ID = ...; // get from Axinom DRM Key Service configuration
const MOSAIC_KEY_MANAGEMENT_KEY = ...; // get from Axinom DRM Key Service configuration
const token = Buffer.from(`${MOSAIC_KEY_TENANT_ID}:${MOSAIC_KEY_MANAGEMENT_KEY}`).toString('base64'); // base64("TenantID:ManagementKey")
const response = await superagent
.post(MOSAIC_KEY_SERVICE_ENDPOINT)
.set('Content-Type', 'text/xml')
.set('Accept', 'application/xml, text/xml')
.set('X-Speke-Version', version.toString() + '.0')
.set('Authorization', `Basic ${token}`)
.send(requestCPIX);
return response.body.toString();
}