All files AuthenticationMiddleware.ts

16.28% Statements 7/43
0% Branches 0/18
0% Functions 0/6
16.28% Lines 7/43

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                              1x     1x 1x   1x   1x   1x               1x                                                                                                                                                                                                                  
// Copyright (C) 2017  Norman Breau
 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
import {StatusCode} from './StatusCode';
import {Request} from './Request';
import {Response} from './Response';
import {ResponseData} from './ResponseData';
import {Token} from './Token';
import {Logger} from './Logger';
import {getInstance, getApplicationLogger} from './instance';
import {TokenManager} from './TokenManager';
import {StormError} from './StormError';
import {IConfig} from './IConfig';
import {InternalError} from './InternalError';
 
/**
 * A base authentication strategy that handles 90% of the authentication process.
 * This will verify that the token hasn't been manipulated or tainted.
 * The authenticate API must be implemented by subclasses to further validate the token data 
 * for their specific use cases.
 */
export abstract class AuthenticationMiddleware {
    private logger: Logger;
 
    public constructor() {
        this.logger = getApplicationLogger();
    }
 
    /**
     * 
     * @param request 
     * @param response 
     * @param options Arbituary object containing any relevant information used for authentication.
     */
    public execute(request: Request, response: Response, options?: any): Promise<any> {
        let config: IConfig = getInstance().getConfig();
        let authHeader: string = config.authentication_header;
        let backendAuthHeader: string = config.backend_authentication_header;
        let backend: string = request.getHeader(backendAuthHeader);
 
        return new Promise<any>((resolve, reject) => {
            let token: Token = new Token(request.getHeader(authHeader));
            let tokenManager: TokenManager = getInstance().getTokenManager();
            let isBackendCall: boolean = false;
 
            if (backend) {
                if (options.allowedBackends) {
                    if (options.allowedBackends.indexOf(backend) > -1) {
                        isBackendCall = true;
                    }
                    else {
                        return reject(new ResponseData(StatusCode.ERR_FORBIDDEN, {
                            code: 1,
                            reason: ''
                        }));
                    }
                }
                else {
                    return reject(new ResponseData(StatusCode.ERR_FORBIDDEN, {
                        code: 0,
                        reason: 'This API is not back-end enabled'
                    }));
                }
            }
 
            let tokenManagerPromise: Promise<any>;
 
            if (isBackendCall) {
                tokenManagerPromise = tokenManager.decode(token);
            }
            else {
                tokenManagerPromise = tokenManager.verify(token);
            }
 
            tokenManagerPromise.then((data: any) => {
                return this.authenticate(data, options, isBackendCall);
            }).then((data) => {
                resolve(data);
            }).catch((error: any) => {
                this.logger.error(error);
                let responseData: ResponseData = null;
 
                // If an error is a TokenExpiredError|JsonWebTokenError, then we can handle it here. Otherwise propagate based on the rules below
                if (error && error.name) {
                    switch (error.name) {
                        case 'TokenExpiredError':
                            error = new ResponseData(StatusCode.ERR_UNAUTHORIZED, {
                                code: error.name,
                                reason: error.message
                            });
                            break;
                        case 'JsonWebTokenError':
                            error = new ResponseData(StatusCode.ERR_UNAUTHORIZED, {
                                code: error.name,
                                reason : error.message
                            });
                            break;
                    }
                }
 
                if (error instanceof StormError) {
                    responseData = new ResponseData(error.getHTTPCode(), error.getErrorResponse());
                }
                else if (error instanceof ResponseData) {
                    responseData = error;
                }
                else {
                    let e: InternalError = new InternalError(error);
                    responseData = new ResponseData(e.getHTTPCode(), {
                        code : e.getCode(),
                        reason : e.getMessage()
                    });
                }
 
                reject(responseData);
            });
        });
    }
 
    /**
     * Subclasses are expected to implement this API to further validate the token data, as required by their application or API.
     * @param tokenData 
     * @param options 
     */
    protected abstract authenticate(tokenData: any, options: any, isBackendCall: boolean): Promise<any>;
}