-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
170570f
commit 6fb7666
Showing
14 changed files
with
331 additions
and
16 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/no/digdir/service_catalog/security/EndpointPermissions.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,36 @@ | ||
package no.digdir.service_catalog.security | ||
|
||
import org.springframework.security.oauth2.jwt.Jwt | ||
import org.springframework.stereotype.Service | ||
|
||
private const val ROLE_ROOT_ADMIN = "system:root:admin" | ||
private fun roleOrgAdmin(orgnr: String) = "organization:$orgnr:admin" | ||
private fun roleOrgWrite(orgnr: String) = "organization:$orgnr:write" | ||
private fun roleOrgRead(orgnr: String) = "organization:$orgnr:read" | ||
|
||
@Service | ||
class EndpointPermissions { | ||
fun hasOrgReadPermission(jwt: Jwt, orgnr: String): Boolean { | ||
val authorities: String? = jwt.claims["authorities"] as? String | ||
|
||
return when { | ||
authorities == null -> false | ||
authorities.contains(roleOrgAdmin(orgnr)) -> true | ||
authorities.contains(roleOrgWrite(orgnr)) -> true | ||
authorities.contains(roleOrgRead(orgnr)) -> true | ||
authorities.contains(ROLE_ROOT_ADMIN) -> true | ||
else -> false | ||
} | ||
} | ||
|
||
fun hasOrgWritePermission(jwt: Jwt, orgnr: String): Boolean { | ||
val authorities: String? = jwt.claims["authorities"] as? String | ||
|
||
return when { | ||
authorities == null -> false | ||
authorities.contains(roleOrgAdmin(orgnr)) -> true | ||
authorities.contains(roleOrgWrite(orgnr)) -> true | ||
else -> false | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/no/digdir/service_catalog/security/SecurityConfig.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,39 @@ | ||
package no.digdir.service_catalog.security | ||
|
||
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity | ||
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator | ||
import org.springframework.security.oauth2.jwt.* | ||
import org.springframework.security.oauth2.jwt.JwtClaimNames.AUD | ||
import org.springframework.security.web.SecurityFilterChain | ||
|
||
@Configuration | ||
open class SecurityConfig { | ||
@Bean | ||
open fun filterChain(http: HttpSecurity): SecurityFilterChain { | ||
http.authorizeHttpRequests { authorize -> | ||
authorize.requestMatchers(HttpMethod.OPTIONS).permitAll() | ||
.requestMatchers(HttpMethod.GET, "/actuator/health/readiness").permitAll() | ||
.requestMatchers(HttpMethod.GET, "/actuator/health/liveness").permitAll() | ||
.anyRequest().authenticated() } | ||
.oauth2ResourceServer { resourceServer -> resourceServer.jwt() } | ||
return http.build() | ||
} | ||
|
||
@Bean | ||
open fun jwtDecoder(properties: OAuth2ResourceServerProperties): JwtDecoder { | ||
val jwtDecoder = NimbusJwtDecoder.withJwkSetUri(properties.jwt.jwkSetUri).build() | ||
jwtDecoder.setJwtValidator( | ||
DelegatingOAuth2TokenValidator( | ||
JwtTimestampValidator(), | ||
JwtIssuerValidator(properties.jwt.issuerUri), | ||
JwtClaimValidator(AUD) { aud: List<String> -> aud.contains("service-catalog") } | ||
) | ||
) | ||
return jwtDecoder | ||
} | ||
|
||
} |
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
27 changes: 27 additions & 0 deletions
27
src/test/kotlin/no/digdir/service_catalog/utils/WireMock.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,27 @@ | ||
package no.digdir.service_catalog.utils | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.client.WireMock.* | ||
import no.digdir.service_catalog.utils.jwt.JwkStore | ||
|
||
private val mockserver = WireMockServer(5050) | ||
|
||
fun startMockServer() { | ||
if(!mockserver.isRunning) { | ||
mockserver.stubFor(get(urlEqualTo("/ping")) | ||
.willReturn(aResponse() | ||
.withStatus(200)) | ||
) | ||
|
||
mockserver.stubFor(get(urlEqualTo("/auth/realms/fdk/protocol/openid-connect/certs")) | ||
.willReturn(okJson(JwkStore.get()))) | ||
|
||
mockserver.start() | ||
} | ||
} | ||
|
||
fun stopMockServer() { | ||
|
||
if (mockserver.isRunning) mockserver.stop() | ||
|
||
} |
Oops, something went wrong.