<commit_before>package sttp.client

import java.nio.charset.Charset
import java.security.MessageDigest

import sttp.client.DigestAuthenticationBackend._
import sttp.client.monad.MonadError
import sttp.client.monad.syntax._
import sttp.client.ws.WebSocketResponse
import sttp.model.{HeaderNames, StatusCode}

import scala.language.higherKinds
import scala.util.Random

//TODO: support auth-int
//TODO: support stale param
//TODO: auth-info
class DigestAuthenticationBackend[F[_], S, WS_HANDLER[_]](delegate: SttpBackend[F, S, WS_HANDLER])
    extends SttpBackend[F, S, WS_HANDLER] {
  override def send[T](request: Request[T, S]): F[Response[T]] = {
    if (request.tag(DigestAuthTag).isDefined) {
      val digestAuthData = request.tag(DigestAuthTag).get.asInstanceOf[DigestAuthData]
      implicit val m: MonadError[F] = responseMonad
      delegate.send(request).flatMap { response =>
        response
          .header(HeaderNames.WwwAuthenticate)
          .map { wwwAuthHeader =>
            if (response.code == StatusCode.Unauthorized) {
              callWithDigestAuth(request, digestAuthData, response, wwwAuthHeader).getOrElse(response.unit)
            } else {
              response.unit
            }
          }
          .getOrElse(response.unit)
      }
    } else {
      delegate.send(request)
    }
  }

  private def callWithDigestAuth[T](
      request: Request[T, S],
      digestAuthData: DigestAuthData,
      response: Response[T],
      wwwAuthHeader: String
  )(implicit m: MonadError[F]) = {
    val parsed = WwwAuthHeaderParser.parse(wwwAuthHeader)
    for {
      realm <- parsed.realm
      nonce <- parsed.nonce
    } yield {
      val authHeaderValue =<commit_msg>Perform digestAuth only if digest schema is present in wwwAuthHeader<commit_after>package sttp.client

import java.nio.charset.Charset
import java.security.MessageDigest

import sttp.client.DigestAuthenticationBackend._
import sttp.client.monad.MonadError
import sttp.client.monad.syntax._
import sttp.client.ws.WebSocketResponse
import sttp.model.{HeaderNames, StatusCode}

import scala.language.higherKinds
import scala.util.Random

//TODO: support auth-int
//TODO: support stale param
//TODO: auth-info
class DigestAuthenticationBackend[F[_], S, WS_HANDLER[_]](delegate: SttpBackend[F, S, WS_HANDLER])
    extends SttpBackend[F, S, WS_HANDLER] {
  override def send[T](request: Request[T, S]): F[Response[T]] = {
    if (request.tag(DigestAuthTag).isDefined) {
      val digestAuthData = request.tag(DigestAuthTag).get.asInstanceOf[DigestAuthData]
      implicit val m: MonadError[F] = responseMonad
      delegate.send(request).flatMap { response =>
        response
          .header(HeaderNames.WwwAuthenticate)
          .map { wwwAuthHeader =>
            if (response.code == StatusCode.Unauthorized && wwwAuthHeader.contains("Digest")) {
              callWithDigestAuth(request, digestAuthData, response, wwwAuthHeader).getOrElse(response.unit)
            } else {
              response.unit
            }
          }
          .getOrElse(response.unit)
      }
    } else {
      delegate.send(request)
    }
  }

  private def callWithDigestAuth[T](
      request: Request[T, S],
      digestAuthData: DigestAuthData,
      response: Response[T],
      wwwAuthHeader: String
  )(implicit m: MonadError[F]) = {
    val parsed = WwwAuthHeaderParser.parse(wwwAuthHeader)
    for {
      realm <- parsed.realm
      nonce <- parsed.nonce
    } yield {
      val authHeaderValue =
{'fraction_non_alphanumeric': 0.1057609630266552, 'fraction_numerical': 0.0, 'mean_word_length': 3.179640718562874, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, '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': '2145217', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1086, 'n_words': 212}