Search in sources :

Example 1 with KeePassFile

use of de.slackspace.openkeepass.domain.KeePassFile in project TinyKeePass by sorz.

the class FetchDatabaseTask method doInBackground.

@Override
protected String doInBackground(Void... voids) {
    File tmpDbFile = new File(cacheDir, DB_FILENAME);
    File dbFile = new File(filesDir, DB_FILENAME);
    try {
        OutputStream output = new BufferedOutputStream(new FileOutputStream(tmpDbFile));
        InputStream input = getInputStream(uri);
        IOUtils.copy(input, output);
        input.close();
        output.close();
    } catch (InterruptedIOException e) {
        // task cancelled
        return null;
    } catch (IOException e) {
        Log.w(TAG, "fail to open database file.", e);
        return e.getClass().getSimpleName() + ": " + e.getLocalizedMessage();
    }
    KeePassFile db;
    try {
        db = KeePassDatabase.getInstance(tmpDbFile).openDatabase(masterPassword);
    } catch (KeePassDatabaseUnreadableException | UnsupportedOperationException e) {
        Log.w(TAG, "cannot open database.", e);
        return e.getLocalizedMessage();
    } catch (NullPointerException e) {
        // happen on try to open a KDBX 4 database with Argon2 (openkeepass 8.0)
        Log.e(TAG, "Underlying library throw null pointer exception", e);
        return "Database broken or not support";
    }
    Meta meta = db.getMeta();
    Log.d(TAG, "Database opened, name: " + meta.getDatabaseName());
    if (!tmpDbFile.renameTo(dbFile)) {
        try {
            InputStream input = new FileInputStream(tmpDbFile);
            OutputStream output = new FileOutputStream(dbFile);
            IOUtils.copy(input, output);
            if (!tmpDbFile.delete())
                Log.w(TAG, "fail to delete temp database on cache");
            input.close();
            output.close();
        } catch (IOException e) {
            Log.e(TAG, "cannot copy new database.", e);
            return "Fail to save database";
        }
    }
    Context context = this.context.get();
    if (context != null)
        KeePassStorage.set(context, db);
    return null;
}
Also used : Context(android.content.Context) InterruptedIOException(java.io.InterruptedIOException) Meta(de.slackspace.openkeepass.domain.Meta) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) KeePassDatabaseUnreadableException(de.slackspace.openkeepass.exception.KeePassDatabaseUnreadableException) KeePassFile(de.slackspace.openkeepass.domain.KeePassFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) KeePassFile(de.slackspace.openkeepass.domain.KeePassFile)

Example 2 with KeePassFile

use of de.slackspace.openkeepass.domain.KeePassFile in project TinyKeePass by sorz.

the class AuthActivity method onDatabaseOpened.

@Override
protected void onDatabaseOpened() {
    StructureParser.Result result = parseStructure();
    KeePassFile keePass = KeePassStorage.get(this);
    SearchIndex index = new SearchIndex(keePass);
    StringBuilder queryBuilder = new StringBuilder();
    result.title.forEach(title -> queryBuilder.append(title).append(' '));
    Stream<Entry> entryStream = index.search(queryBuilder.toString()).map(keePass::getEntryByUUID);
    FillResponse.Builder responseBuilder = new FillResponse.Builder();
    // add matched entities
    entryStream.map(entry -> AutofillUtils.buildDataset(this, entry, result)).filter(Objects::nonNull).limit(MAX_NUM_CANDIDATE_ENTRIES).forEach(responseBuilder::addDataset);
    // add "show all" item
    RemoteViews presentation = AutofillUtils.getRemoteViews(this, getString(R.string.autofill_item_show_all), R.drawable.ic_more_horiz_gray_24dp);
    presentation.setTextColor(R.id.textView, getColor(R.color.hint));
    Dataset.Builder datasetBuilder = new Dataset.Builder(presentation).setAuthentication(EntrySelectActivity.getAuthIntentSenderForResponse(this));
    result.allAutofillIds().forEach(id -> datasetBuilder.setValue(id, null));
    responseBuilder.addDataset(datasetBuilder.build());
    setFillResponse(responseBuilder.build());
    finish();
}
Also used : RemoteViews(android.widget.RemoteViews) Context(android.content.Context) Dataset(android.service.autofill.Dataset) KeyException(java.security.KeyException) KeePassFile(de.slackspace.openkeepass.domain.KeePassFile) R(org.sorz.lab.tinykeepass.R) SearchIndex(org.sorz.lab.tinykeepass.search.SearchIndex) Intent(android.content.Intent) RequiresApi(android.support.annotation.RequiresApi) PendingIntent(android.app.PendingIntent) Objects(java.util.Objects) IntentSender(android.content.IntentSender) Stream(java.util.stream.Stream) FillResponse(android.service.autofill.FillResponse) KeePassStorage(org.sorz.lab.tinykeepass.keepass.KeePassStorage) Build(android.os.Build) Entry(de.slackspace.openkeepass.domain.Entry) SearchIndex(org.sorz.lab.tinykeepass.search.SearchIndex) Dataset(android.service.autofill.Dataset) FillResponse(android.service.autofill.FillResponse) Entry(de.slackspace.openkeepass.domain.Entry) RemoteViews(android.widget.RemoteViews) Objects(java.util.Objects) KeePassFile(de.slackspace.openkeepass.domain.KeePassFile)

