All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spice.http.server.validation.IPAddressValidator.scala Maven / Gradle / Ivy

package spice.http.server.validation

import cats.effect.IO
import spice.http.{HttpExchange, HttpStatus}
import spice.net.IP

class IPAddressValidator(allow: Set[IP], reject: Set[IP], defaultAllow: Boolean) extends Validator {
  override def validate(exchange: HttpExchange): IO[ValidationResult] = IO {
    val ip = exchange.request.originalSource
    if ((allow.contains(ip) || defaultAllow) && !reject.contains(ip)) {
      ValidationResult.Continue(exchange)
    } else {
      scribe.warn(s"Unauthorized attempt to access: ${exchange.request.url} from IP: $ip. Allowed: ${allow.mkString(", ")}, Reject: ${reject.mkString(", ")}, Default Allow? $defaultAllow")
      ValidationResult.Error(exchange, HttpStatus.Forbidden.code, s"Unauthorized IP address: $ip")
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy