Skip to content

Commit

Permalink
Merge pull request #892 from softwaremill/auth-lowercase
Browse files Browse the repository at this point in the history
Fixes #890: Accept authorization type in lower case
  • Loading branch information
adamw authored Dec 21, 2020
2 parents e787bf9 + 7e4c781 commit 43bd2c0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
9 changes: 5 additions & 4 deletions core/src/main/scala/sttp/tapir/Mapping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ object Mapping {
override def validator: Validator[H] = Validator.pass
}

/** A mapping which, during encoding, adds the given `prefix`. When decoding, the prefix is removed (if present),
* otherwise an error is reported.
/** A mapping which, during encoding, adds the given `prefix`. When decoding, the prefix is removed (case
* insensitive,if present), otherwise an error is reported.
*/
def stringPrefix(prefix: String): Mapping[String, String] = {
def stringPrefixCaseInsensitive(prefix: String): Mapping[String, String] = {
val prefixLength = prefix.length
val prefixLower = prefix.toLowerCase
def removePrefix(v: String): DecodeResult[String] =
if (v.startsWith(prefix)) DecodeResult.Value(v.substring(prefixLength))
if (v.toLowerCase.startsWith(prefixLower)) DecodeResult.Value(v.substring(prefixLength))
else DecodeResult.Error(v, new IllegalArgumentException(s"The given value doesn't start with $prefix"))
Mapping.fromDecode(removePrefix)(v => s"$prefix$v")
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/sttp/tapir/TapirAuth.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ object TapirAuth {
EndpointInput.Auth.Http(authType, header[T]("Authorization")(authCodec), challenge, None)
}

private def stringPrefixWithSpace(prefix: String) = Mapping.stringPrefix(prefix + " ")
private def stringPrefixWithSpace(prefix: String) = Mapping.stringPrefixCaseInsensitive(prefix + " ")
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ServerAuthenticationTests[F[_], S, ROUTE](backend: SttpBackend[IO, Any], s
List(
("basic", basic, (r: Request[_, Any]) => r.header("Authorization", "Basic dXNlcjpzZWNyZXQ=")),
("bearer", bearer, (r: Request[_, Any]) => r.header("Authorization", "Bearer kajsdhf[")),
("lower case bearer", bearer, (r: Request[_, Any]) => r.header("Authorization", "bearer kajsdhf[")),
(
"apiKey in query param",
apiKeyInQuery,
Expand All @@ -59,7 +60,7 @@ class ServerAuthenticationTests[F[_], S, ROUTE](backend: SttpBackend[IO, Any], s

private def expectedChallenge(authType: String) = authType match {
case "basic" => s"""Basic realm="$Realm""""
case "bearer" => s"""Bearer realm="$Realm""""
case "bearer" | "lower case bearer" => s"""Bearer realm="$Realm""""
case "apiKey in query param" | "apiKey in header" => s"""ApiKey realm="$Realm""""
}

Expand Down

0 comments on commit 43bd2c0

Please sign in to comment.