Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/actors/Cleaner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Cleaner @Inject()(unifiService: UnifiService,
persons <- EitherT(peopleService.getPersons)
accounts <- EitherT(unifiService.getRadiusAccounts)
} yield {
accounts.map(_.name).toSet diff persons.map(_.email).toSet
accounts.map(_.name).toSet diff persons.map(_.emailAddress).toSet
}

oldAccounts.value.map {
Expand Down
2 changes: 1 addition & 1 deletion app/models/Person.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package models

import play.api.libs.json.{Format, Json}

case class Person(email: String)
case class Person(emailAddress: String)

object Person {
implicit val PersonFormat: Format[Person] = Json.format[Person]
Expand Down
68 changes: 54 additions & 14 deletions app/services/PeopleService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,70 @@ package services
import com.google.inject.Inject
import models.Person
import play.api.http.Status
import play.api.libs.json.Format
import play.api.libs.ws.WSClient
import play.api.libs.json.Json
import play.api.libs.ws.{WSAuthScheme, WSClient}
import play.api.{Configuration, Logging}

import java.time.Instant
import scala.concurrent.{ExecutionContext, Future}

class PeopleService @Inject()(configuration: Configuration,
ws: WSClient)(implicit ec: ExecutionContext) extends Logging {

private val baseUrl: String = configuration.get[String]("people.baseUrl")
private val apiKey: String = configuration.get[String]("people.apiKey")
private val query =
"""
|{
| people(employeeStatuses: ACTIVE) {
| emailAddress
| }
|}
|""".stripMargin

def getPersons: Future[Either[String, Seq[Person]]] = {
withAuth[Seq[Person]](s"$baseUrl/api/people")
}
private val baseUrl: String = configuration.get[String]("lunagraph.baseUrl")

private val clientId: String = configuration.get[String]("lunagraph.client.id")
private val clientSecret: String = configuration.get[String]("lunagraph.client.secret")
private val clientIssuer: String = configuration.get[String]("lunagraph.client.issuer")

private var tokenExpiry: Option[Instant] = None
private var token: Option[String] = None

private def withAuth[A](url: String)(implicit fjs: Format[A]): Future[Either[String, A]] = {
ws.url(url).addQueryStringParameters("apiKey" -> apiKey).get().map { response =>
response.status match {
case Status.OK => Right(response.json.as[A])
case _ =>
logger.error(response.body)
Left(response.body)
def getPersons: Future[Either[String, Seq[Person]]] = for {
_ <- ensureToken()
wsRes <- ws.url(s"${baseUrl}/graphql")
.withHttpHeaders("Authorization"-> s"Bearer ${token.get}")
.post(Json.toJsObject(Map("query" -> query)))
res = wsRes.status match {
case Status.OK =>
val jsonResult = Json.parse(wsRes.body)
Right((jsonResult \ "data" \ "people").get.as[Seq[Person]])
case _ => logger.error(wsRes.body)
Left(wsRes.body)
}
} yield res


private def ensureToken(): Future[Either[String, Unit]] = {
val cutOff = Instant.now().minusSeconds(30)
if (token == None || tokenExpiry == None || tokenExpiry.get.isAfter(cutOff)){
for {
clientResponse <- ws.url(s"${clientIssuer}/protocol/openid-connect/token")
.withAuth(clientId, clientSecret, WSAuthScheme.BASIC)
.post(Map("grant_type" -> Seq("client_credentials")))
parsedResponse = clientResponse.status match {
case Status.OK =>
val jsonResponse = Json.parse(clientResponse.body)
val expiresIn = (jsonResponse \ "expires_in").get.as[Int]
token = Some((jsonResponse \ "access_token").get.as[String])
tokenExpiry = Some(Instant.now().plusSeconds(expiresIn))
Right(())
case _ =>
logger.error(clientResponse.body)
Left(clientResponse.body)
}
} yield parsedResponse
} else {
Future.successful(Right(()))
}
}
}
10 changes: 7 additions & 3 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ unifi {
]
}

people {
baseUrl = "https://people.lunatech.com"
apiKey = ${?PEOPLE_API_KEY}
lunagraph {
baseUrl = "https://lunagraph.acceptance.lunatech.cloud"
client {
issuer = "https://keycloak.lunatech.com/realms/lunatech"
id = ${LUNAGRAPH_CLIENT_ID}
secret = ${?LUNAGRAPH_CLIENT_SECRET}
}
}

play.http.forwarded {
Expand Down