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

Fikse feil med ugyldig token #4

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 16 additions & 0 deletions .github/workflows/ktlint.yml
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id("maven-publish")
}
group = "no.nav.helsearbeidsgiver"
version = "0.1.8"
version = "0.1.9"

kotlin {
compilerOptions {
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/MaskinportenClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/MaskinportenClientConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/TokenResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading