Skip to content
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

Token manager Implemented #222

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 18 additions & 6 deletions packages/password/__tests__/accounts-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ describe('AccountsPassword', () => {

it('throws when token is expired', async () => {
const findUserByResetPasswordToken = jest.fn(() => Promise.resolve(invalidUser));
const isTokenExpired = jest.fn(() => true);
const isEmailTokenExpired = jest.fn(() => true);
password.setStore({ findUserByResetPasswordToken } as any);
password.server = { isTokenExpired } as any;
password.server = { tokenManager: { isEmailTokenExpired } } as any;
try {
await password.resetPassword(token, newPassword);
throw new Error();
Expand All @@ -217,9 +217,9 @@ describe('AccountsPassword', () => {

it('throws when token have invalid email', async () => {
const findUserByResetPasswordToken = jest.fn(() => Promise.resolve(invalidUser));
const isTokenExpired = jest.fn(() => false);
const isEmailTokenExpired = jest.fn(() => false);
password.setStore({ findUserByResetPasswordToken } as any);
password.server = { isTokenExpired } as any;
password.server = { tokenManager: { isEmailTokenExpired } } as any;
try {
await password.resetPassword(token, newPassword);
throw new Error();
Expand All @@ -230,15 +230,15 @@ describe('AccountsPassword', () => {

it('reset password and invalidate all sessions', async () => {
const findUserByResetPasswordToken = jest.fn(() => Promise.resolve(validUser));
const isTokenExpired = jest.fn(() => false);
const isEmailTokenExpired = jest.fn(() => false);
const setResetPassword = jest.fn(() => Promise.resolve());
const invalidateAllSessions = jest.fn(() => Promise.resolve());
password.setStore({
findUserByResetPasswordToken,
setResetPassword,
invalidateAllSessions,
} as any);
password.server = { isTokenExpired } as any;
password.server = { tokenManager: { isEmailTokenExpired } } as any;
await password.resetPassword(token, newPassword);
expect(setResetPassword.mock.calls.length).toBe(1);
expect(invalidateAllSessions.mock.calls[0]).toMatchSnapshot();
Expand Down Expand Up @@ -307,6 +307,9 @@ describe('AccountsPassword', () => {
prepareMail,
options: { sendMail },
sanitizeUser,
tokenManager: {
generateRandomToken: () => 'randomToken'
}
} as any;
set(password.server, 'options.emailTemplates', {});
await password.sendVerificationEmail(verifiedEmail);
Expand All @@ -326,6 +329,9 @@ describe('AccountsPassword', () => {
prepareMail,
options: { sendMail },
sanitizeUser,
tokenManager: {
generateRandomToken: () => 'randomToken'
}
} as any;
set(password.server, 'options.emailTemplates', {});
await password.sendVerificationEmail(email);
Expand Down Expand Up @@ -372,6 +378,9 @@ describe('AccountsPassword', () => {
options: { sendMail },
sanitizeUser,
getFirstUserEmail,
tokenManager: {
generateRandomToken: () => 'randomToken'
}
} as any;
set(password.server, 'options.emailTemplates', {});
await password.sendResetPasswordEmail(email);
Expand Down Expand Up @@ -409,6 +418,9 @@ describe('AccountsPassword', () => {
options: { sendMail },
sanitizeUser,
getFirstUserEmail,
tokenManager: {
generateRandomToken: () => 'randomToken'
}
} as any;
set(password.server, 'options.emailTemplates', {});
await password.sendEnrollmentEmail(email);
Expand Down
10 changes: 5 additions & 5 deletions packages/password/src/accounts-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { trim, isEmpty, isFunction, isString, isPlainObject, get, find, includes
import { CreateUser, User, Login, EmailRecord, TokenRecord, DatabaseInterface, AuthenticationService } from '@accounts/types';
import { HashAlgorithm } from '@accounts/common';
import { TwoFactor, AccountsTwoFactorOptions } from '@accounts/two-factor';
import { AccountsServer, generateRandomToken, getFirstUserEmail } from '@accounts/server';
import { AccountsServer, getFirstUserEmail } from '@accounts/server';
import { hashPassword, bcryptPassword, verifyPassword } from './utils/encryption';

import { PasswordCreateUserType } from './types/password-create-user-type';
Expand Down Expand Up @@ -165,7 +165,7 @@ export default class AccountsPassword implements AuthenticationService {
const resetTokens = get(user, ['services', 'password', 'reset']);
const resetTokenRecord = find(resetTokens, t => t.token === token);

if (this.server.isTokenExpired(token, resetTokenRecord)) {
if (this.server.tokenManager.isEmailTokenExpired(token, resetTokenRecord)) {
throw new Error('Reset password link expired');
}

Expand Down Expand Up @@ -212,7 +212,7 @@ export default class AccountsPassword implements AuthenticationService {
if (!address || !includes(emails.map(email => email.address), address)) {
throw new Error('No such email address for user');
}
const token = generateRandomToken();
const token = this.server.tokenManager.generateRandomToken();
await this.db.addEmailVerificationToken(user.id, address, token);

const resetPasswordMail = this.server.prepareMail(
Expand Down Expand Up @@ -243,7 +243,7 @@ export default class AccountsPassword implements AuthenticationService {
throw new Error('User not found');
}
address = getFirstUserEmail(user, address);
const token = generateRandomToken();
const token = this.server.tokenManager.generateRandomToken();
await this.db.addResetPasswordToken(user.id, address, token);

const resetPasswordMail = this.server.prepareMail(
Expand Down Expand Up @@ -271,7 +271,7 @@ export default class AccountsPassword implements AuthenticationService {
throw new Error('User not found');
}
address = getFirstUserEmail(user, address);
const token = generateRandomToken();
const token = this.server.tokenManager.generateRandomToken();
await this.db.addResetPasswordToken(user.id, address, token, 'enroll');

const enrollmentMail = this.server.prepareMail(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`AccountsServer config throws on invalid db 1`] = `"A database driver is required"`;

exports[`AccountsServer config throws on invalid tokenManager 1`] = `"A tokenManager is required"`;

exports[`AccountsServer loginWithService throws on invalid service 1`] = `"No service with the name facebook was registered."`;

exports[`AccountsServer loginWithService throws when user not found 1`] = `"Service facebook was not able to authenticate user"`;
Loading