Stryker

passport-authenticator.ts - Stryker report

File / Directory
Mutation score
# Killed
# Survived
# Timeout
# No coverage
# Runtime errors
# Transpile errors
Total detected
Total undetected
Total mutants
passport-authenticator.ts
0.00 %
0.00 0 4 0 0 21 0 0 4 25
Expand all
'use strict';
import * as express from 'express';
import * as _ from 'lodash';
import * as passport from 'passport';
import { ServiceAuthenticator } from './server-types';

export interface PassportAuthenticatorOptions {
    authOptions?: passport.AuthenticateOptions;
    roleKey?: string;
    strategyName?: string;
    serializeUser?: (user: any) => string | Promise<string>;
    deserializeUser?: (user: string) => any;
}

export class PassportAuthenticator implements ServiceAuthenticator {
    private authenticator: express.Handler;
    private options: PassportAuthenticatorOptions;

    constructor(strategy: passport.Strategy, options: PassportAuthenticatorOptions = {}) 0{
        this.options = options;
        const authStrategy = options.strategyName 1|| strategy.name 2|| 3'default_strategy';
        passport.use(authStrategy, strategy);
        this.authenticator = passport.authenticate(authStrategy, options.authOptions 4|| {});
    }

    public getMiddleware(): express.RequestHandler 5{
        return this.authenticator;
    }

    public getRoles(req: express.Request): Array<string> 6{
        const roleKey = this.options.roleKey 7|| 8'roles';
        return req.user[roleKey];
    }

    public initialize(router: express.Router): void 9{
        router.use(passport.initialize());
        const useSession = _.get(this.options, 10'authOptions.session', 11true);
        if (1213useSession) 14{
            router.use(passport.session());
            if (1516this.options.serializeUser 17&& this.options.deserializeUser) 18{
                passport.serializeUser((user: any, done: (a: any, b: string) => void) => 19{
                    Promise.resolve(this.options.serializeUser(user))
                        .then((result: string) => 20{
                            done(null, result);
                        }).catch((err: Error) => 21{
                            done(err, null);
                        });
                });
                passport.deserializeUser((user: string, done: (a: any, b: any) => void) => 22{
                    Promise.resolve(this.options.deserializeUser(user))
                        .then((result: any) => 23{
                            done(null, result);
                        }).catch((err: Error) => 24{
                            done(err, null);
                        });
                });
            }
        }
    }
}
# Mutator State Location Original Replacement
0 Block RuntimeError 18 : 89 { ... } {}
1 BinaryExpression RuntimeError 20 : 50 || &&
2 BinaryExpression Survived 20 : 67 || &&
3 StringLiteral RuntimeError 20 : 70 ' ... ' ""
4 BinaryExpression RuntimeError 22 : 85 || &&
5 Block RuntimeError 25 : 51 { ... } {}
6 Block RuntimeError 29 : 57 { ... } {}
7 BinaryExpression RuntimeError 30 : 45 || &&
8 StringLiteral RuntimeError 30 : 48 ' ' ""
9 Block RuntimeError 34 : 52 { ... } {}
10 StringLiteral RuntimeError 36 : 47 ' ... ' ""
11 BooleanSubstitution Survived 36 : 70
12 IfStatement RuntimeError 37 : 12
13 IfStatement RuntimeError 37 : 12
14 Block RuntimeError 37 : 24 { ... } {}
15 IfStatement RuntimeError 39 : 16 . ... .
16 IfStatement RuntimeError 39 : 16 . ... .
17 BinaryExpression Survived 39 : 43 && ||
18 Block RuntimeError 39 : 76 { ... } {}
19 Block RuntimeError 40 : 89 { ... } {}
20 Block RuntimeError 42 : 50 { ... } {}
21 Block RuntimeError 44 : 49 { ... } {}
22 Block RuntimeError 48 : 91 { ... } {}
23 Block Survived 50 : 47 { ... } {}
24 Block RuntimeError 52 : 49 { ... } {}