use of es.wolfi.passman.API.Credential in project passman-android by nextcloud.
the class CredentialDeleteResponseHandler method onSuccess.
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
String result = new String(responseBody);
if (statusCode == 200) {
try {
JSONObject credentialObject = new JSONObject(result);
Vault v = (Vault) SingleTon.getTon().getExtra(SettingValues.ACTIVE_VAULT.toString());
if (credentialObject.has("credential_id") && credentialObject.getInt("vault_id") == v.vault_id) {
Credential currentCredential = Credential.fromJSON(credentialObject, v);
Toast.makeText(view.getContext(), R.string.successfully_deleted, Toast.LENGTH_LONG).show();
Objects.requireNonNull(passwordListActivity).deleteCredentialInCurrentLocalVaultList(currentCredential);
Objects.requireNonNull(passwordListActivity).showLockVaultButton();
int backStackCount = fragmentManager.getBackStackEntryCount();
int backStackId = 0;
if (backStackCount - 2 >= 0) {
backStackId = fragmentManager.getBackStackEntryAt(backStackCount - 2).getId();
}
alreadySaving.set(false);
progress.dismiss();
fragmentManager.popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
alreadySaving.set(false);
progress.dismiss();
Toast.makeText(view.getContext(), R.string.error_occurred, Toast.LENGTH_LONG).show();
}
use of es.wolfi.passman.API.Credential in project passman-android by nextcloud.
the class CredentialSaveResponseHandler method onSuccess.
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
String result = new String(responseBody);
if (statusCode == 200) {
try {
JSONObject credentialObject = new JSONObject(result);
Vault v = (Vault) SingleTon.getTon().getExtra(SettingValues.ACTIVE_VAULT.toString());
if (credentialObject.has("credential_id") && credentialObject.getInt("vault_id") == v.vault_id) {
Credential currentCredential = Credential.fromJSON(credentialObject, v);
Toast.makeText(view.getContext(), R.string.successfully_saved, Toast.LENGTH_LONG).show();
if (updateCredential) {
Objects.requireNonNull(passwordListActivity).editCredentialInCurrentLocalVaultList(currentCredential);
} else {
Objects.requireNonNull(passwordListActivity).addCredentialToCurrentLocalVaultList(currentCredential);
}
alreadySaving.set(false);
progress.dismiss();
fragmentManager.popBackStack();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
alreadySaving.set(false);
progress.dismiss();
Toast.makeText(view.getContext(), R.string.error_occurred, Toast.LENGTH_LONG).show();
}
use of es.wolfi.passman.API.Credential in project passman-android by nextcloud.
the class CredentialItemFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_credential_item_list, container, false);
// Set the adapter
View credentialView = view.findViewById(R.id.list);
if (credentialView instanceof RecyclerView) {
Context context = credentialView.getContext();
final RecyclerView recyclerView = (RecyclerView) credentialView;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
final Vault v = (Vault) SingleTon.getTon().getExtra(SettingValues.ACTIVE_VAULT.toString());
final EditText searchInput = (EditText) view.findViewById(R.id.search_input);
searchInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String searchText = searchInput.getText().toString().toLowerCase();
if (filterTask != null) {
filterTask.cancel(true);
}
filterTask = new FilterListAsyncTask(searchText, recyclerView, mListener);
ArrayList<Credential>[] input = new ArrayList[] { v.getCredentials() };
filterTask.execute((Object[]) input);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
recyclerView.setAdapter(new CredentialViewAdapter(v.getCredentials(), mListener));
}
return view;
}
use of es.wolfi.passman.API.Credential in project passman-android by nextcloud.
the class CredentialAutofillService method onFillRequest.
@Override
public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal, FillCallback callback) {
Log.d(TAG, "onFillRequest()");
List<FillContext> fillContexts = request.getFillContexts();
ArrayList<AssistStructure> structures = new ArrayList<>();
for (FillContext fc : fillContexts) {
structures.add(fc.getStructure());
}
final AssistStructure latestAssistStructure = fillContexts.get(fillContexts.size() - 1).getStructure();
// Find autofillable fields
AutofillFieldCollection fields = getAutofillableFields(latestAssistStructure, false);
final String packageName = getApplicationContext().getPackageName();
final String requesterPackageName = latestAssistStructure.getActivityComponent().getPackageName();
Log.d(TAG, "autofillable fields for: " + requesterPackageName + ": " + fields);
// Passman should not authenticate itself
if (fields.isEmpty() || requesterPackageName.startsWith("es.wolfi.app.passman")) {
Log.d(TAG, "No autofillable fields for: " + requesterPackageName);
callback.onSuccess(null);
return;
}
// Create the base response
FillResponse.Builder response = new FillResponse.Builder();
// Open Vault
SingleTon ton = SingleTon.getTon();
final Vault v = getAutofillVault(ton);
if (v == null) {
Toast.makeText(getApplicationContext(), getString(R.string.autofill_noactivevault), Toast.LENGTH_SHORT).show();
Log.d(TAG, getString(R.string.autofill_noactivevault));
callback.onSuccess(null);
return;
}
if (!v.is_unlocked()) {
Toast.makeText(getApplicationContext(), getString(R.string.autofill_vaultlocked), Toast.LENGTH_SHORT).show();
Log.d(TAG, getString(R.string.autofill_vaultlocked));
callback.onSuccess(null);
return;
}
// If we get here, we have an unlocked vault
Log.d(TAG, "Vault ready to go");
CredentialAutofillService.WebDomainResult domain = getLikelyDomain(structures);
// Grab Credentials from vault
ArrayList<Credential> allCred = v.getCredentials();
if (allCred.isEmpty()) {
Toast.makeText(getApplicationContext(), getString(R.string.autofill_vaultempty), Toast.LENGTH_SHORT).show();
Log.d(TAG, getString(R.string.autofill_vaultempty));
callback.onSuccess(null);
return;
}
// Find the credentials which match the requesting package name
List<Credential> matchingCredentials = findMatchingCredentials(allCred, requesterPackageName, domain);
Log.d(TAG, "Number of matching credentials for package: " + requesterPackageName + ":" + matchingCredentials.size());
/*
* TODO: validate package signature
* (Maybe store apk signature or signing cert thumbprint in custom field)
*/
Set<AutofillId> tempFields = new HashSet<>();
for (Credential thisCred : matchingCredentials) {
String credLabel = returnBestString(thisCred.getLabel(), thisCred.getUrl(), thisCred.getUsername());
Dataset.Builder dataset = new Dataset.Builder();
// simplify into a function
AutofillField bestUsername = fields.getRequiredId(View.AUTOFILL_HINT_USERNAME);
AutofillField bestEmail = fields.getRequiredId(View.AUTOFILL_HINT_EMAIL_ADDRESS);
AutofillField bestPassword = fields.getRequiredId(View.AUTOFILL_HINT_PASSWORD);
if (bestUsername != null) {
String value = returnBestString(thisCred.getUsername(), thisCred.getEmail(), thisCred.getLabel());
buildAndAddPresentation(dataset, packageName, bestUsername, value, credLabel);
tempFields.add(bestUsername.getAutofillid());
}
if (bestEmail != null) {
String value = returnBestString(thisCred.getEmail(), thisCred.getUsername(), thisCred.getLabel());
buildAndAddPresentation(dataset, packageName, bestEmail, value, credLabel);
tempFields.add(bestEmail.getAutofillid());
}
if (bestPassword != null) {
String value = thisCred.getPassword();
buildAndAddPresentation(dataset, packageName, bestPassword, value, "Password for: " + credLabel);
tempFields.add(bestPassword.getAutofillid());
}
if (bestUsername != null || bestEmail != null || bestPassword != null) {
response.addDataset(dataset.build());
}
}
if (tempFields.size() > 0) {
Log.d(TAG, "Requesting save info");
AutofillId[] requiredIds = new AutofillId[tempFields.size()];
tempFields.toArray(requiredIds);
response.setSaveInfo(new SaveInfo.Builder(SaveInfo.SAVE_DATA_TYPE_PASSWORD, requiredIds).setFlags(FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE).build());
Log.d(TAG, "Building and calling success");
callback.onSuccess(response.build());
return;
}
Log.d(TAG, "Failed to find anything to do, bailing");
callback.onSuccess(null);
}
use of es.wolfi.passman.API.Credential in project passman-android by nextcloud.
the class CredentialAutofillService method onSaveRequest.
@Override
public void onSaveRequest(SaveRequest request, SaveCallback callback) {
Log.d(TAG, "onSaveRequest()");
List<FillContext> fillContexts = request.getFillContexts();
final AssistStructure latestStructure = fillContexts.get(fillContexts.size() - 1).getStructure();
final String requesterPackageName = latestStructure.getActivityComponent().getPackageName();
String requesterDomainName = null;
String requesterApplicationLabel = null;
// Find autofillable fields
ArrayList<AssistStructure> structures = new ArrayList<>();
for (FillContext fc : request.getFillContexts()) {
structures.add(fc.getStructure());
}
CredentialAutofillService.WebDomainResult domain = getLikelyDomain(structures);
if (domain.firstDomain != null) {
requesterDomainName = domain.firstDomain;
} else {
requesterDomainName = "";
}
AutofillFieldCollection fields = getAutofillableFields(latestStructure, true);
// We don't have any fields to work with
if (fields.isEmpty()) {
Log.d(TAG, "No autofillable fields for: " + requesterPackageName);
callback.onSuccess();
return;
}
SingleTon ton = SingleTon.getTon();
final Vault v = getAutofillVault(ton);
if (v == null) {
Toast.makeText(getApplicationContext(), getString(R.string.autofill_noactivevault), Toast.LENGTH_SHORT).show();
Log.d(TAG, getString(R.string.autofill_noactivevault));
callback.onSuccess();
return;
}
if (!v.is_unlocked()) {
Toast.makeText(getApplicationContext(), getString(R.string.autofill_vaultlocked), Toast.LENGTH_SHORT).show();
Log.d(TAG, getString(R.string.autofill_vaultlocked));
callback.onSuccess();
return;
}
try {
ApplicationInfo requesterAppInfo = getPackageManager().getApplicationInfo(requesterPackageName, 0);
requesterApplicationLabel = getPackageManager().getApplicationLabel(requesterAppInfo).toString();
} catch (Exception ex) {
Log.d(TAG, "Couldn't read application label for: " + requesterPackageName);
}
if (TextUtils.isEmpty(requesterApplicationLabel)) {
requesterApplicationLabel = requesterPackageName;
}
Log.d(TAG, "onSaveRequest(): Application: " + requesterApplicationLabel);
if (!requesterDomainName.equals("")) {
String parsedDomain = getDomainName(requesterDomainName);
if (parsedDomain.equals("")) {
parsedDomain = requesterDomainName;
}
requesterApplicationLabel += " - " + parsedDomain;
}
AutofillField bestUsername = fields.getRequiredId(View.AUTOFILL_HINT_USERNAME);
AutofillField bestEmail = fields.getRequiredId(View.AUTOFILL_HINT_EMAIL_ADDRESS);
AutofillField bestPassword = fields.getRequiredId(View.AUTOFILL_HINT_PASSWORD);
String username = AutofillField.toStringValue(bestUsername);
String email = AutofillField.toStringValue(bestEmail);
String password = AutofillField.toStringValue(bestPassword);
if (email == null || email.equals("true") || email.equals("false")) {
email = "";
}
if (username == null || username.equals("true") || username.equals("false")) {
username = "";
}
String customFieldString = "";
try {
JSONArray customFields = new JSONArray();
JSONObject customField = new JSONObject();
customField.put("label", "androidCredPackageName");
customField.put("value", requesterPackageName);
customField.put("secret", false);
customField.put("field_type", "text");
customFields.put(customField);
customFieldString = customFields.toString();
} catch (JSONException e) {
Log.e(TAG, "onSaveRequest(), error creating customField");
}
Log.d(TAG, "onSaveRequest(), building Credential");
Credential newCred = new Credential();
newCred.setVault(v);
newCred.setDescription(getString(R.string.autofill_createdbyautofillservice));
newCred.setEmail(email);
newCred.setLabel(requesterApplicationLabel);
newCred.setCustomFields(customFieldString);
newCred.setUsername(username);
newCred.setPassword(password);
newCred.setFiles((new JSONArray()).toString());
newCred.setTags((new JSONArray()).toString());
newCred.setOtp((new JSONObject()).toString());
newCred.setUrl(requesterDomainName);
newCred.setCompromised(false);
Log.d(TAG, "onSaveRequest(), saving Credential");
final AsyncHttpResponseHandler responseHandler = new AutofillCredentialSaveResponseHandler(getAutofillVault(ton), getBaseContext(), getApplicationContext(), ton, TAG);
newCred.save(getApplicationContext(), responseHandler);
Log.d(TAG, "onSaveRequest() finished");
callback.onSuccess();
}
Aggregations