Skip to content

Commit 18bbd75

Browse files
committed
Clean up; extract method.
1 parent 9f76847 commit 18bbd75

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

rest-lib/src/main/scala/com/gu/mediaservice/lib/auth/Authentication.scala

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,7 @@ class Authentication(config: CommonConfig,
8989
logger.info(s"Checking that $principal is allowed to access instance $instance")
9090
// Use the cookie instances for now but we are in a Future so are able to call the instances service for a canonical answer if we need to
9191

92-
val eventualPrincipalsInstances = {
93-
val instancesRequest: WSRequest = wsClient.url(myInstancesEndpoint)
94-
val onBehalfOfPrincipal: OnBehalfOfPrincipal = getOnBehalfOfPrincipal(principal)
95-
val authedInstancesRequest: WSRequest = onBehalfOfPrincipal(instancesRequest)
96-
authedInstancesRequest.get().map { r =>
97-
r.status match {
98-
case 200 =>
99-
implicit val ir: Reads[Instance] = Json.reads[Instance]
100-
Json.parse(r.body).as[Seq[Instance]]
101-
case _ =>
102-
logger.warn("Got non 200 status for instances call: " + r.status)
103-
Seq.empty
104-
}
105-
}
106-
}
107-
108-
eventualPrincipalsInstances.flatMap { principalsInstances: Seq[Instance] =>
92+
getMyInstances(principal).flatMap { principalsInstances: Seq[Instance] =>
10993
if (principalsInstances.exists(_.id == instance.id)) {
11094
logger.debug("Allowing this request!")
11195
block(new AuthenticatedRequest(principal, request))
@@ -137,6 +121,20 @@ class Authentication(config: CommonConfig,
137121
/** Use this for originating calls to other Grid services (this will sign the request and the receiving service will extract an `InnerServicePrincipal`)
138122
* IMPORTANT: Do not use this for simply making ongoing calls to other Grid services - instead use `getOnBehalfOfPrincipal` */
139123
def innerServiceCall(wsRequest: WSRequest): WSRequest = providers.innerServiceProvider.signRequest(wsRequest)
124+
125+
private def getMyInstances(principal: Principal): Future[Seq[Instance]] = {
126+
val onBehalfOfPrincipal = getOnBehalfOfPrincipal(principal)
127+
onBehalfOfPrincipal(wsClient.url(myInstancesEndpoint)).get().map { r =>
128+
r.status match {
129+
case 200 =>
130+
implicit val ir: Reads[Instance] = Json.reads[Instance]
131+
Json.parse(r.body).as[Seq[Instance]]
132+
case _ =>
133+
logger.warn("Got non 200 status for instances call: " + r.status)
134+
Seq.empty
135+
}
136+
}
137+
}
140138
}
141139

142140
object Authentication {

0 commit comments

Comments
 (0)