<commit_before>import ApiResource from "../../ApiResource";
import ApiEndpoint from "../../ApiEndpoint";
import * as Hapi from "hapi";
import {JsonApiDocument, JsonApiResource} from "../../../jsonapi/JsonApiObjects";
import {ObjectParser, RequirementType} from "../../../jsonapi/Parser";
import winston = require("winston");
import jwt = require('jsonwebtoken');
import passwd = require('passwd-linux');
import {wrapCallbackAsPromise} from "../../../../common/utils";
import JsonApiDocumentBuilder from "../../../jsonapi/JsonApiBuilder";

export default class AuthenticationResource extends ApiResource {

    public constructor (private jwtSecret: string) {
        super('/auth');
    }

    @ApiEndpoint('POST', '/login')
    public async login(req: Hapi.Request, reply: Hapi.IReply) {
        let parser = JsonApiDocument.getParser().addProperties({
            name: 'data',
            required: RequirementType.Required,
            handler: JsonApiResource.getParser().addProperties(
                {
                    name: 'id',
                    required: RequirementType.Forbidden
                },
                {
                    name: 'attributes',
                    required: RequirementType.Required,
                    handler: new ObjectParser(() => ({}),
                        {
                            name: 'username',
                            required: RequirementType.Required
                        },
                        {
                            name: 'password',
                            required: RequirementType.Required
                        }
                    )
                }
            )
        });

        let requestData: JsonApiResource;
        try {
            requestData = parser.parse(req.payload).data as JsonApiResource;
        } catch (err) {
            winston.error(err);
            return reply({
                error: 'Error while parsing the request. Argument might be missing.'
            }).code(400);
        }

        // Check userdata
        let passwordCorrect: boolean;
        try {
            passwordCorrect = (await wrapCallbackAsPromise(
                passwd.checkPass,
                requestData.attributes.username,
                requestData.attributes.password
            ) === 'passwordCorrect');
        } catch (err) {
            winston.error(err);
            return reply({
                error: 'Could not log in user.'
            }).code(500);
        }

        if (!passwordCorrect) {
            return reply({
                error: 'Could not log in user. Wrong username or password.'
            }).code(400);
        }
<commit_msg>return appropriate status code on wrong username/password<commit_after>import ApiResource from "../../ApiResource";
import ApiEndpoint from "../../ApiEndpoint";
import * as Hapi from "hapi";
import {JsonApiDocument, JsonApiResource} from "../../../jsonapi/JsonApiObjects";
import {ObjectParser, RequirementType} from "../../../jsonapi/Parser";
import winston = require("winston");
import jwt = require('jsonwebtoken');
import passwd = require('passwd-linux');
import {wrapCallbackAsPromise} from "../../../../common/utils";
import JsonApiDocumentBuilder from "../../../jsonapi/JsonApiBuilder";

export default class AuthenticationResource extends ApiResource {

    public constructor (private jwtSecret: string) {
        super('/auth');
    }

    @ApiEndpoint('POST', '/login')
    public async login(req: Hapi.Request, reply: Hapi.IReply) {
        let parser = JsonApiDocument.getParser().addProperties({
            name: 'data',
            required: RequirementType.Required,
            handler: JsonApiResource.getParser().addProperties(
                {
                    name: 'id',
                    required: RequirementType.Forbidden
                },
                {
                    name: 'attributes',
                    required: RequirementType.Required,
                    handler: new ObjectParser(() => ({}),
                        {
                            name: 'username',
                            required: RequirementType.Required
                        },
                        {
                            name: 'password',
                            required: RequirementType.Required
                        }
                    )
                }
            )
        });

        let requestData: JsonApiResource;
        try {
            requestData = parser.parse(req.payload).data as JsonApiResource;
        } catch (err) {
            winston.error(err);
            return reply({
                error: 'Error while parsing the request. Argument might be missing.'
            }).code(400);
        }

        // Check userdata
        let passwordCorrect: boolean;
        try {
            passwordCorrect = (await wrapCallbackAsPromise(
                passwd.checkPass,
                requestData.attributes.username,
                requestData.attributes.password
            ) === 'passwordCorrect');
        } catch (err) {
            winston.error(err);
            return reply({
                error: 'Could not log in user.'
            }).code(500);
        }

        if (!passwordCorrect) {
            return reply({
                error: 'Could not log in user. Wrong username or password.'
            }).code(401);
        }

{'fraction_non_alphanumeric': 0.1117908787541713, 'fraction_numerical': 0.0033370411568409346, 'mean_word_length': 1.488468634686347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '828378', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1222, 'n_words': 310}