2019-05-29 14:52:00 +02:00
|
|
|
package lu.circl.mispbump.activities;
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
import android.content.Intent;
|
2019-06-17 09:49:09 +02:00
|
|
|
import android.net.Uri;
|
2019-05-27 16:06:07 +02:00
|
|
|
import android.os.Bundle;
|
2019-06-17 09:49:09 +02:00
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
|
|
|
import com.google.android.material.textfield.TextInputLayout;
|
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
2019-05-29 14:52:00 +02:00
|
|
|
import lu.circl.mispbump.R;
|
|
|
|
import lu.circl.mispbump.auxiliary.DialogManager;
|
2019-05-27 16:06:07 +02:00
|
|
|
import lu.circl.mispbump.auxiliary.PreferenceManager;
|
2019-06-19 20:01:49 +02:00
|
|
|
import lu.circl.mispbump.auxiliary.MispRestClient;
|
|
|
|
import lu.circl.mispbump.models.restModels.Organisation;
|
|
|
|
import lu.circl.mispbump.models.restModels.User;
|
2019-05-27 16:06:07 +02:00
|
|
|
|
2019-06-04 20:48:24 +02:00
|
|
|
/**
|
2019-06-19 17:24:00 +02:00
|
|
|
* This activity is shown when the current device has no misp user and organisation associated with it.
|
|
|
|
* It takes care of downloading all information necessary for a sync with other misp instances.
|
2019-06-04 20:48:24 +02:00
|
|
|
*/
|
2019-05-27 16:06:07 +02:00
|
|
|
public class LoginActivity extends AppCompatActivity {
|
|
|
|
|
2019-06-04 20:48:24 +02:00
|
|
|
private PreferenceManager preferenceManager;
|
2019-07-05 03:45:04 +02:00
|
|
|
private MispRestClient mispRestClient;
|
|
|
|
|
2019-05-27 16:06:07 +02:00
|
|
|
private ConstraintLayout constraintLayout;
|
|
|
|
private TextInputLayout serverAutomationKey;
|
2019-06-04 20:48:24 +02:00
|
|
|
private TextInputLayout serverUrl;
|
2019-05-27 16:06:07 +02:00
|
|
|
private ProgressBar progressBar;
|
|
|
|
|
2019-06-04 20:48:24 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_login);
|
2019-07-05 03:45:04 +02:00
|
|
|
|
|
|
|
preferenceManager = PreferenceManager.getInstance(this);
|
|
|
|
mispRestClient = MispRestClient.getInstance(LoginActivity.this);
|
|
|
|
|
|
|
|
getWindow().setStatusBarColor(getColor(R.color.colorPrimary));
|
|
|
|
|
2019-06-17 09:49:09 +02:00
|
|
|
initializeViews();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
getMenuInflater().inflate(R.menu.menu_login, menu);
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-04 20:48:24 +02:00
|
|
|
|
2019-06-17 09:49:09 +02:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
if (item.getItemId() == R.id.menu_login_help) {
|
|
|
|
DialogManager.loginHelpDialog(LoginActivity.this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoke superclass to handle unrecognized item (eg. homeAsUp)
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:45:04 +02:00
|
|
|
|
2019-06-17 09:49:09 +02:00
|
|
|
private void initializeViews() {
|
2019-07-05 03:45:04 +02:00
|
|
|
constraintLayout = findViewById(R.id.rootLayout);
|
|
|
|
|
2019-06-04 20:48:24 +02:00
|
|
|
// populate Toolbar (Actionbar)
|
|
|
|
Toolbar myToolbar = findViewById(R.id.appbar);
|
|
|
|
setSupportActionBar(myToolbar);
|
|
|
|
ActionBar ab = getSupportActionBar();
|
|
|
|
if (ab != null) {
|
|
|
|
ab.setDisplayHomeAsUpEnabled(false);
|
2019-07-05 03:45:04 +02:00
|
|
|
ab.setDisplayShowTitleEnabled(false);
|
2019-06-04 20:48:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Button downloadInfoButton = findViewById(R.id.login_download_button);
|
|
|
|
downloadInfoButton.setOnClickListener(onClickDownload);
|
|
|
|
|
2019-06-19 17:24:00 +02:00
|
|
|
serverUrl = findViewById(R.id.login_server_url);
|
|
|
|
serverAutomationKey = findViewById(R.id.login_automation_key);
|
2019-06-17 09:49:09 +02:00
|
|
|
progressBar = findViewById(R.id.login_progressbar);
|
2019-06-04 20:48:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is called when the user clicks on the login button.
|
|
|
|
*/
|
2019-05-27 16:06:07 +02:00
|
|
|
private View.OnClickListener onClickDownload = new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
2019-07-05 03:45:04 +02:00
|
|
|
final String url = Objects.requireNonNull(serverUrl.getEditText()).getText().toString();
|
|
|
|
final String authkey = Objects.requireNonNull(serverAutomationKey.getEditText()).getText().toString();
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
boolean error = false;
|
|
|
|
|
|
|
|
serverUrl.setError(null);
|
|
|
|
serverAutomationKey.setError(null);
|
|
|
|
|
2019-05-29 14:52:00 +02:00
|
|
|
if (!isValidUrl(url)) {
|
2019-05-27 16:06:07 +02:00
|
|
|
error = true;
|
|
|
|
serverUrl.setError("Invalid Server URL");
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:52:00 +02:00
|
|
|
if (!isValidAutomationKey(authkey)) {
|
2019-05-27 16:06:07 +02:00
|
|
|
error = true;
|
|
|
|
serverAutomationKey.setError("Invalid automation key");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:45:04 +02:00
|
|
|
mispRestClient.initMispRestInterface(url);
|
2019-05-27 16:06:07 +02:00
|
|
|
|
|
|
|
// display progress bar
|
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
// get my user information and the organisation associated with my user
|
2019-06-17 09:49:09 +02:00
|
|
|
mispRestClient.isAvailable(new MispRestClient.AvailableCallback() {
|
2019-05-27 16:06:07 +02:00
|
|
|
@Override
|
2019-06-17 09:49:09 +02:00
|
|
|
public void available() {
|
|
|
|
mispRestClient.getMyUser(new MispRestClient.UserCallback() {
|
2019-05-27 16:06:07 +02:00
|
|
|
@Override
|
2019-06-17 09:49:09 +02:00
|
|
|
public void success(final User user) {
|
|
|
|
preferenceManager.setUserInfo(user);
|
|
|
|
mispRestClient.getOrganisation(user.org_id, new MispRestClient.OrganisationCallback() {
|
|
|
|
@Override
|
|
|
|
public void success(Organisation organisation) {
|
|
|
|
preferenceManager.setUserOrgInfo(organisation);
|
2019-07-05 03:45:04 +02:00
|
|
|
|
|
|
|
// save authkey
|
|
|
|
preferenceManager.setAutomationKey(authkey);
|
|
|
|
|
|
|
|
// save url
|
|
|
|
preferenceManager.setServerUrl(url);
|
|
|
|
|
2019-06-17 09:49:09 +02:00
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
Intent home = new Intent(getApplicationContext(), HomeActivity.class);
|
|
|
|
startActivity(home);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void failure(String error) {
|
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
Snackbar.make(constraintLayout, error, Snackbar.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
});
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void failure(String error) {
|
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
Snackbar.make(constraintLayout, error, Snackbar.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-06-17 09:49:09 +02:00
|
|
|
public void unavailable(String error) {
|
2019-05-27 16:06:07 +02:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2019-06-17 09:49:09 +02:00
|
|
|
Snackbar sb = Snackbar.make(constraintLayout, error, Snackbar.LENGTH_LONG);
|
|
|
|
sb.show();
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-04 20:48:24 +02:00
|
|
|
/**
|
2019-07-05 03:45:04 +02:00
|
|
|
* TODO: Check if url is valid.
|
2019-06-04 20:48:24 +02:00
|
|
|
*
|
|
|
|
* @param url url to check
|
|
|
|
* @return true or false
|
|
|
|
*/
|
2019-05-27 16:06:07 +02:00
|
|
|
private boolean isValidUrl(String url) {
|
2019-06-17 09:49:09 +02:00
|
|
|
Uri uri = Uri.parse(url);
|
|
|
|
|
|
|
|
if (uri == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri.getScheme() != null;
|
2019-05-27 16:06:07 +02:00
|
|
|
}
|
|
|
|
|
2019-06-04 20:48:24 +02:00
|
|
|
/**
|
2019-07-05 03:45:04 +02:00
|
|
|
* TODO: Check if automation key is valid.
|
2019-06-04 20:48:24 +02:00
|
|
|
*
|
|
|
|
* @param automationKey the key to check
|
|
|
|
* @return true or false
|
|
|
|
*/
|
2019-05-27 16:06:07 +02:00
|
|
|
private boolean isValidAutomationKey(String automationKey) {
|
|
|
|
return !TextUtils.isEmpty(automationKey);
|
|
|
|
}
|
|
|
|
}
|