40
loading...
This website collects cookies to deliver better user experience
// Generating a new secret
const secret = generateSecret();
// Storing it in the device's secure storage
await SecureStorage.store(secret);
// This is the data, that we want to store
const user = {
platform: "dev.to",
email: "[email protected]",
password: "secret123",
};
// Then we encrypt the data
const encrypted = AES.encrypt(JSON.stringify(user), secret);
// Sending the user to the database
// The safe is an object, where we want to store the item inside
await API.item.create(safe, encrypted);
// And finally, on a remote device, when we want to access the data, we use the secret to decrypt
const decrypted = AES.decrypt(encrypted, secret);
// It would print the original user object ({ platform: "dev.to", username.... })
console.log(decrypted);
Note: this is just a presentation of how things work... you should just consider it as a pseudo-code, instead of an actual working example
Authenticator
and the Authenticated
device.Authenticated
device generates an RSA key pair and sends a signal to the remote server to start the authentication process with the public key.ckqz9n52r000001la810jfjee
) and a secret (e.g.: 11879182178653d376fc6b129d1d315b
).bcrypt
hashed version of the secret in the database and sends back the secret and the id to the Authenticated
device.Authenticated
device, then generates a QR code, that stores only the ID, that it got from the server.Authenticator
device, we scan this QR code. This will send a signal to the remote server about who scanned the QR code (first).Authenticated
device, we will see the username of the user, who scanned this QR code.Authenticator
device, the user presses a Verify button. This will send the encryption keys from that device to the Authenticated
one (as described above) and this will also send a signal, to allow the remote server, to generate an access token and a refresh token for the user.Authenticated
device pings the server, if the state has changed. If the auth process went through successfully, then it will just download all data, that has been sent to this device.Authenticated
device, we use the private RSA key, to decrypt the keys.Authenticated
device:// Generate a public and a private RSA key
const { publicKey, privateKey } = RSA.generate(2048);
// Send this publicKey to the server and receive the id and the secret
const { id, secret } = await API.authentication.start({
publicKey,
});
// Store the private RSA key for future use
await SecureStorage.store("rsa_private_key", privateKey);
// Check the server periodically for response
setInterval(async () => {
const response = await API.authentication.check({
id,
secret,
});
if(response.state === "not_yet_scanned") {
// If noone scanned the code yet do nothing
} else if(response.state === "scanned_but_not_verified") {
// If the code has been scanned, but the code has not been verified just show the username for the user, to be able to verify, that they are allowing the right device in
displayUsername(response.user.username);
} else {
// Finally, if everything has been sent and verified, we can do the real job
for(const exchange of response.keyExchanges) {
EncryptionKey.save(exchange.safeid, RSA.decrypt(exchange.content));
}
AccessToken.save(response.access_token);
RefreshToken.save(response.refresh_token);
}
}, 1000);
Authenticator
device:// Initialize a new QR code scanner instance
const scanner = new QRCodeScanner();
// Add an event listener for scan
scanner.on("scan", async (id) => {
// Tell the remote server, that the QR code has been scanned on this device
const { rsaPublicKey } = await API.authentication.onScan(id);
// Show the username for the user
await waitForVerification();
// Get all the encryption keys
const keys = await EncryptionKeys.getAll();
// Iterate over all keys and encrypt them with the RSA key
const enrypted = [];
for(const key of keys) {
encrypted.push({
safeid: key.safeid,
content: RSA.encrypt(key.content, publicKey),
}
}
// Send the keys to the device
await API.authentication.send({
// The ID, that we scanned with the QR code
id,
encryptedKeys: encrypted,
});
// Show success screen!
showSuccessScreen();
});
// Show the QRCode scanner for the user
scanner.start();
Note: This code is also just serves the purpose of demonstration, implementation may vary from platform to platform
40