<commit_before>import got from 'got';
import { deleteUser, getUser, setUser } from '../services/user_cache.js';

export async function getToken(request, response) {
  const accessTokenName = 'authorization_code';
  const grantTypes = [accessTokenName, 'refresh_token'];
  if (request.body.client_id !== process.env.CLIENT_ID) {
    return response.status(400).send('invalid client_id');
  } else if (!grantTypes.includes(request.body.grant_type)) {
    return response.status(400).json({
      error_description: `invalid grant_type: ${request.body.grant_type}, must be one of ${grantTypes.join(', ')}`,
      error: 'invalid_request',
    });
  }

  const passed_through_params =
    request.body.grant_type === accessTokenName
      ? ['client_id', 'code_verifier', 'code_challenge', 'redirect_uri', 'code']
      : ['client_id', 'refresh_token'];
  const body = { grant_type: request.body.grant_type };
  try {
    passed_through_params.forEach((name) => {
      const param = request.body[name];
      if (param) {
        body[name] = param;
      } else {
        const error = new Error(`${name} is required`);
        error.status = 400;

        throw error;
      }
    });
  } catch (error) {
    return response.status(400).json({ error_description: error.message, error: 'invalid_request' });
  }

  try {
    const tokenRequest = await got.post('https://login.dts.utah.gov:443/sso/oauth2/access_token', {
      form: body,
      responseType: 'json',
      headers: {
        authorization: request.headers.authorization,
        accept: request.headers.accept,
      },
    });

    return response.send(tokenRequest.body);
  } catch (error) {
    const errorMessage = error.body || error.message || error;

    return response.status(500).json({ error_description: errorMessage, error: 'server_error' });
  }
}

export async function logout(request, response) {
  const { token, isJWTToken } = getTokenFromHeader(request.headers.authorization);

  if (!token) {
    return response.status(401).send('empty token');
  }

  if (isJWTToken) {
    await deleteUser(token);
  }

  return response.status(200).send('user logged out successfully');
}

function getTokenFromHeader(authorization) {
  const [authProvider, authToken] = authorization.split(':');

  let token;
  let isJWTToken;
  if (authToken) {
    token = authToken.split(' ').pop();
    isJWTToken = authProvider === 'utahid';
  }

  return { token, isJWTToken, authProvider, authToken };
}

export async function authenticate(request, response, next) {
  if (request.headers.authorization) {
    const { token, isJWTToken, authProvider, authToken } = getTokenFromHeader(request.headers.authorization);

    if (!token) {
      return response.status(401).send('empty token');
    }

    if (isJWTToken) {
      const cachedUser = await getUser(token);
      if (cachedUser) {
        response.locals.user = cachedUser;

        return next();
      }
    }

    let userResponse;
    const userInfos = {
      utahid: 'https://login.dts.utah.gov/sso/oauth2/userinfo',
      google: 'https://openidconnect.googleapis.com/v1/userinfo',
      facebook: `https://graph.facebook.com/me?access_token=${token}`,
    };

    try {
      userResponse = await got(userInfos[authProvider], {
        responseType: 'json',
        headers: {
          Authorization: authToken,
        },
        retry: 5,
        throwHttpErrors: false,
      });
    } catch (error) {
      const errorMessage = error.body || error.message || error;

      return response.status(500).json({ error_description: errorMessage, error: 'server_error' });
    }

    if (userResponse.statusCode === 200 && userResponse.body) {
      response.locals.user = userResponse.body;

      if (isJWTToken) {
        setUser(token, userResponse.body);
      }

      return next();
    }

    response.status(userResponse.statusCode).json(userResponse.body);
  }

  return response.status(400).send('authorization header is required');
}<commit_msg>fix: add missing return<commit_after>import got from 'got';
import { deleteUser, getUser, setUser } from '../services/user_cache.js';

export async function getToken(request, response) {
  const accessTokenName = 'authorization_code';
  const grantTypes = [accessTokenName, 'refresh_token'];
  if (request.body.client_id !== process.env.CLIENT_ID) {
    return response.status(400).send('invalid client_id');
  } else if (!grantTypes.includes(request.body.grant_type)) {
    return response.status(400).json({
      error_description: `invalid grant_type: ${request.body.grant_type}, must be one of ${grantTypes.join(', ')}`,
      error: 'invalid_request',
    });
  }

  const passed_through_params =
    request.body.grant_type === accessTokenName
      ? ['client_id', 'code_verifier', 'code_challenge', 'redirect_uri', 'code']
      : ['client_id', 'refresh_token'];
  const body = { grant_type: request.body.grant_type };
  try {
    passed_through_params.forEach((name) => {
      const param = request.body[name];
      if (param) {
        body[name] = param;
      } else {
        const error = new Error(`${name} is required`);
        error.status = 400;

        throw error;
      }
    });
  } catch (error) {
    return response.status(400).json({ error_description: error.message, error: 'invalid_request' });
  }

  try {
    const tokenRequest = await got.post('https://login.dts.utah.gov:443/sso/oauth2/access_token', {
      form: body,
      responseType: 'json',
      headers: {
        authorization: request.headers.authorization,
        accept: request.headers.accept,
      },
    });

    return response.send(tokenRequest.body);
  } catch (error) {
    const errorMessage = error.body || error.message || error;

    return response.status(500).json({ error_description: errorMessage, error: 'server_error' });
  }
}

export async function logout(request, response) {
  const { token, isJWTToken } = getTokenFromHeader(request.headers.authorization);

  if (!token) {
    return response.status(401).send('empty token');
  }

  if (isJWTToken) {
    await deleteUser(token);
  }

  return response.status(200).send('user logged out successfully');
}

function getTokenFromHeader(authorization) {
  const [authProvider, authToken] = authorization.split(':');

  let token;
  let isJWTToken;
  if (authToken) {
    token = authToken.split(' ').pop();
    isJWTToken = authProvider === 'utahid';
  }

  return { token, isJWTToken, authProvider, authToken };
}

export async function authenticate(request, response, next) {
  if (request.headers.authorization) {
    const { token, isJWTToken, authProvider, authToken } = getTokenFromHeader(request.headers.authorization);

    if (!token) {
      return response.status(401).send('empty token');
    }

    if (isJWTToken) {
      const cachedUser = await getUser(token);
      if (cachedUser) {
        response.locals.user = cachedUser;

        return next();
      }
    }

    let userResponse;
    const userInfos = {
      utahid: 'https://login.dts.utah.gov/sso/oauth2/userinfo',
      google: 'https://openidconnect.googleapis.com/v1/userinfo',
      facebook: `https://graph.facebook.com/me?access_token=${token}`,
    };

    try {
      userResponse = await got(userInfos[authProvider], {
        responseType: 'json',
        headers: {
          Authorization: authToken,
        },
        retry: 5,
        throwHttpErrors: false,
      });
    } catch (error) {
      const errorMessage = error.body || error.message || error;

      return response.status(500).json({ error_description: errorMessage, error: 'server_error' });
    }

    if (userResponse.statusCode === 200 && userResponse.body) {
      response.locals.user = userResponse.body;

      if (isJWTToken) {
        setUser(token, userResponse.body);
      }

      return next();
    }

    return response.status(userResponse.statusCode).json(userResponse.body);
  }

  return response.status(400).send('authorization header is required');
}
{'fraction_non_alphanumeric': 0.13806483915383652, 'fraction_numerical': 0.010013768932281888, 'mean_word_length': 3.252261841405003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 8, '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': '5798058', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2418, 'n_words': 498}