From eb46d51d2c43e245a63c9e4d70d6196a2efa0f0d Mon Sep 17 00:00:00 2001 From: Quan Tran Date: Tue, 29 Oct 2024 13:33:13 +0700 Subject: [PATCH] ISSUE-1240 Use JMAPExtensionConfiguration to handle Webfinger config --- .../common/WebFingerRoutesContract.scala | 2 +- .../jmap/JMAPExtensionConfiguration.scala | 19 ++++++++++++++++--- .../james/jmap/oidc/WebFingerRoutes.scala | 15 +++------------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tmail-backend/integration-tests/jmap/jmap-integration-tests-common/src/main/scala/com/linagora/tmail/james/common/WebFingerRoutesContract.scala b/tmail-backend/integration-tests/jmap/jmap-integration-tests-common/src/main/scala/com/linagora/tmail/james/common/WebFingerRoutesContract.scala index 65507f335a..ffa995f1f0 100644 --- a/tmail-backend/integration-tests/jmap/jmap-integration-tests-common/src/main/scala/com/linagora/tmail/james/common/WebFingerRoutesContract.scala +++ b/tmail-backend/integration-tests/jmap/jmap-integration-tests-common/src/main/scala/com/linagora/tmail/james/common/WebFingerRoutesContract.scala @@ -3,7 +3,7 @@ package com.linagora.tmail.james.common import java.net.URL import com.google.inject.Module -import com.linagora.tmail.james.jmap.oidc.WebFingerConfiguration +import com.linagora.tmail.james.jmap.WebFingerConfiguration import io.netty.handler.codec.http.HttpHeaderNames.ACCEPT import io.restassured.RestAssured.{`given`, requestSpecification} import io.restassured.authentication.NoAuthScheme diff --git a/tmail-backend/jmap/extensions/src/main/scala/com/linagora/tmail/james/jmap/JMAPExtensionConfiguration.scala b/tmail-backend/jmap/extensions/src/main/scala/com/linagora/tmail/james/jmap/JMAPExtensionConfiguration.scala index 994c707e7f..ad19572ae6 100644 --- a/tmail-backend/jmap/extensions/src/main/scala/com/linagora/tmail/james/jmap/JMAPExtensionConfiguration.scala +++ b/tmail-backend/jmap/extensions/src/main/scala/com/linagora/tmail/james/jmap/JMAPExtensionConfiguration.scala @@ -1,5 +1,6 @@ package com.linagora.tmail.james.jmap +import java.net.{URI, URL} import java.time.Duration import java.time.temporal.ChronoUnit import java.util.Locale @@ -50,8 +51,10 @@ object JMAPExtensionConfiguration { val emailRecoveryActionConfiguration: EmailRecoveryActionConfiguration = EmailRecoveryActionConfiguration.from(configuration) + val webFingerConfiguration: WebFingerConfiguration = WebFingerConfiguration.parse(configuration) + JMAPExtensionConfiguration(publicAssetTotalSizeLimit, supportMailAddressOpt, ticketIpValidationEnable, - calendarEventReplySupportedLanguagesConfig, emailRecoveryActionConfiguration) + calendarEventReplySupportedLanguagesConfig, emailRecoveryActionConfiguration, webFingerConfiguration) } } @@ -66,7 +69,8 @@ case class JMAPExtensionConfiguration(publicAssetTotalSizeLimit: PublicAssetTota supportMailAddress: Option[MailAddress] = Option.empty, ticketIpValidationEnable: TicketIpValidationEnable = TICKET_IP_VALIDATION_ENABLED, calendarEventReplySupportedLanguagesConfig: CalendarEventReplySupportedLanguagesConfig = CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_DEFAULT, - emailRecoveryActionConfiguration: EmailRecoveryActionConfiguration = EmailRecoveryActionConfiguration.DEFAULT) { + emailRecoveryActionConfiguration: EmailRecoveryActionConfiguration = EmailRecoveryActionConfiguration.DEFAULT, + webFingerConfiguration: WebFingerConfiguration = WebFingerConfiguration.DEFAULT) { def this(publicAssetTotalSizeLimit: PublicAssetTotalSizeLimit) = { this(publicAssetTotalSizeLimit, Option.empty) } @@ -104,4 +108,13 @@ object EmailRecoveryActionConfiguration { } } -case class EmailRecoveryActionConfiguration(maxEmailRecoveryPerRequest: Long, restorationHorizon: Duration) \ No newline at end of file +case class EmailRecoveryActionConfiguration(maxEmailRecoveryPerRequest: Long, restorationHorizon: Duration) + +object WebFingerConfiguration { + val DEFAULT: WebFingerConfiguration = WebFingerConfiguration(None) + + def parse(configuration: Configuration): WebFingerConfiguration = + WebFingerConfiguration(Option(configuration.getString("oidc.provider.url", null)).map(new URI(_).toURL)) +} + +case class WebFingerConfiguration(openIdUrl: Option[URL]) \ No newline at end of file diff --git a/tmail-backend/jmap/extensions/src/main/scala/com/linagora/tmail/james/jmap/oidc/WebFingerRoutes.scala b/tmail-backend/jmap/extensions/src/main/scala/com/linagora/tmail/james/jmap/oidc/WebFingerRoutes.scala index 9dd2637c44..77a4ed3202 100644 --- a/tmail-backend/jmap/extensions/src/main/scala/com/linagora/tmail/james/jmap/oidc/WebFingerRoutes.scala +++ b/tmail-backend/jmap/extensions/src/main/scala/com/linagora/tmail/james/jmap/oidc/WebFingerRoutes.scala @@ -7,16 +7,15 @@ import java.util.stream import com.google.inject.multibindings.Multibinder import com.google.inject.{AbstractModule, Provides} import com.linagora.tmail.james.jmap.oidc.WebFingerRoutes.LOGGER +import com.linagora.tmail.james.jmap.{JMAPExtensionConfiguration, WebFingerConfiguration} import io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE import io.netty.handler.codec.http.HttpResponseStatus.{BAD_REQUEST, INTERNAL_SERVER_ERROR} import io.netty.handler.codec.http.{HttpMethod, HttpResponseStatus, QueryStringDecoder} import jakarta.inject.Inject -import org.apache.commons.configuration2.Configuration import org.apache.james.jmap.HttpConstants.JSON_CONTENT_TYPE import org.apache.james.jmap.core.ProblemDetails import org.apache.james.jmap.json.ResponseSerializer import org.apache.james.jmap.{Endpoint, JMAPRoute, JMAPRoutes} -import org.apache.james.utils.PropertiesProvider import org.slf4j.{Logger, LoggerFactory} import play.api.libs.json.{JsString, Json, Writes} import reactor.core.publisher.Mono @@ -37,15 +36,8 @@ private[oidc] object Serializers { def serialise(response: WebFingerResponse): String = Json.stringify(Json.toJson(response)) } -object WebFingerConfiguration { - def parse(configuration: Configuration): WebFingerConfiguration = - WebFingerConfiguration(Option(configuration.getString("oidc.provider.url", null)).map(new URL(_))) -} - case class WebFingerRequest(resource: URL) -case class WebFingerConfiguration(openIdUrl: Option[URL]) - case class WebFingerModule() extends AbstractModule { override def configure(): Unit = { val routes = Multibinder.newSetBinder(binder, classOf[JMAPRoutes]) @@ -53,9 +45,8 @@ case class WebFingerModule() extends AbstractModule { } @Provides - def configuration(propertiesProvider: PropertiesProvider): WebFingerConfiguration = - Try(propertiesProvider.getConfiguration("jmap")) - .fold(_ => WebFingerConfiguration(None), WebFingerConfiguration.parse) + def configuration(jmapExtensionConfiguration: JMAPExtensionConfiguration): WebFingerConfiguration = + jmapExtensionConfiguration.webFingerConfiguration } object WebFingerRoutes {