-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
64 lines (58 loc) · 1.63 KB
/
client.js
File metadata and controls
64 lines (58 loc) · 1.63 KB
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
// note: this is a base code, this could be far improved
async function GenerateFP() {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.textBaseline = 'top';
ctx.font = '14px Arial';
ctx.fillText(navigator.userAgent, 2, 2);
return btoa(canvas.toDataURL()).slice(-32);
}
async function Authenticate(userId) {
const fingerprint = await GenerateFP();
const res = await fetch('http://localhost:8080/auth', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userId,
fingerprint
})
});
const data = await res.json();
if (!data.ok) throw new Error('Auth failed');
return {
token: data.token,
fingerprint
};
}
async function ValidateToken(token, fingerprint) {
const res = await fetch('http://localhost:8080/validate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token,
fingerprint
})
});
return (await res.json()).ok;
}
// example usage
(async () => {
try {
const {
token,
fingerprint
} = await Authenticate('user123');
console.log('token:', token);
const isValid = await ValidateToken(token, fingerprint);
if (isValid) {
console.log('access granted');
// do something here, like load ur code
}
} catch (err) {
console.error(err);
}
})();