-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
76 lines (74 loc) · 2.55 KB
/
index.html
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
<html>
<head>
<title>SAFE Web App Sample</title>
<script src="./bower_components/jquery/dist/jquery.min.js"></script>
<script src="./bower_components/tweetnacl/nacl-fast.min.js"></script>
<script src="./bower_components/base64-js/base64js.min.js"></script>
<script>
var authToken;
var symmKey;
var symmNonce;
var authorise = function() {
var asymKeys = nacl.box.keyPair();
var asymNonce = nacl.randomBytes(nacl.box.nonceLength);
var authPayload = {
app: {
id: 'com.samp-company.web',
name: 'Safe Web Sample',
vendor: 'Sample-Company',
version: '0.1'
},
publicKey: base64js.fromByteArray(asymKeys.publicKey),
nonce: base64js.fromByteArray(asymNonce),
permissions: []
};
$.ajax({
method: 'POST',
url: 'http://api.safenet/auth',
headers: {
'content-type': 'application/json'
},
data: JSON.stringify(authPayload)
})
.done(function(response) {
var launcherPubKey = base64js.toByteArray(response.publicKey);
authToken = response.token;
var encryptedKey = base64js.toByteArray(response.encryptedKey);
var key = nacl.box.open(encryptedKey, asymNonce, launcherPubKey, asymKeys.secretKey);
symmKey = key.slice(0, nacl.secretbox.keyLength);
symmNonce = key.slice(nacl.secretbox.keyLength);
alert('Obtained Auth token');
})
.fail(function (res) {
alert('Request Failed: \n status: ' + res.status + '\n Msg: ' + res.responseText);
});
};
var getPublicId = function() {
$.ajax({
method: 'GET',
url: 'http://api.safenet/dns',
headers: {
'Authorization': 'Bearer ' + authToken
}
})
.done(function(response) {
var encryptedData = base64js.toByteArray(response);
var publicIds = base64js.fromByteArray(nacl.secretbox.open(encryptedData, symmNonce, symmKey));
publicIds = JSON.parse(atob(publicIds));
alert('ID :: ' + publicIds[0]);
})
.fail(function (res) {
alert('Request Failed: \n status: ' + res.status + '\n Msg: ' + res.responseText);
});
};
</script>
</head>
<body>
<h3>
Click on the buttons to invoke the API
</h3>
<div>
<button onclick="authorise()">Authorise</button>
<button onclick="getPublicId()">Get Public ID</button>
</div>
</html>