Skip to content

Commit

Permalink
Merge pull request #35 from hmrc/PE-3167
Browse files Browse the repository at this point in the history
Pe 3167
  • Loading branch information
fenallen authored Sep 8, 2017
2 parents 8aa2595 + f5e80fb commit ca4afa8
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import uk.gov.hmrc.play.http.{HeaderCarrier, HttpGet, NotFoundException}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait AuthConnector extends uk.gov.hmrc.play.auth.microservice.connectors.AuthConnector with AuthorisedFunctions{
trait AuthConnector extends uk.gov.hmrc.play.auth.microservice.connectors.AuthConnector with AuthorisedFunctions {
val authBaseUrl: String

val http: HttpGet
Expand All @@ -48,22 +48,25 @@ trait AuthConnector extends uk.gov.hmrc.play.auth.microservice.connectors.AuthCo
}
}

def fetchUserDetails(auth: Authority)(implicit hc: HeaderCarrier): Future[Option[UserDetails]] = {
authorised().retrieve(Retrievals.allUserDetails) {
def fetchUserDetails()(implicit hc: HeaderCarrier): Future[Option[UserDetails]] = {
authorised().retrieve(Retrievals.allUserDetails and Retrievals.mdtpInformation and Retrievals.gatewayInformation) {
case credentials ~ name ~ birthDate ~ postCode ~ email ~ affinityGroup ~ agentCode ~ agentInformation ~
credentialRole ~ description ~ groupId =>
credentialRole ~ description ~ groupId ~ mdtp ~ gatewayInformation =>
Future.successful(Some(UserDetails(authProviderId = Some(credentials.providerId), authProviderType = Some(credentials.providerType),
name = name.name, lastName = name.lastName, dateOfBirth = birthDate, postCode = postCode, email = email,
affinityGroup = affinityGroup.map(_.toString()), agentCode = agentCode,
agentFriendlyName = agentInformation.agentFriendlyName, credentialRole = credentialRole.map(_.toString),
description = description, groupIdentifier = groupId, agentId = agentInformation.agentId)))
description = description, groupIdentifier = groupId, agentId = agentInformation.agentId,
gatewayInformation = gatewayInformation, mdtpInformation = mdtp)))
case _ => Future.successful(None)
}.recover {
case e: NotFoundException => None
}
}

