All files / src simple-auth.service.ts

100% Statements 38/38
100% Branches 10/10
100% Functions 8/8
100% Lines 35/35

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                1x 3x     3x 3x 3x 3x         23x       19x 19x 19x   19x 4x     15x 15x 15x 15x 15x 15x 90x 90x     15x 15x       4x 4x 4x 2x 2x   2x       9x     18x 18x       9x 9x         9x   8x    
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { ConfigService, ExternalAuthenticationService, RequestContext } from '@vendure/core';
import { Cache } from 'cache-manager';
import crypto from 'crypto';
import { SIMPLE_AUTH_PLUGIN_OPTIONS, STRATEGY_NAME } from './constants';
import { ISimpleAuthPluginOptions } from './interfaces';
 
@Injectable()
export class SimpleAuthService {
  readonly prefix: string = 'simple-auth-service';
 
  constructor(
    @Inject(CACHE_MANAGER) private cache: Cache,
    @Inject(SIMPLE_AUTH_PLUGIN_OPTIONS) private options: ISimpleAuthPluginOptions,
    @Inject(ConfigService) private configService: ConfigService,
    @Inject(ExternalAuthenticationService) private externalAuthService: ExternalAuthenticationService,
  ) {
  }
 
  private keyof(email: string) {
    return `${this.prefix}:${email}`;
  }
 
  async generateCode(email: string) {
    const ttl = this.options.ttl;
    const key = this.keyof(email);
    let code = await this.cache.get<string>(key);
 
    if (typeof code === 'string') {
      return code;
    }
 
    const alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    const digits = '0123456789';
    const target = digits + (this.options.includeAlphabet ? alphabets : '');
    const length = this.options.length;
    code = '';
    for (let i = 0; i < length; i++) {
      const index = crypto.randomInt(0, target.length);
      code += target[index];
    }
 
    await this.cache.set(key, code, ttl);
    return code;
  }
 
  async verifyCode(email: string, code: string) {
    const key = this.keyof(email);
    const savedCode = await this.cache.get<string>(key);
    if (typeof savedCode === 'string' && code === savedCode) {
      await this.cache.del(key);
      return true;
    }
    return false;
  }
 
  getAllStrategyNames () {
    return this.configService
    .authOptions
    .shopAuthenticationStrategy
    .map(strategy => strategy.name)
    .filter(name => name !== STRATEGY_NAME);
  }
 
  async checkCrossStrategies(ctx: RequestContext, email: string) {
    for (const strategyName of this.getAllStrategyNames()) {
      const user = await this.externalAuthService.findCustomerUser(
        ctx,
        strategyName,
        email
      );
      if (user) return strategyName;
    }
    return null;
  }
}