Skip to content

Commit

Permalink
updated linting
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-fabian committed Oct 27, 2023
1 parent 6b6ff2f commit 4090141
Show file tree
Hide file tree
Showing 26 changed files with 4,385 additions and 3,174 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

34 changes: 0 additions & 34 deletions .eslintrc.json

This file was deleted.

8 changes: 8 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
config = require('eslint-config-service-soft');

module.exports = [
...config,
{
ignores: ['showcase']
}
];
7,304 changes: 4,291 additions & 3,013 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lbx-jwt",
"description": "Provides JWT authentication for loopback applications. Includes storing roles inside tokens and handling refreshing. Built-in reuse detection.",
"version": "1.2.0",
"version": "1.2.1",
"keywords": [
"lb4",
"LoopBack",
Expand Down Expand Up @@ -73,7 +73,7 @@
"@types/node": "^20.5.0",
"@types/nodemailer": "^6.4.9",
"eslint": "^8.47.0",
"eslint-config-service-soft": "^1.1.0",
"eslint-config-service-soft": "^1.2.2",
"handlebars": "^4.7.8",
"nodemailer": "^6.9.4",
"typescript": "~5.1.6"
Expand Down
2 changes: 1 addition & 1 deletion showcase/src/controllers/register.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class RegisterController {
})
newUser: Omit<NewUser, DefaultEntityOmitKeys | 'id'>
): Promise<Omit<BaseUser<Roles>, DefaultEntityOmitKeys | 'credentials'>> {
// eslint-disable-next-line @typescript-eslint/typedef
// eslint-disable-next-line typescript/typedef
const transaction = await this.dataSource.beginTransaction(IsolationLevel.READ_COMMITTED);
try {
const baseUser: Omit<BaseUser<Roles>, DefaultEntityOmitKeys | 'credentials' | 'id'> = {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/fixtures/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Sleeps for the given amount of milliseconds.
* You need to await this to work.
*
* @param ms - The amount of milliseconds everything should sleep.
* @returns When the time has passed.
*/
export async function sleep(ms: number): Promise<void> {
return new Promise( resolve => setTimeout(resolve, ms) );
return new Promise(resolve => setTimeout(resolve, ms) );
}
3 changes: 3 additions & 0 deletions src/__tests__/fixtures/test-db.datasource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { juggler } from '@loopback/repository';

/**
* An in memory database used for testing.
*/
export const testDb: juggler.DataSource = new juggler.DataSource({
name: 'db',
connector: 'memory'
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/unit/access-token.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HttpErrors } from '@loopback/rest';
import { securityId } from '@loopback/security';
import { expect } from '@loopback/testlab';
import { BaseUserProfile } from '../../models';
import { AccessTokenService } from '../../services';
import { expect } from '@loopback/testlab';
import { sleep } from '../fixtures/helpers';
import { HttpErrors } from '@loopback/rest';

const USER_PROFILE: BaseUserProfile<string> = {
id: '1',
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('AccessTokenService', () => {
expect(userProfileFromToken).to.deepEqual(DECODED_USER_PROFILE);

const expectedError: HttpErrors.HttpError<401> = new HttpErrors.Unauthorized('Error verifying token: invalid token');
const invalidToken: string = 'aaa.bbb.ccc';
await expect(accessTokenService.verifyToken(invalidToken)).to.be.rejectedWith(expectedError);
const INVALID_TOKEN: string = 'aaa.bbb.ccc';
await expect(accessTokenService.verifyToken(INVALID_TOKEN)).to.be.rejectedWith(expectedError);
});
});
18 changes: 9 additions & 9 deletions src/__tests__/unit/base-mail.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import { BaseUser, PasswordResetToken } from '../../models';
import { BaseMailService } from '../../services';
import { DefaultEntityOmitKeys } from '../../types';

// eslint-disable-next-line jsdoc/require-jsdoc

class MailService extends BaseMailService<string> {
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly WEBSERVER_MAIL: string = '[email protected]';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly BASE_RESET_PASSWORD_LINK: string = 'http://localhost:4200/reset-password';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly webserverMailTransporter: Transporter;
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly PRODUCTION: boolean = false;
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly SAVED_EMAILS_PATH: string = './test-emails';
// eslint-disable-next-line jsdoc/require-jsdoc

protected override readonly LOGO_HEADER_URL: string = 'https://via.placeholder.com/165x165';
// eslint-disable-next-line jsdoc/require-jsdoc

protected override readonly LOGO_FOOTER_URL?: string = 'https://via.placeholder.com/500x60';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly ADDRESS_LINES: string[] = ['my address', 'my name'];
}

Expand Down
30 changes: 15 additions & 15 deletions src/__tests__/unit/base-user.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { BaseUserRepository, CredentialsRepository, PasswordResetTokenRepository
import { BaseMailService, BaseUserService } from '../../services';
import { DefaultEntityOmitKeys } from '../../types';

// eslint-disable-next-line jsdoc/require-jsdoc

enum Roles {
USER = 'user',
ADMIN = 'admin'
Expand All @@ -28,33 +28,33 @@ const transaction: juggler.Transaction = {
};
testDb.stubs.beginTransaction.resolves(transaction);

// eslint-disable-next-line jsdoc/require-jsdoc

class MailService extends BaseMailService<string> {
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly WEBSERVER_MAIL: string = '[email protected]';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly BASE_RESET_PASSWORD_LINK: string = 'http://localhost:4200/reset-password';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly webserverMailTransporter: Transporter;
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly PRODUCTION: boolean = false;
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly SAVED_EMAILS_PATH: string = './test-emails';
// eslint-disable-next-line jsdoc/require-jsdoc

protected override readonly LOGO_HEADER_URL: string = 'https://via.placeholder.com/165x165';
// eslint-disable-next-line jsdoc/require-jsdoc

protected override readonly LOGO_FOOTER_URL: string = 'https://via.placeholder.com/500x60';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly ADDRESS_LINES: string[] = ['my address', 'my name'];
}

// eslint-disable-next-line max-len

const baseUserRepository: StubbedInstanceWithSinonAccessor<BaseUserRepository<Roles>> = createStubInstance(BaseUserRepository) as StubbedInstanceWithSinonAccessor<BaseUserRepository<Roles>>;
const credentialsRepository: StubbedInstanceWithSinonAccessor<CredentialsRepository> = createStubInstance(CredentialsRepository);
// eslint-disable-next-line max-len

const passwordResetTokenRepository: StubbedInstanceWithSinonAccessor<PasswordResetTokenRepository<Roles>> = createStubInstance(PasswordResetTokenRepository);
const mailService: MailService = new MailService();
// eslint-disable-next-line max-len

const baseUserService: BaseUserService<Roles> = new BaseUserService<Roles>(baseUserRepository, passwordResetTokenRepository, 300000, testDb, mailService);

describe('BaseUserService', () => {
Expand Down Expand Up @@ -106,10 +106,10 @@ describe('BaseUserService', () => {
});

baseUserRepository.stubs.findOne.resolves(user);
// eslint-disable-next-line max-len

const credentialsHasOneRepository: StubbedInstanceWithSinonAccessor<HasOneRepository<Credentials>> = createStubInstance(DefaultHasOneRepository);
credentialsHasOneRepository.stubs.get.resolves(credentials);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line typescript/no-unused-vars
(baseUserRepository.stubs.credentials as unknown) = (id: string) => credentialsHasOneRepository;
const userFromVerifiedCredentials: BaseUser<Roles> = await baseUserService.verifyCredentials({
email: '[email protected]',
Expand Down
26 changes: 13 additions & 13 deletions src/__tests__/unit/refresh-token.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ import { AccessTokenService, BaseMailService, BaseUserService, RefreshTokenServi
import { DefaultEntityOmitKeys, TokenObject } from '../../types';
import { sleep } from '../fixtures/helpers';

// eslint-disable-next-line jsdoc/require-jsdoc

enum Roles {
USER = 'user',
ADMIN = 'admin'
}

// eslint-disable-next-line jsdoc/require-jsdoc

class MailService extends BaseMailService<Roles> {
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly WEBSERVER_MAIL: string = '[email protected]';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly BASE_RESET_PASSWORD_LINK: string = 'http://localhost:4200/reset-password';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly webserverMailTransporter: Transporter;
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly PRODUCTION: boolean = false;
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly SAVED_EMAILS_PATH: string = './test-emails';
// eslint-disable-next-line jsdoc/require-jsdoc

protected override readonly LOGO_HEADER_URL: string = 'https://via.placeholder.com/165x165';
// eslint-disable-next-line jsdoc/require-jsdoc

protected override readonly LOGO_FOOTER_URL: string = 'https://via.placeholder.com/500x60';
// eslint-disable-next-line jsdoc/require-jsdoc

protected readonly ADDRESS_LINES: string[] = ['my address', 'my name'];
}

Expand All @@ -45,13 +45,13 @@ const transaction: juggler.Transaction = {
};
testDb.stubs.beginTransaction.resolves(transaction);

// eslint-disable-next-line max-len

const baseUserRepository: StubbedInstanceWithSinonAccessor<BaseUserRepository<Roles>> = createStubInstance(BaseUserRepository) as StubbedInstanceWithSinonAccessor<BaseUserRepository<Roles>>;
// eslint-disable-next-line max-len

const passwordResetTokenRepository: StubbedInstanceWithSinonAccessor<PasswordResetTokenRepository<Roles>> = createStubInstance(PasswordResetTokenRepository);
const refreshTokenRepository: StubbedInstanceWithSinonAccessor<RefreshTokenRepository> = createStubInstance(RefreshTokenRepository);
const mailService: MailService = new MailService();
// eslint-disable-next-line max-len

const userService: BaseUserService<Roles> = new BaseUserService<Roles>(baseUserRepository, passwordResetTokenRepository, 300000, testDb, mailService);
const accessTokenService: AccessTokenService<Roles> = new AccessTokenService('accessSecret', 3600000);

Expand Down
11 changes: 1 addition & 10 deletions src/controllers/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export class LbxJwtAuthController<RoleType extends string> {

/**
* Tries to login a user with the provided email and password.
*
* @param loginCredentials - Contains the email and password of a user.
* @param request - The injected request object. Is needed to access the two factor code inside a custom header.
* @returns Auth Data for the user including the jwt.
Expand Down Expand Up @@ -148,7 +147,6 @@ export class LbxJwtAuthController<RoleType extends string> {

/**
* Refreshes a token.
*
* @param refreshGrant - The refresh token send by the user.
* @returns Auth Data for the user including the jwt.
*/
Expand Down Expand Up @@ -195,7 +193,6 @@ export class LbxJwtAuthController<RoleType extends string> {

/**
* Logout a user. Cleans up all existing refresh tokens of the current token family.
*
* @param refreshGrant - The refresh token of the user that should be logged out.
*/
@post('logout', {
Expand All @@ -221,7 +218,6 @@ export class LbxJwtAuthController<RoleType extends string> {

/**
* Requests the reset of a password.
*
* @param requestResetPassword - Contains the email of the user for which a password reset should be requested.
*/
@post('request-reset-password', {
Expand All @@ -248,7 +244,6 @@ export class LbxJwtAuthController<RoleType extends string> {
/**
* Verifies a given reset password token.
* Throws an error if something is wrong with the token, does noting otherwise.
*
* @param token - The token that should be verified.
* @returns Whether or not the provided token is valid.
*/
Expand Down Expand Up @@ -291,7 +286,6 @@ export class LbxJwtAuthController<RoleType extends string> {

/**
* Confirms the reset of the password and tries to set it to the given password.
*
* @param resetPasswordData - Contains the password reset token and the new password value.
*/
@post('confirm-reset-password', {
Expand Down Expand Up @@ -336,14 +330,13 @@ export class LbxJwtAuthController<RoleType extends string> {
}
catch (error) {
await transaction.rollback();
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
// eslint-disable-next-line typescript/no-unsafe-member-access
throw new HttpErrors.InternalServerError(`Error trying to set a new password: ${error.message}`);
}
}

/**
* Generates a two factor secret for the requesting user and returns a qr code url to display.
*
* @param userProfile - The currently logged in user.
* @returns A qr code url for the user.
*/
Expand Down Expand Up @@ -377,7 +370,6 @@ export class LbxJwtAuthController<RoleType extends string> {

/**
* Confirms turning on the two factor authentication by checking the provided code.
*
* @param userProfile - The currently logged in user.
* @param request - The injected request object. Is needed to access the two factor code inside a custom header.
*/
Expand Down Expand Up @@ -409,7 +401,6 @@ export class LbxJwtAuthController<RoleType extends string> {

/**
* Turns off two factor authentication for the current user.
*
* @param userProfile - The currently logged in user.
*/
@authenticate('jwt')
Expand Down
2 changes: 0 additions & 2 deletions src/encapsulation/bcrypt.utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { compare, genSalt, hash } from 'bcryptjs';
export abstract class BcryptUtilities {
/**
* Asynchronously compares the given data against the given hash.
*
* @param s - Data to compare.
* @param hash - Data to be compared to.
* @returns Promise, if callback has been omitted.
Expand All @@ -17,7 +16,6 @@ export abstract class BcryptUtilities {

/**
* Asynchronously generates a hash for the given string.
*
* @param value - The value that should be hashed.
* @returns A hash of the given value.
*/
Expand Down
1 change: 0 additions & 1 deletion src/encapsulation/handlebars.utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { compile } from 'handlebars';
export abstract class HandlebarsUtilities {
/**
* Compiles the given data to a html template.
*
* @param data - The data that should be compiled. This is usually the string result of reading an html file.
* @returns The compiled templates.
*/
Expand Down
2 changes: 0 additions & 2 deletions src/encapsulation/jwt.utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export interface EncodedJwt<RoleType extends string> {
export abstract class JwtUtilities {
/**
* Asynchronously sign the given payload into a JSON Web Token string payload.
*
* @param payload - Any info that should be put inside the token.
* @param secret - The secret used to encrypt the token.
* @param options - Additional options like "expiresIn".
Expand All @@ -48,7 +47,6 @@ export abstract class JwtUtilities {

/**
* Asynchronously verify given token using a secret or a public key to get a decoded token.
*
* @param token - The token to encode.
* @param secret - The secret to encode the token with.
* @returns The encoded token.
Expand Down
Loading

0 comments on commit 4090141

Please sign in to comment.