Skip to content

Commit

Permalink
Felles ByUserIdStrategy for unleash next
Browse files Browse the repository at this point in the history
  • Loading branch information
olekvernberg committed Aug 31, 2023
1 parent 3bcaf53 commit 94b2916
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions unleash/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<artifactId>unleash-client-java</artifactId>
<version>8.2.1</version>
</dependency>
<dependency>
<groupId>no.nav.security</groupId>
<artifactId>token-validation-spring</artifactId>
<version>${nav.security.token.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
36 changes: 36 additions & 0 deletions unleash/src/main/kotlin/no/nav/familie/unleash/ByUserIdStrategy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package no.nav.familie.unleash

import io.getunleash.strategy.Strategy
import no.nav.security.token.support.spring.SpringTokenValidationContextHolder

class ByUserIdStrategy : Strategy {
val SYSTEM_FORKORTELSE = "VL"
fun hentSaksbehandlerEllerSystembruker() =
Result.runCatching { SpringTokenValidationContextHolder().tokenValidationContext }
.fold(
onSuccess = {
it.getClaims("azuread")?.get("NAVident")?.toString() ?: SYSTEM_FORKORTELSE
},
onFailure = { SYSTEM_FORKORTELSE }
)

fun hentSaksbehandler(): String {
val result = hentSaksbehandlerEllerSystembruker()

if (result == SYSTEM_FORKORTELSE) {
error("Finner ikke NAVident i token")
}
return result
}

override fun getName(): String {
return "byUserId"
}

override fun isEnabled(map: MutableMap<String, String>): Boolean {
return map["user"]
?.split(',')
?.any { hentSaksbehandler() == it }
?: false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class DefaultUnleashService(
.appName(appName)
.unleashAPI("$apiUrl/api")
.apiKey(apiToken)
.unleashContextProvider(lagUnleashContextProvider()).build()
.unleashContextProvider(lagUnleashContextProvider()).build(),
ByUserIdStrategy()
)
}

Expand Down

0 comments on commit 94b2916

Please sign in to comment.