2016-10-21 17:20:36 +00:00
|
|
|
import srp from 'srp';
|
|
|
|
import * as crypt from './crypt';
|
2016-11-10 02:40:53 +00:00
|
|
|
import * as util from '../common/fetch';
|
2016-11-10 05:47:20 +00:00
|
|
|
import {trackEvent, setAccountId} from '../analytics';
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
const NO_SESSION = '__NO_SESSION__';
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
/** Create a new Account for the user */
|
2016-10-21 17:20:36 +00:00
|
|
|
export async function signup (firstName, lastName, rawEmail, rawPassphrase) {
|
2016-11-10 02:40:53 +00:00
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
const email = _sanitizeEmail(rawEmail);
|
|
|
|
const passphrase = _sanitizePassphrase(rawPassphrase);
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
// Get a fancy new Account object
|
2016-10-21 17:20:36 +00:00
|
|
|
const account = await _initAccount(firstName, lastName, email);
|
2016-11-10 02:40:53 +00:00
|
|
|
|
|
|
|
// Generate some secrets for the user base'd on password
|
2016-10-21 17:20:36 +00:00
|
|
|
const authSecret = await crypt.deriveKey(passphrase, account.email, account.saltKey);
|
|
|
|
const derivedSymmetricKey = await crypt.deriveKey(passphrase, account.email, account.saltEnc);
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
// Generate public/private keypair and symmetric key for Account
|
|
|
|
const {publicKey, privateKey} = await crypt.generateKeyPairJWK();
|
|
|
|
const symmetricKeyJWK = await crypt.generateAES256Key();
|
|
|
|
|
|
|
|
// Compute the verifier key and add it to the Account object
|
2016-10-21 17:20:36 +00:00
|
|
|
account.verifier = srp.computeVerifier(
|
|
|
|
_getSrpParams(),
|
|
|
|
Buffer.from(account.saltAuth, 'hex'),
|
|
|
|
Buffer.from(account.email, 'utf8'),
|
|
|
|
Buffer.from(authSecret, 'hex')
|
|
|
|
).toString('hex');
|
|
|
|
|
|
|
|
// Encode keypair
|
|
|
|
const encSymmetricJWKMessage = crypt.encryptAES(derivedSymmetricKey, JSON.stringify(symmetricKeyJWK));
|
|
|
|
const encPrivateJWKMessage = crypt.encryptAES(symmetricKeyJWK, JSON.stringify(privateKey));
|
|
|
|
|
|
|
|
// Add keys to account
|
|
|
|
account.publicKey = JSON.stringify(publicKey);
|
|
|
|
account.encPrivateKey = JSON.stringify(encPrivateJWKMessage);
|
|
|
|
account.encSymmetricKey = JSON.stringify(encSymmetricJWKMessage);
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
const response = await util.post('/auth/signup', account);
|
2016-10-29 05:40:09 +00:00
|
|
|
|
2016-11-10 01:34:03 +00:00
|
|
|
trackEvent('Session', 'Signup');
|
2016-10-29 05:40:09 +00:00
|
|
|
|
|
|
|
return response;
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
/** Create a new session for the user */
|
2016-10-21 17:20:36 +00:00
|
|
|
export async function login (rawEmail, rawPassphrase) {
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~ //
|
|
|
|
// Sanitize Inputs //
|
|
|
|
// ~~~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
const email = _sanitizeEmail(rawEmail);
|
|
|
|
const passphrase = _sanitizePassphrase(rawPassphrase);
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Fetch Salt and Submit A To Server //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
const {saltKey, saltAuth} = await util.post('/auth/login-s', {email});
|
2016-10-21 17:20:36 +00:00
|
|
|
const authSecret = await crypt.deriveKey(passphrase, email, saltKey);
|
|
|
|
const secret1 = await crypt.srpGenKey();
|
|
|
|
const c = new srp.Client(
|
|
|
|
_getSrpParams(),
|
|
|
|
Buffer.from(saltAuth, 'hex'),
|
|
|
|
Buffer.from(email, 'utf8'),
|
|
|
|
Buffer.from(authSecret, 'hex'),
|
|
|
|
Buffer.from(secret1, 'hex')
|
|
|
|
);
|
|
|
|
const srpA = c.computeA().toString('hex');
|
2016-11-10 02:40:53 +00:00
|
|
|
const {sessionStarterId, srpB} = await util.post('/auth/login-a', {srpA, email});
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Compute and Submit M1 //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
c.setB(new Buffer(srpB, 'hex'));
|
|
|
|
const srpM1 = c.computeM1().toString('hex');
|
2016-11-10 02:40:53 +00:00
|
|
|
const {srpM2} = await util.post('/auth/login-m1', {srpM1, sessionStarterId,});
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Verify Server Identity M2 //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
c.checkM2(new Buffer(srpM2, 'hex'));
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Initialize the Session //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
|
|
|
// Compute K (used for session ID)
|
|
|
|
const sessionId = c.computeK().toString('hex');
|
|
|
|
|
|
|
|
// Get and store some extra info (salts and keys)
|
|
|
|
const {
|
|
|
|
publicKey,
|
|
|
|
encPrivateKey,
|
|
|
|
encSymmetricKey,
|
|
|
|
saltEnc,
|
|
|
|
accountId,
|
2016-11-07 20:24:38 +00:00
|
|
|
firstName,
|
|
|
|
lastName,
|
2016-11-10 02:40:53 +00:00
|
|
|
} = await _whoami(sessionId);
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
const derivedSymmetricKey = await crypt.deriveKey(passphrase, email, saltEnc);
|
|
|
|
const symmetricKeyStr = await crypt.decryptAES(derivedSymmetricKey, JSON.parse(encSymmetricKey));
|
|
|
|
|
|
|
|
// Store the information for later
|
|
|
|
setSessionData(
|
|
|
|
sessionId,
|
|
|
|
accountId,
|
|
|
|
firstName,
|
2016-11-07 20:24:38 +00:00
|
|
|
lastName,
|
2016-10-21 17:20:36 +00:00
|
|
|
email,
|
|
|
|
JSON.parse(symmetricKeyStr),
|
|
|
|
JSON.parse(publicKey),
|
|
|
|
JSON.parse(encPrivateKey),
|
|
|
|
);
|
2016-10-29 05:40:09 +00:00
|
|
|
|
|
|
|
// Set the ID for Google Analytics
|
2016-11-10 01:34:03 +00:00
|
|
|
setAccountId(accountId);
|
|
|
|
trackEvent('Session', 'Login');
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
export async function subscribe (tokenId, planId) {
|
|
|
|
const response = await util.post('/api/billing/subscriptions', {
|
|
|
|
token: tokenId,
|
|
|
|
quantity: 1,
|
|
|
|
plan: planId,
|
|
|
|
});
|
2016-11-10 01:34:03 +00:00
|
|
|
trackEvent('Session', 'Subscribe', planId, 1);
|
2016-11-07 20:24:38 +00:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
export function getPublicKey () {
|
|
|
|
return getSessionData().publicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPrivateKey () {
|
|
|
|
const {symmetricKey, encPrivateKey} = getSessionData();
|
|
|
|
const privateKeyStr = crypt.decryptAES(symmetricKey, encPrivateKey);
|
|
|
|
return JSON.parse(privateKeyStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCurrentSessionId () {
|
|
|
|
return localStorage.getItem('currentSessionId') || NO_SESSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAccountId () {
|
|
|
|
return getSessionData().accountId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getEmail () {
|
|
|
|
return getSessionData().email;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFirstName () {
|
|
|
|
return getSessionData().firstName;
|
|
|
|
}
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
export function getFullName () {
|
|
|
|
const {firstName, lastName} = getSessionData();
|
|
|
|
return `${firstName || ''} ${lastName || ''}`.trim();
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
/**
|
|
|
|
* get Data about the current session
|
|
|
|
* @returns Object
|
|
|
|
*/
|
|
|
|
export function getSessionData () {
|
|
|
|
const sessionId = getCurrentSessionId();
|
|
|
|
if (sessionId == NO_SESSION) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const dataStr = localStorage.getItem(_getSessionKey(sessionId));
|
|
|
|
return JSON.parse(dataStr);
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
/** Set data for the new session and store it encrypted with the sessionId */
|
2016-11-07 20:24:38 +00:00
|
|
|
export function setSessionData (sessionId,
|
|
|
|
accountId,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
email,
|
|
|
|
symmetricKey,
|
|
|
|
publicKey,
|
|
|
|
encPrivateKey) {
|
2016-10-21 17:20:36 +00:00
|
|
|
const dataStr = JSON.stringify({
|
|
|
|
id: sessionId,
|
|
|
|
accountId: accountId,
|
|
|
|
symmetricKey: symmetricKey,
|
|
|
|
publicKey: publicKey,
|
|
|
|
encPrivateKey: encPrivateKey,
|
|
|
|
email: email,
|
|
|
|
firstName: firstName,
|
2016-11-07 20:24:38 +00:00
|
|
|
lastName: lastName,
|
2016-10-21 17:20:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
localStorage.setItem(_getSessionKey(sessionId), dataStr);
|
|
|
|
|
|
|
|
// NOTE: We're setting this last because the stuff above might fail
|
|
|
|
localStorage.setItem('currentSessionId', sessionId);
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
/** Unset the session data (log out) */
|
2016-10-21 17:20:36 +00:00
|
|
|
export function unsetSessionData () {
|
|
|
|
const sessionId = getCurrentSessionId();
|
|
|
|
localStorage.removeItem(_getSessionKey(sessionId));
|
|
|
|
localStorage.removeItem(`currentSessionId`);
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
/** Check if we (think) we have a session */
|
2016-10-21 17:20:36 +00:00
|
|
|
export function isLoggedIn () {
|
|
|
|
return getCurrentSessionId() !== NO_SESSION;
|
|
|
|
}
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
/** Log out and delete session data */
|
2016-10-21 17:20:36 +00:00
|
|
|
export async function logout () {
|
2016-11-07 20:24:38 +00:00
|
|
|
await util.post('/auth/logout');
|
2016-10-21 17:20:36 +00:00
|
|
|
unsetSessionData();
|
2016-11-10 01:34:03 +00:00
|
|
|
trackEvent('Session', 'Logout');
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
/** Cancel the user's subscription Account */
|
2016-11-07 20:24:38 +00:00
|
|
|
export async function cancelAccount () {
|
|
|
|
await util.del('/api/billing/subscriptions');
|
2016-11-10 01:34:03 +00:00
|
|
|
trackEvent('Session', 'Cancel Account');
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~ //
|
|
|
|
// Helper Functions //
|
|
|
|
// ~~~~~~~~~~~~~~~~ //
|
|
|
|
|
2016-11-10 02:40:53 +00:00
|
|
|
function _whoami (sessionId = null) {
|
|
|
|
return util.get('/auth/whoami', sessionId);
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
function _getSessionKey (sessionId) {
|
|
|
|
return `session__${sessionId.slice(0, 10)}`
|
|
|
|
}
|
|
|
|
|
|
|
|
async function _initAccount (firstName, lastName, email) {
|
|
|
|
return {
|
|
|
|
email,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
id: await crypt.generateAccountId(),
|
|
|
|
saltEnc: await crypt.getRandomHex(),
|
|
|
|
saltAuth: await crypt.getRandomHex(),
|
|
|
|
saltKey: await crypt.getRandomHex(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getSrpParams () {
|
|
|
|
return srp.params[2048];
|
|
|
|
}
|
|
|
|
|
|
|
|
function _sanitizeEmail (email) {
|
|
|
|
return email.trim().toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
function _sanitizePassphrase (passphrase) {
|
|
|
|
return passphrase.trim().normalize('NFKD');
|
|
|
|
}
|