mirror of
https://github.com/dyrkin/luxmed-bot.git
synced 2026-01-04 12:05:05 +01:00
api: Fix Authorization-Token
This commit is contained in:
@@ -26,9 +26,9 @@ class LuxmedApi[F[_]: ThrowableMonad] extends ApiBase {
|
||||
post[LoginResponse](request)
|
||||
}
|
||||
|
||||
def loginToApp(session: Session): F[HttpResponse[Unit]] = {
|
||||
def loginToApp(session: Session): F[HttpResponse[String]] = {
|
||||
val request = httpNewApiWithOldToken("Account/LogInToApp?app=search&client=3&lang=pl", session)
|
||||
getVoid(request)
|
||||
getString(request)
|
||||
}
|
||||
|
||||
def getForgeryToken(session: Session): F[HttpResponse[ForgeryTokenResponse]] = {
|
||||
|
||||
@@ -155,11 +155,11 @@ class ApiService extends SessionSupport {
|
||||
r1 <- luxmedApi.login(username, password, clientId)
|
||||
tmpSession = Session(r1.body.accessToken, r1.body.accessToken, "", r1.cookies)
|
||||
r2 <- luxmedApi.loginToApp(tmpSession)
|
||||
jwtToken = extractAuthorizationTokenFromCookies(r2)
|
||||
cookies = joinCookies(r1.cookies, r2.cookies, Seq(new HttpCookie("GlobalLang", "pl")))
|
||||
accessToken = r1.body.accessToken
|
||||
tokenType = r1.body.tokenType
|
||||
r3 <- luxmedApi.getReservationPage(tmpSession, cookies)
|
||||
jwtToken = extractAccessTokenFromReservationPage(r3.body)
|
||||
} yield Session(accessToken, tokenType, jwtToken, joinCookies(cookies, r3.cookies))
|
||||
} catch {
|
||||
case e: Exception if !secondAttempt => {
|
||||
@@ -184,4 +184,23 @@ class ApiService extends SessionSupport {
|
||||
case _ => throw new java.lang.RuntimeException(s"Unable to extract authorization token from reservation page")
|
||||
}
|
||||
}
|
||||
|
||||
private def extractAuthorizationTokenFromCookies(response: HttpResponse[_]): String = {
|
||||
response.headers.get("Set-Cookie") match {
|
||||
case Some(cookieHeaders) =>
|
||||
cookieHeaders
|
||||
.find(_.startsWith("Authorization-Token="))
|
||||
.flatMap { header =>
|
||||
header.split(";").headOption.flatMap {
|
||||
_.split("=", 2) match {
|
||||
case Array(_, value) => Some(value)
|
||||
case _ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
.getOrElse(throw new RuntimeException("Authorization-Token cookie not found in response headers"))
|
||||
case None =>
|
||||
throw new RuntimeException("No Set-Cookie headers found in response")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user