Search in sources :

Example 1 with Credential

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();
}
Also used : Credential(es.wolfi.passman.API.Credential) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Vault(es.wolfi.passman.API.Vault)

Example 2 with Credential

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();
}
Also used : Credential(es.wolfi.passman.API.Credential) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Vault(es.wolfi.passman.API.Vault)

Example 3 with Credential

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;
}
Also used : Context(android.content.Context) EditText(android.widget.EditText) Credential(es.wolfi.passman.API.Credential) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) GridLayoutManager(android.support.v7.widget.GridLayoutManager) TextWatcher(android.text.TextWatcher) FilterListAsyncTask(es.wolfi.utils.FilterListAsyncTask) Editable(android.text.Editable) RecyclerView(android.support.v7.widget.RecyclerView) Vault(es.wolfi.passman.API.Vault)

Example 4 with Credential

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);
}
Also used : ArrayList(java.util.ArrayList) FillResponse(android.service.autofill.FillResponse) AssistStructure(android.app.assist.AssistStructure) SingleTon(es.wolfi.app.passman.SingleTon) AutofillId(android.view.autofill.AutofillId) HashSet(java.util.HashSet) SaveInfo(android.service.autofill.SaveInfo) Credential(es.wolfi.passman.API.Credential) Dataset(android.service.autofill.Dataset) FillContext(android.service.autofill.FillContext) Vault(es.wolfi.passman.API.Vault)

Example 5 with Credential

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();
}
Also used : Credential(es.wolfi.passman.API.Credential) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) JSONArray(org.json.JSONArray) AssistStructure(android.app.assist.AssistStructure) JSONException(org.json.JSONException) AsyncHttpResponseHandler(com.loopj.android.http.AsyncHttpResponseHandler) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) SingleTon(es.wolfi.app.passman.SingleTon) AutofillCredentialSaveResponseHandler(es.wolfi.app.ResponseHandlers.AutofillCredentialSaveResponseHandler) FillContext(android.service.autofill.FillContext) Vault(es.wolfi.passman.API.Vault)

Aggregations

Credential (es.wolfi.passman.API.Credential)9 Vault (es.wolfi.passman.API.Vault)8 JSONException (org.json.JSONException)5 JSONObject (org.json.JSONObject)5 ArrayList (java.util.ArrayList)3 AssistStructure (android.app.assist.AssistStructure)2 FillContext (android.service.autofill.FillContext)2 SingleTon (es.wolfi.app.passman.SingleTon)2 URISyntaxException (java.net.URISyntaxException)2 JSONArray (org.json.JSONArray)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 Dataset (android.service.autofill.Dataset)1 FillResponse (android.service.autofill.FillResponse)1 SaveInfo (android.service.autofill.SaveInfo)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 Editable (android.text.Editable)1