package com.sap.stepbystep;
import com.sap.cloud.mobile.foundation.authentication.BasicAuthCredentialStore;
import com.sap.cloud.mobile.foundation.securestore.SecureKeyValueStore;
public class BasicAuthPersistentStore implements BasicAuthCredentialStore {
SecureKeyValueStore mSecureStore;
final String KEY_PREFIX = "basic auth key prefix";
public BasicAuthPersistentStore() {}
public BasicAuthPersistentStore(SecureKeyValueStore secureKeyValueStore) {
mSecureStore = secureKeyValueStore;
}
@Override
public void storeCredential(String host, String realm, String[] credentials) {
if (mSecureStore != null) {
mSecureStore.put(key(host, realm), credentials);
}
}
@Override
public String[] getCredential(String host, String realm) {
if (mSecureStore != null) {
return mSecureStore.getSerializable(key(host, realm));
}
return null;
}
@Override
public void deleteCredential(String host, String realm) {
if (mSecureStore != null) {
mSecureStore.remove(key(host, realm));
}
}
@Override
public void deleteAllCredentials() {
if (mSecureStore != null) {
String[] keys = mSecureStore.keys();
for (String key:keys) {
if (key.startsWith(KEY_PREFIX)) {
mSecureStore.remove(key);
}
}
}
}
public void setStore(SecureKeyValueStore secureKeyValueStore) {
mSecureStore = secureKeyValueStore;
}
private String key(String host, String realm) {
return KEY_PREFIX + host + ":" + realm;
}
}
private BasicAuthPersistentStore bapStore;
SecureKeyValueStore myStore = new SecureKeyValueStore(this.getApplicationContext(), "mySecureStore");
try {
myStore.open(EncryptionUtil.getEncryptionKey("myAlias")); //For additional security, consider using a passcode screen.
bapStore = new BasicAuthPersistentStore(myStore);
}
catch (OpenFailureException e) {
e.printStackTrace();
}
catch (EncryptionError encryptionError) {
encryptionError.printStackTrace();
}//.authenticator(new BasicAuthDialogAuthenticator())
.authenticator(new BasicAuthDialogAuthenticator(bapStore))
<Button
android:id="@+id/b_unregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onUnregister"
android:enabled="false"
android:text="Unregister" />
private void logout() {
Log.d(myTag, "In logout");
Request request = new Request.Builder()
.post(RequestBody.create(null, ""))
.url(serviceURL + "/mobileservices/sessions/logout")
.build();
Callback updateUICallback = new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.d(myTag, "onFailure called during registration " + e.getLocalizedMessage());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (response.isSuccessful()) {
Log.d(myTag, "Successfully logged out");
toastAMessage("Successfully logged out");
enableButtonsOnRegister(false);
onRegister(null);
}
else {
Log.d(myTag, "Logout failed " + response.networkResponse());
toastAMessage("Logout failed " + response.networkResponse());
}
}
};
myOkHttpClient.newCall(request).enqueue(updateUICallback);
}
public void onUnregister(final View view) {
Log.d(myTag, "In onUnregister");
bapStore.deleteAllCredentials();
logout();
}
final Button unregisterButton = (Button) findViewById(R.id.b_unregister);unregisterButton.setEnabled(enable);
private long startTime;
startTime = System.currentTimeMillis();
long registerTime = System.currentTimeMillis() - startTime;
float registerTimeInSeconds = new Float(Math.round(registerTime/10)) / 100;
Log.d(myTag, "Registration finished " + registerTimeInSeconds + " seconds after the application started.");
long startOfOpenOfflineStore = System.currentTimeMillis();long openOfflineStoreTime = System.currentTimeMillis() - startOfOpenOfflineStore;
long appReadyTime = System.currentTimeMillis() - startTime;
float openOfflineStoreTimeInSeconds = new Float(Math.round(openOfflineStoreTime/10)) / 100;
float appReadyTimeInSeconds = new Float(Math.round(appReadyTime/10)) / 100;
Log.d(myTag, "Offline store opened in " + openOfflineStoreTimeInSeconds + " seconds.");
Log.d(myTag, "App is ready " + appReadyTimeInSeconds + " seconds after the application started.");

You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 52 | |
| 13 | |
| 11 | |
| 10 | |
| 10 | |
| 9 | |
| 9 | |
| 9 | |
| 9 | |
| 9 |