Skip to content

Commit 1b5d963

Browse files
committed
Fix token handling.
1 parent d6ef548 commit 1b5d963

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

app/actors/Cleaner.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import scala.concurrent.ExecutionContext
1212
@Singleton
1313
class CleanerScheduler @Inject()(val system: ActorSystem, @Named("cleaner-actor") val cleanerActor: ActorRef)(implicit ec: ExecutionContext) {
1414
QuartzSchedulerExtension(system).schedule("Clean", cleanerActor, Clean)
15+
cleanerActor ! Clean
1516
}
1617

1718
class Cleaner @Inject()(unifiService: UnifiService,

app/services/PeopleService.scala

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package services
22

3+
import cats.data.EitherT
4+
import cats.syntax._
35
import com.google.inject.Inject
46
import models.Person
57
import play.api.http.Status
68
import play.api.libs.json.Json
79
import play.api.libs.ws.{WSAuthScheme, WSClient}
810
import play.api.{Configuration, Logging}
911

10-
import java.time.Instant
1112
import scala.concurrent.{ExecutionContext, Future}
1213

1314
class PeopleService @Inject()(configuration: Configuration,
@@ -28,35 +29,42 @@ class PeopleService @Inject()(configuration: Configuration,
2829
private val clientSecret: String = configuration.get[String]("lunagraph.client.secret")
2930
private val clientIssuer: String = configuration.get[String]("lunagraph.client.issuer")
3031

31-
def getPersons: Future[Either[String, Seq[Person]]] = for {
32-
token <- getToken()
33-
wsRes <- ws.url(s"${baseUrl}/graphql")
34-
.withHttpHeaders("Authorization"-> s"Bearer ${token}")
32+
def getPersons: Future[Either[String, Seq[Person]]] = {
33+
for {
34+
token <- EitherT(getToken())
35+
people <- EitherT(getPersonsWithToken(token))
36+
} yield people
37+
}.value
38+
39+
private def getPersonsWithToken(token: String): Future[Either[String, Seq[Person]]] = {
40+
ws.url(s"${baseUrl}/graphql")
41+
.withHttpHeaders("Authorization" -> s"Bearer $token")
3542
.post(Json.toJsObject(Map("query" -> query)))
36-
res = wsRes.status match {
37-
case Status.OK =>
38-
val jsonResult = Json.parse(wsRes.body)
39-
Right((jsonResult \ "data" \ "people").get.as[Seq[Person]])
40-
case _ => logger.error(wsRes.body)
41-
Left(wsRes.body)
42-
}
43-
} yield res
44-
45-
46-
private def getToken(): Future[Either[String, String]] = {
47-
for {
48-
clientResponse <- ws.url(s"${clientIssuer}/protocol/openid-connect/token")
49-
.withAuth(clientId, clientSecret, WSAuthScheme.BASIC)
50-
.post(Map("grant_type" -> Seq("client_credentials")))
51-
parsedResponse = clientResponse.status match {
52-
case Status.OK =>
53-
val jsonResponse = Json.parse(clientResponse.body)
54-
val token = (jsonResponse \ "access_token").get.as[String]
55-
Right(token)
56-
case _ =>
57-
logger.error(clientResponse.body)
58-
Left(clientResponse.body)
43+
.map { wsRes =>
44+
wsRes.status match {
45+
case Status.OK =>
46+
val jsonResult = Json.parse(wsRes.body)
47+
Right((jsonResult \ "data" \ "people").get.as[Seq[Person]])
48+
case _ => logger.error(s"${wsRes.status} - ${wsRes.body}")
49+
Left(wsRes.body)
50+
}
5951
}
60-
} yield parsedResponse
6152
}
53+
54+
55+
private def getToken(): Future[Either[String, String]] =
56+
ws.url(s"${clientIssuer}/protocol/openid-connect/token")
57+
.withAuth(clientId, clientSecret, WSAuthScheme.BASIC)
58+
.post(Map("grant_type" -> Seq("client_credentials")))
59+
.map { clientResponse =>
60+
clientResponse.status match {
61+
case Status.OK =>
62+
val jsonResponse = Json.parse(clientResponse.body)
63+
val token = (jsonResponse \ "access_token").get.as[String]
64+
Right(token)
65+
case _ =>
66+
logger.error(s"${clientResponse.status} - ${clientResponse.body}")
67+
Left(clientResponse.body)
68+
}
69+
}
6270
}

conf/application.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ lunagraph {
3434
baseUrl = "https://lunagraph.acceptance.lunatech.cloud"
3535
client {
3636
issuer = "https://keycloak.lunatech.com/realms/lunatech"
37-
id = ${LUNAGRAPH_CLIENT_ID}
37+
id = ${?LUNAGRAPH_CLIENT_ID}
3838
secret = ${?LUNAGRAPH_CLIENT_SECRET}
3939
}
4040
}

0 commit comments

Comments
 (0)