From 2e452eaa81f99e056c220a2d453ea6397fa7bf8e Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Mon, 13 Apr 2020 08:01:56 +0000 Subject: [PATCH] Fix version API auth check (#399) Merge branch 'master' into 389_alternate_version-api-auth rm comment get gitea Version Authentificated Co-authored-by: M M Arif Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/399 Reviewed-by: M M Arif --- .../mian/gitnex/activities/LoginActivity.java | 29 +++++++++++++++---- .../mian/gitnex/activities/MainActivity.java | 4 ++- .../mian/gitnex/interfaces/ApiInterface.java | 8 ++++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java index 634ee952..4fdf828a 100644 --- a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java @@ -377,7 +377,6 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener } tinyDb.putString("instanceUrlRaw", instanceHost); - //tinyDb.putString("loginUid", loginUid); tinyDb.putString("instanceUrl", instanceUrl); tinyDb.putString("instanceUrlWithProtocol", instanceUrlWithProtocol); @@ -414,10 +413,28 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP, final String loginToken_, final int loginType) { - Call callVersion = RetrofitClient - .getInstance(instanceUrl, getApplicationContext()) - .getApiInterface() - .getGiteaVersion(); + Call callVersion; + if (!loginToken_.isEmpty()) { + callVersion = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getGiteaVersionWithToken(loginToken_); + } + else { + final String credential = Credentials.basic(loginUid, loginPass, StandardCharsets.UTF_8); + if (loginOTP != 0) { + callVersion = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getGiteaVersionWithOTP(credential,loginOTP); + } + else { + callVersion = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getGiteaVersionWithBasic(credential); + } + } callVersion.enqueue(new Callback() { @@ -520,7 +537,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener if (response.isSuccessful()) { if (response.code() == 200) { - + tinyDb.putBoolean("loggedInMode", true); assert userDetails != null; tinyDb.putString(userDetails.getLogin() + "-token", loginToken_); diff --git a/app/src/main/java/org/mian/gitnex/activities/MainActivity.java b/app/src/main/java/org/mian/gitnex/activities/MainActivity.java index ff127df6..7f67e58e 100644 --- a/app/src/main/java/org/mian/gitnex/activities/MainActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/MainActivity.java @@ -404,10 +404,12 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig final TinyDB tinyDb = new TinyDB(getApplicationContext()); + final String token = "token " + tinyDb.getString(tinyDb.getString("loginUid") + "-token"); + Call callVersion = RetrofitClient .getInstance(instanceUrl, getApplicationContext()) .getApiInterface() - .getGiteaVersion(); + .getGiteaVersionWithToken(token); callVersion.enqueue(new Callback() { diff --git a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java index cceceb68..6aeceba0 100644 --- a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java +++ b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java @@ -52,7 +52,13 @@ import retrofit2.http.Query; public interface ApiInterface { @GET("version") // gitea version API - Call getGiteaVersion(); + Call getGiteaVersionWithBasic(@Header("Authorization") String authorization); + + @GET("version") // gitea version API + Call getGiteaVersionWithOTP(@Header("Authorization") String authorization, @Header("X-Gitea-OTP") int loginOTP); + + @GET("version") // gitea version API + Call getGiteaVersionWithToken(@Header("Authorization") String token); @GET("user") // username, full name, email Call getUserInfo(@Header("Authorization") String token);