Example 3 with KeePassFile

use of de.slackspace.openkeepass.domain.KeePassFile in project TinyKeePass by sorz.

the class OpenKeePassTask method doInBackground.

@Override
protected KeePassFile doInBackground(Void... voids) {
    try {
        long t = System.currentTimeMillis();
        KeePassDatabase instance = KeePassDatabase.getInstance(path);
        Log.d(TAG, "get instance in " + (System.currentTimeMillis() - t) + "ms");
        t = System.currentTimeMillis();
        KeePassFile keePassFile = instance.openDatabase(key);
        Log.d(TAG, "open db in " + (System.currentTimeMillis() - t) + "ms");
        return keePassFile;
    } catch (KeePassDatabaseUnreadableException | UnsupportedOperationException e) {
        Log.w(TAG, "cannot open database.", e);
        errorMessage = e.getLocalizedMessage();
    }
    return null;
}
Also used : KeePassDatabase(de.slackspace.openkeepass.KeePassDatabase) KeePassDatabaseUnreadableException(de.slackspace.openkeepass.exception.KeePassDatabaseUnreadableException) KeePassFile(de.slackspace.openkeepass.domain.KeePassFile)

Example 4 with KeePassFile

use of de.slackspace.openkeepass.domain.KeePassFile in project TinyKeePass by sorz.

the class EntryRecyclerViewAdapter method reloadEntries.

public void reloadEntries(Context context) {
    KeePassFile db = KeePassStorage.get(context);
    if (db != null) {
        allEntries = KeePassHelper.allEntriesNotInRecycleBin(db).collect(Collectors.toList());
        allEntries.sort((a, b) -> {
            if (a.getTitle() != null && b.getTitle() != null)
                return a.getTitle().compareTo(b.getTitle());
            else if (a.getUsername() != null && b.getUsername() != null)
                return a.getUsername().compareTo(b.getUsername());
            else if (a.getUrl() != null && b.getUrl() != null)
                return a.getUrl().compareTo(b.getUrl());
            else
                return a.getTimes().getCreationTime().compareTo(b.getTimes().getCreationTime());
        });
        Log.d(TAG, allEntries.size() + " entries loaded");
    } else {
        Log.w(TAG, "database is locked");
        allEntries = new ArrayList<>();
    }
    setFilter(filter);
}
Also used : KeePassFile(de.slackspace.openkeepass.domain.KeePassFile)

Aggregations

KeePassFile (de.slackspace.openkeepass.domain.KeePassFile)4 Context (android.content.Context)2 KeePassDatabaseUnreadableException (de.slackspace.openkeepass.exception.KeePassDatabaseUnreadableException)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 IntentSender (android.content.IntentSender)1 Build (android.os.Build)1 Dataset (android.service.autofill.Dataset)1 FillResponse (android.service.autofill.FillResponse)1 RequiresApi (android.support.annotation.RequiresApi)1 RemoteViews (android.widget.RemoteViews)1 KeePassDatabase (de.slackspace.openkeepass.KeePassDatabase)1 Entry (de.slackspace.openkeepass.domain.Entry)1 Meta (de.slackspace.openkeepass.domain.Meta)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1