All files / src/services authentication.service.ts

100% Statements 14/14
100% Branches 8/8
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 408x 8x 8x           8x   8x       17x 1x   16x 1x                     8x       1x     7x      
import {inject, service} from "@waytrade/microservice-core";
import {IBApiApp} from "../app";
import {SecurityUtils} from "../utils/security.utils";
 
/**
 * The user authentication service.
 */
@service()
export class AuthenticationService {
  @inject("IBApiApp")
  private app!: IBApiApp;
 
  /** Start the service. */
  start(): void {
    if (!this.app.config.REST_API_USERNAME) {
      throw new Error("REST_API_USERNAME not configured.");
    }
    if (!this.app.config.REST_API_PASSWORD) {
      throw new Error("REST_API_PASSWORD not configured.");
    }
  }
 
  /**
   * Login with username and password.
   *
   * @returns the JWT token.
   */
  async loginUserPassword(username: string, password: string): Promise<string> {
    // verify username/password
    if (
      username !== this.app.config.REST_API_USERNAME ||
      password !== this.app.config.REST_API_PASSWORD
    ) {
      throw new Error("Wrong username or password");
    }
 
    return SecurityUtils.createJWT();
  }
}