<commit_before>import 'package:angel_framework/angel_framework.dart';

/// Forces Basic authentication over the requested resource, with the given [realm] name, if no JWT is present.
///
/// [realm] defaults to `'angel_auth'`.
RequestHandler forceBasicAuth<User>({String realm}) {
  return (RequestContext req, ResponseContext res) async {
    if (req.container.has<User>()) return true;

    res
      ..statusCode = 401
      ..headers['www-authenticate'] = 'Basic realm="${realm ?? 'angel_auth'}"'
      ..close();
  };
}

/// Restricts access to a resource via authentication.
RequestHandler requireAuthentication<User>() {
  return (RequestContext req, ResponseContext res,
      {bool throwError: true}) async {
    bool _reject(ResponseContext res) {
      if (throwError) {
        res.statusCode = 403;
        throw new AngelHttpException.forbidden();
      } else
        return false;
    }

    if (req.container.has<User>() || req.method == 'OPTIONS')
      return true;
    else
      return _reject(res);
  };
}<commit_msg>force basic<commit_after>import 'package:angel_framework/angel_framework.dart';

/// Forces Basic authentication over the requested resource, with the given [realm] name, if no JWT is present.
///
/// [realm] defaults to `'angel_auth'`.
RequestHandler forceBasicAuth<User>({String realm}) {
  return (RequestContext req, ResponseContext res) async {
    if (req.container.has<User>()) return true;
    res.headers['www-authenticate'] = 'Basic realm="${realm ?? 'angel_auth'}"';
    throw new AngelHttpException.notAuthenticated();
  };
}

/// Restricts access to a resource via authentication.
RequestHandler requireAuthentication<User>() {
  return (RequestContext req, ResponseContext res,
      {bool throwError: true}) async {
    bool _reject(ResponseContext res) {
      if (throwError) {
        res.statusCode = 403;
        throw new AngelHttpException.forbidden();
      } else
        return false;
    }

    if (req.container.has<User>() || req.method == 'OPTIONS')
      return true;
    else
      return _reject(res);
  };
}
{'fraction_non_alphanumeric': 0.12890812890812892, 'fraction_numerical': 0.004329004329004329, 'mean_word_length': 3.434968017057569, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5698289', 'n_tokens_mistral': 603, 'n_tokens_neox': 589, 'n_words': 168}