-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add support for request/response interceptors, support CORS …
…with credentials (#199) * feat: add request/response interceptor feature to route handling * feat: move CORS functionality into CorsInterceptor
- Loading branch information
1 parent
cf05496
commit dac8b61
Showing
8 changed files
with
205 additions
and
60 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/no/nav/security/mock/oauth2/http/CorsInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package no.nav.security.mock.oauth2.http | ||
|
||
import mu.KotlinLogging | ||
|
||
private val log = KotlinLogging.logger {} | ||
|
||
class CorsInterceptor( | ||
private val allowedMethods: List<String> = listOf("POST", "GET", "OPTIONS") | ||
) : ResponseInterceptor { | ||
|
||
companion object HeaderNames { | ||
const val ORIGIN = "origin" | ||
const val ACCESS_CONTROL_ALLOW_CREDENTIALS = "access-control-allow-credentials" | ||
const val ACCESS_CONTROL_REQUEST_HEADERS = "access-control-request-headers" | ||
const val ACCESS_CONTROL_ALLOW_HEADERS = "access-control-allow-headers" | ||
const val ACCESS_CONTROL_ALLOW_METHODS = "access-control-allow-methods" | ||
const val ACCESS_CONTROL_ALLOW_ORIGIN = "access-control-allow-origin" | ||
} | ||
|
||
override fun intercept(request: OAuth2HttpRequest, response: OAuth2HttpResponse): OAuth2HttpResponse { | ||
val origin = request.headers[ORIGIN] | ||
log.debug("intercept response if request origin header is set: $origin") | ||
return if (origin != null) { | ||
val headers = response.headers.newBuilder() | ||
if (request.method == "OPTIONS") { | ||
val reqHeader = request.headers[ACCESS_CONTROL_REQUEST_HEADERS] | ||
if (reqHeader != null) { | ||
headers[ACCESS_CONTROL_ALLOW_HEADERS] = reqHeader | ||
} | ||
headers[ACCESS_CONTROL_ALLOW_METHODS] = allowedMethods.joinToString(", ") | ||
} | ||
headers[ACCESS_CONTROL_ALLOW_ORIGIN] = origin | ||
headers[ACCESS_CONTROL_ALLOW_CREDENTIALS] = "true" | ||
log.debug("adding CORS response headers") | ||
response.copy(headers = headers.build()) | ||
} else { | ||
response | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.