Skip to content

Commit

Permalink
BDOG-237 Fix token forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed Jul 25, 2019
1 parent eaf210b commit c596668
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
11 changes: 8 additions & 3 deletions app/uk/gov/hmrc/cataloguefrontend/actions/UmpAuthenticated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ import play.api.mvc._
import uk.gov.hmrc.cataloguefrontend.{ routes => appRoutes }
import uk.gov.hmrc.cataloguefrontend.connector.UserManagementAuthConnector
import uk.gov.hmrc.cataloguefrontend.connector.UserManagementAuthConnector.UmpToken
import uk.gov.hmrc.http.Token
import uk.gov.hmrc.play.HeaderCarrierConverter
import play.api.mvc.Results._
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.{ExecutionContext, Future}

final case class UmpAuthenticatedRequest[A](request: Request[A], token: Token)
extends WrappedRequest(request)


/** Creates an Action will only proceed to invoke the action body, if there is a valid [[UmpToken]] in session.
* If there isn't, it will short circuit with a Redirect to SignIn page.
*
Expand All @@ -39,15 +44,15 @@ class UmpAuthenticated @Inject()(
userManagementAuthConnector: UserManagementAuthConnector,
cc : MessagesControllerComponents
)(implicit val ec: ExecutionContext)
extends ActionBuilder[Request, AnyContent] {
extends ActionBuilder[UmpAuthenticatedRequest, AnyContent] {

def invokeBlock[A](request: Request[A], block: Request[A] => Future[Result]): Future[Result] = {
def invokeBlock[A](request: Request[A], block: UmpAuthenticatedRequest[A] => Future[Result]): Future[Result] = {
implicit val hc: HeaderCarrier = HeaderCarrierConverter.fromHeadersAndSession(request.headers, Some(request.session))
OptionT(
request.session.get(UmpToken.SESSION_KEY_NAME)
.filterA(token => userManagementAuthConnector.isValid(UmpToken(token)))
)
.semiflatMap(_ => block(request))
.semiflatMap(token => block(UmpAuthenticatedRequest(request, token = Token(token))))
.getOrElse(Redirect(appRoutes.AuthController.showSignInPage(targetUrl = Some(request.target.uriString).filter(_ => request.method == "GET"))))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package uk.gov.hmrc.cataloguefrontend.shuttering

import javax.inject.{Inject, Singleton}
import play.api.Logger
import play.api.libs.json.Reads
import uk.gov.hmrc.http.HeaderCarrier
import play.api.libs.json.{Reads, Writes}
import uk.gov.hmrc.http.{HeaderCarrier, HttpReads, Token}
import uk.gov.hmrc.play.bootstrap.config.ServicesConfig
import uk.gov.hmrc.play.bootstrap.http.HttpClient
import uk.gov.hmrc.http.HttpResponse

import scala.concurrent.{ExecutionContext, Future}

Expand Down Expand Up @@ -81,17 +82,20 @@ class ShutterConnector @Inject()(
* Shutters/un-shutters the application in the given environment
*/
def updateShutterStatus(
appName : String
umpToken : Token
, appName : String
, env : Environment
, status : ShutterStatus
)(implicit hc: HeaderCarrier): Future[Unit] = {
implicit val isf = ShutterStatus.format

implicit val ur = new uk.gov.hmrc.http.HttpReads[Unit] {
def read(method: String, url: String, response: uk.gov.hmrc.http.HttpResponse): Unit = ()
}

http.PUT[ShutterStatus, Unit](s"$urlStates/$appName/${env.asString}", status)
http.PUT[ShutterStatus, HttpResponse](s"$urlStates/$appName/${env.asString}", status)(
implicitly[Writes[ShutterStatus]]
, implicitly[HttpReads[HttpResponse]]
, hc.copy(token = Some(umpToken))
, implicitly[ExecutionContext]
)
.map(_ => ())
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package uk.gov.hmrc.cataloguefrontend.shuttering
import java.time.LocalDateTime

import javax.inject.{Inject, Singleton}
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.http.{HeaderCarrier, Token}

import scala.concurrent.{ExecutionContext, Future}

Expand All @@ -33,11 +33,12 @@ class ShutterService @Inject()(
shutterConnector.shutterStates

def updateShutterStatus(
serviceName: String
, env : Environment
, status : ShutterStatus
umpToken : Token
, serviceName: String
, env : Environment
, status : ShutterStatus
)(implicit hc: HeaderCarrier): Future[Unit] =
shutterConnector.updateShutterStatus(serviceName, env, status)
shutterConnector.updateShutterStatus(umpToken, serviceName, env, status)

def outagePageByAppAndEnv(serviceName: String, env: Environment)(implicit hc: HeaderCarrier): Future[Option[OutagePage]] =
shutterConnector.outagePageByAppAndEnv(serviceName, env)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class ShutterServiceController @Inject()(
_ <- step1Out.serviceNames.toList.traverse_[EitherT[Future, Result, ?], Unit] { serviceName =>
EitherT.right[Result] {
shutterService
.updateShutterStatus(serviceName, step1Out.env, status)
.updateShutterStatus(request.token, serviceName, step1Out.env, status)
}
}
} yield Redirect(appRoutes.ShutterServiceController.step4Get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package uk.gov.hmrc.cataloguefrontend.actions

import play.api.mvc.{MessagesControllerComponents, Request, Result}
import uk.gov.hmrc.cataloguefrontend.connector.UserManagementAuthConnector
import uk.gov.hmrc.http.Token

import scala.concurrent.{ExecutionContext, Future}

Expand All @@ -28,8 +29,8 @@ trait ActionsSupport {
umac: UserManagementAuthConnector,
cc: MessagesControllerComponents
) extends UmpAuthenticated(umac, cc) {
override def invokeBlock[A](request: Request[A], block: Request[A] => Future[Result]): Future[Result] =
block(request)
override def invokeBlock[A](request: Request[A], block: UmpAuthenticatedRequest[A] => Future[Result]): Future[Result] =
block(UmpAuthenticatedRequest(request, token = Token("asdasdasd")))
}

class VerifySignInStatusPassThrough(
Expand Down

0 comments on commit c596668

Please sign in to comment.