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

View File

@ -3,7 +3,6 @@ package lu.circl.mispbump.activities;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -12,6 +11,7 @@ import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityOptionsCompat; import androidx.core.app.ActivityOptionsCompat;
import androidx.core.util.Pair;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -21,9 +21,13 @@ import java.util.List;
import lu.circl.mispbump.R; import lu.circl.mispbump.R;
import lu.circl.mispbump.adapters.SyncInfoAdapter; import lu.circl.mispbump.adapters.SyncInfoAdapter;
import lu.circl.mispbump.auxiliary.MispRestClient;
import lu.circl.mispbump.auxiliary.PreferenceManager; import lu.circl.mispbump.auxiliary.PreferenceManager;
import lu.circl.mispbump.interfaces.OnRecyclerItemClickListener; import lu.circl.mispbump.interfaces.OnRecyclerItemClickListener;
import lu.circl.mispbump.models.SyncInformation; 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 { public class HomeActivity extends AppCompatActivity {
@ -43,6 +47,7 @@ public class HomeActivity extends AppCompatActivity {
initViews(); initViews();
initRecyclerView(); initRecyclerView();
checkRequiredInformationAvailable();
} }
@Override @Override
@ -100,11 +105,55 @@ public class HomeActivity extends AppCompatActivity {
} else { } else {
emptyRecyclerView.setVisibility(View.GONE); emptyRecyclerView.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE); recyclerView.setVisibility(View.VISIBLE);
syncInfoAdapter.setItems(syncInformationList);
for (SyncInformation si : syncInformationList) { // TODO Update from server if available
Log.d("DEBUG", si.toString());
} 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() { mispRestClient.getMyUser(new MispRestClient.UserCallback() {
@Override @Override
public void success(final User user) { public void success(final User user) {
preferenceManager.setUserInfo(user); preferenceManager.setMyUser(user);
for (Role role : roles) { for (Role role : roles) {
if (role.getId().equals(user.getRole_id())) { if (role.getId().equals(user.getRoleId())) {
if (!role.getPermAdmin()) { if (!role.getPermAdmin()) {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
Snackbar.make(constraintLayout, "No admin is associated with this authkey.", Snackbar.LENGTH_LONG).show(); 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 @Override
public void success(Organisation organisation) { public void success(Organisation organisation) {
preferenceManager.setUserOrgInfo(organisation); preferenceManager.setMyOrganisation(organisation);
preferenceManager.setUserCredentials(url, authkey); preferenceManager.setUserCredentials(url, authkey);
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);

View File

@ -44,6 +44,12 @@ public class ProfileActivity extends AppCompatActivity {
private FloatingActionButton fab; private FloatingActionButton fab;
private AnimatedVectorDrawable fabLoadingDrawable; private AnimatedVectorDrawable fabLoadingDrawable;
private View.OnClickListener onFabClicked = view -> {
fab.setImageDrawable(fabLoadingDrawable);
fabLoadingDrawable.start();
updateProfileInformation();
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -53,55 +59,12 @@ public class ProfileActivity extends AppCompatActivity {
Pair<String, String> credentials = preferenceManager.getUserCredentials(); Pair<String, String> credentials = preferenceManager.getUserCredentials();
mispRestClient = MispRestClient.getInstance(credentials.first, credentials.second); mispRestClient = MispRestClient.getInstance(credentials.first, credentials.second);
init(); initToolbar();
initViews();
populateInformationViews(); 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 @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_profile, menu); getMenuInflater().inflate(R.menu.menu_profile, menu);
@ -118,20 +81,51 @@ public class ProfileActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
private View.OnClickListener onFabClicked() {
return v -> { private void initToolbar() {
fab.setImageDrawable(fabLoadingDrawable); Toolbar myToolbar = findViewById(R.id.toolbar);
fabLoadingDrawable.start(); setSupportActionBar(myToolbar);
updateProfile();
}; ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(true);
}
} }
private Drawable getRandomHeader() { private void initViews() {
int[] ids = {R.drawable.ic_bank_note, R.drawable.ic_polka_dots, R.drawable.ic_wiggle, R.drawable.ic_circuit_board}; rootLayout = findViewById(R.id.rootLayout);
return getDrawable(ids[new Random().nextInt(ids.length)]);
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() { mispRestClient.getRoles(new MispRestClient.AllRolesCallback() {
@Override @Override
public void success(Role[] roles) { public void success(Role[] roles) {
@ -147,12 +141,12 @@ public class ProfileActivity extends AppCompatActivity {
mispRestClient.getMyUser(new MispRestClient.UserCallback() { mispRestClient.getMyUser(new MispRestClient.UserCallback() {
@Override @Override
public void success(final User user) { public void success(final User user) {
preferenceManager.setUserInfo(user); preferenceManager.setMyUser(user);
mispRestClient.getOrganisation(user.getRole_id(), new MispRestClient.OrganisationCallback() { mispRestClient.getOrganisation(user.getRoleId(), new MispRestClient.OrganisationCallback() {
@Override @Override
public void success(Organisation organisation) { public void success(Organisation organisation) {
fabLoadingDrawable.stop(); fabLoadingDrawable.stop();
preferenceManager.setUserOrgInfo(organisation); preferenceManager.setMyOrganisation(organisation);
Snackbar.make(rootLayout, "Successfully update profile", Snackbar.LENGTH_SHORT).show(); Snackbar.make(rootLayout, "Successfully update profile", Snackbar.LENGTH_SHORT).show();
} }
@ -190,4 +184,10 @@ public class ProfileActivity extends AppCompatActivity {
builder.create().show(); 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) { private User generateSyncUser(Organisation organisation) {
User syncUser = syncInformation.getRemote().getSyncUser(); User syncUser = syncInformation.getRemote().getSyncUser();
syncUser.setOrg_id(organisation.getId()); syncUser.setOrgId(organisation.getId());
syncUser.setRole_id(6); syncUser.setRoleId(6);
syncUser.setTermsaccepted(true); syncUser.setTermsAccepted(true);
return syncUser; return syncUser;
} }

View File

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

View File

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

View File

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