Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Konvertert til Kotlin og JDK 21, i tillegg en masse tender love & care. #812

Merged
merged 76 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
86d8b94
første konverteringer
jan-olaveide Dec 4, 2023
5857936
oversatt HttpRequest
jan-olaveide Dec 5, 2023
0734c63
translated JwtToken
jan-olaveide Dec 5, 2023
bef4c25
translated JwtTokenClains
jan-olaveide Dec 5, 2023
e6bfe3b
translated JwtTokenClains
jan-olaveide Dec 5, 2023
4589fa7
moved util to kotlin directory
jan-olaveide Dec 5, 2023
dbb1a88
inkomplett
jan-olaveide Dec 5, 2023
1513bd0
convert multiissuer
jan-olaveide Dec 6, 2023
ca7a559
convert more config
jan-olaveide Dec 6, 2023
1a1a184
convert more config
jan-olaveide Dec 6, 2023
58bd0a0
convert more config
jan-olaveide Dec 6, 2023
ffb7ee8
convert more config
jan-olaveide Dec 6, 2023
611a928
logback
jan-olaveide Dec 6, 2023
19c0969
logback
jan-olaveide Dec 6, 2023
40a0cb4
log
jan-olaveide Dec 6, 2023
90593e5
Supply default values for JWKSCache, fix tests
jan-olaveide Dec 6, 2023
0d3a217
fjernet deprecated validatorer
jan-olaveide Dec 6, 2023
ef7bfe9
merge
jan-olaveide Dec 6, 2023
3d4c361
translated JwtTokenValidatorFactory and JwtTokenValidator
jan-olaveide Dec 6, 2023
6728507
context is nullable
jan-olaveide Dec 6, 2023
cdead5f
translated JwtTokenValidationHandler
jan-olaveide Dec 6, 2023
eab1f62
translated JwtTokenRetriever
jan-olaveide Dec 7, 2023
87d53e0
converted DefaultConfigurableJwtValidator
jan-olaveide Dec 7, 2023
f5e0ba4
translated ProxyAwareResourceRetriever
jan-olaveide Dec 7, 2023
5081533
tranlated a test
jan-olaveide Dec 7, 2023
e1ac9d6
translated a test
jan-olaveide Dec 7, 2023
2affee6
translated a test
jan-olaveide Dec 7, 2023
1ee33ab
translated a test
jan-olaveide Dec 7, 2023
bc662f9
translated a test
jan-olaveide Dec 7, 2023
de49e60
translated a test
jan-olaveide Dec 7, 2023
65f19bc
converted final tests
jan-olaveide Dec 7, 2023
2897014
idiomatic kotlin
jan-olaveide Dec 8, 2023
568918a
idiomatic kotlin
jan-olaveide Dec 8, 2023
cd42096
more cleaning
jan-olaveide Dec 9, 2023
9b983d9
more cleaning
jan-olaveide Dec 9, 2023
8805c18
more cleanup
jan-olaveide Dec 10, 2023
45e92bd
more cleanup
jan-olaveide Dec 10, 2023
a49eeb0
more cleanup
jan-olaveide Dec 11, 2023
4d28284
more cleanup
jan-olaveide Dec 11, 2023
38ead67
more cleanup
jan-olaveide Dec 11, 2023
423266c
fix ktor2 tests
jan-olaveide Dec 11, 2023
8c93371
cleanup
jan-olaveide Dec 12, 2023
4c47603
log
jan-olaveide Dec 12, 2023
680773f
no proxy for test
jan-olaveide Dec 12, 2023
44c7972
no proxy for test
jan-olaveide Dec 12, 2023
2c9ed79
no proxy for test
jan-olaveide Dec 12, 2023
e5c8543
Ingere proxy retriever i test
jan-olaveide Dec 12, 2023
3269698
Ingen proxy retriever i test
jan-olaveide Dec 12, 2023
0b94cab
proxy
jan-olaveide Dec 12, 2023
beac804
proxy
jan-olaveide Dec 12, 2023
0abae46
proxy
jan-olaveide Dec 12, 2023
3001ab6
more fix
jan-olaveide Dec 13, 2023
8ec4951
nesten token-client-kotlin-demo
jan-olaveide Dec 15, 2023
7347cd0
fix demo
jan-olaveide Dec 16, 2023
a5f7a63
Merge branch 'master' into kotlin-core
jan-olaveide Dec 16, 2023
0d00b20
more cleanup
jan-olaveide Dec 16, 2023
0c13f38
fix more
jan-olaveide Dec 17, 2023
7191311
fix more
jan-olaveide Dec 18, 2023
0c71c8a
17
jan-olaveide Dec 18, 2023
a5b31e8
kotlin
jan-olaveide Dec 18, 2023
ac726c9
converted jaxrs
jan-olaveide Dec 19, 2023
848695c
port to kotlin
jan-olaveide Dec 20, 2023
9058c34
port to kotlin
jan-olaveide Dec 20, 2023
f61edcd
tomcat out
jan-olaveide Dec 20, 2023
40bc032
pom fix!
jan-olaveide Dec 20, 2023
229014b
Merge branch 'master' into kotlin-core
jan-olaveide Dec 20, 2023
4801879
cosmetics
jan-olaveide Dec 20, 2023
de7123b
cosmetics
jan-olaveide Dec 20, 2023
257b2b9
try running the test
jan-olaveide Dec 20, 2023
8dd8a9c
remove unused
jan-olaveide Dec 22, 2023
33b475b
bump
jan-olaveide Dec 22, 2023
29b14d6
fix tests
jan-olaveide Dec 22, 2023
771dee7
fjernet resteasy test
jan-olaveide Jan 2, 2024
d3f5b9c
cleanup
jan-olaveide Jan 3, 2024
6a81aab
jdk 21
jan-olaveide Jan 3, 2024
f23ae03
Merge branch 'master' into kotlin-core
jan-olaveide Jan 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
- name: Checkout latest code
uses: actions/checkout@v4

- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 17
java-version: 21
distribution: temurin
- name: Setup build cache
uses: actions/cache@v3
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setter opp Java 17
- name: Setter opp Java 21
uses: actions/setup-java@v4
with:
java-version: 17
java-version: 21
distribution: temurin
cache: maven

Expand All @@ -50,4 +50,4 @@ jobs:
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
category: "/language:${{matrix.language}}"
6 changes: 3 additions & 3 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
- name: Checkout latest code
uses: actions/checkout@v4

- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 17
java-version: 21
distribution: temurin
cache: maven

Expand Down Expand Up @@ -51,4 +51,4 @@ jobs:
# run: |
# git config user.email "[email protected]"
# git config user.name "GitHub Actions release"
# ./mvnw --settings .github/settings.xml -Pgithub --batch-mode -Dmaven.main.skip=true -Dmaven.test.skip=true deploy
# ./mvnw --settings .github/settings.xml -Pgithub --batch-mode -Dmaven.main.skip=true -Dmaven.test.skip=true deploy
7 changes: 3 additions & 4 deletions .github/workflows/test-pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
with:
fetch-depth: 0

- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 17
java-version: 21
distribution: temurin

- name: Setup build cache
Expand All @@ -30,5 +30,4 @@ jobs:
GITHUB_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: mvn -B test

run: mvn -B test
2 changes: 1 addition & 1 deletion .java-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17
21.0.1
56 changes: 16 additions & 40 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<module>token-validation-spring-test</module>
<module>token-validation-jaxrs</module>
<module>token-validation-spring-demo</module>
<module>token-validation-ktor</module>
<module>token-validation-ktor-v2</module>
<module>token-validation-ktor-demo</module>
<module>token-client-spring</module>
Expand All @@ -42,6 +41,7 @@
<module>token-client-core</module>
</modules>
<properties>
<kotlin.version>1.9.22</kotlin.version>
<kotlin-coroutines.version>1.6.2</kotlin-coroutines.version>
<doclint>none</doclint>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
Expand All @@ -55,63 +55,26 @@
<spring-boot.version>3.2.1</spring-boot.version>
<oauth2-oidc-sdk.version>11.9</oauth2-oidc-sdk.version>
<validation-api.version>2.0.1.Final</validation-api.version>
<rest-assured.version>5.4.0</rest-assured.version>
<mockwebserver.version>4.12.0</mockwebserver.version>
<caffeine.version>3.1.8</caffeine.version>
<okhttp3.version>4.12.0</okhttp3.version>
<ktor.version>1.6.8</ktor.version>
<ktor.version>2.3.6</ktor.version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.version>1.9.22</kotlin.version>
<mock-oauth2-server.version>2.1.0</mock-oauth2-server.version>
<nimbus.jose.jwt.version>9.37.3</nimbus.jose.jwt.version>
<kotest.version>5.8.0</kotest.version>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
</properties>