def fetchDesUserInfo(authority: Authority)(implicit hc: HeaderCarrier): Future[Option[DesUserInfo]] = {
def fetchDesUserInfo(auth: Authority)(implicit hc: HeaderCarrier): Future[Option[DesUserInfo]] = {
val nothing = Future.successful(None)
if (authority.nino.isDefined)
if (auth.nino.isDefined)
authorised().retrieve(Retrievals.allItmpUserDetails) {
case name ~ dateOfBirth ~ address =>
Future.successful(Some(DesUserInfo(name, dateOfBirth, address)))
Expand All @@ -87,6 +90,7 @@ object AuthConnector extends AuthConnector with ServicesConfig {

override def authConnector = new PlayAuthConnector {
override val serviceUrl = baseUrl("auth")

override def http = WSHttp
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ 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.openidconnect.userinfo.config.UserInfoFeatureSwitches
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Enrolment, EnrolmentIdentifier, GovernmentGatewayDetails, UserInfo}
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Enrolment, EnrolmentIdentifier, 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)
val middleNames = List(Some("De"), Some("Donald"), Some("Billy"), Some("E"), Some("Alex"), Some("Abel"), None, None, None, None, None, None)
val lastNames = List(Some("Deschain"), Some("Dean"), Some("Dean"), Some("Chambers"), Some("Bumbler"), Some("Allgood"), Some("Johns"), Some("Curry"), Some("Whitman"), Some("Delgado"), Some("Flagg"), Some("Bowen"), None)
val deviceId = "device-id-abc"
val sessionId = "session-id-abcd"

val fullAddress = Some(Address(
"""221B Baker Street
Expand Down Expand Up @@ -52,7 +55,8 @@ trait UserInfoGenerator {

val enrolments = Seq(Enrolment("IR-SA", List(EnrolmentIdentifier("UTR", "174371121"))))
val government_gateway: GovernmentGatewayDetails = GovernmentGatewayDetails(Some("32131"),Some(Seq("User")),Some("affinityGroup"),
Some("agent-code-12345"), Some("agent-id-12345"), Some("agent-friendly-name-12345"), Some("gateway-token-val"))
Some("agent-code-12345"), Some("agent-id-12345"), Some("agent-friendly-name-12345"), Some("gateway-token-val"), Some(10))
val mdtp = Mdtp(deviceId, sessionId)

private lazy val ninoPrefixes = "ABCEGHJKLMNPRSTWXYZ"

Expand Down Expand Up @@ -98,7 +102,8 @@ trait UserInfoGenerator {
middleName <- middleNameGen
dob <- dateOfBirth
nino <- formattedNino
} yield UserInfo(name, lastName, middleName, address, email(name, lastName), Some(dob), Some(nino), Some(enrolments), Some(government_gateway))
} yield UserInfo(name, lastName, middleName, address, email(name, lastName), Some(dob), Some(nino), Some(enrolments),
Some(government_gateway), Some(mdtp))
}

object UserInfoGenerator extends UserInfoGenerator
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ case class Authority(credentialStrength: Option[String] = None,
userDetailsLink: Option[String] = None,
enrolments: Option[String] = None,
affinityGroup: Option[String] = None,
credId: Option[String] = None,
gatewayToken: Option[String] = None)
credId: Option[String] = None)
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ case class GovernmentGatewayDetails(
agent_code: Option[String],
agent_id: Option[String],
agent_friendly_name: Option[String],
gateway_token: Option[String]
gateway_token: Option[String],
unread_message_count: Option[Int]
)
35 changes: 19 additions & 16 deletions app/uk/gov/hmrc/openidconnect/userinfo/domain/UserDetails.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@
package uk.gov.hmrc.openidconnect.userinfo.domain

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

case class UserDetails (
authProviderId: Option[String] = None,
authProviderType: Option[String] = None,
name: Option[String] = None,
lastName: Option[String] = None,
dateOfBirth: Option[LocalDate] = None,
postCode: Option[String] = None,
email: Option[String] = None,
affinityGroup: Option[String] = None,
agentCode: Option[String] = None,
agentFriendlyName: Option[String] = None,
credentialRole: Option[String] = None,
description: Option[String] = None,
groupIdentifier: Option[String] = None,
agentId: Option[String] = None
)
case class UserDetails(
authProviderId: Option[String] = None,
authProviderType: Option[String] = None,
name: Option[String] = None,
lastName: Option[String] = None,
dateOfBirth: Option[LocalDate] = None,
postCode: Option[String] = None,
email: Option[String] = None,
affinityGroup: Option[String] = None,
agentCode: Option[String] = None,
agentFriendlyName: Option[String] = None,
credentialRole: Option[String] = None,
description: Option[String] = None,
groupIdentifier: Option[String] = None,
agentId: Option[String] = None,
gatewayInformation: Option[GatewayInformation],
mdtpInformation: Option[MdtpInformation]
)
7 changes: 5 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,13 +17,15 @@
package uk.gov.hmrc.openidconnect.userinfo.domain

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

case class Address(formatted: String,
postal_code: Option[String],
country: Option[String],
country_code: Option[String])

case class Mdtp(device_id: String, session_id: String)

case class UserInfo(given_name: Option[String],
family_name: Option[String],
middle_name: Option[String],
Expand All @@ -32,7 +34,8 @@ case class UserInfo(given_name: Option[String],
birthdate: Option[LocalDate],
uk_gov_nino: Option[String],
hmrc_enrolments: Option[Seq[Enrolment]],
government_gateway: Option[GovernmentGatewayDetails])
government_gateway: Option[GovernmentGatewayDetails],
mdtp: Option[Mdtp])

case class UserInformation(profile: Option[UserProfile],
address: Option[Address],
Expand Down
5 changes: 4 additions & 1 deletion app/uk/gov/hmrc/openidconnect/userinfo/domain/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package uk.gov.hmrc.openidconnect.userinfo

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

package object domain {
Expand All @@ -30,7 +30,10 @@ package object domain {
implicit val enrloment = Json.format[Enrolment]

implicit val token = Json.format[Token]
implicit val gatewayInformationFmt: Format[GatewayInformation] = Json.format[GatewayInformation]
implicit val mdtpInformationFmt: Format[MdtpInformation] = Json.format[MdtpInformation]
implicit val userDetails = Json.format[UserDetails]
implicit val mdtp = Json.format[Mdtp]
implicit val governmentGatewayDetails = Json.format[GovernmentGatewayDetails]

implicit val dateReads = Reads.jodaDateReads("yyyy-MM-dd")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ trait LiveUserInfoService extends UserInfoService {
else Future.successful(None)
}

val scopesForAuthority = Set("openid:government_gateway", "email", "profile", "address", "openid:gov-uk-identifiers", "openid:hmrc_enrolments")
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 scopesForDes = Set("profile", "address")
Expand All @@ -64,9 +64,9 @@ trait LiveUserInfoService extends UserInfoService {
else Future.successful(None)
}

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

def maybeEnrolments = maybeAuthority flatMap { authority =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package uk.gov.hmrc.openidconnect.userinfo.services
import org.joda.time.LocalDate
import uk.gov.hmrc.auth.core.retrieve.ItmpAddress
import uk.gov.hmrc.openidconnect.userinfo.config.UserInfoFeatureSwitches
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Authority, DesUserInfo, Enrolment, GovernmentGatewayDetails, UserDetails, UserInfo}
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Authority, DesUserInfo, Enrolment, GovernmentGatewayDetails, Mdtp, UserDetails, UserInfo}

trait UserInfoTransformer {

Expand All @@ -44,6 +44,9 @@ trait UserInfoTransformer {

val email = if (scopes.contains("email")) userDetails flatMap {_.email} else None

val mdtp = if (scopes.contains("openid:mdtp")) userDetails.flatMap(_.mdtpInformation.map(m => Mdtp(m.deviceId, m.sessionId))) else None


UserInfo(profile.flatMap(_.firstName),
profile.flatMap(_.familyName),
profile.flatMap(_.middleName),
Expand All @@ -52,7 +55,8 @@ trait UserInfoTransformer {
profile.flatMap(_.birthDate),
identifier,
userEnrolments,
ggInfo)
ggInfo,
mdtp)
}

private def formattedAddress(desAddress: ItmpAddress) = {
Expand All @@ -69,10 +73,13 @@ trait UserInfoTransformer {
val agentCode = userDetails flatMap { _.agentCode}
val agentFriendlyName = userDetails flatMap { _.agentFriendlyName}
val agentId = userDetails flatMap { _.agentId}
val gatewayToken = authority flatMap { _.gatewayToken }
val gatewayInformation = userDetails flatMap { _.gatewayInformation }
val mdtp = userDetails flatMap { _.mdtpInformation }

Some(GovernmentGatewayDetails(user_id = credId, roles = credentialRoles, affinity_group = affinityGroup,
agent_code = agentCode, agent_id = agentId, agent_friendly_name = agentFriendlyName, gateway_token = gatewayToken))
agent_code = agentCode, agent_id = agentId, agent_friendly_name = agentFriendlyName,
gateway_token = gatewayInformation.flatMap(_.gatewayToken),
unread_message_count = gatewayInformation.flatMap(_.unreadMessageCount)))
}

private case class UserProfile(firstName: Option[String], familyName: Option[String], middleName: Option[String], birthDate: Option[LocalDate])
Expand Down
2 changes: 1 addition & 1 deletion project/MicroserviceBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private object AppDependencies {
"org.scalacheck" %% "scalacheck" % "1.12.5",
"uk.gov.hmrc" %% "play-hmrc-api" % "1.2.0",
"uk.gov.hmrc" %% "domain" % "4.0.0",
"uk.gov.hmrc" %% "play-auth" % "2.2.1"
"uk.gov.hmrc" %% "play-auth" % "2.4.0"
)

val test = Seq(
Expand Down
7 changes: 6 additions & 1 deletion public/api/conf/1.0/examples/get-user-info-example-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
"agent_code": "123456-abc",
"agent_friendly_name": "AC Accounting",
"agent_id": "AC",
"gateway_token": "token-value-1234"
"gateway_token": "token-value-1234",
"unread_message_count": 0
},
"mdtp": {
"device_id": "device_id-12345",
"session_id": "session_id_123455"
}
}
21 changes: 21 additions & 0 deletions public/api/conf/1.0/schemas/userinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@
]
}
},
"mdtp": {
"type": "object",
"description": "Mdtp information based on government gateway input",
"properties": {
"device_id": {
"type": "string",
"description": "Device id",
"example": "3012345"
},
"session_id": {
"type": "string",
"description": "Session id",
"example": "2012345"
}
}
},
"government_gateway": {
"type": "object",
"description": "Legacy properties based on government gateway input.",
Expand Down Expand Up @@ -155,6 +171,11 @@
"type": "string",
"description": "Government gateway token",
"example": "123456789"
},
"unread_message_count": {
"type": "integer",
"description": "Unread message count",
"example": 12345
}
},
"additionalProperties": false
Expand Down
Loading

0 comments on commit ca4afa8

Please sign in to comment.