-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from navikt/aktor-bytte
Resttjeneste for aktørbytte
- Loading branch information
Showing
15 changed files
with
406 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ internal class SecurityConfiguration | |
|
||
object Issuers { | ||
const val TOKEN_X = "tokenx" | ||
const val AZURE = "azure" | ||
} |
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
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/no/nav/sifinnsynapi/forvaltning/AktørBytteController.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,47 @@ | ||
package no.nav.sifinnsynapi.forvaltning | ||
|
||
import no.nav.security.token.support.core.api.ProtectedWithClaims | ||
import no.nav.security.token.support.core.api.RequiredIssuers | ||
import no.nav.sifinnsynapi.common.AktørId | ||
import no.nav.sifinnsynapi.config.Issuers | ||
import no.nav.sifinnsynapi.sikkerhet.AuthorizationService | ||
import no.nav.sifinnsynapi.sikkerhet.ContextHolder | ||
import no.nav.sifinnsynapi.soknad.SøknadService | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.ProblemDetail | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.ErrorResponseException | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequiredIssuers( | ||
ProtectedWithClaims(issuer = Issuers.AZURE) | ||
) | ||
class AktørBytteController( | ||
private val søknadService: SøknadService, | ||
private val authorizationService: AuthorizationService | ||
) { | ||
|
||
@PostMapping( | ||
"/forvaltning/oppdaterAktoerId", | ||
consumes = [MediaType.APPLICATION_JSON_VALUE], | ||
produces = [MediaType.APPLICATION_JSON_VALUE] | ||
) | ||
@ProtectedWithClaims(issuer = ContextHolder.AZURE_AD, claimMap = ["NAVident=*"]) | ||
fun oppdaterAktoerId(@RequestBody aktørBytteRequest: AktørBytteRequest): ResponseEntity<AktørBytteRespons> { | ||
if (!authorizationService.harTilgangTilDriftRolle()) { | ||
val problemDetail = ProblemDetail.forStatus(HttpStatus.FORBIDDEN) | ||
problemDetail.detail = "Mangler driftsrolle" | ||
throw ErrorResponseException(HttpStatus.FORBIDDEN, problemDetail, null) | ||
} | ||
val antallOppdaterte = søknadService.oppdaterAktørId( | ||
AktørId(aktørBytteRequest.gyldigAktør), | ||
AktørId(aktørBytteRequest.utgåttAktør) | ||
) | ||
return ResponseEntity.ok(AktørBytteRespons(antallOppdaterte)) | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/no/nav/sifinnsynapi/forvaltning/AktørBytteRequest.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,19 @@ | ||
package no.nav.sifinnsynapi.forvaltning | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import jakarta.validation.constraints.Pattern | ||
import jakarta.validation.constraints.Size | ||
import org.jetbrains.annotations.NotNull | ||
|
||
data class AktørBytteRequest( | ||
@JsonProperty | ||
@NotNull | ||
@Size(max = 20) | ||
@Pattern(regexp = "^\\d+$", message = "AktørId [\${validatedValue}] matcher ikke tillatt pattern [{regexp}]") | ||
val utgåttAktør: String, | ||
@JsonProperty | ||
@NotNull | ||
@Size(max = 20) | ||
@Pattern(regexp = "^\\d+$", message = "AktørId [\${validatedValue}] matcher ikke tillatt pattern [{regexp}]") | ||
val gyldigAktør: String | ||
) |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/no/nav/sifinnsynapi/forvaltning/AktørBytteRespons.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,13 @@ | ||
package no.nav.sifinnsynapi.forvaltning | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import jakarta.validation.constraints.Pattern | ||
import jakarta.validation.constraints.Size | ||
import org.jetbrains.annotations.NotNull | ||
|
||
data class AktørBytteRespons( | ||
@JsonProperty | ||
@NotNull | ||
val antallOppdaterteRader: Int, | ||
|
||
) |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/no/nav/sifinnsynapi/sikkerhet/AuthorizationService.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,22 @@ | ||
package no.nav.sifinnsynapi.sikkerhet | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
class AuthorizationService( | ||
private val contextHolder: ContextHolder, | ||
private val k9DriftGruppeId: String, | ||
) { | ||
fun harTilgangTilDriftRolle(): Boolean { | ||
return contextHolder.requestKontekst()?.jwtToken?.containsClaim("groups", k9DriftGruppeId) ?: false | ||
} | ||
} | ||
|
||
@Configuration | ||
class AuthorizationConfig( | ||
@Value("\${no.nav.security.k9-drift-gruppe}") private val k9DriftGruppeId: String, | ||
) { | ||
@Bean | ||
fun authorizationService() = AuthorizationService(ContextHolder.INSTANCE, k9DriftGruppeId) | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/no/nav/sifinnsynapi/sikkerhet/ContextHolder.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.nav.sifinnsynapi.sikkerhet | ||
|
||
import no.nav.security.token.support.core.jwt.JwtToken | ||
import no.nav.security.token.support.spring.SpringTokenValidationContextHolder | ||
import org.springframework.web.context.request.RequestContextHolder | ||
|
||
class ContextHolder private constructor(private val context: SpringTokenValidationContextHolder) { | ||
|
||
companion object { | ||
const val AZURE_AD = "azure" | ||
private var instans: ContextHolder? = null | ||
val INSTANCE: ContextHolder | ||
get() { | ||
if (instans == null) { | ||
instans = ContextHolder(SpringTokenValidationContextHolder()) | ||
} | ||
return instans!! | ||
} | ||
|
||
} | ||
|
||
fun requestKontekst(): RequestKontekst? { | ||
if (RequestContextHolder.getRequestAttributes() == null) | ||
return null | ||
|
||
val tokenContext = context.getTokenValidationContext() | ||
val reqIssuerShortNames = tokenContext.issuers //alle issuers på alle validerte tokens i context | ||
if (reqIssuerShortNames.contains(AZURE_AD)) { | ||
val jwtToken: JwtToken? = tokenContext.getJwtToken(AZURE_AD) | ||
return jwtToken?.let { RequestKontekst(it, AZURE_AD) } | ||
} | ||
return null | ||
} | ||
|
||
@JvmRecord | ||
data class RequestKontekst(val jwtToken: JwtToken, val issuerShortname: String) | ||
|
||
|
||
} |
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.