Skip to content

Commit

Permalink
Merge pull request #47 from hmrc/PE-3211
Browse files Browse the repository at this point in the history
PE-3211 - Update UserInfoService to use auth-client
  • Loading branch information
Tamer Moustafa authored Dec 19, 2017
2 parents d69eed9 + f7a42f4 commit fb1c35e
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 420 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,31 @@

package uk.gov.hmrc.openidconnect.userinfo.connectors

import play.api.Logger
import uk.gov.hmrc.auth.core.retrieve.{Retrievals, ~}
import uk.gov.hmrc.auth.core.{AuthorisedFunctions, PlayAuthConnector}
import uk.gov.hmrc.auth.core.{AuthorisedFunctions, Enrolments, PlayAuthConnector}
import uk.gov.hmrc.http.{HeaderCarrier, NotFoundException}
import uk.gov.hmrc.openidconnect.userinfo.config.WSHttp
import uk.gov.hmrc.openidconnect.userinfo.domain.{Authority, DesUserInfo, Enrolment, UserDetails}
import uk.gov.hmrc.play.auth.microservice.connectors.ConfidenceLevel._
import uk.gov.hmrc.openidconnect.userinfo.domain.{Authority, DesUserInfo, UserDetails}
import uk.gov.hmrc.play.config.ServicesConfig

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ExecutionContext, Future}
import uk.gov.hmrc.http.{CoreGet, HeaderCarrier, HttpReads, NotFoundException}
import scala.concurrent.Future

trait AuthConnector extends AuthorisedFunctions {
val authBaseUrl: String

val http: CoreGet
def fetchEnrolments()(implicit headerCarrier: HeaderCarrier): Future[Option[Enrolments]] = {
authorised().retrieve(Retrievals.allEnrolments) {
enrolments => Future.successful(Some(enrolments))
}.recover {
case e: NotFoundException => None
}
}

def fetchEnrolments(auth: Authority)(implicit headerCarrier: HeaderCarrier): Future[Option[Seq[Enrolment]]] = {
auth.enrolments map { enrolmentsUri =>
http.GET(s"$authBaseUrl$enrolmentsUri") map { response =>
response.json.asOpt[Seq[Enrolment]]
} recover {
case e: NotFoundException => {
None
}
}
} getOrElse {
Logger.debug("No enrolment uri.")
Future.successful(None)
def fetchAuthority()(implicit headerCarrier: HeaderCarrier): Future[Option[Authority]] = {
authorised().retrieve(Retrievals.credentials and Retrievals.nino) {
case credentials ~ nino => Future.successful(Some(Authority(credentials.providerId, nino)))
case _ => Future.successful(None)
}.recover {
case e: NotFoundException => None
}
}

Expand All @@ -64,33 +60,23 @@ trait AuthConnector extends AuthorisedFunctions {
}
}

def fetchDesUserInfo(auth: Authority)(implicit hc: HeaderCarrier): Future[Option[DesUserInfo]] = {
def fetchDesUserInfo()(implicit hc: HeaderCarrier): Future[Option[DesUserInfo]] = {
val nothing = Future.successful(None)
if (auth.nino.isDefined)
authorised().retrieve(Retrievals.allItmpUserDetails) {
case name ~ dateOfBirth ~ address =>
Future.successful(Some(DesUserInfo(name, dateOfBirth, address)))
case _ => nothing
}.recoverWith {
case ex: NotFoundException => nothing
}
else nothing
}

def fetchAuthority()(implicit headerCarrier: HeaderCarrier): Future[Option[Authority]] = {
http.GET(s"$authBaseUrl/auth/authority") map { response =>
response.json.asOpt[Authority]
authorised().retrieve(Retrievals.allItmpUserDetails) {
case name ~ dateOfBirth ~ address =>
Future.successful(Some(DesUserInfo(name, dateOfBirth, address)))
case _ => nothing
}.recoverWith {
case ex: NotFoundException => nothing
}
}
}

object AuthConnector extends AuthConnector with ServicesConfig {
override lazy val authBaseUrl = baseUrl("auth")
override lazy val http = WSHttp

override def authConnector = new PlayAuthConnector {
override val serviceUrl = baseUrl("auth")
override def authConnector: uk.gov.hmrc.auth.core.AuthConnector = ConcreteAuthConnector
}

override def http = WSHttp
}
object ConcreteAuthConnector extends PlayAuthConnector with ServicesConfig {
override val serviceUrl = baseUrl("auth")
override def http = WSHttp
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ package uk.gov.hmrc.openidconnect.userinfo.controllers

import play.api.libs.json.Json
import uk.gov.hmrc.api.controllers.HeaderValidator
import uk.gov.hmrc.http.{BadRequestException, Upstream4xxResponse, Upstream5xxResponse}
import uk.gov.hmrc.openidconnect.userinfo.services.{LiveUserInfoService, SandboxUserInfoService, UserInfoService}
import uk.gov.hmrc.play.microservice.controller.BaseController

import scala.concurrent.ExecutionContext.Implicits.global
import uk.gov.hmrc.http.{BadRequestException, Upstream4xxResponse, Upstream5xxResponse}

trait UserInfoController extends BaseController with HeaderValidator {
val service: UserInfoService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package uk.gov.hmrc.openidconnect.userinfo.data

import org.joda.time._
import org.scalacheck.Gen
import uk.gov.hmrc.auth.core.retrieve.MdtpInformation
import uk.gov.hmrc.auth.core.{Enrolment, EnrolmentIdentifier}
import uk.gov.hmrc.openidconnect.userinfo.config.UserInfoFeatureSwitches
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Enrolment, EnrolmentIdentifier, GovernmentGatewayDetails, Mdtp, UserInfo}
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, GovernmentGatewayDetails, Mdtp, UserInfo}

trait UserInfoGenerator {
val firstNames = List(Some("Roland"), Some("Eddie"), Some("Susanna"), Some("Jake"), Some("Oy"), Some("Cuthbert"), Some("Alain"), Some("Jamie"), Some("Thomas"), Some("Susan"), Some("Randall"), None)
Expand Down Expand Up @@ -53,7 +53,7 @@ trait UserInfoGenerator {

def address = addressWithToggleableFeatures(UserInfoFeatureSwitches.addressLine5.isEnabled, UserInfoFeatureSwitches.countryCode.isEnabled)

val enrolments = Seq(Enrolment("IR-SA", List(EnrolmentIdentifier("UTR", "174371121"))))
val enrolments = Set(Enrolment("IR-SA", List(EnrolmentIdentifier("UTR", "174371121")), "Activated"))
val government_gateway: GovernmentGatewayDetails = GovernmentGatewayDetails(Some("32131"), Some(Seq("User")), Some("Chambers"),Some("affinityGroup"),
Some("agent-code-12345"), Some("agent-id-12345"), Some("agent-friendly-name-12345"), Some("gateway-token-val"), Some(10))
val mdtp = Mdtp(deviceId, sessionId)
Expand Down
9 changes: 2 additions & 7 deletions app/uk/gov/hmrc/openidconnect/userinfo/domain/Authority.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@

package uk.gov.hmrc.openidconnect.userinfo.domain

case class Authority(credentialStrength: Option[String] = None,
confidenceLevel: Option[Int] = None,
nino: Option[String] = None,
userDetailsLink: Option[String] = None,
enrolments: Option[String] = None,
affinityGroup: Option[String] = None,
credId: Option[String] = None)
case class Authority(credId: String,
nino: Option[String] = None)
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@

package uk.gov.hmrc.openidconnect.userinfo.domain

case class EnrolmentIdentifier(key: String, value: String)

case class Enrolment(key: String,
identifiers: Seq[EnrolmentIdentifier] = Seq(),
state: String = "Activated") {

def isActivated = state.toLowerCase == "activated"
}
import uk.gov.hmrc.auth.core.Enrolment

case object ActiveEnrolment {
def unapply(enrolment: Enrolment): Option[String] =
Expand Down
5 changes: 3 additions & 2 deletions app/uk/gov/hmrc/openidconnect/userinfo/domain/UserInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package uk.gov.hmrc.openidconnect.userinfo.domain

import org.joda.time.LocalDate
import uk.gov.hmrc.auth.core.retrieve.{ItmpAddress, ItmpName, MdtpInformation}
import uk.gov.hmrc.auth.core.Enrolment
import uk.gov.hmrc.auth.core.retrieve.{ItmpAddress, ItmpName}

case class Address(formatted: String,
postal_code: Option[String],
Expand All @@ -33,7 +34,7 @@ case class UserInfo(given_name: Option[String] = None,
email: Option[String] = None,
birthdate: Option[LocalDate] = None,
uk_gov_nino: Option[String] = None,
hmrc_enrolments: Option[Seq[Enrolment]] = None,
hmrc_enrolments: Option[Set[Enrolment]] = None,
government_gateway: Option[GovernmentGatewayDetails] = None,
mdtp: Option[Mdtp] = None)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package uk.gov.hmrc.openidconnect.userinfo

import play.api.libs.json._
import uk.gov.hmrc.auth.core.retrieve.{GatewayInformation, ItmpAddress, ItmpName, MdtpInformation}
import uk.gov.hmrc.auth.core.{Enrolment, EnrolmentIdentifier}
import uk.gov.hmrc.http.Token

package object domain {
Expand All @@ -39,7 +40,6 @@ package object domain {
implicit val dateReads = Reads.jodaDateReads("yyyy-MM-dd")
implicit val dateWrites = Writes.jodaDateWrites("yyyy-MM-dd")
implicit val addressFmt = Json.format[Address]
implicit val authorityFmt = Json.format[Authority]
implicit val userInfoFmt = Json.format[UserInfo]
implicit val apiAccessFmt = Json.format[APIAccess]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ import controllers.Default.Unauthorized
import play.api.libs.json.Json
import play.api.mvc.{Filter, RequestHeader, Result}
import play.api.routing.Router
import uk.gov.hmrc.auth.core.{AuthorisationException, AuthorisedFunctions}
import uk.gov.hmrc.openidconnect.userinfo.config.{AuthParamsControllerConfiguration, ControllerConfiguration}
import uk.gov.hmrc.openidconnect.userinfo.connectors.ConcreteAuthConnector
import uk.gov.hmrc.openidconnect.userinfo.controllers.ErrorUnauthorized
import uk.gov.hmrc.openidconnect.userinfo.services.AuthService
import uk.gov.hmrc.play.HeaderCarrierConverter
import uk.gov.hmrc.play.auth.controllers.{AuthConfig, AuthParamsControllerConfig}
import uk.gov.hmrc.play.microservice.filters.MicroserviceFilterSupport

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.HeaderCarrierConverter
import uk.gov.hmrc.play.microservice.filters.MicroserviceFilterSupport

trait MicroserviceAuthFilter extends Filter {
trait MicroserviceAuthFilter extends Filter with AuthorisedFunctions {
def apply(next: (RequestHeader) => Future[Result])(rh: RequestHeader): Future[Result] = {
implicit val hc = HeaderCarrierConverter.fromHeadersAndSession(rh.headers)

Expand All @@ -43,21 +43,22 @@ trait MicroserviceAuthFilter extends Filter {
}

authConfig(rh) match {
case Some(authConfig) => authService.isAuthorised().flatMap {
case true => next(rh)
case _ => Future.successful(Unauthorized(Json.toJson(ErrorUnauthorized())))
}
case Some(authConfig) =>
authorised() {
next(rh)
} recoverWith {
case e: AuthorisationException => Future.successful(Unauthorized(Json.toJson(ErrorUnauthorized())))
}
case _ => next(rh)
}
}

val authService: AuthService
val authParamsConfig: AuthParamsControllerConfig

def controllerNeedsAuth(controllerName: String): Boolean = ControllerConfiguration.paramsForController(controllerName).needsAuth
}

object MicroserviceAuthFilter extends MicroserviceAuthFilter with MicroserviceFilterSupport {
override lazy val authService = AuthService
override lazy val authParamsConfig = AuthParamsControllerConfiguration
lazy val authConnector = ConcreteAuthConnector
}
41 changes: 0 additions & 41 deletions app/uk/gov/hmrc/openidconnect/userinfo/services/AuthService.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package uk.gov.hmrc.openidconnect.userinfo.services

import uk.gov.hmrc.auth.core.Enrolments
import uk.gov.hmrc.http.logging.Authorization
import uk.gov.hmrc.http.{BadRequestException, HeaderCarrier, UnauthorizedException}
import uk.gov.hmrc.openidconnect.userinfo.connectors._
Expand Down Expand Up @@ -48,42 +49,31 @@ trait LiveUserInfoService extends UserInfoService {
else Future.successful(None)
}

def getMaybeByParamForScopes[I, O](maybeScopes: Set[String], allScopes: Set[String], param: I, f: I => Future[Option[O]]): Future[Option[O]] = {
if ((maybeScopes intersect allScopes).nonEmpty) f(param)
else Future.successful(None)
}

val scopesForAuthority = Set("openid:government-gateway", "email", "profile", "address", "openid:gov-uk-identifiers", "openid:hmrc-enrolments", "openid:mdtp")
val maybeAuthority = getMaybeForScopes(scopesForAuthority, scopes, authConnector.fetchAuthority)
val maybeAuthority = getMaybeForScopes(scopesForAuthority, scopes, authConnector.fetchAuthority())

val scopesForUserDetails = Set("openid:government-gateway", "email", "openid:mdtp")
def maybeUserDetails = getMaybeForScopes[UserDetails](scopesForUserDetails, scopes, authConnector.fetchUserDetails)

val scopesForDes = Set("profile", "address")
val maybeDesUserInfo = maybeAuthority flatMap { authority =>
val concreteAuthority = authority.getOrElse(Authority())
getMaybeByParamForScopes[Authority, DesUserInfo](scopesForDes, scopes, concreteAuthority,
concreteAuthority.nino match {
case Some(_) => authConnector.fetchDesUserInfo
case _ => (_) => Future.failed(new BadRequestException("NINO not found for this user"))
def maybeDesUserInfo = {
getMaybeForScopes[DesUserInfo](scopesForDes, scopes,
maybeAuthority flatMap {
case Some(auth) if auth.nino.isDefined => authConnector.fetchDesUserInfo
case _ => Future.failed(new BadRequestException("NINO not found for this user"))
}
)
}

val scopesForUserDetails = Set("openid:government-gateway", "email", "openid:mdtp")
val maybeUserDetails = maybeAuthority flatMap { authority =>
getMaybeByParamForScopes[Authority, UserDetails](scopesForUserDetails, scopes, authority.getOrElse(Authority()), _ => authConnector.fetchUserDetails)
}

def maybeEnrolments = maybeAuthority flatMap { authority =>
getMaybeByParamForScopes[Authority, Seq[Enrolment]](Set("openid:hmrc-enrolments"), scopes,
authority.getOrElse(Authority()), authConnector.fetchEnrolments)
}
def maybeEnrolments = getMaybeForScopes[Enrolments](Set("openid:hmrc-enrolments"), scopes, authConnector.fetchEnrolments)

for {
authority <- maybeAuthority
enrolments <- maybeEnrolments
desUserInfo <- maybeDesUserInfo
userDetails <- maybeUserDetails
} yield
userInfoTransformer.transform(scopes, desUserInfo, enrolments, authority, userDetails)
userInfoTransformer.transform(scopes, authority, desUserInfo, enrolments, userDetails)
}
}
}
Expand Down
Loading

0 comments on commit fb1c35e

Please sign in to comment.