Skip to content
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
1 change: 1 addition & 0 deletions oauth-token-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dependencies{
implementation(libs.springBoot)
implementation(libs.kotlinReflect)
implementation(libs.kotlinLoggingJvm)
implementation(libs.msal)

// Used to generate properties metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import com.microsoft.aad.msal4j.ClientCredentialFactory
import com.microsoft.aad.msal4j.ClientCredentialParameters
import com.microsoft.aad.msal4j.ConfidentialClientApplication
import com.microsoft.aad.msal4j.IClientCredential
import io.github.oshai.kotlinlogging.KotlinLogging
import java.security.KeyFactory
import java.security.PrivateKey
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
import java.security.spec.PKCS8EncodedKeySpec
import java.util.Base64
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Conditional
import org.springframework.context.annotation.Configuration
Expand All @@ -26,10 +26,11 @@ import org.springframework.core.io.Resource
class MsalClientConfig {

companion object {
private val LOGGER = LoggerFactory.getLogger(MsalClientConfig::class.java)
private val PEM_REMOVAL_PATTERN = Regex("-----[A-Z ]*-----")
}

private val logger = KotlinLogging.logger {}

@Bean
fun clientCredentialParameters(properties: OAuthClientProperties): ClientCredentialParameters =
ClientCredentialParameters.builder(setOf(properties.scope)).build()
Expand Down Expand Up @@ -59,7 +60,7 @@ class MsalClientConfig {
}

try {
LOGGER.info("Reading private key: ${resource.description}")
logger.info { "Reading private key: ${resource.description}" }
val privateKeyContent = readPEMFile(resource)
val keySpecPKCS8 = PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyContent))
return KeyFactory.getInstance("RSA").generatePrivate(keySpecPKCS8)
Expand All @@ -76,7 +77,7 @@ class MsalClientConfig {
}

try {
LOGGER.info("Reading certificate: ${resource.description}")
logger.info { "Reading certificate: ${resource.description}" }
val certificateContent = readPEMFile(resource)
val inputStream = Base64.getDecoder().decode(certificateContent).inputStream()
return CertificateFactory.getInstance("X.509").generateCertificate(inputStream) as X509Certificate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.gxf.utilities.spring.oauth.providers
import com.gxf.utilities.spring.oauth.config.OAuthClientProperties
import com.gxf.utilities.spring.oauth.config.condition.OAuthTokenFileEnabledCondition
import com.gxf.utilities.spring.oauth.exceptions.OAuthTokenException
import io.github.oshai.kotlinlogging.KotlinLogging
import java.nio.charset.Charset
import java.util.Optional
import org.springframework.context.annotation.Conditional
Expand All @@ -16,13 +17,17 @@ import org.springframework.stereotype.Component
@Conditional(OAuthTokenFileEnabledCondition::class)
internal final class FileTokenProvider(clientProperties: OAuthClientProperties) : TokenProvider {

private val logger = KotlinLogging.logger {}
private val tokenResource: Resource

init {
if (clientProperties.tokenLocation?.isReadable != true) {
throw OAuthTokenException("The token location '${clientProperties.tokenLocation}` is not readable")
}
tokenResource = clientProperties.tokenLocation
logger.info {
"Configured File Token Provider with token location: ${clientProperties.tokenLocation.description}"
}
}

/** Read the resource file everytime since it may be updated while the application is running */
Expand Down
Loading