-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bruke AzureAD token mot PDL * Behandlingsnummer header
- Loading branch information
1 parent
dca72fd
commit ad3c484
Showing
16 changed files
with
152 additions
and
135 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -45,6 +45,7 @@ jobs: | |
|
||
deploy-branch: | ||
name: Deploy til dev | ||
needs: bygg | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
|
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
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/no/nav/tag/innsynAareg/client/azure/AccessTokenResponse.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,10 @@ | ||
package no.nav.tag.innsynAareg.client.azure | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
class AccessTokenResponse( | ||
@field:JsonProperty("expires_in") var expiresIn: Long, | ||
@field:JsonProperty("access_token") var accessToken: String | ||
) |
78 changes: 78 additions & 0 deletions
78
src/main/kotlin/no/nav/tag/innsynAareg/client/azure/AzureClient.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,78 @@ | ||
package no.nav.tag.innsynAareg.client.azure | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.MediaType | ||
import org.springframework.stereotype.Component | ||
import org.springframework.util.LinkedMultiValueMap | ||
import org.springframework.util.MultiValueMap | ||
import org.springframework.web.client.RestTemplate | ||
import java.time.LocalDateTime | ||
import java.util.* | ||
|
||
@Component | ||
class AzureClient @Autowired constructor( | ||
@Value("\${azure.tokenUrl}") private val tokenUrl: String, | ||
@Value("\${AZURE_APP_CLIENT_ID}") private val clientId: String, | ||
@Value("\${AZURE_APP_CLIENT_SECRET}") private val clientSecret: String, | ||
private val restTemplate: RestTemplate | ||
) { | ||
private val log = LoggerFactory.getLogger(AzureClient::class.java)!! | ||
private val tokens: LinkedHashMap<String, AzureToken> = LinkedHashMap() | ||
|
||
fun getToken(scope: String): String { | ||
if (!tokens.containsKey(scope)) { | ||
updateToken(scope) | ||
} | ||
updateTokenIfNeeded(scope) | ||
return tokens.getValue(scope).access_token | ||
} | ||
|
||
private fun updateTokenIfNeeded(scope: String) { | ||
synchronized(this) { | ||
val token = tokens.getValue(scope) | ||
if (shouldRefresh(token.expires_in)) { | ||
updateToken(scope) | ||
} | ||
} | ||
} | ||
|
||
private fun updateToken(scope: String) { | ||
try { | ||
val formParameters = formParameters(scope) | ||
|
||
val headers = HttpHeaders() | ||
headers.contentType = MediaType.APPLICATION_FORM_URLENCODED | ||
headers.accept = listOf(MediaType.APPLICATION_JSON) | ||
headers.setBasicAuth(clientId, clientSecret) | ||
|
||
val requestEntity = HttpEntity<MultiValueMap<String, String>>(formParameters, headers) | ||
|
||
val response = | ||
restTemplate.exchange(tokenUrl, HttpMethod.POST, requestEntity, AccessTokenResponse::class.java).body!! | ||
|
||
val token = AzureToken(response.accessToken, LocalDateTime.now().plusSeconds(response.expiresIn)) | ||
|
||
tokens[scope] = token | ||
} catch (e: Exception) { | ||
log.error("Feil ved henting av token fra Azure. $e", e) | ||
throw RuntimeException("AG-ARBEIDSFORHOLD Klarte ikke hente token fra azure. $e", e) | ||
} | ||
} | ||
|
||
private fun shouldRefresh(expiry: LocalDateTime): Boolean { | ||
return Objects.isNull(expiry) || LocalDateTime.now().plusMinutes(1).isAfter(expiry) | ||
} | ||
|
||
private fun formParameters(scope: String): MultiValueMap<String, String> { | ||
val formParameters: MultiValueMap<String, String> = LinkedMultiValueMap() | ||
formParameters.add("grant_type", "client_credentials") | ||
formParameters.add("scope", scope) | ||
|
||
return formParameters | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/no/nav/tag/innsynAareg/client/azure/AzureToken.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,9 @@ | ||
package no.nav.tag.innsynAareg.client.azure | ||
|
||
import java.time.LocalDateTime | ||
|
||
@Suppress("Unused") /* dto */ | ||
data class AzureToken( | ||
var access_token: String, | ||
var expires_in: LocalDateTime | ||
) |
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
26 changes: 0 additions & 26 deletions
26
src/main/kotlin/no/nav/tag/innsynAareg/client/sts/STSCacheConfig.kt
This file was deleted.
Oops, something went wrong.
48 changes: 0 additions & 48 deletions
48
src/main/kotlin/no/nav/tag/innsynAareg/client/sts/STSClient.kt
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/main/kotlin/no/nav/tag/innsynAareg/client/sts/STStoken.kt
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
Oops, something went wrong.