done with token logins, refactor some code

This commit is contained in:
M M Arif 2019-10-24 22:42:09 +05:00
parent 96ab992264
commit f9d51094e4
3 changed files with 254 additions and 115 deletions

View File

@ -27,6 +27,7 @@ import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.helpers.Toasty; import org.mian.gitnex.helpers.Toasty;
import org.mian.gitnex.helpers.VersionCheck; import org.mian.gitnex.helpers.VersionCheck;
import org.mian.gitnex.models.GiteaVersion; import org.mian.gitnex.models.GiteaVersion;
import org.mian.gitnex.models.UserInfo;
import org.mian.gitnex.models.UserTokens; import org.mian.gitnex.models.UserTokens;
import org.mian.gitnex.util.AppUtil; import org.mian.gitnex.util.AppUtil;
import org.mian.gitnex.util.TinyDB; import org.mian.gitnex.util.TinyDB;
@ -44,10 +45,11 @@ import retrofit2.Callback;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener { public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private Button login_button; private Button loginButton;
private EditText instance_url, login_uid, login_passwd, otpCode, loginTokenCode; private EditText instanceUrlET, loginUidET, loginPassword, otpCode, loginTokenCode;
private Spinner protocolSpinner; private Spinner protocolSpinner;
private TextView otpInfo; private TextView otpInfo;
private RadioGroup loginMethod;
final Context ctx = this; final Context ctx = this;
@Override @Override
@ -59,16 +61,16 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
TinyDB tinyDb = new TinyDB(getApplicationContext()); TinyDB tinyDb = new TinyDB(getApplicationContext());
boolean connToInternet = AppUtil.haveNetworkConnection(getApplicationContext()); boolean connToInternet = AppUtil.haveNetworkConnection(getApplicationContext());
login_button = findViewById(R.id.login_button); loginButton = findViewById(R.id.login_button);
instance_url = findViewById(R.id.instance_url); instanceUrlET = findViewById(R.id.instance_url);
login_uid = findViewById(R.id.login_uid); loginUidET = findViewById(R.id.login_uid);
login_passwd = findViewById(R.id.login_passwd); loginPassword = findViewById(R.id.login_passwd);
otpCode = findViewById(R.id.otpCode); otpCode = findViewById(R.id.otpCode);
otpInfo = findViewById(R.id.otpInfo); otpInfo = findViewById(R.id.otpInfo);
ImageView info_button = findViewById(R.id.info); ImageView info_button = findViewById(R.id.info);
final TextView viewTextAppVersion = findViewById(R.id.appVersion); final TextView viewTextAppVersion = findViewById(R.id.appVersion);
protocolSpinner = findViewById(R.id.httpsSpinner); protocolSpinner = findViewById(R.id.httpsSpinner);
RadioGroup loginMethod = findViewById(R.id.loginMethod); loginMethod = findViewById(R.id.loginMethod);
loginTokenCode = findViewById(R.id.loginTokenCode); loginTokenCode = findViewById(R.id.loginTokenCode);
viewTextAppVersion.setText(AppUtil.getAppVersion(getApplicationContext())); viewTextAppVersion.setText(AppUtil.getAppVersion(getApplicationContext()));
@ -108,14 +110,14 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
@Override @Override
public void onCheckedChanged(RadioGroup group, int checkedId) { public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.loginUsernamePassword){ if(checkedId == R.id.loginUsernamePassword){
login_uid.setVisibility(View.VISIBLE); loginUidET.setVisibility(View.VISIBLE);
login_passwd.setVisibility(View.VISIBLE); loginPassword.setVisibility(View.VISIBLE);
otpCode.setVisibility(View.VISIBLE); otpCode.setVisibility(View.VISIBLE);
otpInfo.setVisibility(View.VISIBLE); otpInfo.setVisibility(View.VISIBLE);
loginTokenCode.setVisibility(View.GONE); loginTokenCode.setVisibility(View.GONE);
} else { } else {
login_uid.setVisibility(View.GONE); loginUidET.setVisibility(View.GONE);
login_passwd.setVisibility(View.GONE); loginPassword.setVisibility(View.GONE);
otpCode.setVisibility(View.GONE); otpCode.setVisibility(View.GONE);
otpInfo.setVisibility(View.GONE); otpInfo.setVisibility(View.GONE);
loginTokenCode.setVisibility(View.VISIBLE); loginTokenCode.setVisibility(View.VISIBLE);
@ -125,10 +127,10 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
//login_button.setOnClickListener(this); //login_button.setOnClickListener(this);
if(!tinyDb.getString("instanceUrlRaw").isEmpty()) { if(!tinyDb.getString("instanceUrlRaw").isEmpty()) {
instance_url.setText(tinyDb.getString("instanceUrlRaw")); instanceUrlET.setText(tinyDb.getString("instanceUrlRaw"));
} }
if(!tinyDb.getString("loginUid").isEmpty()) { if(!tinyDb.getString("loginUid").isEmpty()) {
login_uid.setText(tinyDb.getString("loginUid")); loginUidET.setText(tinyDb.getString("loginUid"));
} }
if(tinyDb.getBoolean("loggedInMode")) { if(tinyDb.getBoolean("loggedInMode")) {
@ -138,20 +140,15 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
} }
login_button.setOnClickListener(loginListener); loginButton.setOnClickListener(loginListener);
} }
@Override @Override
public void onClick(View v) { public void onClick(View v) {
switch(v.getId()) { if (v.getId() == R.id.login_button) {
login();
case R.id.login_button:
login();
break;
default:
} }
} }
@ -160,7 +157,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
public void onClick(View v) { public void onClick(View v) {
disableProcessButton(); disableProcessButton();
login_button.setText(R.string.processingText); loginButton.setText(R.string.processingText);
login(); login();
} }
@ -188,113 +185,181 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
AppUtil appUtil = new AppUtil(); AppUtil appUtil = new AppUtil();
boolean connToInternet = AppUtil.haveNetworkConnection(LoginActivity.this); boolean connToInternet = AppUtil.haveNetworkConnection(LoginActivity.this);
String instanceUrl = instance_url.getText().toString().trim(); String instanceUrl = instanceUrlET.getText().toString().trim();
String loginUid = login_uid.getText().toString(); String loginUid = loginUidET.getText().toString();
String loginPass = login_passwd.getText().toString(); String loginPass = loginPassword.getText().toString();
String protocol = protocolSpinner.getSelectedItem().toString(); String protocol = protocolSpinner.getSelectedItem().toString();
String loginOTP_ = otpCode.getText().toString().trim(); String loginOTP_ = otpCode.getText().toString().trim();
int loginMethodType = loginMethod.getCheckedRadioButtonId();
String loginToken_ = loginTokenCode.getText().toString().trim();
if(instanceUrl.contains("@")) { if(loginMethodType == R.id.loginUsernamePassword) {
String[] urlForHttpAuth = instanceUrl.split("@"); if(instanceUrl.contains("@")) {
tinyDb.putString("basicAuthPassword", loginPass); String[] urlForHttpAuth = instanceUrl.split("@");
tinyDb.putBoolean("basicAuthFlag", true);
instanceUrl = urlForHttpAuth[1]; tinyDb.putString("basicAuthPassword", loginPass);
loginUid = urlForHttpAuth[0]; tinyDb.putBoolean("basicAuthFlag", true);
} instanceUrl = urlForHttpAuth[1];
loginUid = urlForHttpAuth[0];
String instanceHost;
if(AppUtil.httpCheck(instanceUrl)) {
URI uri = null;
try {
uri = new URI(instanceUrl);
} catch (URISyntaxException e) {
e.printStackTrace();
}
assert uri != null;
instanceHost = uri.getHost();
}
else {
instanceHost = instanceUrl;
}
String instanceUrlWithProtocol;
if(protocol.toLowerCase().equals("https")) {
instanceUrl = "https://" + instanceHost + "/api/v1/";
instanceUrlWithProtocol = "https://" + instanceHost;
}
else {
instanceUrl = "http://" + instanceHost + "/api/v1/";
instanceUrlWithProtocol = "https://" + instanceHost;
}
tinyDb.putString("instanceUrlRaw", instanceHost);
tinyDb.putString("loginUid", loginUid);
tinyDb.putString("instanceUrl", instanceUrl);
tinyDb.putString("instanceUrlWithProtocol", instanceUrlWithProtocol);
if(connToInternet) {
if(instance_url.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL));
enableProcessButton();
login_button.setText(R.string.btnLogin);
return;
}
if(loginUid.equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldUsername));
enableProcessButton();
login_button.setText(R.string.btnLogin);
return;
}
if(login_passwd.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldPassword));
enableProcessButton();
login_button.setText(R.string.btnLogin);
return;
} }
int loginOTP = 0; String instanceHost;
if(loginOTP_.length() == 6) { if(AppUtil.httpCheck(instanceUrl)) {
if(appUtil.checkIntegers(loginOTP_)) { URI uri = null;
try {
loginOTP = Integer.valueOf(loginOTP_); uri = new URI(instanceUrl);
} catch (URISyntaxException e) {
e.printStackTrace();
} }
else { assert uri != null;
instanceHost = uri.getHost();
Toasty.info(getApplicationContext(), getString(R.string.loginOTPTypeError)); }
else {
instanceHost = instanceUrl;
}
String instanceUrlWithProtocol;
if(protocol.toLowerCase().equals("https")) {
instanceUrl = "https://" + instanceHost + "/api/v1/";
instanceUrlWithProtocol = "https://" + instanceHost;
}
else {
instanceUrl = "http://" + instanceHost + "/api/v1/";
instanceUrlWithProtocol = "https://" + instanceHost;
}
tinyDb.putString("instanceUrlRaw", instanceHost);
tinyDb.putString("loginUid", loginUid);
tinyDb.putString("instanceUrl", instanceUrl);
tinyDb.putString("instanceUrlWithProtocol", instanceUrlWithProtocol);
if(connToInternet) {
if(instanceUrlET.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL));
enableProcessButton(); enableProcessButton();
login_button.setText(R.string.btnLogin); loginButton.setText(R.string.btnLogin);
return;
}
if(loginUid.equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldUsername));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
}
if(loginPassword.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldPassword));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return; return;
} }
} int loginOTP = 0;
if(loginOTP_.length() == 6) {
versionCheck(instanceUrl, loginUid, loginPass, loginOTP); if(appUtil.checkIntegers(loginOTP_)) {
loginOTP = Integer.valueOf(loginOTP_);
}
else {
Toasty.info(getApplicationContext(), getString(R.string.loginOTPTypeError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
}
}
versionCheck(instanceUrl, loginUid, loginPass, loginOTP, loginToken_, 1);
}
else {
Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection));
}
} }
else { else {
Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection)); String instanceHost;
if(AppUtil.httpCheck(instanceUrl)) {
URI uri = null;
try {
uri = new URI(instanceUrl);
} catch (URISyntaxException e) {
e.printStackTrace();
}
assert uri != null;
instanceHost = uri.getHost();
}
else {
instanceHost = instanceUrl;
}
String instanceUrlWithProtocol;
if(protocol.toLowerCase().equals("https")) {
instanceUrl = "https://" + instanceHost + "/api/v1/";
instanceUrlWithProtocol = "https://" + instanceHost;
}
else {
instanceUrl = "http://" + instanceHost + "/api/v1/";
instanceUrlWithProtocol = "https://" + instanceHost;
}
tinyDb.putString("instanceUrlRaw", instanceHost);
//tinyDb.putString("loginUid", loginUid);
tinyDb.putString("instanceUrl", instanceUrl);
tinyDb.putString("instanceUrlWithProtocol", instanceUrlWithProtocol);
if(connToInternet) {
if (instanceUrlET.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
}
if (loginToken_.equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.loginTokenError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
return;
}
versionCheck(instanceUrl, loginUid, loginPass, 123, loginToken_, 2);
}
else {
Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection));
}
} }
} }
private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP) { private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP, final String loginToken_, final int loginType) {
final TinyDB tinyDb = new TinyDB(getApplicationContext()); final TinyDB tinyDb = new TinyDB(getApplicationContext());
@ -322,7 +387,12 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
case SUPPORTED_LATEST: case SUPPORTED_LATEST:
case SUPPORTED_OLD: case SUPPORTED_OLD:
case DEVELOPMENT: case DEVELOPMENT:
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP); if(loginType == 1) {
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP);
}
else if(loginType == 2) { // token
letTheUserInViaToken(instanceUrl, loginToken_);
}
return; return;
case UNSUPPORTED_OLD: case UNSUPPORTED_OLD:
@ -345,7 +415,12 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP); if(loginType == 1) {
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP);
}
else if(loginType == 2) { // token
letTheUserInViaToken(instanceUrl, loginToken_);
}
} }
}); });
@ -375,6 +450,68 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
} }
private void letTheUserInViaToken(String instanceUrl, final String loginToken_) {
final TinyDB tinyDb = new TinyDB(getApplicationContext());
Call<UserInfo> call = RetrofitClient
.getInstance(instanceUrl)
.getApiInterface()
.getUserInfo("token " + loginToken_);
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(@NonNull Call<UserInfo> call, @NonNull retrofit2.Response<UserInfo> response) {
UserInfo userDetails = response.body();
if (response.isSuccessful()) {
if (response.code() == 200) {
tinyDb.remove("loginPass");
tinyDb.putBoolean("loggedInMode", true);
assert userDetails != null;
tinyDb.putString(userDetails.getLogin() + "-token", loginToken_);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
}
else if(response.code() == 401) {
String toastError = getResources().getString(R.string.unauthorizedApiError);
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
else {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
}
@Override
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
}
});
}
private void letTheUserIn(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP) { private void letTheUserIn(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP) {
final String credential = Credentials.basic(loginUid, loginPass); final String credential = Credentials.basic(loginUid, loginPass);
@ -467,10 +604,10 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
} }
else if(responseCreate.code() == 500) { else if(responseCreate.code() == 500) {
String toastError = getResources().getString(R.string.genericApiStatusError) + String.valueOf(responseCreate.code()); String toastError = getResources().getString(R.string.genericApiStatusError) + responseCreate.code();
Toasty.info(getApplicationContext(), toastError); Toasty.info(getApplicationContext(), toastError);
enableProcessButton(); enableProcessButton();
login_button.setText(R.string.btnLogin); loginButton.setText(R.string.btnLogin);
} }
@ -497,20 +634,20 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
} }
else if(response.code() == 500) { else if(response.code() == 500) {
String toastError = getResources().getString(R.string.genericApiStatusError) + String.valueOf(response.code()); String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError); Toasty.info(getApplicationContext(), toastError);
enableProcessButton(); enableProcessButton();
login_button.setText(R.string.btnLogin); loginButton.setText(R.string.btnLogin);
} }
else { else {
String toastError = getResources().getString(R.string.genericApiStatusError) + String.valueOf(response.code()); String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
//Log.i("error message else4", String.valueOf(response.code())); //Log.i("error message else4", String.valueOf(response.code()));
Toasty.info(getApplicationContext(), toastError); Toasty.info(getApplicationContext(), toastError);
enableProcessButton(); enableProcessButton();
login_button.setText(R.string.btnLogin); loginButton.setText(R.string.btnLogin);
} }
@ -521,7 +658,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
Log.e("onFailure-login", t.toString()); Log.e("onFailure-login", t.toString());
Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedJson)); Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedJson));
enableProcessButton(); enableProcessButton();
login_button.setText(R.string.btnLogin); loginButton.setText(R.string.btnLogin);
} }
}); });
@ -529,21 +666,21 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
private void disableProcessButton() { private void disableProcessButton() {
login_button.setEnabled(false); loginButton.setEnabled(false);
GradientDrawable shape = new GradientDrawable(); GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius( 8 ); shape.setCornerRadius( 8 );
shape.setColor(getResources().getColor(R.color.hintColor)); shape.setColor(getResources().getColor(R.color.hintColor));
login_button.setBackground(shape); loginButton.setBackground(shape);
} }
private void enableProcessButton() { private void enableProcessButton() {
login_button.setEnabled(true); loginButton.setEnabled(true);
GradientDrawable shape = new GradientDrawable(); GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius( 8 ); shape.setCornerRadius( 8 );
shape.setColor(getResources().getColor(R.color.btnBackground)); shape.setColor(getResources().getColor(R.color.btnBackground));
login_button.setBackground(shape); loginButton.setBackground(shape);
} }

View File

@ -29,7 +29,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/loginMethodText" android:text="@string/loginMethodText"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="14sp" android:textSize="12sp"
android:paddingStart="5dp" android:paddingStart="5dp"
android:paddingEnd="5dp" android:paddingEnd="5dp"
android:layout_marginBottom="10dp" android:layout_marginBottom="10dp"

View File

@ -531,5 +531,7 @@
<string name="loginViaPassword">Username / Password</string> <string name="loginViaPassword">Username / Password</string>
<string name="loginViaToken">Token</string> <string name="loginViaToken">Token</string>
<string name="loginMethodText">Choose your preferred login method to access your account. Token is more secure!</string> <string name="loginMethodText">Choose your preferred login method to access your account. Token is more secure!</string>
<string name="unauthorizedApiError">Instance has returned an error - Unauthorized. Check your credentials and try again</string>
<string name="loginTokenError">Please enter the correct token</string>
</resources> </resources>