61 lines
2.2 KiB
JavaScript
61 lines
2.2 KiB
JavaScript
function register() {
|
|
fetch(url_prefix + '/api/register/begin', {
|
|
method: 'POST',
|
|
}).then(function(response) {
|
|
if(!response.ok) { throw new Error('Error getting registration data!'); }
|
|
return response.arrayBuffer();
|
|
}).then(CBOR.decode).then(function(options) {
|
|
return navigator.credentials.create(options);
|
|
}).then(function(attestation) {
|
|
return fetch(url_prefix + '/api/register/complete', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/cbor'},
|
|
body: CBOR.encode({
|
|
"attestationObject": new Uint8Array(attestation.response.attestationObject),
|
|
"clientDataJSON": new Uint8Array(attestation.response.clientDataJSON),
|
|
"security_key_nick": document.querySelector('#security_key_nick').value,
|
|
})
|
|
});
|
|
}).then(function(response) {
|
|
return response.json();
|
|
}).then(function(json) {
|
|
console.log(json);
|
|
if (!json.ok) { throw new Error('HTTP error, status = ' + json.status + ', message = ' + json.message); }
|
|
window.location = url_prefix + '/login';
|
|
});
|
|
}
|
|
|
|
function login() {
|
|
fetch(url_prefix + '/api/authenticate/begin', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/cbor'},
|
|
body: CBOR.encode({
|
|
"username": document.querySelector('#username').value,
|
|
})
|
|
}).then(function(response) {
|
|
if(!response.ok) { throw new Error('Error getting registration data!'); }
|
|
return response.arrayBuffer();
|
|
}).then(CBOR.decode).then(function(options) {
|
|
return navigator.credentials.get(options);
|
|
}).then(function(assertion) {
|
|
return fetch(url_prefix + '/api/authenticate/complete', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/cbor'},
|
|
body: CBOR.encode({
|
|
"credentialId": new Uint8Array(assertion.rawId),
|
|
"authenticatorData": new Uint8Array(assertion.response.authenticatorData),
|
|
"clientDataJSON": new Uint8Array(assertion.response.clientDataJSON),
|
|
"signature": new Uint8Array(assertion.response.signature)
|
|
})
|
|
});
|
|
}, function(reason) {
|
|
console.log('navigator.credentials.get() failed for the following reason: ' + reason);
|
|
}).then(function(response) {
|
|
return response.json();
|
|
}).then(function(json) {
|
|
console.log(json);
|
|
if (!json.ok) { throw new Error('HTTP error, status = ' + json.status + ', message = ' + json.message); }
|
|
window.location = url_prefix + '/';
|
|
});
|
|
}
|