Skip to content

Commit f5e0a08

Browse files
author
Christian Wick
committed
Added support for X-Auka-Integrator
1 parent f6e3f88 commit f5e0a08

2 files changed

Lines changed: 147 additions & 70 deletions

File tree

index.html

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
animation: pulse 0.5s 1;
2424
}
2525

26+
.pure-form-aligned .pure-control-group label {
27+
width: 11em;
28+
}
29+
2630
@keyframes pulse {
2731
0% {
2832
background-color: rgb(230, 255, 230);
@@ -40,17 +44,38 @@ <h1 id="test">Settle API PKCS#1 1.5 signature generator</h1>
4044
<div class="pure-form pure-form-aligned">
4145

4246
<div class="pure-control-group">
43-
<label for="merchant_id">Merchant ID</label>
47+
<label for="merchant_id">X-Auka-Merchant</label>
4448
<input type="text" name="merchant_id" id="merchant_id"></input>
4549
</div>
4650

47-
<div class="pure-control-group">
48-
<label for="api_user">API key ID</label>
51+
<p>Is an Integrator acting as a proxy on behalf of a Merchant client?</p>
52+
<div class="pure-u-1-2">
53+
<div class="pure-u-1-8">
54+
<label for="is-integrator-false" class="pure-radio">
55+
<input type="radio" id="is-integrator-false" name="optionsRadios" value="integratorFalse" checked="" onclick="integratorFalse();" /> No
56+
</label>
57+
</fieldset>
58+
</div>
59+
<div class="pure-u-1-8">
60+
<label for="is-integrator-true" class="pure-radio">
61+
<input type="radio" id="is-integrator-true" name="optionsRadios" value="integratorTrue" onclick="integratorTrue();" /> Yes
62+
</label>
63+
</fieldset>
64+
</div>
65+
</div>
66+
67+
<div class="pure-control-group" id="controllGroupApiUser">
68+
<label for="api_user">X-Auka-User</label>
4969
<input type="text" name="api_user" id="api_user"></input>
5070
</div>
5171

72+
<div class="pure-control-group integrator" id="controllGroupIntegrator">
73+
<label for="X_Auka_Integrator">X-Auka-Integrator</label>
74+
<input type="text" name="X_Auka_Integrator" id="X_Auka_Integrator"></input>
75+
</div>
76+
5277
<div class="pure-control-group">
53-
<label for="private_key">Private key</label>
78+
<label for="private_key">Private RSA key</label>
5479
<textarea name="private_key" id="private_key" cols="70" rows="15"></textarea>
5580
</div>
5681

index.js

Lines changed: 118 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
// dec2hex :: Integer -> String
22
// i.e. 0-255 -> '00'-'ff'
33
function dec2hex(dec) {
4-
return ('0' + dec.toString(16)).substr(-2)
4+
return ("0" + dec.toString(16)).substr(-2);
55
}
66

77
// generateId :: Integer -> String
88
function generateId(len) {
9-
var arr = new Uint8Array((len || 10) / 2)
10-
window.crypto.getRandomValues(arr)
11-
return `"${Array.from(arr, dec2hex).join('')}"`
9+
var arr = new Uint8Array((len || 10) / 2);
10+
window.crypto.getRandomValues(arr);
11+
return `"${Array.from(arr, dec2hex).join("")}"`;
1212
}
1313

1414
// getAukaTimestamp :: Void -> String
1515
function getAukaTimestamp() {
1616
const d = new Date();
17-
const fYear = d.getUTCFullYear().toString()
18-
const month = (d.getUTCMonth()+1).toString()
19-
const day = d.getUTCDate().toString()
20-
const hours = d.getUTCHours().toString()
21-
const minutes = (parseInt(d.getUTCMinutes())).toString()
22-
const seconds = (parseInt(d.getUTCSeconds())).toString()
23-
24-
const fMonth = month.length == 2 ? month : `0${month}`
25-
const fDay = day.length == 2 ? day : `0${day}`
26-
const fHours = hours.length == 2 ? hours: `0${hours}`
27-
const fMinutes = minutes.length == 2 ? minutes: `0${minutes}`
28-
const fSeconds = seconds.length == 2 ? seconds: `0${seconds}`
17+
const fYear = d.getUTCFullYear().toString();
18+
const month = (d.getUTCMonth() + 1).toString();
19+
const day = d.getUTCDate().toString();
20+
const hours = d.getUTCHours().toString();
21+
const minutes = parseInt(d.getUTCMinutes()).toString();
22+
const seconds = parseInt(d.getUTCSeconds()).toString();
23+
24+
const fMonth = month.length == 2 ? month : `0${month}`;
25+
const fDay = day.length == 2 ? day : `0${day}`;
26+
const fHours = hours.length == 2 ? hours : `0${hours}`;
27+
const fMinutes = minutes.length == 2 ? minutes : `0${minutes}`;
28+
const fSeconds = seconds.length == 2 ? seconds : `0${seconds}`;
2929

3030
return `${fYear}-${fMonth}-${fDay} ${fHours}:${fMinutes}:${fSeconds}`;
3131
}
3232

3333
// generatePubKey :: String -> Void
3434
function generatePubKey(privateKey) {
35-
console.log('Generating new public key from private key')
35+
console.log("Generating new public key from private key");
3636
const crypt = new JSEncrypt();
3737
crypt.setKey(privateKey);
38-
document.getElementById('public_key').value = crypt.getPublicKey()
38+
document.getElementById("public_key").value = crypt.getPublicKey();
3939
}
4040

4141
// getHash :: String -> String
4242
function getHash(body) {
43-
return CryptoJS.SHA256(body).toString(CryptoJS.enc.Base64)
43+
return CryptoJS.SHA256(body).toString(CryptoJS.enc.Base64);
4444
}
4545

4646
// getSignedMessage :: String -> String
@@ -59,69 +59,121 @@ function triggerAnimation(element) {
5959

6060
// copyToClipboard :: Object -> Void
6161
function copyToClipboard(event) {
62-
const element = event.target
63-
void triggerAnimation(element)
64-
console.log(`Copying contents of ${element.name} to clipboard`)
65-
element.select()
66-
document.execCommand('copy')
67-
window.getSelection().removeAllRanges()
62+
const element = event.target;
63+
void triggerAnimation(element);
64+
console.log(`Copying contents of ${element.name} to clipboard`);
65+
element.select();
66+
document.execCommand("copy");
67+
window.getSelection().removeAllRanges();
6868
}
6969

70+
let isIntegrator = false;
71+
7072
// setDefaultValues :: Void
7173
function setDefaultValues() {
72-
const method = ''
73-
const url = ''
74-
const merchantID = ''
75-
const apiUser = ''
76-
const privateKey = ''
77-
const body = ''
78-
79-
document.getElementById('private_key').value = privateKey
80-
document.getElementById('method').value = method
81-
document.getElementById('url').value = url
82-
document.getElementById('body').value = body
83-
document.getElementById('merchant_id').value = merchantID
84-
document.getElementById('api_user').value = apiUser
74+
const method = "";
75+
const url = "";
76+
const merchantID = "";
77+
const apiUser = "";
78+
const integratorID = "";
79+
const privateKey = "";
80+
const body = "";
81+
82+
document.getElementById("private_key").value = privateKey;
83+
document.getElementById("method").value = method;
84+
document.getElementById("url").value = url;
85+
document.getElementById("body").value = body;
86+
document.getElementById("merchant_id").value = merchantID;
87+
document.getElementById("api_user").value = apiUser;
88+
document.getElementById("X_Auka_Integrator").value = integratorID;
89+
}
90+
91+
document.getElementById("controllGroupIntegrator").style.display = "none";
92+
93+
function integratorFalse() {
94+
document.getElementById("controllGroupApiUser").style.display = "block";
95+
document.getElementById("controllGroupIntegrator").style.display = "none";
96+
isIntegrator = false;
97+
// console.log(isIntegrator);
98+
}
99+
100+
function integratorTrue() {
101+
document.getElementById("controllGroupApiUser").style.display = "none";
102+
document.getElementById("controllGroupIntegrator").style.display = "block";
103+
isIntegrator = true;
104+
// console.log(isIntegrator);
85105
}
86106

87107
// generateHeaders :: Void
88108
function generateHeaders() {
89-
console.log('Generating hedears')
90-
const element = document.getElementById('bulk_headers');
91-
void triggerAnimation(element)
92-
93-
const privateKey = document.getElementById('private_key').value
94-
const method = document.getElementById('method').value
95-
const url = document.getElementById('url').value
96-
const merchantID = document.getElementById('merchant_id').value
97-
const timestamp = getAukaTimestamp()
98-
const apiUser = document.getElementById('api_user').value
99-
const body = document.getElementById('body').value
100-
const bodyHash = getHash(body)
101-
const contentDigest = `SHA256=${bodyHash}`
102-
const headers = `X-AUKA-CONTENT-DIGEST=${contentDigest}&X-AUKA-MERCHANT=${merchantID}&X-AUKA-TIMESTAMP=${timestamp}&X-AUKA-USER=${apiUser}`
103-
const message = `${method}|${url}|${headers}`
104-
const signature = getSignedMessage(privateKey, message)
105-
const authorization = 'RSA-SHA256 ' + signature
106-
const bulkHeaders =
107-
`Accept:application/vnd.mcash.api.merchant.v1+json
109+
// console.log("Generating hedears");
110+
const element = document.getElementById("bulk_headers");
111+
void triggerAnimation(element);
112+
113+
if (isIntegrator == false) {
114+
console.log("Generating hedears with X-Auka-User");
115+
116+
const privateKey = document.getElementById("private_key").value;
117+
const method = document.getElementById("method").value;
118+
const url = document.getElementById("url").value;
119+
const merchantID = document.getElementById("merchant_id").value;
120+
const timestamp = getAukaTimestamp();
121+
const apiUser = document.getElementById("api_user").value;
122+
const body = document.getElementById("body").value;
123+
const bodyHash = getHash(body);
124+
const contentDigest = `SHA256=${bodyHash}`;
125+
const headers = `X-AUKA-CONTENT-DIGEST=${contentDigest}&X-AUKA-MERCHANT=${merchantID}&X-AUKA-TIMESTAMP=${timestamp}&X-AUKA-USER=${apiUser}`;
126+
const message = `${method}|${url}|${headers}`;
127+
const signature = getSignedMessage(privateKey, message);
128+
const authorization = "RSA-SHA256 " + signature;
129+
const bulkHeaders = `Accept:application/vnd.mcash.api.merchant.v1+json
108130
Content-Type:application/json
109131
X-Auka-Merchant:${merchantID}
110132
X-Auka-User:${apiUser}
111133
X-Auka-Timestamp:${timestamp}
112134
X-Auka-Content-Digest:${contentDigest}
113-
Authorization:${authorization}`
135+
Authorization:${authorization}`;
136+
137+
element.value = bulkHeaders;
138+
139+
} else {
140+
console.log("Generating hedears with X-Auka-Integrator");
141+
142+
const privateKey = document.getElementById("private_key").value;
143+
const method = document.getElementById("method").value;
144+
const url = document.getElementById("url").value;
145+
const merchantID = document.getElementById("merchant_id").value;
146+
const timestamp = getAukaTimestamp();
147+
const integratorID = document.getElementById("X_Auka_Integrator").value;
148+
const body = document.getElementById("body").value;
149+
const bodyHash = getHash(body);
150+
const contentDigest = `SHA256=${bodyHash}`;
151+
const headers = `X-AUKA-CONTENT-DIGEST=${contentDigest}&X-AUKA-MERCHANT=${merchantID}&X-AUKA-TIMESTAMP=${timestamp}&X-Auka-Integrator=${integratorID}`;
152+
const message = `${method}|${url}|${headers}`;
153+
const signature = getSignedMessage(privateKey, message);
154+
const authorization = "RSA-SHA256 " + signature;
155+
const bulkHeaders = `Accept:application/vnd.mcash.api.merchant.v1+json
156+
Content-Type:application/json
157+
X-Auka-Merchant:${merchantID}
158+
X-Auka-Integrator:${integratorID}
159+
X-Auka-Timestamp:${timestamp}
160+
X-Auka-Content-Digest:${contentDigest}
161+
Authorization:${authorization}`;
162+
163+
element.value = bulkHeaders;
114164

115-
element.value = bulkHeaders;
165+
}
116166
}
117167

118168
// Main script
119-
document.getElementById('generate').onclick = () => generateHeaders()
120-
document.getElementById('method').onchange = () => generateHeaders()
121-
document.getElementById('url').onchange = () => generateHeaders()
122-
document.getElementById('body').onchange = () => generateHeaders()
123-
document.getElementById('bulk_headers').onclick = event => copyToClipboard(event)
169+
document.getElementById("generate").onclick = () => generateHeaders();
170+
document.getElementById("method").onchange = () => generateHeaders();
171+
document.getElementById("url").onchange = () => generateHeaders();
172+
document.getElementById("body").onchange = () => generateHeaders();
173+
document.getElementById("bulk_headers").onclick = (event) =>
174+
copyToClipboard(event);
124175

176+
setDefaultValues();
177+
generateHeaders();
125178

126-
setDefaultValues()
127-
generateHeaders()
179+
// console.log(isIntegrator);

0 commit comments

Comments
 (0)