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.VersionCheck;
import org.mian.gitnex.models.GiteaVersion;
import org.mian.gitnex.models.UserInfo;
import org.mian.gitnex.models.UserTokens;
import org.mian.gitnex.util.AppUtil;
import org.mian.gitnex.util.TinyDB;
@ -44,10 +45,11 @@ import retrofit2.Callback;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private Button login_button;
private EditText instance_url, login_uid, login_passwd, otpCode, loginTokenCode;
private Button loginButton;
private EditText instanceUrlET, loginUidET, loginPassword, otpCode, loginTokenCode;
private Spinner protocolSpinner;
private TextView otpInfo;
private RadioGroup loginMethod;
final Context ctx = this;
@Override
@ -59,16 +61,16 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
TinyDB tinyDb = new TinyDB(getApplicationContext());
boolean connToInternet = AppUtil.haveNetworkConnection(getApplicationContext());
login_button = findViewById(R.id.login_button);
instance_url = findViewById(R.id.instance_url);
login_uid = findViewById(R.id.login_uid);
login_passwd = findViewById(R.id.login_passwd);
loginButton = findViewById(R.id.login_button);
instanceUrlET = findViewById(R.id.instance_url);
loginUidET = findViewById(R.id.login_uid);
loginPassword = findViewById(R.id.login_passwd);
otpCode = findViewById(R.id.otpCode);
otpInfo = findViewById(R.id.otpInfo);
ImageView info_button = findViewById(R.id.info);
final TextView viewTextAppVersion = findViewById(R.id.appVersion);
protocolSpinner = findViewById(R.id.httpsSpinner);
RadioGroup loginMethod = findViewById(R.id.loginMethod);
loginMethod = findViewById(R.id.loginMethod);
loginTokenCode = findViewById(R.id.loginTokenCode);
viewTextAppVersion.setText(AppUtil.getAppVersion(getApplicationContext()));
@ -108,14 +110,14 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.loginUsernamePassword){
login_uid.setVisibility(View.VISIBLE);
login_passwd.setVisibility(View.VISIBLE);
loginUidET.setVisibility(View.VISIBLE);
loginPassword.setVisibility(View.VISIBLE);
otpCode.setVisibility(View.VISIBLE);
otpInfo.setVisibility(View.VISIBLE);
loginTokenCode.setVisibility(View.GONE);
} else {
login_uid.setVisibility(View.GONE);
login_passwd.setVisibility(View.GONE);
loginUidET.setVisibility(View.GONE);
loginPassword.setVisibility(View.GONE);
otpCode.setVisibility(View.GONE);
otpInfo.setVisibility(View.GONE);
loginTokenCode.setVisibility(View.VISIBLE);
@ -125,10 +127,10 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
//login_button.setOnClickListener(this);
if(!tinyDb.getString("instanceUrlRaw").isEmpty()) {
instance_url.setText(tinyDb.getString("instanceUrlRaw"));
instanceUrlET.setText(tinyDb.getString("instanceUrlRaw"));
}
if(!tinyDb.getString("loginUid").isEmpty()) {
login_uid.setText(tinyDb.getString("loginUid"));
loginUidET.setText(tinyDb.getString("loginUid"));
}
if(tinyDb.getBoolean("loggedInMode")) {
@ -138,20 +140,15 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
}
login_button.setOnClickListener(loginListener);
loginButton.setOnClickListener(loginListener);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.login_button:
if (v.getId() == R.id.login_button) {
login();
break;
default:
}
}
@ -160,7 +157,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
public void onClick(View v) {
disableProcessButton();
login_button.setText(R.string.processingText);
loginButton.setText(R.string.processingText);
login();
}
@ -188,11 +185,15 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
AppUtil appUtil = new AppUtil();
boolean connToInternet = AppUtil.haveNetworkConnection(LoginActivity.this);
String instanceUrl = instance_url.getText().toString().trim();
String loginUid = login_uid.getText().toString();
String loginPass = login_passwd.getText().toString();
String instanceUrl = instanceUrlET.getText().toString().trim();
String loginUid = loginUidET.getText().toString();
String loginPass = loginPassword.getText().toString();
String protocol = protocolSpinner.getSelectedItem().toString();
String loginOTP_ = otpCode.getText().toString().trim();
int loginMethodType = loginMethod.getCheckedRadioButtonId();
String loginToken_ = loginTokenCode.getText().toString().trim();
if(loginMethodType == R.id.loginUsernamePassword) {
if(instanceUrl.contains("@")) {
@ -240,11 +241,11 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
if(connToInternet) {
if(instance_url.getText().toString().equals("")) {
if(instanceUrlET.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL));
enableProcessButton();
login_button.setText(R.string.btnLogin);
loginButton.setText(R.string.btnLogin);
return;
}
@ -252,15 +253,15 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldUsername));
enableProcessButton();
login_button.setText(R.string.btnLogin);
loginButton.setText(R.string.btnLogin);
return;
}
if(login_passwd.getText().toString().equals("")) {
if(loginPassword.getText().toString().equals("")) {
Toasty.info(getApplicationContext(), getString(R.string.emptyFieldPassword));
enableProcessButton();
login_button.setText(R.string.btnLogin);
loginButton.setText(R.string.btnLogin);
return;
}
@ -276,14 +277,14 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
Toasty.info(getApplicationContext(), getString(R.string.loginOTPTypeError));
enableProcessButton();
login_button.setText(R.string.btnLogin);
loginButton.setText(R.string.btnLogin);
return;
}
}
versionCheck(instanceUrl, loginUid, loginPass, loginOTP);
versionCheck(instanceUrl, loginUid, loginPass, loginOTP, loginToken_, 1);
}
else {
@ -293,8 +294,72 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
}
}
else {
private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP) {
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, final String loginToken_, final int loginType) {
final TinyDB tinyDb = new TinyDB(getApplicationContext());
@ -322,7 +387,12 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
case SUPPORTED_LATEST:
case SUPPORTED_OLD:
case DEVELOPMENT:
if(loginType == 1) {
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP);
}
else if(loginType == 2) { // token
letTheUserInViaToken(instanceUrl, loginToken_);
}
return;
case UNSUPPORTED_OLD:
@ -345,7 +415,12 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
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) {
final String credential = Credentials.basic(loginUid, loginPass);
@ -467,10 +604,10 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
}
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);
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) {
String toastError = getResources().getString(R.string.genericApiStatusError) + String.valueOf(response.code());
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
login_button.setText(R.string.btnLogin);
loginButton.setText(R.string.btnLogin);
}
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()));
Toasty.info(getApplicationContext(), toastError);
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());
Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedJson));
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() {
login_button.setEnabled(false);
loginButton.setEnabled(false);
GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius( 8 );
shape.setColor(getResources().getColor(R.color.hintColor));
login_button.setBackground(shape);
loginButton.setBackground(shape);
}
private void enableProcessButton() {
login_button.setEnabled(true);
loginButton.setEnabled(true);
GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius( 8 );
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:text="@string/loginMethodText"
android:textColor="@color/white"
android:textSize="14sp"
android:textSize="12sp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_marginBottom="10dp"

View File

@ -531,5 +531,7 @@
<string name="loginViaPassword">Username / Password</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="unauthorizedApiError">Instance has returned an error - Unauthorized. Check your credentials and try again</string>
<string name="loginTokenError">Please enter the correct token</string>
</resources>