SPEKE Sample Code
This is a sample code to acquire encryption keys from Axinom DRM Key Service using SPEKE v1 protocol.
warning
See SPEKE v2 Sample Code for the latest version of the protocol.
Setup​
The following CPIX request will be used in all samples below.
const requestCPIX = `
<?xml version="1.0"?>
<cpix:CPIX xmlns:cpix="urn:dashif:org:cpix" xmlns:pskc="urn:ietf:params:xml:ns:keyprov:pskc" id="test_ch1">
<cpix:ContentKeyList>
<cpix:ContentKey kid="af1ed63c-5784-460b-9e51-309dd47b7d9c"/>
</cpix:ContentKeyList>
<cpix:DRMSystemList>
<cpix:DRMSystem systemId="94ce86fb-07ff-4f43-adb8-93d2fa968ca2" kid="af1ed63c-5784-460b-9e51-309dd47b7d9c"/>
<cpix:DRMSystem systemId="9a04f079-9840-4286-ab92-e65be0885f95" kid="af1ed63c-5784-460b-9e51-309dd47b7d9c"/>
<cpix:DRMSystem systemId="edef8ba9-79d6-4ace-a3c8-27dcd51d21ed" kid="af1ed63c-5784-460b-9e51-309dd47b7d9c"/>
</cpix:DRMSystemList>
<cpix:ContentKeyPeriodList>
<cpix:ContentKeyPeriod id="keyPeriod_19d21813-874e-4804-8a56-1952722abeb5" index="213"/>
</cpix:ContentKeyPeriodList>
<cpix:ContentKeyUsageRuleList>
<cpix:ContentKeyUsageRule kid="af1ed63c-5784-460b-9e51-309dd47b7d9c">
<cpix:KeyPeriodFilter periodId="keyPeriod_19d21813-874e-4804-8a56-1952722abeb5"/>
</cpix:ContentKeyUsageRule>
</cpix:ContentKeyUsageRuleList>
</cpix:CPIX>
`;
JavaScript / NodeJS​
This example uses superagent to send HTTP requests.
const superagent = require('superagent');
async function invokeSpeke1() {
const responseCPIX = await executeSpeke(requestCPIX, 1);
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();
}