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

Max/remove unused login function #397

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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@sendgrid/mail": "^7.4.4",
"amazon-s3-uri": "^0.1.1",
"aws-sdk": "^2.906.0",
"bad-words-next": "^2.2.1",
"bcrypt": "^5.0.0",
"body-parser": "^1.19.0",
"class-transformer": "^0.3.1",
Expand All @@ -55,6 +56,7 @@
"moment-timezone": "^0.5.34",
"morgan": "^1.10.0",
"multer": "^1.4.2",
"obscenity": "^0.2.0",
"pg": "^8.6.0",
"reflect-metadata": "^0.1.13",
"routing-controllers": "^0.9.0",
Expand Down
16 changes: 15 additions & 1 deletion services/UserAccountService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { BadRequestError, NotFoundError } from 'routing-controllers';
import { BadRequestError, ForbiddenError, NotFoundError } from 'routing-controllers';
import { Service } from 'typedi';
import { InjectManager } from 'typeorm-typedi-extensions';
import { EntityManager } from 'typeorm';
import * as moment from 'moment';
import * as faker from 'faker';
import { UserAccessUpdates } from 'api/validators/AdminControllerRequests';
import {
RegExpMatcher,
englishDataset,
englishRecommendedTransformers,
} from 'obscenity';
import Repositories, { TransactionsManager } from '../repositories';
import {
Uuid,
Expand All @@ -23,8 +28,14 @@ import { UserModel } from '../models/UserModel';
export default class UserAccountService {
private transactions: TransactionsManager;

private matcher: RegExpMatcher;

constructor(@InjectManager() entityManager: EntityManager) {
this.transactions = new TransactionsManager(entityManager);
this.matcher = new RegExpMatcher({
...englishDataset.build(),
...englishRecommendedTransformers,
});
}

public async findByUuid(uuid: Uuid): Promise<UserModel> {
Expand Down Expand Up @@ -104,6 +115,9 @@ export default class UserAccountService {
}
changes.hash = await UserRepository.generateHash(newPassword);
}
if (this.matcher.hasMatch(userPatches.handle)) {
throw new ForbiddenError('Please remove profanity from handle.');
}
return this.transactions.readWrite(async (txn) => {
if (userPatches.handle) {
const userRepository = Repositories.user(txn);
Expand Down
30 changes: 3 additions & 27 deletions services/UserAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import * as jwt from 'jsonwebtoken';
import { InjectManager } from 'typeorm-typedi-extensions';
import { EntityManager } from 'typeorm';
import { UserRepository } from '../repositories/UserRepository';
import { Uuid, ActivityType, UserState, UserRegistration } from '../types';
import { Uuid, ActivityType, UserState, UserRegistration, UserAccessType } from '../types';
import { Config } from '../config';
import { UserModel } from '../models/UserModel';
import Repositories, { TransactionsManager } from '../repositories';
import UserAccountService from './UserAccountService';

interface AuthToken {
uuid: Uuid;
admin: boolean;
accessType: UserAccessType;
}

@Service()
Expand Down Expand Up @@ -78,30 +78,6 @@ export default class UserAuthService {
return user;
}

public async login(email: string, pass: string): Promise<string> {
const authenticatedUser = await this.transactions.readWrite(async (txn) => {
let user = await Repositories
.user(txn)
.findByEmail(email.toLowerCase());
if (!user) throw new NotFoundError('There is no account associated with that email');
if (user.isBlocked()) throw new ForbiddenError('Your account has been blocked');
if (!(await user.verifyPass(pass))) throw new ForbiddenError('Incorrect password');
await Repositories.activity(txn).logActivity({
user,
type: ActivityType.ACCOUNT_LOGIN,
});
if (user.state === UserState.PASSWORD_RESET) {
user = await Repositories.user(txn).upsertUser(user, { state: UserState.ACTIVE });
}
return user;
});
const token: AuthToken = {
uuid: authenticatedUser.uuid,
admin: authenticatedUser.isAdmin(),
};
return jwt.sign(token, Config.auth.secret, { expiresIn: Config.auth.tokenLifespan });
}

public async checkCredentials(email: string, pass: string): Promise<UserModel> {
const authenticatedUser = await this.transactions.readWrite(async (txn) => {
const user = await Repositories
Expand All @@ -123,7 +99,7 @@ export default class UserAuthService {
public static generateAuthToken(user: UserModel): string {
const token: AuthToken = {
uuid: user.uuid,
admin: user.isAdmin(),
accessType: user.accessType,
};
return jwt.sign(token, Config.auth.secret, { expiresIn: Config.auth.tokenLifespan });
}
Expand Down
36 changes: 36 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,14 @@ babel-preset-jest@^27.5.1:
babel-plugin-jest-hoist "^27.5.1"
babel-preset-current-node-syntax "^1.0.0"

bad-words-next@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/bad-words-next/-/bad-words-next-2.2.1.tgz#3bdcf9b8c7021b177bc8ff825349fc7995c87ec7"
integrity sha512-kVb5vtKX3Rn3S+3rI2kkaSC5bwTyduxn13Dbr3VCzNdRGjMFRUipdZ3uXTt196TZ7uXutFdWPoKVoXDvL8N7Iw==
dependencies:
confusables "^1.1.1"
moize "^6.1.6"

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
Expand Down Expand Up @@ -1694,6 +1702,11 @@ configstore@^5.0.1:
write-file-atomic "^3.0.0"
xdg-basedir "^4.0.0"

confusables@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/confusables/-/confusables-1.1.1.tgz#d3aafa1666d13a3a2fa9483bf556f641588d9a02"
integrity sha512-BzFtzUrufackm00Wb2zvrZV0ItRqPdWaUprU5FXHeZiJRrOWxGmXmQl/muGTF9EQl+MdBXz+Irk99meskGZmXw==

confusing-browser-globals@^1.0.10:
version "1.0.11"
resolved "https://registry.yarnpkg.com/confusing-browser-globals/-/confusing-browser-globals-1.0.11.tgz#ae40e9b57cdd3915408a2805ebd3a5585608dc81"
Expand Down Expand Up @@ -2438,6 +2451,11 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

fast-equals@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/fast-equals/-/fast-equals-3.0.3.tgz#8e6cb4e51ca1018d87dd41982ef92758b3e4197f"
integrity sha512-NCe8qxnZFARSHGztGMZOO/PC1qa5MIFB5Hp66WdzbCRAz8U8US3bx1UTgLS49efBQPcUtO9gf5oVEY8o7y/7Kg==

fast-glob@^3.2.9:
version "3.2.11"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9"
Expand Down Expand Up @@ -4046,6 +4064,11 @@ methods@^1.0.1, methods@~1.1.2:
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=

micro-memoize@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/micro-memoize/-/micro-memoize-4.1.2.tgz#ce719c1ba1e41592f1cd91c64c5f41dcbf135f36"
integrity sha512-+HzcV2H+rbSJzApgkj0NdTakkC+bnyeiUxgT6/m7mjcz1CmM22KYFKp+EVj1sWe4UYcnriJr5uqHQD/gMHLD+g==

micromatch@^4.0.4:
version "4.0.4"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.4.tgz#896d519dfe9db25fce94ceb7a500919bf881ebf9"
Expand Down Expand Up @@ -4120,6 +4143,14 @@ mkdirp@^1.0.3, mkdirp@^1.0.4:
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

moize@^6.1.6:
version "6.1.6"
resolved "https://registry.yarnpkg.com/moize/-/moize-6.1.6.tgz#ac2e723e74b951875fe2c0c3433405c2b098c3e6"
integrity sha512-vSKdIUO61iCmTqhdoIDrqyrtp87nWZUmBPniNjO0fX49wEYmyDO4lvlnFXiGcaH1JLE/s/9HbiK4LSHsbiUY6Q==
dependencies:
fast-equals "^3.0.1"
micro-memoize "^4.1.2"

moment-timezone@^0.5.34:
version "0.5.34"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.34.tgz#a75938f7476b88f155d3504a9343f7519d9a405c"
Expand Down Expand Up @@ -4342,6 +4373,11 @@ object.values@^1.1.5:
define-properties "^1.1.3"
es-abstract "^1.19.1"

obscenity@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/obscenity/-/obscenity-0.2.0.tgz#5a234c7af0aaebcd58fc762b29d2852104350fa2"
integrity sha512-eYe8r9hqJk5dEMZkLtWlGlwGxKYO6xA/yEOzd8MGSG3vzn8hAgo6MUZ9dirA3kPAfy/1d1jEOkCUpIU1nGI1EQ==

on-finished@^2.3.0:
version "2.4.1"
resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.4.1.tgz#58c8c44116e54845ad57f14ab10b03533184ac3f"
Expand Down
Loading