# All files / utils/services/auth auth.service.ts

27.59% Statements 8/29
40% Branches 2/5
0% Functions 0/7
22.22% Lines 6/27

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
    683x

    3x
    3x
    3x
    3x

    3x

     import { sign, verify } from 'jsonwebtoken';
    import { readFileSync } from 'fs';
    import { createDecipheriv, createCipheriv, randomBytes, createHash, pseudoRandomBytes } from 'crypto';
    import * as Moment from 'moment';
    import { Container, Service } from '../../../utils/container/index';
    import { ConfigService } from '../../services/config/config.service';

    export let iv, key;

    export interface TokenData {
        email: string;
        scope: Array<string>;
        id: number;
    }

    @Service()
    export class AuthService {
        modifyFunctions: { validateToken?: Function } = {
            validateToken: this.validateToken
        };
        constructor(
            private config: ConfigService
        ) { }

        validateToken(token: string) {
            return { id: 1, type: 'ADMIN' };
        }

        verifyToken(token): TokenData {
            let result;
            try {
                result = verify(token, this.config.APP_CONFIG.cert, { algorithm: 'HS256' });
            } catch (e) {
                result = false;
            }
            return result;
        }

        decrypt(password: string) {
            const decipher = createDecipheriv(this.config.APP_CONFIG.cyper.algorithm, this.config.APP_CONFIG.cyper.privateKey, this.config.APP_CONFIG.cyper.iv);
            let dec = decipher.update(password, 'hex', 'utf8');
            dec += decipher.final('utf8');
            return dec;
        }

        encrypt(password: string) {
            const cipher = createCipheriv(this.config.APP_CONFIG.cyper.algorithm, this.config.APP_CONFIG.cyper.privateKey, this.config.APP_CONFIG.cyper.iv);
            let crypted = cipher.update(password, 'utf8', 'hex');
            crypted += cipher.final('hex');
            return crypted;
        }

        validate(token, callback) {
            // Check token timestamp
            const ttl = 30 * 1000 * 60;
            const diff = Moment().diff(Moment(token.iat * 1000));
            if (diff > ttl) {
                return callback(null, false);
            }
            callback(null, true, token);
        }

        sign(tokenData: TokenData): string {
            return sign(tokenData, this.config.APP_CONFIG.cert, { algorithm: 'HS256' });
        }

    }