<commit_before>package utils.auth

import java.security.SecureRandom
import javax.inject.Inject

import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import com.mohiva.play.silhouette.impl.util.SecureRandomIDGenerator
import play.api.mvc.{ RequestHeader, Result }
import play.api.mvc.Results.{ Forbidden, Unauthorized }
import scala.concurrent.ExecutionContext.Implicits.global

import scala.concurrent.Future

/**
 * When twilio requests our PDF document, it will initially do so with no auth.
 * This response will indicate to twilio to try again with digest auth.
 */
class DigestAuthErrorHandler @Inject() (secureRandomIDGenerator: SecureRandomIDGenerator)() extends SecuredErrorHandler {

  override def onNotAuthorized(implicit request: RequestHeader): Future[Result] = {
    Future.successful(Forbidden)
  }

  /**
   * Issue the Digest Authentication challenge if they are not authenticated.
   */
  override def onNotAuthenticated(implicit request: RequestHeader): Future[Result] = {
    secureRandomIDGenerator.generate.map {
      nonce =>
        Unauthorized.withHeaders("WWW-Authenticate" -> s"Digest realm=\"twilio\",nonce=\"$nonce\"")
    }<commit_msg>more logging<commit_after>package utils.auth

import java.security.SecureRandom
import javax.inject.Inject

import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import com.mohiva.play.silhouette.impl.util.SecureRandomIDGenerator
import org.log4s.getLogger
import play.api.mvc.{RequestHeader, Result}
import play.api.mvc.Results.{Forbidden, Unauthorized}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/**
 * When twilio requests our PDF document, it will initially do so with no auth.
 * This response will indicate to twilio to try again with digest auth.
 */
class DigestAuthErrorHandler @Inject() (secureRandomIDGenerator: SecureRandomIDGenerator)() extends SecuredErrorHandler {
  private[this] val logger = getLogger

  override def onNotAuthorized(implicit request: RequestHeader): Future[Result] = {
    logger.info("DigestAuthErrorHandler onNotAuthorized for: ")
    logger.info(request.headers.toSimpleMap.toString())
    logger.info(request.method)
    Future.successful(Forbidden)
  }

  /**
   * Issue the Digest Authentication challenge if they are not authenticated.
   */
  override def onNotAuthenticated(implicit request: RequestHeader): Future[Result] = {
    logger.info("DigestAuthErrorHandler onNotAuthenticated for: ")
    logger.info(request.headers.toSimpleMap.toString())
    logger.info(request.method)
    secureRandomIDGenerator.generate.map {
      nonce =>
        Unauthorized.withHeaders("WWW-Authenticate" -> s"Digest realm=\"twilio\",nonce=\"$nonce\"")
    }
{'fraction_non_alphanumeric': 0.0978021978021978, 'fraction_numerical': 0.0003663003663003663, 'mean_word_length': 5.02869757174393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4031535', 'n_tokens_mistral': 782, 'n_tokens_neox': 786, 'n_words': 181}