Skip to content

Cleanup/structure: apply structure pattern to the suite #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions packages/oauth-twitter/src/accounts-oauth-twitter.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import * as oauth from 'oauth';

export interface AccountsOauthTwitterOptions {
key: string;
secret: string;
}
import { Configuration } from './types/configuration'

export class AccountsOAuthTwitter {
private options: AccountsOauthTwitterOptions;
export default class AccountsOAuthTwitter {
private config: Configuration;
private oauth: any;

constructor(options: AccountsOauthTwitterOptions) {
this.options = options;
constructor(config: Configuration) {
this.config = config;
this.oauth = new oauth.OAuth(
'https://twitter.com/oauth/request_token',
'https://twitter.com/oauth/access_token',
this.options.key,
this.options.secret,
this.config.key,
this.config.secret,
'1.0A',
null,
'HMAC-SHA1'
Expand Down
6 changes: 2 additions & 4 deletions packages/oauth-twitter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import { AccountsOAuthTwitter } from './accounts-oauth-twitter';

export { AccountsOauthTwitterOptions } from './accounts-oauth-twitter';
export default AccountsOAuthTwitter;
export { default } from './accounts-oauth-twitter';
export { Configuration } from './types/configuration';
4 changes: 4 additions & 0 deletions packages/oauth-twitter/src/types/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Configuration {
key: string;
secret: string;
}
6 changes: 4 additions & 2 deletions packages/oauth/src/accounts-oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { UserObjectType } from '@accounts/common';
import { AccountsServer, DBInterface, AuthService } from '@accounts/server';
import * as requestPromise from 'request-promise';

import { Configuration } from './types/configuration';

export interface OauthUser {
id: string;
email?: string;
Expand All @@ -18,9 +20,9 @@ export class AccountsOauth implements AuthService {
public server: AccountsServer;
public serviceName = 'oauth';
private db: DBInterface;
private options: OauthOptions;
private options: Configuration;

constructor(options) {
constructor(options: Configuration) {
this.options = options;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/oauth/src/types/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { OAuthUser } from './oauth-user';

export interface Configuration {
[provider: string]: {
authenticate: (params: any) => Promise<OAuthUser>;
};
}
5 changes: 5 additions & 0 deletions packages/oauth/src/types/oauth-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface OAuthUser {
id: string;
email?: string;
profile?: object;
}
4 changes: 2 additions & 2 deletions packages/password/__tests__/accounts-password.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { set } from 'lodash';
import { AccountsPassword } from '../src';
import AccountsPassword from '../src';

describe('AccountsPassword', () => {
const password = new AccountsPassword({});

describe('config', () => {
describe('options', () => {
it('should have default options', async () => {
expect(password.options.passwordResetTokenExpirationInDays).toBe(3);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
bcryptPassword,
hashPassword,
verifyPassword,
} from '../src/encryption';
} from '../../src/utils/encryption';

describe('encryption', () => {
describe('bcryptPassword', () => {
Expand Down
38 changes: 12 additions & 26 deletions packages/password/src/accounts-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,18 @@ import {
AccountsServer,
generateRandomToken,
AuthService,
getFirstUserEmail
} from '@accounts/server';
import { TwoFactor, AccountsTwoFactorOptions } from '@accounts/two-factor';
import { getFirstUserEmail } from '@accounts/server/lib/utils';
import { hashPassword, bcryptPassword, verifyPassword } from './encryption';
import {
PasswordCreateUserType,
PasswordLoginType,
PasswordType,
} from './types';

export const isEmail = (email?: string) => {
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return email && re.test(email);
};

export interface AccountsPasswordOptions {
twoFactor?: AccountsTwoFactorOptions;
passwordHashAlgorithm?: HashAlgorithm;
passwordResetTokenExpirationInDays?: number;
passwordEnrollTokenExpirationInDays?: number;
minimumPasswordLength?: number;
validateNewUser?: (user: CreateUserType) => Promise<boolean>;
validateEmail?(email?: string): boolean;
validatePassword?(password?: PasswordType): boolean;
validateUsername?(username?: string): boolean;
}
import TwoFactor, { TwoFactorConfiguration } from '@accounts/two-factor';

import { Configuration } from './types/configuration';
import { PasswordCreateUserType } from './types/password-create-user-type';
import { PasswordLoginType } from './types/password-login-type';
import { PasswordType } from './types/password-type';

import { hashPassword, bcryptPassword, verifyPassword } from './utils/encryption';
import { isEmail } from './utils/isEmail';

const defaultOptions = {
passwordResetTokenExpirationInDays: 3,
Expand All @@ -72,10 +58,10 @@ export default class AccountsPassword implements AuthService {
public serviceName = 'password';
public server: AccountsServer;
public twoFactor: TwoFactor;
private options: AccountsPasswordOptions;
private options: Configuration;
private db: DBInterface;

constructor(options: AccountsPasswordOptions = {}) {
constructor(options: Configuration = {}) {
this.options = { ...defaultOptions, ...options };
this.twoFactor = new TwoFactor(options.twoFactor);
}
Expand Down
24 changes: 0 additions & 24 deletions packages/password/src/types.ts

This file was deleted.

16 changes: 16 additions & 0 deletions packages/password/src/types/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CreateUserType, HashAlgorithm } from '@accounts/common';
import { TwoFactorConfiguration } from '@accounts/two-factor';

import { PasswordType } from './password-type';

export interface Configuration {
twoFactor?: TwoFactorConfiguration;
passwordHashAlgorithm?: HashAlgorithm;
passwordResetTokenExpirationInDays?: number;
passwordEnrollTokenExpirationInDays?: number;
minimumPasswordLength?: number;
validateNewUser?: (user: CreateUserType) => Promise<boolean>;
validateEmail?(email?: string): boolean;
validatePassword?(password?: PasswordType): boolean;
validateUsername?(username?: string): boolean;
}
6 changes: 6 additions & 0 deletions packages/password/src/types/password-create-user-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { CreateUserType } from '@accounts/common';
import { PasswordType } from './password-type';

export interface PasswordCreateUserType extends CreateUserType {
password: PasswordType;
}
10 changes: 10 additions & 0 deletions packages/password/src/types/password-login-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { LoginUserIdentityType } from '@accounts/common';
import { PasswordType } from './password-type';

export interface PasswordLoginType {
user: string | LoginUserIdentityType;
// User password
password: PasswordType;
// Two factor code
code?: string;
}
8 changes: 8 additions & 0 deletions packages/password/src/types/password-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { HashAlgorithm } from '@accounts/common';

export type PasswordType =
| string
| {
digest: string;
algorithm: HashAlgorithm;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as bcrypt from 'bcryptjs';
import * as crypto from 'crypto';
import { PasswordType } from './types';
import { PasswordType } from '../types/password-type';

export const bcryptPassword = async (password: string): Promise<string> => {
const salt = await bcrypt.genSalt(10);
Expand All @@ -18,7 +18,6 @@ export const hashPassword = (password: PasswordType, algorithm: string) => {
return password.digest;
};

export const verifyPassword = async (
password: string,
export const verifyPassword = async ( password: string,
hash: string
): Promise<boolean> => bcrypt.compare(password, hash);
4 changes: 4 additions & 0 deletions packages/password/src/utils/isEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const isEmail = (email?: string) => {
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return email && re.test(email);
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as jwtDecode from 'jwt-decode';
import { AccountsServer, JwtData } from '../src/accounts-server';
import { AccountsServer } from '../src/accounts-server';
import { JwtData } from '../src/types/jwt-data';
import {
bcryptPassword,
hashPassword,
verifyPassword,
} from '../src/encryption';
} from '../src/utils/encryption';

describe('AccountsServer', () => {
const db = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { emailTemplates } from '../src/email';
import { emailTemplates } from '../../src/utils/email';

describe('email', () => {
describe('emailTemplates', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
bcryptPassword,
hashPassword,
verifyPassword,
} from '../src/encryption';
} from '../../src/utils/encryption';

describe('bcryptPassword', () => {
it('hashes password using bcrypt', async () => {
Expand Down
Loading