Skip to content
Snippets Groups Projects
Commit afc232f5 authored by François Henri Théron's avatar François Henri Théron
Browse files

style: removing brackets

parent 4529e19a
No related branches found
No related tags found
1 merge request!41feat: decorator for admin endpoint authentication
...@@ -23,23 +23,21 @@ object decorators: ...@@ -23,23 +23,21 @@ object decorators:
cask.Response(f"Invalid or missing 'Origin' header: must match the Host", 403) cask.Response(f"Invalid or missing 'Origin' header: must match the Host", 403)
class adminAuth(expectedAdminKeyOpt: Option[String]) extends cask.router.Decorator[cask.Response[JsonData], cask.Response[JsonData], Any]: class adminAuth(expectedAdminKeyOpt: Option[String]) extends cask.router.Decorator[cask.Response[JsonData], cask.Response[JsonData], Any]:
private val expectedAuthOpt: Option[String] = expectedAdminKeyOpt.map { key => private val expectedAuthOpt: Option[String] = expectedAdminKeyOpt.map:
s"Basic ${Base64.getEncoder.encodeToString(s"$key:".getBytes("UTF-8"))}" key => s"Basic ${Base64.getEncoder.encodeToString(s"$key:".getBytes("UTF-8"))}"
}
private val expectedAuthBytesOpt: Option[Array[Byte]] = expectedAuthOpt.map(_.getBytes("UTF-8")) private val expectedAuthBytesOpt: Option[Array[Byte]] = expectedAuthOpt.map(_.getBytes("UTF-8"))
override def wrapFunction(request: cask.Request, delegate: Delegate) = override def wrapFunction(request: cask.Request, delegate: Delegate) =
val authHeaderOpt: Option[String] = request.headers.get("authorization").flatMap(_.headOption) val authHeaderOpt: Option[String] = request.headers.get("authorization").flatMap(_.headOption)
val authHeaderBytesOpt: Option[Array[Byte]] = authHeaderOpt.map(_.getBytes("UTF-8")) val authHeaderBytesOpt: Option[Array[Byte]] = authHeaderOpt.map(_.getBytes("UTF-8"))
// Perform the check using constant time comparison // Perform the check using constant time comparison
val authorized = (expectedAuthBytesOpt, authHeaderBytesOpt) match { val authorized = (expectedAuthBytesOpt, authHeaderBytesOpt) match
case (Some(expectedBytes), Some(actualBytes)) => case (Some(expectedBytes), Some(actualBytes)) =>
MessageDigest.isEqual(actualBytes, expectedBytes) MessageDigest.isEqual(actualBytes, expectedBytes)
case _ => case _ =>
// Either expected key wasn't configured OR header was missing/malformed // Either expected key wasn't configured OR header was missing/malformed
false false
}
if (authorized){ if (authorized){
delegate(Map()) delegate(Map())
} else { } else {
...@@ -49,5 +47,4 @@ object decorators: ...@@ -49,5 +47,4 @@ object decorators:
statusCode = 401, statusCode = 401,
headers = Seq("WWW-Authenticate" -> "Basic realm=\"Admin API\"") headers = Seq("WWW-Authenticate" -> "Basic realm=\"Admin API\"")
) )
} }
\ No newline at end of file
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment