add user info sanity check

pull/4/head
Felix Prahl-Kamps 2019-08-22 18:09:45 +02:00
parent 95594b9d11
commit 10a82580b3
8 changed files with 144 additions and 111 deletions

View File

@ -69,6 +69,7 @@ public class ExchangeActivity extends AppCompatActivity {
publicKeyQr = generatePublicKeyBitmap();
syncInformation = new SyncInformation();
syncInformation.setLocal(generateSyncExchangeInformation());
setSyncState(SyncState.KEY_EXCHANGE);
}
@ -119,9 +120,7 @@ public class ExchangeActivity extends AppCompatActivity {
}
private Bitmap generateLocalSyncInfoBitmap() {
ExchangeInformation exchangeInformation = generateSyncExchangeInformation();
syncInformation.setLocal(exchangeInformation);
return qrCodeGenerator.generateQrCode(diffieHellman.encrypt(new Gson().toJson(exchangeInformation)));
return qrCodeGenerator.generateQrCode(diffieHellman.encrypt(new Gson().toJson(syncInformation.getLocal())));
}
@ -274,8 +273,7 @@ public class ExchangeActivity extends AppCompatActivity {
break;
case DATA_EXCHANGE:
try {
ExchangeInformation remoteSyncInfo = new Gson().fromJson(diffieHellman.decrypt(qrData), ExchangeInformation.class);
syncInformation.setRemote(remoteSyncInfo);
syncInformation.setRemote(new Gson().fromJson(diffieHellman.decrypt(qrData), ExchangeInformation.class));
preferenceManager.addSyncInformation(syncInformation);
setSyncState(SyncState.DATA_EXCHANGE_DONE);
} catch (JsonSyntaxException e) {

View File

@ -3,7 +3,6 @@ package lu.circl.mispbump.activities;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -12,6 +11,7 @@ import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityOptionsCompat;
import androidx.core.util.Pair;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -21,9 +21,13 @@ import java.util.List;
import lu.circl.mispbump.R;
import lu.circl.mispbump.adapters.SyncInfoAdapter;
import lu.circl.mispbump.auxiliary.MispRestClient;
import lu.circl.mispbump.auxiliary.PreferenceManager;
import lu.circl.mispbump.interfaces.OnRecyclerItemClickListener;
import lu.circl.mispbump.models.SyncInformation;
import lu.circl.mispbump.models.restModels.Organisation;
import lu.circl.mispbump.models.restModels.Role;
import lu.circl.mispbump.models.restModels.User;
public class HomeActivity extends AppCompatActivity {
@ -43,6 +47,7 @@ public class HomeActivity extends AppCompatActivity {
initViews();
initRecyclerView();
checkRequiredInformationAvailable();
}
@Override
@ -100,11 +105,55 @@ public class HomeActivity extends AppCompatActivity {
} else {
emptyRecyclerView.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
syncInfoAdapter.setItems(syncInformationList);
for (SyncInformation si : syncInformationList) {
Log.d("DEBUG", si.toString());
}
// TODO Update from server if available
syncInfoAdapter.setItems(syncInformationList);
}
}
private void checkRequiredInformationAvailable() {
if (preferenceManager.getRoles() == null || preferenceManager.getUserInfo() == null || preferenceManager.getUserOrganisation() == null) {
Pair<String, String> credentials = preferenceManager.getUserCredentials();
MispRestClient client = MispRestClient.getInstance(credentials.first, credentials.second);
// get roles
client.getRoles(new MispRestClient.AllRolesCallback() {
@Override
public void success(Role[] roles) {
preferenceManager.setRoles(roles);
}
@Override
public void failure(String error) {
}
});
// get user and organisation
client.getMyUser(new MispRestClient.UserCallback() {
@Override
public void success(User user) {
preferenceManager.setMyUser(user);
client.getOrganisation(user.getOrg_id(), new MispRestClient.OrganisationCallback() {
@Override
public void success(Organisation organisation) {
preferenceManager.setMyOrganisation(organisation);
}
@Override
public void failure(String error) {
}
});
}
@Override
public void failure(String error) {
}
});
}
}

View File

@ -131,9 +131,9 @@ public class LoginActivity extends AppCompatActivity {
mispRestClient.getMyUser(new MispRestClient.UserCallback() {
@Override
public void success(final User user) {
preferenceManager.setUserInfo(user);
preferenceManager.setMyUser(user);
for (Role role : roles) {
if (role.getId().equals(user.getRole_id())) {
if (role.getId().equals(user.getRoleId())) {
if (!role.getPermAdmin()) {
progressBar.setVisibility(View.GONE);
Snackbar.make(constraintLayout, "No admin is associated with this authkey.", Snackbar.LENGTH_LONG).show();
@ -142,10 +142,10 @@ public class LoginActivity extends AppCompatActivity {
}
}
mispRestClient.getOrganisation(user.getRole_id(), new MispRestClient.OrganisationCallback() {
mispRestClient.getOrganisation(user.getRoleId(), new MispRestClient.OrganisationCallback() {
@Override
public void success(Organisation organisation) {
preferenceManager.setUserOrgInfo(organisation);
preferenceManager.setMyOrganisation(organisation);
preferenceManager.setUserCredentials(url, authkey);
progressBar.setVisibility(View.GONE);

View File

@ -44,6 +44,12 @@ public class ProfileActivity extends AppCompatActivity {
private FloatingActionButton fab;
private AnimatedVectorDrawable fabLoadingDrawable;
private View.OnClickListener onFabClicked = view -> {
fab.setImageDrawable(fabLoadingDrawable);
fabLoadingDrawable.start();
updateProfileInformation();
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -53,55 +59,12 @@ public class ProfileActivity extends AppCompatActivity {
Pair<String, String> credentials = preferenceManager.getUserCredentials();
mispRestClient = MispRestClient.getInstance(credentials.first, credentials.second);
init();
initToolbar();
initViews();
populateInformationViews();
}
private void init() {
rootLayout = findViewById(R.id.rootLayout);
ImageView headerBg = findViewById(R.id.headerBg);
headerBg.setImageDrawable(new TileDrawable(getRandomHeader(), Shader.TileMode.REPEAT));
// populate Toolbar (Actionbar)
Toolbar myToolbar = findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(true);
}
fab = findViewById(R.id.fab);
fab.setOnClickListener(onFabClicked());
fabLoadingDrawable = (AnimatedVectorDrawable) getDrawable(R.drawable.animated_sync);
}
private void populateInformationViews() {
Organisation organisation = preferenceManager.getUserOrganisation();
TextView name = findViewById(R.id.orgName);
name.setText(organisation.getName());
final MaterialPreferenceText uuid = findViewById(R.id.uuid);
uuid.setSubtitle(organisation.getUuid());
MaterialPreferenceText nationality = findViewById(R.id.nationality);
nationality.setSubtitle(organisation.getNationality());
MaterialPreferenceText sector = findViewById(R.id.sector);
if (organisation.getSector() == null) {
sector.setVisibility(View.GONE);
} else {
sector.setSubtitle(organisation.getSector());
}
MaterialPreferenceText description = findViewById(R.id.description);
description.setSubtitle(organisation.getDescription());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_profile, menu);
@ -118,20 +81,51 @@ public class ProfileActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
private View.OnClickListener onFabClicked() {
return v -> {
fab.setImageDrawable(fabLoadingDrawable);
fabLoadingDrawable.start();
updateProfile();
};
private void initToolbar() {
Toolbar myToolbar = findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(true);
}
}
private Drawable getRandomHeader() {
int[] ids = {R.drawable.ic_bank_note, R.drawable.ic_polka_dots, R.drawable.ic_wiggle, R.drawable.ic_circuit_board};
return getDrawable(ids[new Random().nextInt(ids.length)]);
private void initViews() {
rootLayout = findViewById(R.id.rootLayout);
ImageView headerBg = findViewById(R.id.headerBg);
headerBg.setImageDrawable(new TileDrawable(getRandomHeader(), Shader.TileMode.REPEAT));
fab = findViewById(R.id.fab);
fab.setOnClickListener(onFabClicked);
fabLoadingDrawable = (AnimatedVectorDrawable) getDrawable(R.drawable.animated_sync);
}
public void updateProfile() {
private void populateInformationViews() {
Organisation organisation = preferenceManager.getUserOrganisation();
TextView name = findViewById(R.id.orgName);
name.setText(organisation.getName());
final MaterialPreferenceText uuid = findViewById(R.id.uuid);
uuid.setSubtitle(organisation.getUuid());
MaterialPreferenceText nationality = findViewById(R.id.nationality);
nationality.setSubtitle(organisation.getNationality());
MaterialPreferenceText sector = findViewById(R.id.sector);
sector.setSubtitle(organisation.getSector());
MaterialPreferenceText description = findViewById(R.id.description);
description.setSubtitle(organisation.getDescription());
}
public void updateProfileInformation() {
mispRestClient.getRoles(new MispRestClient.AllRolesCallback() {
@Override
public void success(Role[] roles) {
@ -147,12 +141,12 @@ public class ProfileActivity extends AppCompatActivity {
mispRestClient.getMyUser(new MispRestClient.UserCallback() {
@Override
public void success(final User user) {
preferenceManager.setUserInfo(user);
mispRestClient.getOrganisation(user.getRole_id(), new MispRestClient.OrganisationCallback() {
preferenceManager.setMyUser(user);
mispRestClient.getOrganisation(user.getRoleId(), new MispRestClient.OrganisationCallback() {
@Override
public void success(Organisation organisation) {
fabLoadingDrawable.stop();
preferenceManager.setUserOrgInfo(organisation);
preferenceManager.setMyOrganisation(organisation);
Snackbar.make(rootLayout, "Successfully update profile", Snackbar.LENGTH_SHORT).show();
}
@ -190,4 +184,10 @@ public class ProfileActivity extends AppCompatActivity {
builder.create().show();
}
private Drawable getRandomHeader() {
int[] ids = {R.drawable.ic_bank_note, R.drawable.ic_polka_dots, R.drawable.ic_wiggle, R.drawable.ic_circuit_board};
return getDrawable(ids[new Random().nextInt(ids.length)]);
}
}

View File

@ -155,9 +155,9 @@ public class UploadActivity extends AppCompatActivity {
private User generateSyncUser(Organisation organisation) {
User syncUser = syncInformation.getRemote().getSyncUser();
syncUser.setOrg_id(organisation.getId());
syncUser.setRole_id(6);
syncUser.setTermsaccepted(true);
syncUser.setOrgId(organisation.getId());
syncUser.setRoleId(6);
syncUser.setTermsAccepted(true);
return syncUser;
}

View File

@ -5,15 +5,12 @@ import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import java.io.IOException;
import java.net.NoRouteToHostException;
import java.security.cert.CertificateException;
import java.util.List;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
@ -28,7 +25,6 @@ import lu.circl.mispbump.models.restModels.Role;
import lu.circl.mispbump.models.restModels.Server;
import lu.circl.mispbump.models.restModels.User;
import lu.circl.mispbump.models.restModels.Version;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
@ -95,12 +91,12 @@ public class MispRestClient {
new X509TrustManager() {
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}
@Override
@ -118,12 +114,7 @@ public class MispRestClient {
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
builder.hostnameVerifier((hostname, session) -> true);
}
if (logging) {
@ -132,16 +123,13 @@ public class MispRestClient {
builder.addInterceptor(interceptor);
}
// create authorization interceptor
builder.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request.Builder ongoing = chain.request().newBuilder();
ongoing.addHeader("Accept", "application/json");
ongoing.addHeader("Content-Type", "application/json");
ongoing.addHeader("Authorization", authkey);
return chain.proceed(ongoing.build());
}
// create interceptor
builder.addInterceptor(chain -> {
Request.Builder ongoing = chain.request().newBuilder();
ongoing.addHeader("Accept", "application/json");
ongoing.addHeader("Content-Type", "application/json");
ongoing.addHeader("Authorization", authkey);
return chain.proceed(ongoing.build());
});
return builder.build();
@ -185,7 +173,7 @@ public class MispRestClient {
Call<List<MispRole>> call = mispService.getRoles();
call.enqueue(new Callback<List<MispRole>>() {
@Override
public void onResponse(Call<List<MispRole>> call, Response<List<MispRole>> response) {
public void onResponse(@NonNull Call<List<MispRole>> call, @NonNull Response<List<MispRole>> response) {
if (!response.isSuccessful()) {
callback.failure(extractError(response));
@ -205,7 +193,7 @@ public class MispRestClient {
}
@Override
public void onFailure(Call<List<MispRole>> call, Throwable t) {
public void onFailure(@NonNull Call<List<MispRole>> call, @NonNull Throwable t) {
callback.failure(extractError(t));
}
});
@ -248,7 +236,6 @@ public class MispRestClient {
* @param userId user identifier
* @param callback {@link UserCallback} wrapper to return user directly
*/
public void getUser(int userId, final UserCallback callback) {
Call<MispUser> call = mispService.getUser(userId);
@ -611,7 +598,6 @@ public class MispRestClient {
}
// interfaces
public interface AvailableCallback {
void available();

View File

@ -83,10 +83,10 @@ public class PreferenceManager {
public Role[] getRoles() {
Type type = new TypeToken<Role[]>() {
}.getType();
String rolesString = preferences.getString(MISP_ROLES, "");
assert rolesString != null;
if (rolesString.isEmpty()) {
String rolesString = preferences.getString(MISP_ROLES, null);
if (rolesString == null) {
return null;
} else {
return new Gson().fromJson(rolesString, type);
@ -99,7 +99,7 @@ public class PreferenceManager {
*
* @param user {@link User}
*/
public void setUserInfo(User user) {
public void setMyUser(User user) {
try {
SharedPreferences.Editor editor = preferences.edit();
KeyStoreWrapper keyStoreWrapper = new KeyStoreWrapper(KeyStoreWrapper.USER_INFO_ALIAS);
@ -138,7 +138,7 @@ public class PreferenceManager {
*
* @param organisation Object representation of json organisation information
*/
public void setUserOrgInfo(Organisation organisation) {
public void setMyOrganisation(Organisation organisation) {
try {
String orgStr = new Gson().toJson(organisation);
KeyStoreWrapper keyStoreWrapper = new KeyStoreWrapper(KeyStoreWrapper.USER_ORGANISATION_INFO_ALIAS);

View File

@ -62,7 +62,7 @@ public class User {
return org_id;
}
public void setOrg_id(Integer org_id) {
public void setOrgId(Integer org_id) {
this.org_id = org_id;
}
@ -126,7 +126,7 @@ public class User {
return termsaccepted;
}
public void setTermsaccepted(Boolean termsaccepted) {
public void setTermsAccepted(Boolean termsaccepted) {
this.termsaccepted = termsaccepted;
}
@ -138,11 +138,11 @@ public class User {
this.newsread = newsread;
}
public Integer getRole_id() {
public Integer getRoleId() {
return role_id;
}
public void setRole_id(Integer role_id) {
public void setRoleId(Integer role_id) {
this.role_id = role_id;
}