diff --git a/.github/workflows/ktlint.yml b/.github/workflows/ktlint.yml new file mode 100644 index 0000000..320e34d --- /dev/null +++ b/.github/workflows/ktlint.yml @@ -0,0 +1,16 @@ +name: Ktlint + +on: [pull_request] + +jobs: + Ktlint: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + - name: "Install ktlint" + uses: nbadal/action-ktlint-setup@v1 + with: + ktlint_version: '0.42.1' + - run: ktlint + shell: bash diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..2c65ed7 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,40 @@ +name: Test + +on: [pull_request] + +jobs: + Test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: temurin + cache: gradle + + - name: Cache Gradle wrapper + uses: actions/cache@v3 + with: + path: ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle-wrapper- + + - name: Cache Gradle packages + uses: actions/cache@v3 + with: + path: ~/.gradle/caches + key: ${{ runner.os }}-gradle-cache-${{ hashFiles('build.gradle') }} + restore-keys: | + ${{ runner.os }}-gradle-cache- + + - name: Build + run: ./gradlew build --info + env: + ORG_GRADLE_PROJECT_githubPassword: ${{ secrets.GITHUB_TOKEN }} + + - name: Test + run: ./gradlew test --console=plain + env: + ORG_GRADLE_PROJECT_githubPassword: ${{ secrets.GITHUB_TOKEN }} diff --git a/build.gradle.kts b/build.gradle.kts index c671719..cea254c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { id("maven-publish") } group = "no.nav.helsearbeidsgiver" -version = "0.1.8" +version = "0.1.9" kotlin { compilerOptions { diff --git a/src/main/kotlin/MaskinportenClient.kt b/src/main/kotlin/MaskinportenClient.kt index 08c1772..3801796 100644 --- a/src/main/kotlin/MaskinportenClient.kt +++ b/src/main/kotlin/MaskinportenClient.kt @@ -18,7 +18,7 @@ class MaskinportenClient(private val maskinportenClientConfig: MaskinportenClien private val httpClient = createHttpClient() suspend fun fetchNewAccessToken(): TokenResponseWrapper { - sikkerLogger().info("Henter ny access token fra Maskinporten") + sikkerLogger().info("Maskinporten: Henter ny access token fra Maskinporten") val result = runCatching { val response: HttpResponse = httpClient.post(maskinportenClientConfig.endpoint) { @@ -35,21 +35,22 @@ class MaskinportenClient(private val maskinportenClientConfig: MaskinportenClien return result.fold( onSuccess = { tokenResponse -> TokenResponseWrapper(tokenResponse).also { - sikkerLogger().info("Hentet ny access token. Expires in ${it.remainingTimeInSeconds} seconds.") + sikkerLogger().info("Maskinporten: Hentet ny access token. ${it.tokenResponse} ") + sikkerLogger().info("Maskinporten: Hentet ny access token. Expires in ${it.remainingTimeInSeconds} seconds.") } }, onFailure = { e -> when (e) { is ClientRequestException -> { - sikkerLogger().error("ClientRequestException: Feilet å hente ny access token fra Maskinporten. Status: ${e.response.status}, Message: ${e.message} Exception: $e") + sikkerLogger().error("Maskinporten:: ClientRequestException: Feilet å hente ny access token fra Maskinporten. Status: ${e.response.status}, Message: ${e.message} Exception: $e") } is ServerResponseException -> { - sikkerLogger().error("ServerResponseException: Feilet å hente ny access token fra Maskinporten. Status: ${e.response.status}, Message: ${e.message} Exception: $e") + sikkerLogger().error("Maskinporten: ServerResponseException: Feilet å hente ny access token fra Maskinporten. Status: ${e.response.status}, Message: ${e.message} Exception: $e") } else -> { - sikkerLogger().error("Feilet å hente ny access token fra Maskinporten: $e") + sikkerLogger().error("Maskinporten: Feilet å hente ny access token fra Maskinporten: $e") } } throw e diff --git a/src/main/kotlin/MaskinportenClientConfig.kt b/src/main/kotlin/MaskinportenClientConfig.kt index 5f64023..9fe80c8 100644 --- a/src/main/kotlin/MaskinportenClientConfig.kt +++ b/src/main/kotlin/MaskinportenClientConfig.kt @@ -38,9 +38,9 @@ data class MaskinportenClientConfig( private fun currentTime(): Date = Date.from(Instant.now()) - private val claims: JWTClaimsSet by lazy { + private fun claims(): JWTClaimsSet { val now = currentTime() - JWTClaimsSet.Builder() + return JWTClaimsSet.Builder() .issuer(clientId) .audience(issuer) .issueTime(now) @@ -51,6 +51,6 @@ data class MaskinportenClientConfig( } fun getJwtAssertion(): String { - return SignedJWT(header, claims).apply { sign(signer) }.serialize() + return SignedJWT(header, claims()).apply { sign(signer) }.serialize() } } diff --git a/src/main/kotlin/TokenResponse.kt b/src/main/kotlin/TokenResponse.kt index b3b2b9a..e43a0e8 100644 --- a/src/main/kotlin/TokenResponse.kt +++ b/src/main/kotlin/TokenResponse.kt @@ -17,7 +17,11 @@ data class TokenResponse( @SerialName("token_type") val tokenType: String, @SerialName("expires_in") val expiresInSeconds: Long, val scope: String -) +) { + override fun toString(): String { + return "TokenResponse(accessToken='${accessToken.take(3)}', tokenType='$tokenType', expiresInSeconds=$expiresInSeconds, scope='$scope')" + } +} class TokenResponseWrapper(val tokenResponse: TokenResponse) { private val issueTime = System.currentTimeMillis() / 1000