pull/653/merge
Varun Patil 2023-10-01 20:35:35 -07:00
parent 9a85fa22ce
commit bb4d204228
2 changed files with 40 additions and 49 deletions

View File

@ -28,55 +28,30 @@ class AccountService(private val mCtx: MainActivity, private val mHttp: HttpServ
* @param loginFlowUrl The login flow URL
*/
fun login(baseUrl: String, loginFlowUrl: String) {
// Make POST request to login flow URL
val client = OkHttpClient()
val request = Request.Builder()
.url(loginFlowUrl)
.header("User-Agent", "Memories")
.post("".toRequestBody("application/json".toMediaTypeOrNull()))
.build()
val response: Response
try {
response = client.newCall(request).execute()
val res = mHttp.postLoginFlow(loginFlowUrl)
val body = mHttp.bodyJson(res) ?: throw Exception("Failed to parse login flow response")
// Parse response body as JSON
val pollObj = body.getJSONObject("poll")
val pollToken = pollObj.getString("token")
val pollUrl = pollObj.getString("endpoint")
val loginUrl = body.getString("login")
// Open login page in browser
toast("Opening login page ...")
mCtx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(loginUrl)))
// Start polling in background
Thread { pollLogin(pollUrl, pollToken, baseUrl) }.start()
} catch (e: SocketTimeoutException) {
toast("Failed to connect to login flow URL")
return
}
// Read response body
val body = response.body?.string()
response.body?.close()
if (body == null) {
toast("Failed to get login flow response")
return
}
// Parse response body as JSON
val json = JSONObject(body)
val pollToken: String
val pollUrl: String
val loginUrl: String
try {
val pollObj = json.getJSONObject("poll")
pollToken = pollObj.getString("token")
pollUrl = pollObj.getString("endpoint")
loginUrl = json.getString("login")
toast("Opening login page...")
// Open login page in browser
mCtx.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(loginUrl)))
} catch (e: Exception) {
Log.e(TAG, "login: ", e)
toast("Failed to parse login flow response")
toast(e.message ?: "Unknown error")
return
}
// Start polling in background
Thread {
pollLogin(pollUrl, pollToken, baseUrl)
}.start()
}
/**

View File

@ -3,8 +3,10 @@ package gallery.memories.service
import android.net.Uri
import android.util.Base64
import android.webkit.WebView
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONObject
@ -88,21 +90,35 @@ class HttpService {
/** Get the API description request */
@Throws(Exception::class)
fun getApiDescription(): Response {
return runRequest("api/describe")
return runRequest(buildGet("api/describe"))
}
/** Make login flow request */
@Throws(Exception::class)
fun postLoginFlow(loginFlowUrl: String): Response {
return runRequest(Request.Builder()
.url(loginFlowUrl)
.header("User-Agent", "Memories")
.post("".toRequestBody("application/json".toMediaTypeOrNull()))
.build())
}
/** Run a request and get a JSON object */
@Throws(Exception::class)
private fun runRequest(path: String): Response {
return OkHttpClient().newCall(buildRequest(path)).execute()
private fun runRequest(request: Request): Response {
return OkHttpClient().newCall(request).execute()
}
/** Build a request */
private fun buildRequest(path: String): Request {
return Request.Builder()
/** Build a GET request */
private fun buildGet(path: String, auth: Boolean = true): Request {
val builder = Request.Builder()
.url(memoriesUrl + path)
.header("User-Agent", "Memories")
.get()
.header("Authorization", authHeader ?: "")
.build()
if (auth)
builder.header("Authorization", authHeader ?: "")
return builder.build()
}
}