<scm>
<url>https://github.com/navikt/token-support</url>
<connection>scm:git:[email protected]:navikt/token-support.git</connection>
<developerConnection>scm:git:[email protected]:navikt/token-support.git</developerConnection>
<tag>HEAD</tag>
</scm>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
Expand Down Expand Up @@ -262,6 +225,13 @@
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-bom</artifactId>
<version>${kotlin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
Expand Down Expand Up @@ -432,6 +402,12 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito.kotlin</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>5.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
Expand All @@ -442,4 +418,4 @@
<artifactId>nimbus-jose-jwt</artifactId>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.security.token.support.client.core;
package no.nav.security.token.support.client.core

import com.nimbusds.jose.jwk.RSAKey
import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod
Expand Down Expand Up @@ -43,5 +43,5 @@ class ClientAuthenticationProperties @JvmOverloads constructor(val clientId: Str
class ClientAuthenticationPropertiesBuilder @JvmOverloads constructor(private val clientId: String, private val clientAuthMethod: ClientAuthenticationMethod, private var clientSecret: String? = null, private var clientJwk: String? = null) {
fun clientSecret(clientSecret: String)= this.also { it.clientSecret = clientSecret }
fun clientJwk(clientJwk: String)= this.also { it.clientJwk = clientJwk }
fun build() = ClientAuthenticationProperties(clientId, clientAuthMethod, clientSecret, clientJwk);
fun build() = ClientAuthenticationProperties(clientId, clientAuthMethod, clientSecret, clientJwk)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package no.nav.security.token.support.client.core;
package no.nav.security.token.support.client.core

import com.nimbusds.jose.util.DefaultResourceRetriever;
import com.nimbusds.jose.util.DefaultResourceRetriever
import com.nimbusds.oauth2.sdk.GrantType
import com.nimbusds.oauth2.sdk.GrantType.CLIENT_CREDENTIALS
import com.nimbusds.oauth2.sdk.GrantType.JWT_BEARER
import com.nimbusds.oauth2.sdk.GrantType.TOKEN_EXCHANGE
import com.nimbusds.oauth2.sdk.ParseException
import com.nimbusds.oauth2.sdk.`as`.AuthorizationServerMetadata
import java.io.IOException
import java.net.URI

class ClientProperties @JvmOverloads constructor(var tokenEndpointUrl: URI? = null,
private val wellKnownUrl: URI? = null,
val grantType: GrantType,
Expand All @@ -16,8 +20,8 @@ class ClientProperties @JvmOverloads constructor(var tokenEndpointUrl: URI? = nu


init {
tokenEndpointUrl = tokenEndpointUrl ?: endpointUrlFromMetadata(requireNotNull(wellKnownUrl))
require(grantType in GRANT_TYPES) { "Unsupported grantType $grantType, must be one of $GRANT_TYPES" }
tokenEndpointUrl = tokenEndpointUrl ?: endpointUrlFromMetadata(wellKnownUrl)
}


Expand All @@ -30,34 +34,38 @@ class ClientProperties @JvmOverloads constructor(var tokenEndpointUrl: URI? = nu
.tokenExchange(tokenExchange)

companion object {
private val GRANT_TYPES = listOf(GrantType.JWT_BEARER, GrantType.CLIENT_CREDENTIALS, GrantType.TOKEN_EXCHANGE)
private val GRANT_TYPES = listOf(JWT_BEARER, CLIENT_CREDENTIALS, TOKEN_EXCHANGE)

@JvmStatic
fun builder(grantType: GrantType, authentication: ClientAuthenticationProperties) = ClientPropertiesBuilder(grantType, authentication)

private fun endpointUrlFromMetadata(wellKnown: URI?) =
runCatching {
wellKnown?.let { AuthorizationServerMetadata.parse(DefaultResourceRetriever().retrieveResource(wellKnown.toURL()).content).tokenEndpointURI }
?: throw OAuth2ClientException("Well knowcn url cannot be null, please check your configuration")
?: throw OAuth2ClientException("Well-known url cannot be null, please check your configuration")
}.getOrElse {
when(it) {
is ParseException-> throw OAuth2ClientException("Unable to parse response from $wellKnown", it)
is IOException -> throw OAuth2ClientException("Unable to read from $wellKnown", it)
is IOException -> throw OAuth2ClientException("Unable to read from $wellKnown", it)
is OAuth2ClientException -> throw it
else -> throw OAuth2ClientException("Unexpected error reading from $wellKnown", it)
}
}
}

class ClientPropertiesBuilder @JvmOverloads constructor(private val grantType: GrantType, val authentication: ClientAuthenticationProperties,
class ClientPropertiesBuilder @JvmOverloads constructor(private val grantType: GrantType,
val authentication: ClientAuthenticationProperties,
private var tokenEndpointUrl: URI? = null,
private var wellKnownUrl: URI? = null,
private var scope: List<String> = emptyList(),
private var resourceUrl: URI? = null,
private var tokenExchange: TokenExchangeProperties? = null) {

fun tokenEndpointUrl(endpointURI: String?) = endpointURI?.let { tokenEndpointUrl(URI.create(it)) } ?: this
fun tokenEndpointUrl(endpointURI: URI?) = this.also { it.tokenEndpointUrl = endpointURI }
fun wellKnownUrl(wellKnownURI: String?) = wellKnownURI?.let { wellKnownUrl(URI.create(it)) } ?: this
fun wellKnownUrl(wellKnownURI: URI?) = this.also { it.wellKnownUrl = wellKnownURI }
fun scopes(vararg scopes: String) = scope(scopes.toList())
fun scope(scope: List<String>) = this.also { it.scope = scope}
fun resourceUrl(resourceUrl: URI?) = this.also { it.resourceUrl = resourceUrl }
fun tokenExchange(tokenExchange: TokenExchangeProperties?) = this.also { it.tokenExchange = tokenExchange }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package no.nav.security.token.support.client.core

import com.nimbusds.oauth2.sdk.GrantType
import kotlin.DeprecationLevel.WARNING

@Deprecated("Use GrantType from nimbus instead", ReplaceWith("GrantType"), DeprecationLevel.WARNING)
data class OAuth2GrantType(@JvmField val value : String) {
fun value() = value

@Deprecated("Use GrantType from nimbus instead", ReplaceWith("GrantType"), WARNING)
data class OAuth2GrantType(val value : String) {
companion object {
@JvmField
@Deprecated("Use GrantType.JWT_BEARER from nimbus instead")
val JWT_BEARER = OAuth2GrantType(GrantType.JWT_BEARER.value)
@Deprecated("Use com.nimbusds.oauth2.sdk.GrantType instead", ReplaceWith("GrantType.JWT_BEARER"), WARNING)
val JWT_BEARER = GrantType(GrantType.JWT_BEARER.value)
@JvmField
@Deprecated("Use GrantType.CLIENT_CREDENTIALS from nimbus instead")
val CLIENT_CREDENTIALS = OAuth2GrantType(GrantType.CLIENT_CREDENTIALS.value)
@Deprecated("Use com.nimbusds.oauth2.sdk.GrantType instead", ReplaceWith("GrantType.CLIENT_CREDENTIALS"), WARNING)
val CLIENT_CREDENTIALS = GrantType(GrantType.CLIENT_CREDENTIALS.value)
@JvmField
@Deprecated("Use GrantType.TOKEN_EXCHANGE from nimbus instead")
val TOKEN_EXCHANGE = OAuth2GrantType(GrantType.TOKEN_EXCHANGE.value)
@Deprecated("Use com.nimbusds.oauth2.sdk.GrantType instead", ReplaceWith("GrantType.TOKEN_EXCHANGE"), WARNING)
val TOKEN_EXCHANGE = GrantType(GrantType.TOKEN_EXCHANGE.value)
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
package no.nav.security.token.support.client.core.auth

import com.nimbusds.jose.JOSEObjectType.*
import com.nimbusds.jose.JWSAlgorithm.*
import com.nimbusds.jose.JOSEObjectType.JWT
import com.nimbusds.jose.JWSAlgorithm.RS256
import com.nimbusds.jose.JWSHeader
import com.nimbusds.jose.crypto.RSASSASigner
import com.nimbusds.jose.jwk.RSAKey
import com.nimbusds.jwt.JWTClaimNames.JWT_ID
import com.nimbusds.jwt.JWTClaimsSet
import com.nimbusds.jwt.JWTClaimsSet.Builder
import com.nimbusds.jwt.SignedJWT
import com.nimbusds.oauth2.sdk.auth.JWTAuthentication.*
import com.nimbusds.oauth2.sdk.auth.JWTAuthentication.CLIENT_ASSERTION_TYPE
import java.net.URI
import java.time.Instant.*
import java.time.Instant.now
import java.util.Date
import java.util.UUID
import kotlin.DeprecationLevel.WARNING
import no.nav.security.token.support.client.core.ClientAuthenticationProperties

class ClientAssertion(private val tokenEndpointUrl : URI, private val clientId : String, private val rsaKey : RSAKey, private val expiryInSeconds : Int) {
constructor(tokenEndpointUrl: URI, auth : ClientAuthenticationProperties) : this(tokenEndpointUrl, auth.clientId, auth.clientRsaKey!!, EXPIRY_IN_SECONDS)
class ClientAssertion(private val tokenEndpointUrl : URI?, private val clientId : String, private val rsaKey : RSAKey, private val expiryInSeconds : Int) {
constructor(tokenEndpointUrl: URI?, auth : ClientAuthenticationProperties) : this(tokenEndpointUrl, auth.clientId, auth.clientRsaKey!!, EXPIRY_IN_SECONDS)

fun assertion() =
now().run {
createSignedJWT(rsaKey, Builder()
.audience(tokenEndpointUrl.toString())
.audience("$tokenEndpointUrl")
.expirationTime(Date.from(plusSeconds(expiryInSeconds.toLong())))
.issuer(clientId)
.subject(clientId)
.claim("jti", UUID.randomUUID().toString())
.claim(JWT_ID, "${UUID.randomUUID()}")
.notBeforeTime(Date.from(this))
.issueTime(Date.from(this))
.build()).serialize()
}

@Deprecated("Use com.nimbusds.oauth2.sdk.auth.JWTAuthentication instead", ReplaceWith("JWTAuthentication.CLIENT_ASSERTION_TYPE"), WARNING)
fun assertionType() = CLIENT_ASSERTION_TYPE

private fun createSignedJWT(rsaJwk : RSAKey, claimsSet : JWTClaimsSet) =

runCatching {
SignedJWT(JWSHeader.Builder(RS256)
.keyID(rsaJwk.keyID)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package no.nav.security.token.support.client.core.context

import java.util.Optional

fun interface JwtBearerTokenResolver {
fun token() : Optional<String>

fun token() : String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ package no.nav.security.token.support.client.core.http
import no.nav.security.token.support.client.core.oauth2.OAuth2AccessTokenResponse

interface OAuth2HttpClient {

fun post(oAuth2HttpRequest : OAuth2HttpRequest) : OAuth2AccessTokenResponse?
fun post(request : OAuth2HttpRequest) : OAuth2AccessTokenResponse?
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OAuth2HttpHeaders (val headers : Map<String, List<String>>) {

override fun hashCode() = Objects.hash(headers)

override fun toString() = javaClass.getSimpleName() + " [headers=" + headers + "]"
override fun toString() = "${javaClass.getSimpleName()} [headers=$headers]"

class Builder(private val headers : TreeMap<String, MutableList<String>> = TreeMap(CASE_INSENSITIVE_ORDER)) {

Expand Down
Loading