|
| 1 | +/* |
| 2 | + * ============================================================================ |
| 3 | + * Name : AuthClient.java |
| 4 | + * Author : AppDevForAll |
| 5 | + * Copyright : Copyright (c) 2026 AppDevForAll |
| 6 | + * Description : ADFA-5043. App-side client for the dash-node auto-login endpoint: |
| 7 | + * GET /k2go-api/auth/<service>/session -> { service, cookie } |
| 8 | + * The server logs into Calibre-Web ("books"/"calibre") or Kolibri ("kolibri") with the |
| 9 | + * stored admin credentials and returns the session cookie; the app injects it into the |
| 10 | + * WebView CookieManager so the card opens already authenticated. The password never |
| 11 | + * reaches the app. On any failure (service not installed/ready) the caller just opens the |
| 12 | + * card without a cookie. Only reachable on-device (localhost); the box denies the API to |
| 13 | + * remote clients. |
| 14 | + * ============================================================================ |
| 15 | + */ |
| 16 | +package org.iiab.controller.redesign; |
| 17 | + |
| 18 | +import android.os.Handler; |
| 19 | +import android.os.Looper; |
| 20 | + |
| 21 | +import org.iiab.controller.config.BoxEndpoints; |
| 22 | +import org.iiab.controller.util.AppExecutors; |
| 23 | +import org.json.JSONObject; |
| 24 | + |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.net.HttpURLConnection; |
| 28 | +import java.net.URL; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | + |
| 31 | +public final class AuthClient { |
| 32 | + private AuthClient() {} |
| 33 | + |
| 34 | + private static final Handler MAIN = new Handler(Looper.getMainLooper()); |
| 35 | + |
| 36 | + public interface SessionCb { |
| 37 | + /** {@code cookie} is a ready-to-use Cookie header ("name=value; name2=value2"). */ |
| 38 | + void onOk(String cookie); |
| 39 | + /** The session couldn't be obtained (service not installed/ready, bad creds, etc.). */ |
| 40 | + void onErr(); |
| 41 | + } |
| 42 | + |
| 43 | + /** Ask the box for a signed-in session cookie for a service ("books"/"calibre" or "kolibri"). */ |
| 44 | + public static void session(String service, SessionCb cb) { |
| 45 | + AppExecutors.get().io().execute(() -> { |
| 46 | + try { |
| 47 | + String url = BoxEndpoints.API + "/auth/" + service + "/session"; |
| 48 | + JSONObject o = new JSONObject(httpGet(url)); |
| 49 | + final String cookie = o.optString("cookie", ""); |
| 50 | + if (cookie.isEmpty()) MAIN.post(cb::onErr); |
| 51 | + else MAIN.post(() -> cb.onOk(cookie)); |
| 52 | + } catch (Exception e) { |
| 53 | + MAIN.post(cb::onErr); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + private static String httpGet(String urlStr) throws Exception { |
| 59 | + HttpURLConnection c = (HttpURLConnection) new URL(urlStr).openConnection(); |
| 60 | + try { |
| 61 | + c.setUseCaches(false); |
| 62 | + c.setConnectTimeout(4000); |
| 63 | + // The server does a login handshake with the local service; a missing service fails fast. |
| 64 | + c.setReadTimeout(12000); |
| 65 | + c.setRequestProperty("Accept", "application/json"); |
| 66 | + int code = c.getResponseCode(); |
| 67 | + String text = readAll(code >= 200 && code < 400 ? c.getInputStream() : c.getErrorStream()); |
| 68 | + if (code < 200 || code >= 400) throw new Exception("HTTP " + code); |
| 69 | + return text; |
| 70 | + } finally { |
| 71 | + c.disconnect(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static String readAll(InputStream is) throws Exception { |
| 76 | + if (is == null) return ""; |
| 77 | + ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 78 | + byte[] chunk = new byte[4096]; |
| 79 | + int n; |
| 80 | + while ((n = is.read(chunk)) != -1) buf.write(chunk, 0, n); |
| 81 | + is.close(); |
| 82 | + return buf.toString(StandardCharsets.UTF_8.name()); |
| 83 | + } |
| 84 | +} |
0 commit comments