-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperLCPServer.js
92 lines (72 loc) · 2.55 KB
/
helperLCPServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Module handles REST calls to LCP Server
require('dotenv').config()
//import axios from "axios";
const axios = require('axios');
const crypto = require('crypto');
require('array.prototype.flatmap').shim()
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
auth: {username: 'elastic', password: process.env.ESPWD},
cloud: {id: process.env.ESID}
})
module.exports = {
//Insert a publication LCP Server
storePublicationAsync: async function(LCPEncryptJSON) {return await module.exports.storePublication(LCPEncryptJSON);},
storePublication: function(LCPEncryptJSON){
return new Promise((resolve, reject) => {
try {
const res = axios.put( process.env.LCP_SERVER_URL + '/contents/' + LCPEncryptJSON['content-id'], LCPEncryptJSON, {
headers: {
'Content-Type': 'application/json'
},
auth: {
username: process.env.LCP_SERVER_USERNAME,
password: process.env.LCP_SERVER_PASSWORD
}
})
.then(res => {
console.log(res)
resolve(res)
})
.catch(err => {
console.log(err)
reject(err)
});
}
catch (error) {
console.log('helperES: ' + error)
}
});
},
generatePublicationLicenseAsync: async function(contentId, licenseJSON) {return await module.exports.generatePublicationLicense(contentId, licenseJSON);},
generatePublicationLicense: function(contentId, licenseJSON){
return new Promise((resolve, reject) => {
try {
let hash = crypto.createHash("sha256")
.update(licenseJSON['encryption']['user_key']['hex_value'])
.digest("hex");
console.log('HASH: ' + hash)
licenseJSON['encryption']['user_key']['hex_value'] = hash
const res = axios.post( process.env.LCP_SERVER_URL + '/contents/' + contentId + '/license', licenseJSON, {
headers: {
'Content-Type': 'application/json'
},
auth: {
username: process.env.LCP_SERVER_USERNAME,
password: process.env.LCP_SERVER_PASSWORD
}
})
.then(res => {
resolve(res)
})
.catch(err => {
console.log(err)
reject(err)
});
}
catch (error) {
console.log('helperES: ' + error)
}
});
},
};