-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
84 lines (77 loc) · 2.97 KB
/
example.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
const crypto = require('crypto');
const axios = require('axios');
const util = require('util')
const yaml = require('js-yaml')
const apiClient = (function(){
const userId = process.env.SophtronApiUserId;
const accessKey = process.env.SophtronApiUserSecret;
const apiBaseUrl = 'https://api.sophtron-prod.com/api/';
const apiEndpoints = {
GetInstitutionByName: 'Institution/GetInstitutionByName',
GetUserInstitutionsByUser: 'UserInstitution/GetUserInstitutionsByUser',
GetUserIntegrationKey: 'User/GetUserIntegrationKey',
GetJobInformationByID: 'Job/GetJobInformationByID'
}
// console.log(userId);
// console.log(accessKey);
function buildAuthCode(httpMethod, url) {
var authPath = url.substring(url.lastIndexOf('/')).toLowerCase();
var integrationKey = Buffer.from(accessKey, 'base64');
var plainKey = httpMethod.toUpperCase() + '\n' + authPath;
var b64Sig = crypto.createHmac('sha256', integrationKey).update(plainKey).digest("base64");
var authString = 'FIApiAUTH:' + userId + ':' + b64Sig + ':' + authPath;
return authString;
}
function post(url, data){
let conf = {headers: {Authorization: buildAuthCode('post', url)}};
return axios.post(apiBaseUrl + url, data, conf)
.then(res => {
//console.log('response from ' + url)
//console.log(res.data);
return res.data
})
.catch(error => {
//console.log('error from ' + url);
//console.log(error.message);
});
}
function getIngrationKey(){
return post(apiEndpoints.GetUserIntegrationKey, {Id: userId}).then(res => res.IntegrationKey)
}
function getUserInstitutionsByUser(){
return post(apiEndpoints.GetUserInstitutionsByUser, {UserID: userId});
}
return {
getIngrationKey,
getUserInstitutionsByUser,
}
})();
(async function(){
let integrationKey = await apiClient.getIngrationKey();
// need a UserInstitutionID to demo with. get a random one from the available list
let connections = await apiClient.getUserInstitutionsByUser();
//console.log(connections)
if(connections && connections.length > 0){
let connection = connections.find(c => c.OwnerName) || connections[0]
//connection = connections[0]
let vc = await axios.post(
'https://vc.sophtron-prod.com/api/vc/transactions/' + connection.UserInstitutionID,
{accountId:'acdc569e-365f-4aa9-9de2-004adf0f0770'},
{
headers: {
IntegrationKey: integrationKey
}
})
.then(res => res.data)
.catch(error => {
console.log(error.message);
if(error.response){
console.log(error.response.data)
}
})
console.log(yaml.dump(vc.credentialSubject))
//console.log(util.inspect(vc, {showHidden: false, depth: 4, colors: true}))
}else{
//please create a UserInstitution for demo
}
}())