Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 44 additions & 14 deletions app/services/PeopleService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,60 @@ 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

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")

def getPersons: Future[Either[String, Seq[Person]]] = for {
token <- getToken()
wsRes <- ws.url(s"${baseUrl}/graphql")
.withHttpHeaders("Authorization"-> s"Bearer ${token}")
.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

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

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])
private def getToken(): Future[Either[String, String]] = {
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 token = (jsonResponse \ "access_token").get.as[String]
Right(token)
case _ =>
logger.error(response.body)
Left(response.body)
logger.error(clientResponse.body)
Left(clientResponse.body)
}
}
} yield parsedResponse
}
}
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