Skip to content

Commit

Permalink
use okta id and new identity library
Browse files Browse the repository at this point in the history
  • Loading branch information
pvighi committed May 7, 2024
1 parent 05b847f commit b95595c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
7 changes: 5 additions & 2 deletions app/auth/RequestWithClaims.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package auth

import com.gu.identity.auth.DefaultAccessClaims
import com.gu.identity.auth.{DefaultAccessClaims, DefaultIdentityClaims, OktaAuthenticatedUserInfo}
import play.api.mvc.{Request, WrappedRequest}

class RequestWithClaims[A](val claims: DefaultAccessClaims, request: Request[A]) extends WrappedRequest[A](request)
class RequestWithClaims[A](
val userInfo: OktaAuthenticatedUserInfo[DefaultIdentityClaims, DefaultAccessClaims],
request: Request[A]
) extends WrappedRequest[A](request)
6 changes: 3 additions & 3 deletions app/controllers/UserController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class UserController(
)(implicit ex: ExecutionContext)
extends BaseController {

def me(): Action[AnyContent] = authorisedAction(List(UserReadSelfSecure)).async(request =>
def me(): Action[AnyContent] = authorisedAction(List(UserReadSelfSecure)).async(request => {
userService
.fetchUserByIdentityId(request.claims.identityId)
.fetchUserByOktaId(request.userInfo.oktaId)
.map(_.map(user => Ok(toJson(user)(writes.me))).getOrElse(NotFound))
)
})
}
8 changes: 5 additions & 3 deletions app/services/CompositeUserService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class CompositeUserService(okta: OktaUserService, identityDb: LegacyIdentityDbUs
_ <- identityDb.healthCheck()
} yield ()

def fetchUserByIdentityId(identityId: String): Future[Option[User]] = for {
optLegacyUser <- identityDb.fetchUserByIdentityId(identityId)
optOktaUser <- optLegacyUser.flatMap(_.oktaId.map(okta.fetchUserByOktaId)).getOrElse(Future.successful(None))
override def fetchUserByOktaId(oktaId: String): Future[Option[User]] = for {
optOktaUser <- okta.fetchUserByOktaId(oktaId)
optLegacyUser <- optOktaUser
.map(oktaUser => identityDb.fetchUserByIdentityId(oktaUser.legacyIdentityId))
.getOrElse(Future.successful(None))
} yield for {
oktaUser <- optOktaUser
legacyUser <- optLegacyUser
Expand Down
2 changes: 1 addition & 1 deletion app/services/UserService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import scala.concurrent.Future

trait UserService extends Service {

def fetchUserByIdentityId(identityId: String): Future[Option[User]]
def fetchUserByOktaId(oktaId: String): Future[Option[User]]
}
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ lazy val root = (project in file("."))
("com.gu" %% "simple-configuration-ssm" % "2.0.0").cross(CrossVersion.for3Use2_13),
/* Using Scala 2.13 version of identity-auth-play until a Scala 3 version has been released:
* https://trello.com/c/5kOc41kD/4669-release-scala-3-version-of-identity-libraries */
("com.gu.identity" %% "identity-auth-core" % "4.24")
("com.gu.identity" %% "identity-auth-core" % "4.25")
.cross(CrossVersion.for3Use2_13)
exclude ("org.scala-lang.modules", "scala-xml_2.13")
exclude ("org.scala-lang.modules", "scala-parser-combinators_2.13")
Expand Down
11 changes: 10 additions & 1 deletion test/auth/AuthorisedActionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,19 @@ class AuthorisedActionSpec extends PlaySpec {
}

"return 200 when the token is valid and has the required scopes" in {
val userInfo = OktaAuthenticatedUserInfo[DefaultIdentityClaims, DefaultAccessClaims](
localAccessTokenClaims = DefaultAccessClaims(
oktaId = "someOktaId",
primaryEmailAddress = "a@b.com",
identityId = "I43",
username = None
),
serverSideUserInfo = None
)
val authService = mock[OktaAuthService]
when(authService.validateAccessToken(AccessToken("validToken"), requiredScopes))
.thenReturn(
IO.pure(DefaultAccessClaims(primaryEmailAddress = "a@b.com", identityId = "I43", username = None))
IO.pure(userInfo)
)
val bodyParser = mock[BodyParser[AnyContent]]
val action = new AuthorisedAction(authService, bodyParser, requiredScopes)
Expand Down

0 comments on commit b95595c

Please sign in to comment.