Skip to content

Commit

Permalink
Format sources before release
Browse files Browse the repository at this point in the history
  • Loading branch information
daikoku-github-actions committed Aug 5, 2024
1 parent 45be860 commit 773055b
Show file tree
Hide file tree
Showing 28 changed files with 581 additions and 316 deletions.
14 changes: 10 additions & 4 deletions daikoku/app/controllers/AdminApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,15 @@ class ApiAdminApiController(
.findOne(
Json.obj(
"_id" -> Json.obj("$ne" -> entity.id.asJson),
"name" -> entity.name,
) ++ entity.parent.map(p => Json.obj("_id" -> p.asJson)).getOrElse(Json.obj())
"name" -> entity.name
) ++ entity.parent
.map(p => Json.obj("_id" -> p.asJson))
.getOrElse(Json.obj())
)
.map {
case Some(api) if entity.parent.contains(api.id) || api.parent.contains(entity.id) =>
case Some(api)
if entity.parent.contains(api.id) || api.parent
.contains(entity.id) =>
Right(())
case Some(_) =>
Left(AppError.ParsingPayloadError("Api name already exists"))
Expand All @@ -545,7 +549,9 @@ class ApiAdminApiController(
Json.obj(
"_id" -> Json.obj("$ne" -> entity.id.asJson),
"name" -> entity.name
) ++ entity.parent.map(p => Json.obj("_id" -> p.asJson)).getOrElse(Json.obj())
) ++ entity.parent
.map(p => Json.obj("_id" -> p.asJson))
.getOrElse(Json.obj())
)
.map {
case None =>
Expand Down
20 changes: 16 additions & 4 deletions daikoku/app/controllers/ApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ import cats.data.EitherT
import cats.implicits.{catsSyntaxOptionId, toTraverseOps}
import controllers.AppError
import controllers.AppError._
import fr.maif.otoroshi.daikoku.actions.{DaikokuAction, DaikokuActionContext, DaikokuActionMaybeWithGuest, DaikokuActionMaybeWithoutUser}
import fr.maif.otoroshi.daikoku.actions.{
DaikokuAction,
DaikokuActionContext,
DaikokuActionMaybeWithGuest,
DaikokuActionMaybeWithoutUser
}
import fr.maif.otoroshi.daikoku.audit.AuditTrailEvent
import fr.maif.otoroshi.daikoku.audit.config.ElasticAnalyticsConfig
import fr.maif.otoroshi.daikoku.ctrls.authorizations.async._
import fr.maif.otoroshi.daikoku.domain.NotificationAction.{ApiAccess, ApiSubscriptionDemand}
import fr.maif.otoroshi.daikoku.domain.NotificationAction.{
ApiAccess,
ApiSubscriptionDemand
}
import fr.maif.otoroshi.daikoku.domain.UsagePlanVisibility.Private
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.domain.json._
Expand Down Expand Up @@ -85,7 +93,9 @@ class ApiController(
def fetchSwagger(api: Api): EitherT[Future, AppError, Result] = {
api.swagger match {
case Some(SwaggerAccess(_, Some(content), _, _, _)) =>
val contentType = if(content.startsWith("{")) "application/json" else "application/yaml"
val contentType =
if (content.startsWith("{")) "application/json"
else "application/yaml"
EitherT.pure[Future, AppError](Ok(content).as(contentType))
case Some(SwaggerAccess(Some(url), None, headers, _, _)) =>
val finalUrl =
Expand All @@ -97,7 +107,9 @@ class ApiController(
.withHttpHeaders(headers.toSeq: _*)
.get()
.map { resp =>
val contentType = if(resp.body.startsWith("{")) "application/json" else "application/yaml"
val contentType =
if (resp.body.startsWith("{")) "application/json"
else "application/yaml"
Right(
Ok(resp.body).as(
resp
Expand Down
34 changes: 27 additions & 7 deletions daikoku/app/controllers/HomeController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import com.github.blemale.scaffeine.{Cache, Scaffeine}
import com.nimbusds.jose.util.StandardCharset
import controllers.Assets
import daikoku.BuildInfo
import fr.maif.otoroshi.daikoku.actions.{DaikokuAction, DaikokuActionMaybeWithGuest, DaikokuActionMaybeWithoutUser, DaikokuActionMaybeWithoutUserContext}
import fr.maif.otoroshi.daikoku.actions.{
DaikokuAction,
DaikokuActionMaybeWithGuest,
DaikokuActionMaybeWithoutUser,
DaikokuActionMaybeWithoutUserContext
}
import fr.maif.otoroshi.daikoku.audit.AuditTrailEvent
import fr.maif.otoroshi.daikoku.ctrls.authorizations.async.{DaikokuAdminOrSelf, TenantAdminOnly}
import fr.maif.otoroshi.daikoku.ctrls.authorizations.async.{
DaikokuAdminOrSelf,
TenantAdminOnly
}
import fr.maif.otoroshi.daikoku.ctrls.authorizations.sync.TeamMemberOnly
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.domain.json.{CmsFileFormat, CmsPageFormat, CmsRequestRenderingFormat}
import fr.maif.otoroshi.daikoku.domain.json.{
CmsFileFormat,
CmsPageFormat,
CmsRequestRenderingFormat
}
import fr.maif.otoroshi.daikoku.env.Env
import fr.maif.otoroshi.daikoku.logger.AppLogger
import fr.maif.otoroshi.daikoku.utils.{Errors, IdGenerator, diff_match_patch}
Expand Down Expand Up @@ -113,12 +125,20 @@ class HomeController(

def getConnectedUser() = {
DaikokuActionMaybeWithoutUser { ctx =>

Ok(
Json.obj(
"connectedUser" -> ctx.user.map(_.toUiPayload()).getOrElse(JsNull).as[JsValue],
"impersonator" -> ctx.session.map(_.impersonatorJson()).getOrElse(JsNull).as[JsValue],
"session" -> ctx.session.map(_.asSimpleJson).getOrElse(JsNull).as[JsValue],
"connectedUser" -> ctx.user
.map(_.toUiPayload())
.getOrElse(JsNull)
.as[JsValue],
"impersonator" -> ctx.session
.map(_.impersonatorJson())
.getOrElse(JsNull)
.as[JsValue],
"session" -> ctx.session
.map(_.asSimpleJson)
.getOrElse(JsNull)
.as[JsValue],
"tenant" -> ctx.tenant.toUiPayload(env),
"isTenantAdmin" -> ctx.isTenantAdmin,
"apiCreationPermitted" -> ctx.apiCreationPermitted,
Expand Down
70 changes: 56 additions & 14 deletions daikoku/app/controllers/LoginController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package fr.maif.otoroshi.daikoku.ctrls

import com.eatthepath.otp.TimeBasedOneTimePasswordGenerator
import controllers.Assets
import fr.maif.otoroshi.daikoku.actions.{DaikokuAction, DaikokuActionMaybeWithoutUser, DaikokuTenantAction, DaikokuTenantActionContext}
import fr.maif.otoroshi.daikoku.actions.{
DaikokuAction,
DaikokuActionMaybeWithoutUser,
DaikokuTenantAction,
DaikokuTenantActionContext
}
import fr.maif.otoroshi.daikoku.audit.{AuditTrailEvent, AuthorizationLevel}
import fr.maif.otoroshi.daikoku.domain.TeamPermission.Administrator
import fr.maif.otoroshi.daikoku.domain._
Expand Down Expand Up @@ -97,12 +102,13 @@ class LoginController(
DaikokuActionMaybeWithoutUser { _ =>
Ok(
Json.obj(
"action" -> fr.maif.otoroshi.daikoku.ctrls.routes.LoginController.login(provider).url
"action" -> fr.maif.otoroshi.daikoku.ctrls.routes.LoginController
.login(provider)
.url
)
)
}


def loginPage(provider: String) =
DaikokuTenantAction.async { ctx =>
AuthProvider(provider) match {
Expand Down Expand Up @@ -146,7 +152,12 @@ class LoginController(
s"redirect" -> redirect.getOrElse("/")
)
)
case _ if env.config.isDev => FastFuture.successful(Redirect(env.getDaikokuUrl(ctx.tenant, s"/auth/${p.name}/login")))
case _ if env.config.isDev =>
FastFuture.successful(
Redirect(
env.getDaikokuUrl(ctx.tenant, s"/auth/${p.name}/login")
)
)
case _ => assets.at("index.html").apply(ctx.request)
}
}
Expand Down Expand Up @@ -547,7 +558,10 @@ class LoginController(
ctx.tenant,
Map(
"tenant" -> ctx.tenant.name,
"link" -> env.getDaikokuUrl(ctx.tenant, s"/account/validate?id=$randomId")
"link" -> env.getDaikokuUrl(
ctx.tenant,
s"/account/validate?id=$randomId"
)
)
)
} yield {
Expand Down Expand Up @@ -644,8 +658,12 @@ class LoginController(
} yield ()
userCreation.map { _ =>
Status(302)(
Json.obj("Location" -> "/?message=user.validated.success")
).withHeaders("Location" -> "/?message=user.validated.success")
Json.obj(
"Location" -> "/?message=user.validated.success"
)
).withHeaders(
"Location" -> "/?message=user.validated.success"
)
}
}
case _ =>
Expand Down Expand Up @@ -701,8 +719,9 @@ class LoginController(
ctx.tenant.defaultLanguage.getOrElse("en")
implicit val language: String =
user.defaultLanguage.getOrElse(tenantLanguage)
val link = env.getDaikokuUrl(ctx.tenant, s"/account/reset?id=$randomId")
ctx.tenant.mailer
val link =
env.getDaikokuUrl(ctx.tenant, s"/account/reset?id=$randomId")
ctx.tenant.mailer
.send(
s"Reset your ${ctx.tenant.name} account password",
Seq(email),
Expand Down Expand Up @@ -735,7 +754,10 @@ class LoginController(
ctx.request.getQueryString("id") match {
case None =>
FastFuture.successful(
Redirect(env.getDaikokuUrl(ctx.tenant, "/reset?error=bad.creation.id")))
Redirect(
env.getDaikokuUrl(ctx.tenant, "/reset?error=bad.creation.id")
)
)
case Some(id) => {
env.dataStore.passwordResetRepo
.findOneNotDeleted(Json.obj("randomId" -> id))
Expand All @@ -745,7 +767,12 @@ class LoginController(
env.dataStore.passwordResetRepo
.deleteByIdLogically(pwdReset.id.value)
.map { _ =>
Redirect(env.getDaikokuUrl(ctx.tenant, "/reset?error=not.valid.anymore"))
Redirect(
env.getDaikokuUrl(
ctx.tenant,
"/reset?error=not.valid.anymore"
)
)
}
case Some(pwdReset)
if pwdReset.validUntil.isAfter(DateTime.now()) =>
Expand All @@ -759,21 +786,36 @@ class LoginController(
.flatMap {
case None =>
FastFuture.successful(
Redirect(env.getDaikokuUrl(ctx.tenant, "/reset?error=user.not.found")))
Redirect(
env.getDaikokuUrl(
ctx.tenant,
"/reset?error=user.not.found"
)
)
)
case Some(user) =>
env.dataStore.userRepo
.save(user.copy(password = Some(pwdReset.password)))
.flatMap { _ =>
env.dataStore.passwordResetRepo
.deleteByIdLogically(pwdReset.id.value)
.map { _ =>
Redirect(env.getDaikokuUrl(ctx.tenant, "/?message=password.reset.successfull"))
Redirect(
env.getDaikokuUrl(
ctx.tenant,
"/?message=password.reset.successfull"
)
)
}
}
}
case _ =>
FastFuture.successful(
Redirect(env.getDaikokuUrl(ctx.tenant, "/reset?error=bad.creation.id")))
Redirect(
env
.getDaikokuUrl(ctx.tenant, "/reset?error=bad.creation.id")
)
)
}
}
}
Expand Down
38 changes: 26 additions & 12 deletions daikoku/app/controllers/OtoroshiSettingsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ class OtoroshiSettingsController(
if (previousSettings != actualSettings) {
for {
_ <- otoroshiClient.deleteApiKey(key.clientId)(previousSettings)
newKey <-EitherT(otoroshiClient.createApiKey(key)(actualSettings))
newKey <-
EitherT(otoroshiClient.createApiKey(key)(actualSettings))
} yield newKey
} else {
EitherT(otoroshiClient.updateApiKey(key)(previousSettings))
Expand Down Expand Up @@ -661,16 +662,25 @@ class OtoroshiSettingsController(
val otoroshiSettingsOpt =
(ctx.request.body \ "otoroshiSettings").asOpt[String]


val clientIdOpt = (ctx.request.body \ "clientId").asOpt[String]

(for {
otoroshiSettingsId <- EitherT.fromOption[Future](otoroshiSettingsOpt, AppError.EntityNotFound("Otoroshi settings"))
clientId <- EitherT.fromOption[Future](clientIdOpt, AppError.EntityNotFound("clientId settings"))
otoroshiSettings <- EitherT.fromOption[Future](ctx.tenant.otoroshiSettings
.find(s => s.id.value == otoroshiSettingsId), AppError.EntityNotFound("Otoroshi settings"))
_ <- otoroshiClient
.deleteApiKey(clientId)(otoroshiSettings)
otoroshiSettingsId <- EitherT.fromOption[Future](
otoroshiSettingsOpt,
AppError.EntityNotFound("Otoroshi settings")
)
clientId <- EitherT.fromOption[Future](
clientIdOpt,
AppError.EntityNotFound("clientId settings")
)
otoroshiSettings <- EitherT.fromOption[Future](
ctx.tenant.otoroshiSettings
.find(s => s.id.value == otoroshiSettingsId),
AppError.EntityNotFound("Otoroshi settings")
)
_ <-
otoroshiClient
.deleteApiKey(clientId)(otoroshiSettings)
} yield Ok(Json.obj("done" -> true)))
.leftMap(_.render())
.merge
Expand Down Expand Up @@ -713,7 +723,8 @@ class OtoroshiSettingsController(
.find(_._2 == s"fake-${api.id.value}")
val headerOpt = headers.find(_._2 == s"fake-${api.id.value}")
val finalUrl = api.testing match {
case Some(Testing(_, auth, _, username, _, _)) if queryOpt.isDefined && auth.name == TestingAuth.ApiKey.name =>
case Some(Testing(_, auth, _, username, _, _))
if queryOpt.isDefined && auth.name == TestingAuth.ApiKey.name =>
url
.replace(
s"&${queryOpt.get._1}=fake-${api.id.value}",
Expand All @@ -727,9 +738,12 @@ class OtoroshiSettingsController(
}
val finalHeaders: Map[String, String] =
api.testing match {
case Some(Testing(_, auth, _, username, _, _)) if auth.name == TestingAuth.ApiKey.name && headerOpt.isDefined =>
headers - headerOpt.get._1 + (headerOpt.get._1 -> username.getOrElse(""))
case Some(Testing(_, auth, _, username, password, _)) if auth.name == TestingAuth.Basic.name =>
case Some(Testing(_, auth, _, username, _, _))
if auth.name == TestingAuth.ApiKey.name && headerOpt.isDefined =>
headers - headerOpt.get._1 + (headerOpt.get._1 -> username
.getOrElse(""))
case Some(Testing(_, auth, _, username, password, _))
if auth.name == TestingAuth.Basic.name =>
headers - "Authorization" + ("Authorization" -> s"Basic ${Base64
.encodeBase64String(s"${username.getOrElse("")}:$password".getBytes(Charsets.UTF_8))}")
case _ => headers
Expand Down
7 changes: 5 additions & 2 deletions daikoku/app/controllers/TeamController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,11 @@ class TeamController(
implicit val language: String =
tenant.defaultLanguage.getOrElse("en")

val link = env.getDaikokuUrl(ctx.tenant, s"/join?token=${invitedUser.invitation.get.token}")
tenant.mailer
val link = env.getDaikokuUrl(
ctx.tenant,
s"/join?token=${invitedUser.invitation.get.token}"
)
tenant.mailer
.send(
s"Join ${team.name}",
Seq(email),
Expand Down
13 changes: 11 additions & 2 deletions daikoku/app/controllers/UsersController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import cats.data.EitherT
import org.apache.pekko.http.scaladsl.util.FastFuture
import com.eatthepath.otp.TimeBasedOneTimePasswordGenerator
import controllers.AppError
import fr.maif.otoroshi.daikoku.actions.{DaikokuAction, DaikokuActionMaybeWithGuest, DaikokuActionMaybeWithoutUser}
import fr.maif.otoroshi.daikoku.actions.{
DaikokuAction,
DaikokuActionMaybeWithGuest,
DaikokuActionMaybeWithoutUser
}
import fr.maif.otoroshi.daikoku.audit.AuditTrailEvent
import fr.maif.otoroshi.daikoku.ctrls.authorizations.async._
import fr.maif.otoroshi.daikoku.domain.TeamPermission.Administrator
Expand All @@ -17,7 +21,12 @@ import org.apache.commons.codec.binary.Base32
import org.joda.time.{DateTime, Hours}
import org.mindrot.jbcrypt.BCrypt
import play.api.libs.json.{JsArray, JsError, JsSuccess, Json}
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}
import play.api.mvc.{
AbstractController,
Action,
AnyContent,
ControllerComponents
}

import java.time.Instant
import java.util.Base64
Expand Down
Loading

0 comments on commit 773055b

Please sign in to comment.