Skip to content

Commit

Permalink
API-7572 - Added refreshTokensAvailableFor to StoredApplication (#489)
Browse files Browse the repository at this point in the history
* API-7572 - Added refreshTokensAvailableFor to StoredApplication

* API-7572 - Improve log message

* API-7572 - Ran scalafmtAll and scalafixAll
  • Loading branch information
anjumabbas5 authored Apr 29, 2024
1 parent 7b4ce8a commit 64d3347
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ object Application {
data.collaborators,
data.createdOn,
data.lastAccess,
GrantLength.apply(data.grantLength).getOrElse(GrantLength.EIGHTEEN_MONTHS),
GrantLength.apply(data.refreshTokensAvailableFor).getOrElse(GrantLength.EIGHTEEN_MONTHS),
data.tokens.production.lastAccessTokenUsage,
redirectUris(data),
termsAndConditionsUrl(data),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object ExtendedApplicationResponse {
data.collaborators,
data.createdOn,
data.lastAccess,
GrantLength.apply(data.grantLength).getOrElse(GrantLength.EIGHTEEN_MONTHS),
GrantLength.apply(data.refreshTokensAvailableFor).getOrElse(GrantLength.EIGHTEEN_MONTHS),
Application.redirectUris(data),
Application.termsAndConditionsUrl(data),
Application.privacyPolicyUrl(data),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package uk.gov.hmrc.thirdpartyapplication.models.db

import java.time.Instant
import java.time.{Instant, Period}

import com.typesafe.config.ConfigFactory

Expand All @@ -39,7 +39,7 @@ case class StoredApplication(
access: Access = Access.Standard(),
createdOn: Instant,
lastAccess: Option[Instant],
grantLength: Int = grantLengthConfig,
refreshTokensAvailableFor: Period = Period.ofDays(grantLengthConfig),
rateLimitTier: Option[RateLimitTier] = Some(RateLimitTier.BRONZE),
environment: String = Environment.PRODUCTION.toString,
checkInformation: Option[CheckInformation] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package uk.gov.hmrc.thirdpartyapplication.repository

import java.time.Instant
import java.time.{Instant, Period}
import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

import com.mongodb.client.model.{FindOneAndUpdateOptions, ReturnDocument}
import com.typesafe.config.ConfigFactory
import org.bson.BsonValue
import org.mongodb.scala.bson.conversions.Bson
import org.mongodb.scala.bson.{BsonArray, BsonInt32, BsonString, Document}
Expand Down Expand Up @@ -105,15 +106,16 @@ object ApplicationRepository {
implicit val formatApplicationState: OFormat[ApplicationState] = Json.format[ApplicationState]
implicit val formatApplicationTokens: OFormat[ApplicationTokens] = Json.format[ApplicationTokens]

import uk.gov.hmrc.thirdpartyapplication.models.db.StoredApplication.grantLengthConfig

// Non-standard format compared to companion object
val ipAllowlistReads: Reads[IpAllowlist] = (
((JsPath \ "required").read[Boolean] or Reads.pure(false)) and
((JsPath \ "allowlist").read[Set[String]] or Reads.pure(Set.empty[String]))
)(IpAllowlist.apply _)
implicit val formatIpAllowlist: OFormat[IpAllowlist] = OFormat(ipAllowlistReads, Json.writes[IpAllowlist])

def periodFromInt(i: Int): Period = (GrantLength.apply(i).getOrElse(GrantLength.EIGHTEEN_MONTHS)).period
val grantLengthConfig = ConfigFactory.load().getInt("grantLengthInDays")

// Non-standard format compared to companion object
val readStoredApplication: Reads[StoredApplication] = (
(JsPath \ "id").read[ApplicationId] and
Expand All @@ -127,7 +129,9 @@ object ApplicationRepository {
(JsPath \ "access").read[Access] and
(JsPath \ "createdOn").read[Instant] and
(JsPath \ "lastAccess").readNullable[Instant] and
((JsPath \ "grantLength").read[Int] or Reads.pure(grantLengthConfig)) and
(((JsPath \ "refreshTokensAvailableFor").read[Period]
.orElse((JsPath \ "grantLength").read[Int].map(periodFromInt(_))))
or Reads.pure(periodFromInt(grantLengthConfig))) and
(JsPath \ "rateLimitTier").readNullable[RateLimitTier] and
(JsPath \ "environment").read[String] and
(JsPath \ "checkInformation").readNullable[CheckInformation] and
Expand Down Expand Up @@ -247,8 +251,8 @@ class ApplicationRepository @Inject() (mongo: MongoComponent, val metrics: Metri
def updateApplicationIpAllowlist(applicationId: ApplicationId, ipAllowlist: IpAllowlist): Future[StoredApplication] =
updateApplication(applicationId, Updates.set("ipAllowlist", Codecs.toBson(ipAllowlist)))

def updateApplicationGrantLength(applicationId: ApplicationId, grantLength: Int): Future[StoredApplication] =
updateApplication(applicationId, Updates.set("grantLength", grantLength))
def updateApplicationGrantLength(applicationId: ApplicationId, refreshTokensAvailableFor: Period): Future[StoredApplication] =
updateApplication(applicationId, Updates.set("refreshTokensAvailableFor", Codecs.toBson(refreshTokensAvailableFor)))

def addApplicationTermsOfUseAcceptance(applicationId: ApplicationId, acceptance: TermsOfUseAcceptance): Future[StoredApplication] =
updateApplication(applicationId, Updates.push("access.importantSubmissionData.termsOfUseAcceptances", Codecs.toBson(acceptance)))
Expand Down Expand Up @@ -685,6 +689,8 @@ class ApplicationRepository @Inject() (mongo: MongoComponent, val metrics: Metri
"access",
"createdOn",
"lastAccess",
"grantLength",
"refreshTokensAvailableFor",
"rateLimitTier",
"environment",
"allowAutoDelete"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package uk.gov.hmrc.thirdpartyapplication.services.commands.grantlength

import java.time.Period
import javax.inject.{Inject, Singleton}
import scala.concurrent.ExecutionContext

Expand All @@ -40,8 +41,17 @@ class ChangeGrantLengthCommandHandler @Inject() (
import CommandHandler._

private def validate(app: StoredApplication, cmd: ChangeGrantLength): Validated[Failures, Unit] = {
def printRefreshTokensAvailableFor(refreshTokensAvailableFor: Period): String = {
val refreshTokensAvailableForDays = refreshTokensAvailableFor.getDays
val grantLength = if (refreshTokensAvailableForDays > 0) s"$refreshTokensAvailableForDays days" else "4 hours and no refresh tokens"
s"Grant length is already $grantLength"
}

Apply[Validated[Failures, *]].map(
cond((cmd.grantLength.period.getDays != app.grantLength), CommandFailures.GenericFailure(s"Grant length is already ${app.grantLength} days"))
cond(
(cmd.grantLength.period != app.refreshTokensAvailableFor),
CommandFailures.GenericFailure(printRefreshTokensAvailableFor(app.refreshTokensAvailableFor))
)
) { case _ => () }
}

Expand All @@ -52,7 +62,7 @@ class ChangeGrantLengthCommandHandler @Inject() (
applicationId = app.id,
eventDateTime = cmd.timestamp,
actor = Actors.GatekeeperUser(cmd.gatekeeperUser),
oldGrantLengthInDays = app.grantLength,
oldGrantLengthInDays = app.refreshTokensAvailableFor.getDays,
newGrantLengthInDays = cmd.grantLength.period.getDays
)
)
Expand All @@ -62,7 +72,7 @@ class ChangeGrantLengthCommandHandler @Inject() (

for {
valid <- E.fromEither(validate(app, cmd).toEither)
savedApp <- E.liftF(applicationRepository.updateApplicationGrantLength(app.id, cmd.grantLength.period.getDays))
savedApp <- E.liftF(applicationRepository.updateApplicationGrantLength(app.id, cmd.grantLength.period))
events = asEvents(app, cmd)
} yield (savedApp, events)
}
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ commands ++= Seq(
Command.command("fixAll") { state => "scalafixAll" :: "it/scalafixAll" :: state },
Command.command("testAll") { state => "test" :: "it/test" :: state },
Command.command("run-all-tests") { state => "testAll" :: state },
Command.command("clean-and-test") { state => "clean" :: "compile" :: "run-all-tests" :: state },
Command.command("pre-commit") { state => "clean" :: "scalafmtAll" :: "scalafixAll" :: "coverage" :: "run-all-tests" :: "coverageOff" :: "coverageAggregate" :: state }
Command.command("clean-and-test") { state => "cleanAll" :: "compile" :: "run-all-tests" :: state },
Command.command("pre-commit") { state => "cleanAll" :: "fmtAll" :: "fixAll" :: "coverage" :: "testAll" :: "coverageOff" :: "coverageAggregate" :: state }
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@

package uk.gov.hmrc.thirdpartyapplication.component

import java.util.UUID
import scala.concurrent.Await.{ready, result}
import scala.util.Random

import org.scalatest.Inside
import scalaj.http.{Http, HttpResponse}

import play.api.http.HeaderNames.AUTHORIZATION
import play.api.http.Status._
import play.api.inject.bind
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{Json, OWrites}
import scalaj.http.{Http, HttpResponse}

import uk.gov.hmrc.apiplatform.modules.common.domain.models.LaxEmailAddress.StringSyntax
import uk.gov.hmrc.apiplatform.modules.common.domain.models._
import uk.gov.hmrc.apiplatform.modules.common.utils.FixedClock
import uk.gov.hmrc.apiplatform.modules.apis.domain.models.ApiIdentifierSyntax._
import uk.gov.hmrc.apiplatform.modules.applications.access.domain.models._
import uk.gov.hmrc.apiplatform.modules.applications.core.domain.models._
import uk.gov.hmrc.apiplatform.modules.commands.applications.domain.models.{ApplicationCommand, ApplicationCommands}
import uk.gov.hmrc.apiplatform.modules.common.domain.models.LaxEmailAddress.StringSyntax
import uk.gov.hmrc.apiplatform.modules.common.domain.models._
import uk.gov.hmrc.apiplatform.modules.common.utils.FixedClock
import uk.gov.hmrc.thirdpartyapplication.config.SchedulerModule
import uk.gov.hmrc.thirdpartyapplication.controllers.ApplicationCommandController._
import uk.gov.hmrc.thirdpartyapplication.domain.models._
Expand All @@ -38,10 +44,6 @@ import uk.gov.hmrc.thirdpartyapplication.models._
import uk.gov.hmrc.thirdpartyapplication.repository.{ApplicationRepository, SubscriptionRepository}
import uk.gov.hmrc.thirdpartyapplication.util.{CollaboratorTestData, CredentialGenerator}

import java.util.UUID
import scala.concurrent.Await.{ready, result}
import scala.util.Random

class DummyCredentialGenerator extends CredentialGenerator {
override def generate() = "a" * 10
}
Expand Down
Loading

0 comments on commit 64d3347

Please sign in to comment.