-
Notifications
You must be signed in to change notification settings - Fork 2
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 #4461 from navikt/flytt-altinn-acl
Flytt altinn acl
- Loading branch information
Showing
28 changed files
with
739 additions
and
140 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...rovider/src/main/kotlin/no/nav/mulighetsrommet/tokenprovider/MaskinPortenTokenProvider.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,100 @@ | ||
package no.nav.mulighetsrommet.tokenprovider | ||
|
||
import com.nimbusds.jose.JWSSigner | ||
import com.nimbusds.jose.crypto.RSASSASigner | ||
import com.nimbusds.jose.jwk.RSAKey | ||
import com.nimbusds.jwt.JWTClaimsSet | ||
import com.nimbusds.oauth2.sdk.JWTBearerGrant | ||
import com.nimbusds.oauth2.sdk.Scope | ||
import com.nimbusds.oauth2.sdk.TokenRequest | ||
import com.nimbusds.oauth2.sdk.TokenResponse | ||
import no.nav.common.token_client.utils.TokenClientUtils | ||
import org.slf4j.LoggerFactory | ||
import java.net.URI | ||
import java.util.* | ||
|
||
class MaskinPortenTokenProvider( | ||
private val clientId: String, | ||
private val issuer: String, | ||
tokenEndpointUrl: String, | ||
privateJwk: String, | ||
) { | ||
private val log = LoggerFactory.getLogger(javaClass) | ||
private val tokenEndpoint: URI | ||
private val privateJwkKeyId: String | ||
private val assertionSigner: JWSSigner | ||
|
||
data class Config( | ||
val clientId: String, | ||
val issuer: String, | ||
val tokenEndpointUrl: String, | ||
val privateJwk: String, | ||
) | ||
|
||
init { | ||
val rsaKey = RSAKey.parse(privateJwk) | ||
|
||
tokenEndpoint = URI.create(tokenEndpointUrl) | ||
privateJwkKeyId = rsaKey.keyID | ||
assertionSigner = RSASSASigner(rsaKey) | ||
} | ||
|
||
private fun createToken(scope: String, targetAudience: String): String { | ||
val signedJwt = TokenClientUtils.signedClientAssertion( | ||
TokenClientUtils.clientAssertionHeader(privateJwkKeyId), | ||
clientAssertionClaims(targetAudience = targetAudience, scope = scope), | ||
assertionSigner, | ||
) | ||
|
||
val request = | ||
TokenRequest( | ||
tokenEndpoint, | ||
JWTBearerGrant(signedJwt.clientAssertion), | ||
Scope(*(scope.split(" ")).toTypedArray()), | ||
) | ||
|
||
val response = TokenResponse.parse(request.toHTTPRequest().send()) | ||
|
||
if (!response.indicatesSuccess()) { | ||
val tokenErrorResponse = response.toErrorResponse() | ||
log.error( | ||
"Failed to fetch Maskinporten M2M token for scope={}. Error: {}", | ||
scope, | ||
tokenErrorResponse.toJSONObject().toString(), | ||
) | ||
throw RuntimeException("Failed to fetch Maskinporten M2M token for scope=$scope") | ||
} | ||
|
||
return response | ||
.toSuccessResponse() | ||
.tokens | ||
.accessToken | ||
.value | ||
} | ||
|
||
fun withScope(scope: String, targetAudience: String): M2MTokenProvider { | ||
return M2MTokenProvider exchange@{ accessType -> | ||
createToken(scope, targetAudience) | ||
} | ||
} | ||
|
||
fun clientAssertionClaims( | ||
targetAudience: String, | ||
scope: String, | ||
): JWTClaimsSet { | ||
val now = Date() | ||
val expiration = Date(now.toInstant().plusSeconds(30).toEpochMilli()) | ||
|
||
return JWTClaimsSet.Builder() | ||
.subject(clientId) | ||
.issuer(clientId) | ||
.audience(issuer) | ||
.jwtID(UUID.randomUUID().toString()) | ||
.issueTime(now) | ||
.notBeforeTime(now) | ||
.expirationTime(expiration) | ||
.claim("resource", targetAudience) | ||
.claim("scope", scope) | ||
.build() | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { mulighetsrommetAltinnAclHandlers } from "./altinnMocks"; | ||
import { refusjonHandlers } from "./refusjonMocks"; | ||
|
||
export const handlers = [...mulighetsrommetAltinnAclHandlers, ...refusjonHandlers]; | ||
export const handlers = [...refusjonHandlers]; |
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.