Skip to content

Instantly share code, notes, and snippets.

@xlewkanx
Last active November 23, 2018 11:56
Show Gist options
  • Select an option

  • Save xlewkanx/83783d7cf8316c8743551de56fc1c6c5 to your computer and use it in GitHub Desktop.

Select an option

Save xlewkanx/83783d7cf8316c8743551de56fc1c6c5 to your computer and use it in GitHub Desktop.
Dmytro authentications strategies
// Single Device
// User sign up
async function onUserSignUp(user) {
const getToken = () => fetchToken(user.token);
// Initialize e3kit - see full sample in EThree.initialize page
const eThree = await EThree.initialize(getToken);
// Bootstrap user (i.e.create private key if user with this identity doesn't exists yet)
eThree.bootstrap();
// use initialized eThree instance for lookupPublicKeys/encrypt/decrypt and other commands
return eThree;
}
// User sign In
async function onUserSignIn(user) {
const getToken = () => fetchToken(user.token);
// Initialize e3kit - see full sample in EThree.initialize page
const eThree = await EThree.initialize(getToken);
// use initialized eThree instance for lookupPublicKeys/encrypt/decrypt and other commands
return eThree;
}
// Multiple Devices
// User sign up
async function onUserSignUp(user) {
const getToken = () => fetchToken(user.token);
// Initialize e3kit - see full sample in EThree.initialize page
const eThree = await EThree.initialize(getToken);
// Bootstrap user (i.e. create privateKey if user with this identity doesn't exists yet and
// upload encrypted privateKey with password to the Virgil Cloud)
eThree.bootstrap(user.password);
// use initialized eThree instance for lookupPublicKeys/encrypt/decrypt and other commands
return eThree;
}
async function onUserSignIn(user) {
const getToken = () => fetchToken(user.token);
// Initialize e3kit - see full sample in EThree.initialize page
const eThree = await EThree.initialize(getToken);
// Bootstrap user (i.e. load privateKey if user signing in with new device,
// download privateKey and decrypt it with password from the Virgil Cloud)
if (isNewDevice(eThree)) {
eThree.bootstrap(user.password);
}
// use initialized eThree instance for lookupPublicKeys/encrypt/decrypt and other commands
return eThree;
}
// Enable multi device on-demand:
// User sign up
async function onUserSignUp(user) {
const getToken = () => fetchToken(user.token);
// Initialize e3kit - see full sample in EThree.initialize page
const eThree = await EThree.initialize(getToken);
// Bootstrap user (i.e.create private key if user with this identity doesn't exists yet);
eThree.bootstrap();
// use initialized eThree instance for lookupPublicKeys/encrypt/decrypt and other commands
return eThree;
}
// somewhere in interface ask user to enable multi device support and ask his password
async function onMultyDeviceSupportEnabled(pwd) {
eThree.backupPrivateKey(pwd);
}
// user SignIn
async function onUserSignIn(user) {
const getToken = () => fetchToken(user.token);
// Initialize e3kit - see full sample in EThree.initialize page
const eThree = await EThree.initialize(getToken);
// Bootstrap user (i.e. load privateKey if user signing in with new device,
// download privateKey and decrypt it with password from the Virgil Cloud)
if (await isNewDevice(eThree)) {
try {
eThree.bootstrap(user.password);
} catch (e) {
// user didn't activated multi device support
}
}
// use initialized eThree instance for lookupPrivateKeys/encrypt/decrypt and other commands
return eThree;
}
async function isNewDevice(e3kit: EThree) {
try {
await e3kit.bootstrap();
return false;
} catch (e) {
if (e.name === 'PasswordRequiredError') {
return true
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment