<commit_before>import jwt from 'jsonwebtoken';

// Policy helper function
const generatePolicy = (principalId, effect, resource) => {
    const authResponse = {
        principalId: principalId
    };

    if (effect && resource) {
        const tmp = resource.split(':');
        const apiGatewayArnTmp = tmp[5].split('/');
        const awsAccountId = tmp[4];
        const region = tmp[3];

        const restApiId = apiGatewayArnTmp[0];
        const stage = apiGatewayArnTmp[1];

        const newResource = 'arn:aws:execute-api:' + region + ':' + awsAccountId + ':' + restApiId + '/' + stage + '/*/*'

        const statementOne = {
            Action: 'execute-api:Invoke',
            Effect: effect,
            Resource: newResource
        };
        const policyDocument = {
            Version: '2012-10-17',
            Statement: [statementOne]
        };
        authResponse.policyDocument = policyDocument;
    }
    return authResponse;
};

export function authorizer({ event, callback }) {
    const token = event.authorizationToken;

    if (!token) {
        callback('No bearer token supplied');
        return;
    }

    try {
        const jwtToken = jwt.verify(
            event.authorizationToken.replace('Bearer ', ''),
            process.env.JWT_SECRET,
            { algorithm: 'HS256' }
        );

        callback(null, generatePolicy(jwtToken.employeeId, 'Allow', event.methodArn));

    } catch (e) {
        callback('Invalid token');
        console.log(e.message);
        return;
    }
}<commit_msg>Authorizer should only return a Unauthorized message<commit_after>import jwt from 'jsonwebtoken';

// Policy helper function
const generatePolicy = (principalId, effect, resource) => {
    const authResponse = {
        principalId: principalId
    };

    if (effect && resource) {
        const tmp = resource.split(':');
        const apiGatewayArnTmp = tmp[5].split('/');
        const awsAccountId = tmp[4];
        const region = tmp[3];

        const restApiId = apiGatewayArnTmp[0];
        const stage = apiGatewayArnTmp[1];

        const newResource = 'arn:aws:execute-api:' + region + ':' + awsAccountId + ':' + restApiId + '/' + stage + '/*/*'

        const statementOne = {
            Action: 'execute-api:Invoke',
            Effect: effect,
            Resource: newResource
        };
        const policyDocument = {
            Version: '2012-10-17',
            Statement: [statementOne]
        };
        authResponse.policyDocument = policyDocument;
    }
    return authResponse;
};

export function authorizer({ event, callback }) {
    const token = event.authorizationToken;

    try {
        if (!token) {
            throw new Error('No bearer token supplied');
        }

        const jwtToken = jwt.verify(
            event.authorizationToken.replace('Bearer ', ''),
            process.env.JWT_SECRET,
            { algorithm: 'HS256' }
        );

        callback(null, generatePolicy(jwtToken.employeeId, 'Allow', event.methodArn));

    } catch (e) {
        console.log(e.message);
        callback('Unauthorized');
    }
}
{'fraction_non_alphanumeric': 0.12552301255230125, 'fraction_numerical': 0.010299324106855488, 'mean_word_length': 2.1779141104294477, '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': '638617', 'n_tokens_mistral': 901, 'n_tokens_neox': 869, 'n_words': 195}