diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt index 483d152f..67313e67 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt @@ -22,9 +22,9 @@ const val FAKE_LIBRUS_TOKEN = "https://librus.szkolny.eu/access_token.php" const val FAKE_LIBRUS_ACCOUNT = "/synergia_accounts_fresh.php?login=" const val FAKE_LIBRUS_ACCOUNTS = "/synergia_accounts.php" -val LIBRUS_USER_AGENT = "$SYSTEM_USER_AGENT LibrusMobileApp" +val LIBRUS_USER_AGENT = "${SYSTEM_USER_AGENT}LibrusMobileApp" const val SYNERGIA_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Gecko/20100101 Firefox/62.0" -const val LIBRUS_CLIENT_ID = "wmSyUMo8llDAs4y9tJVYY92oyZ6h4lAt7KCuy0Gv" +const val LIBRUS_CLIENT_ID = "6XPsKf10LPz1nxgHQLcvZ1KM48DYzlBAhxipaXY8" const val LIBRUS_REDIRECT_URL = "http://localhost/bar" const val LIBRUS_AUTHORIZE_URL = "https://portal.librus.pl/oauth2/authorize?client_id=$LIBRUS_CLIENT_ID&redirect_uri=$LIBRUS_REDIRECT_URL&response_type=code" const val LIBRUS_LOGIN_URL = "https://portal.librus.pl/rodzina/login/action" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/login/LibrusLoginPortal.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/login/LibrusLoginPortal.kt index fa667dc6..e8e00950 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/login/LibrusLoginPortal.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/login/LibrusLoginPortal.kt @@ -63,23 +63,31 @@ class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) { .userAgent(LIBRUS_USER_AGENT) .withClient(data.app.httpLazy) .callback(object : TextCallbackHandler() { - override fun onSuccess(json: String, response: Response) { + override fun onSuccess(text: String, response: Response) { val location = response.headers().get("Location") if (location != null) { val authMatcher = Pattern.compile("http://localhost/bar\\?code=([A-z0-9]+?)$", Pattern.DOTALL or Pattern.MULTILINE).matcher(location) - if (authMatcher.find()) { - accessToken(authMatcher.group(1), null) - } else { - authorize(location) + when { + authMatcher.find() -> { + accessToken(authMatcher.group(1), null) + } + location.contains("rejected_client") -> { + data.error(ApiError(TAG, ERROR_LOGIN_LIBRUS_PORTAL_INVALID_CLIENT_ID) + .withResponse(response) + .withApiResponse("Location: $location\n$text")) + } + else -> { + authorize(location) + } } } else { - val csrfMatcher = Pattern.compile("name=\"csrf-token\" content=\"([A-z0-9=+/\\-_]+?)\"", Pattern.DOTALL).matcher(json) + val csrfMatcher = Pattern.compile("name=\"csrf-token\" content=\"([A-z0-9=+/\\-_]+?)\"", Pattern.DOTALL).matcher(text) if (csrfMatcher.find()) { login(csrfMatcher.group(1)) } else { data.error(ApiError(TAG, ERROR_LOGIN_LIBRUS_PORTAL_CSRF_MISSING) .withResponse(response) - .withApiResponse(json)) + .withApiResponse(text)) } } } @@ -186,7 +194,7 @@ class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) { return } val error = if (response?.code() == 200) null else - json.getString("hint") + json.getString("hint") ?: json.getString("error") error?.let { code -> when (code) { "Authorization code has expired" -> ERROR_LOGIN_LIBRUS_PORTAL_CODE_EXPIRED @@ -197,11 +205,9 @@ class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) { "Check the `code` parameter" -> ERROR_LOGIN_LIBRUS_PORTAL_NO_CODE "Check the `refresh_token` parameter" -> ERROR_LOGIN_LIBRUS_PORTAL_NO_REFRESH "Check the `redirect_uri` parameter" -> ERROR_LOGIN_LIBRUS_PORTAL_NO_REDIRECT - else -> when (json.getString("error")) { - "unsupported_grant_type" -> ERROR_LOGIN_LIBRUS_PORTAL_UNSUPPORTED_GRANT - "invalid_client" -> ERROR_LOGIN_LIBRUS_PORTAL_INVALID_CLIENT_ID - else -> ERROR_LOGIN_LIBRUS_PORTAL_OTHER - } + "unsupported_grant_type" -> ERROR_LOGIN_LIBRUS_PORTAL_UNSUPPORTED_GRANT + "invalid_client" -> ERROR_LOGIN_LIBRUS_PORTAL_INVALID_CLIENT_ID + else -> ERROR_LOGIN_LIBRUS_PORTAL_OTHER }.let { errorCode -> data.error(ApiError(TAG, errorCode) .withApiResponse(json) diff --git a/app/src/main/res/values/errors.xml b/app/src/main/res/values/errors.xml index f9842e65..126300bd 100644 --- a/app/src/main/res/values/errors.xml +++ b/app/src/main/res/values/errors.xml @@ -252,7 +252,7 @@ ERROR_LOGIN_LIBRUS_PORTAL_NO_REFRESH ERROR_LOGIN_LIBRUS_PORTAL_NO_REDIRECT ERROR_LOGIN_LIBRUS_PORTAL_UNSUPPORTED_GRANT - ERROR_LOGIN_LIBRUS_PORTAL_INVALID_CLIENT_ID + Błędne ID klienta. Zgłoś błąd programiście oraz zaktualizuj aplikację. ERROR_LOGIN_LIBRUS_PORTAL_REFRESH_INVALID ERROR_LOGIN_LIBRUS_PORTAL_REFRESH_REVOKED ERROR_LIBRUS_SYNERGIA_OTHER