move password_utils.js to typescript

pull/1294/head
Audric Ackermann 5 years ago
parent 89579ebd35
commit e806e912a3
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -54,7 +54,7 @@ const config = require('./app/config');
// Very important to put before the single instance check, since it is based on the // Very important to put before the single instance check, since it is based on the
// userData directory. // userData directory.
const userConfig = require('./app/user_config'); const userConfig = require('./app/user_config');
const passwordUtil = require('./app/password_util'); const passwordUtil = require('./ts/util/passwordUtils');
const importMode = const importMode =
process.argv.some(arg => arg === '--import') || config.get('import'); process.argv.some(arg => arg === '--import') || config.get('import');

@ -40,7 +40,7 @@ window.CONSTANTS = {
MAX_USERNAME_LENGTH: 20, MAX_USERNAME_LENGTH: 20,
}; };
window.passwordUtil = require('./app/password_util'); window.passwordUtil = require('./ts/util/passwordUtils');
window.Signal.Logs = require('./js/modules/logs'); window.Signal.Logs = require('./js/modules/logs');
window.resetDatabase = () => { window.resetDatabase = () => {

@ -164,7 +164,7 @@ window.setPassword = (passPhrase, oldPhrase) =>
ipc.send('set-password', passPhrase, oldPhrase); ipc.send('set-password', passPhrase, oldPhrase);
}); });
window.passwordUtil = require('./app/password_util'); window.passwordUtil = require('./ts/util/passwordUtils');
window.libsession = require('./ts/session'); window.libsession = require('./ts/session');
// We never do these in our code, so we'll prevent it everywhere // We never do these in our code, so we'll prevent it everywhere

@ -1,6 +1,6 @@
const { assert } = require('chai'); const { assert } = require('chai');
const passwordUtil = require('../../app/password_util'); const passwordUtil = require('../../ts/util/passwordUtils');
describe('Password Util', () => { describe('Password Util', () => {
describe('hash generation', () => { describe('hash generation', () => {

@ -2,7 +2,7 @@ import React from 'react';
import { SessionModal } from './SessionModal'; import { SessionModal } from './SessionModal';
import { SessionButton, SessionButtonColor } from './SessionButton'; import { SessionButton, SessionButtonColor } from './SessionButton';
import { PasswordUtil } from '../../util/';
export enum PasswordAction { export enum PasswordAction {
Set = 'set', Set = 'set',
Change = 'change', Change = 'change',
@ -117,7 +117,7 @@ export class SessionPasswordModal extends React.Component<Props, State> {
public async validatePasswordHash(password: string | null) { public async validatePasswordHash(password: string | null) {
// Check if the password matches the hash we have stored // Check if the password matches the hash we have stored
const hash = await window.Signal.Data.getPasswordHash(); const hash = await window.Signal.Data.getPasswordHash();
if (hash && !window.passwordUtil.matchesHash(password, hash)) { if (hash && !PasswordUtil.matchesHash(password, hash)) {
return false; return false;
} }
@ -153,7 +153,7 @@ export class SessionPasswordModal extends React.Component<Props, State> {
const enteredPasswordConfirm = (currentPasswordConfirmEntered || '').trim(); const enteredPasswordConfirm = (currentPasswordConfirmEntered || '').trim();
// if user did not fill the first password field, we can't do anything // if user did not fill the first password field, we can't do anything
const errorFirstInput = window.passwordUtil.validatePassword( const errorFirstInput = PasswordUtil.validatePassword(
enteredPassword, enteredPassword,
window.i18n window.i18n
); );
@ -166,7 +166,7 @@ export class SessionPasswordModal extends React.Component<Props, State> {
// if action is Set or Change, we need a valid ConfirmPassword // if action is Set or Change, we need a valid ConfirmPassword
if (action === Set || action === Change) { if (action === Set || action === Change) {
const errorSecondInput = window.passwordUtil.validatePassword( const errorSecondInput = PasswordUtil.validatePassword(
enteredPasswordConfirm, enteredPasswordConfirm,
window.i18n window.i18n
); );

@ -5,6 +5,7 @@ import { missingCaseError } from './missingCaseError';
import { migrateColor } from './migrateColor'; import { migrateColor } from './migrateColor';
import { makeLookup } from './makeLookup'; import { makeLookup } from './makeLookup';
import * as UserUtil from './user'; import * as UserUtil from './user';
import * as PasswordUtil from './passwordUtils';
export * from './blockedNumberController'; export * from './blockedNumberController';
@ -16,4 +17,5 @@ export {
migrateColor, migrateColor,
missingCaseError, missingCaseError,
UserUtil, UserUtil,
PasswordUtil,
}; };

@ -1,4 +1,5 @@
const crypto = require('crypto'); import * as crypto from 'crypto';
import { LocalizerType } from '../types/Util';
const ERRORS = { const ERRORS = {
TYPE: 'Password must be a string', TYPE: 'Password must be a string',
@ -6,17 +7,17 @@ const ERRORS = {
CHARACTER: 'Password must only contain letters, numbers and symbols', CHARACTER: 'Password must only contain letters, numbers and symbols',
}; };
const sha512 = text => { const sha512 = (text: string) => {
const hash = crypto.createHash('sha512'); const hash = crypto.createHash('sha512');
hash.update(text.trim()); hash.update(text.trim());
return hash.digest('hex'); return hash.digest('hex');
}; };
const generateHash = phrase => phrase && sha512(phrase.trim()); export const generateHash = (phrase: string) => phrase && sha512(phrase.trim());
const matchesHash = (phrase, hash) => export const matchesHash = (phrase: string | null, hash: string) =>
phrase && sha512(phrase.trim()) === hash.trim(); phrase && sha512(phrase.trim()) === hash.trim();
const validatePassword = (phrase, i18n) => { export const validatePassword = (phrase: string, i18n: LocalizerType) => {
if (typeof phrase !== 'string') { if (typeof phrase !== 'string') {
return i18n ? i18n('passwordTypeError') : ERRORS.TYPE; return i18n ? i18n('passwordTypeError') : ERRORS.TYPE;
} }
@ -38,9 +39,3 @@ const validatePassword = (phrase, i18n) => {
return null; return null;
}; };
module.exports = {
generateHash,
matchesHash,
validatePassword,
};
Loading…
Cancel
Save