<commit_before>/**
 * Created by keyvan on 8/20/16.
 */

import {KoaPassport} from 'koa-passport';
import {Strategy as LocalStrategy} from 'passport-local';
import {Strategy as JwtStrategy, ExtractJwt} from 'passport-jwt';
import jwt from 'jsonwebtoken';
import {neo4jInt} from './preprocess';

class Authentication {
    constructor(neo4jConnection, {secret, passwordMatches, tokenExpirationInterval,
        userCypherQueryFile, rolesCypherQueryFile} = {}) {
        this.neo4jConnection = neo4jConnection;
        this.passport = new KoaPassport();

        this.secret = secret;
        this.passwordMatches = <PASSWORD>;
        this.tokenExpirationInterval = tokenExpirationInterval || '1h';
        this.userQuery = userCypherQueryFile;
        this.rolesQuery = rolesCypherQueryFile;


        this.passport.use(new LocalStrategy((username, password, done) => {
            this.neo4jConnection.executeCypher(this.userQuery, {username: username})
                .then(([user]) => {
                    const passwordsMatch = this.passwordMatches ?
                        this.passwordMatches(password, user.password)
                        : password === <PASSWORD>;
                    if (!user || !passwordsMatch)
                        done(new Error('invalid username or password'));
                    else {
                        delete user.password;
                        done(null, user);
                    }
                }, done);
        }));

        this.passport.use(new JwtStrategy(
            {
                jwtFromRequest: ExtractJwt.fromAuthHeader(),
                secretOrKey: secret
            }, (user, done) => {
                // Check whether payload is user
                if (!user.id)
                    done(new Error('invalid token'));
                else
                    done(null, user);
            }));

        // koa-passport uses generators which will be deprecated in koa v3,
        // below block should be refactored accordingly
        // The author of koa-passport has not considered the use cases of done(err),
        // hence we need to wrap calls in a promise
        this.authenticateLocal = async (ctx, next) => await new Promise(
            (resolve, reject) => this.passport.authenticate('local', resolve)(ctx, next)<commit_msg>invalid username or password functional again<commit_after>/**
 * Created by keyvan on 8/20/16.
 */

import {KoaPassport} from 'koa-passport';
import {Strategy as LocalStrategy} from 'passport-local';
import {Strategy as JwtStrategy, ExtractJwt} from 'passport-jwt';
import jwt from 'jsonwebtoken';
import {neo4jInt} from './preprocess';

class Authentication {
    constructor(neo4jConnection, {secret, passwordMatches, tokenExpirationInterval,
        userCypherQueryFile, rolesCypherQueryFile} = {}) {
        this.neo4jConnection = neo4jConnection;
        this.passport = new KoaPassport();

        this.secret = secret;
        this.passwordMatches = <PASSWORD>;
        this.tokenExpirationInterval = tokenExpirationInterval || '1h';
        this.userQuery = userCypherQueryFile;
        this.rolesQuery = rolesCypherQueryFile;

        this.passport.use(new LocalStrategy((username, password, done) => {
            this.neo4jConnection.executeCypher(this.userQuery, {username: username})
                .then(response => {
                    const user = response[0];
                    if (!user)
                        done(new Error('invalid username or password'));
                    else {
                        const passwordsMatch = this.passwordMatches ?
                            this.passwordMatches(password, user.password)
                            : password === <PASSWORD>;
                        if (!passwordsMatch)
                            done(new Error('invalid username or password'));
                        else {
                            delete user.password;
                            done(null, user);
                        }
                    }
                }, done);
        }));

        this.passport.use(new JwtStrategy(
            {
                jwtFromRequest: ExtractJwt.fromAuthHeader(),
                secretOrKey: secret
            }, (user, done) => {
                // Check whether payload is user
                if (!user.id)
                    done(new Error('invalid token'));
                else
                    done(null, user);
            }));

        // koa-passport uses generators which will be deprecated in koa v3,
        // below block should be refactored accordingly
        // The author of koa-passport has not considered the use cases of done(err),
        // hence we need to wrap calls in a promise
        this.authenticateLocal = async (ctx, next) => await new Promise(
            (resolve, reject) => this.passport.authenticate('local', resolve)(ctx, next)
{'fraction_non_alphanumeric': 0.09752606828869352, 'fraction_numerical': 0.005111429155591904, 'mean_word_length': 1.8098793796668582, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1396070', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1206, 'n_words': 322}