-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/scala/fi/oph/koski/http/OtuvaOAuth2ClientFactory.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package fi.oph.koski.http | ||
|
||
import cats.effect.{IO, Resource} | ||
import cats.effect.std.Hotswap | ||
import fi.oph.koski.config.SecretsManager | ||
import fi.oph.koski.log.NotLoggable | ||
import fi.oph.scalaschema.Serializer.format | ||
import org.http4s.{Header, Method, Request, Response, Uri, UrlForm} | ||
import org.http4s.client.Client | ||
import org.json4s.jackson.JsonMethods | ||
import org.typelevel.ci.CIStringSyntax | ||
|
||
case class OtuvaOAuth2Credentials(id: String, secret: String) extends NotLoggable | ||
|
||
object OtuvaOAuth2Credentials { | ||
def fromSecretsManager: OtuvaOAuth2Credentials = { | ||
val cachedSecretsClient = new SecretsManager | ||
val secretId = cachedSecretsClient.getSecretId("Otuva OAuth2 credentials", "OPINTOPOLKU_VIRKAILIJA_OAUTH2_SECRET_ID") | ||
cachedSecretsClient.getStructuredSecret[OtuvaOAuth2Credentials](secretId) | ||
} | ||
} | ||
|
||
class OtuvaOAuth2ClientFactory( | ||
credetials: OtuvaOAuth2Credentials, | ||
otuvaTokenEndpoint: String, | ||
) { | ||
|
||
private var token: Option[String] = None | ||
private val otuvaTokenEndpointUri = Uri.fromString(otuvaTokenEndpoint).right.get | ||
|
||
def apply(serviceUrl: String, serviceClient: Client[IO]): Http = { | ||
|
||
def withOAuth2Token(req: Request[IO], hotswap: Hotswap[IO, Response[IO]]) = { | ||
getToken().flatMap(requestWithToken(req, hotswap, retry = true)) | ||
} | ||
|
||
def getToken(): IO[String] = { | ||
token match { | ||
case Some(s) => IO.pure(s) | ||
case None => refreshToken() | ||
} | ||
} | ||
|
||
def requestWithToken(req: Request[IO], hotswap: Hotswap[IO, Response[IO]], retry: Boolean)(token: String): IO[Response[IO]] = { | ||
val newReq = req.withHeaders(Header.Raw(ci"Authentication", "Bearer " + token)) | ||
hotswap.swap(serviceClient.run(newReq)).flatMap { | ||
// TODO: better expiry check | ||
case r: Response[IO] if r.status.code != 200 => | ||
refreshToken().flatMap(requestWithToken(req, hotswap, retry = false)) | ||
case r: Response[IO] => IO.pure(r) | ||
} | ||
} | ||
|
||
def refreshToken(): IO[String] = { | ||
val body = UrlForm("grant_type" -> "client_credentials", "client_id" -> credetials.id, "client_secret" -> credetials.secret) | ||
val req = Request[IO](Method.POST, otuvaTokenEndpointUri).withEntity(body) | ||
|
||
serviceClient.run(req).use( | ||
res => { | ||
res.as[String].map { | ||
s => | ||
val v = JsonMethods.parse(s) | ||
val t = (v \ "access_token").extract[String] | ||
token = Some(t) | ||
t | ||
} | ||
} | ||
) | ||
} | ||
|
||
val client = Client[IO] { | ||
req => | ||
Hotswap.create[IO, Response[IO]].flatMap { hotswap => | ||
Resource.eval(withOAuth2Token(req, hotswap)) | ||
} | ||
} | ||
|
||
Http(serviceUrl, client) | ||
} | ||
|
||
} |
66 changes: 0 additions & 66 deletions
66
src/main/scala/fi/oph/koski/http/VirkailijaOAuth2Client.scala
This file was deleted.
Oops, something went wrong.