File tree Expand file tree Collapse file tree 4 files changed +54
-2
lines changed
main/java/nl/myndocs/oauth2
test/java/nl/myndocs/oauth2/request/auth Expand file tree Collapse file tree 4 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -213,7 +213,7 @@ class CallRouter(
213213 return
214214 }
215215
216- val authorization = callContext.headers[ " Authorization" ]
216+ val authorization = callContext.headerCaseInsensitive( " Authorization" )
217217
218218 if (authorization == null || ! authorization.startsWith(" bearer " , true )) {
219219 callContext.respondStatus(STATUS_UNAUTHORIZED )
Original file line number Diff line number Diff line change 1+ package nl.myndocs.oauth2.request
2+
3+ fun CallContext.headerCaseInsensitive (key : String ) =
4+ headers
5+ .filter { it.key.equals(key, true ) }
6+ .values
7+ .firstOrNull()
Original file line number Diff line number Diff line change @@ -3,10 +3,13 @@ package nl.myndocs.oauth2.request.auth
33import nl.myndocs.oauth2.authenticator.Authorizer
44import nl.myndocs.oauth2.authenticator.Credentials
55import nl.myndocs.oauth2.request.CallContext
6+ import nl.myndocs.oauth2.request.headerCaseInsensitive
67
8+ // @TODO: BasicAuth should be injected instead of static call
79open class BasicAuthorizer (protected val context : CallContext ) : Authorizer {
810 override fun extractCredentials (): Credentials ? {
9- val authorizationHeader = context.headers[" authorization" ] ? : " "
11+ val authorizationHeader = context.headerCaseInsensitive(" authorization" ) ? : " "
12+
1013 return BasicAuth .parseCredentials(authorizationHeader)
1114 }
1215
Original file line number Diff line number Diff line change 1+ package nl.myndocs.oauth2.request.auth
2+
3+ import io.mockk.every
4+ import io.mockk.mockk
5+ import nl.myndocs.oauth2.request.CallContext
6+ import org.hamcrest.CoreMatchers.*
7+ import org.hamcrest.MatcherAssert.assertThat
8+ import org.junit.jupiter.api.Test
9+ import java.util.*
10+
11+ internal class BasicAuthorizerTest {
12+
13+ @Test
14+ fun `test authorization head is case insensitive with all uppercase input` () {
15+ `test authorization head is case insensitive with input`(
16+ " AUTHORIZATION"
17+ )
18+ }
19+
20+ @Test
21+ fun `test authorization head is case insensitive with all lowercase input` () {
22+ `test authorization head is case insensitive with input`(
23+ " authorization"
24+ )
25+ }
26+
27+ private fun `test authorization head is case insensitive with input` (authorizationKeyName : String ) {
28+ val callContext = mockk<CallContext >()
29+ val username = " test"
30+ val password = " test-password"
31+
32+ val testCredentials = Base64 .getEncoder().encodeToString(" $username :$password " .toByteArray())
33+
34+ every { callContext.headers } returns mapOf (authorizationKeyName to " basic $testCredentials " )
35+ val credentials = BasicAuthorizer (callContext)
36+ .extractCredentials()
37+
38+ assertThat(credentials, `is `(notNullValue()))
39+ assertThat(credentials!! .username, `is `(equalTo(username)))
40+ assertThat(credentials.password, `is `(equalTo(password)))
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments