Browse Source

moved common key names into constants.

pull/3/head
Herbert Poul 4 years ago
parent
commit
8a5784eb54
  1. 2
      lib/kdbx.dart
  2. 41
      lib/src/kdbx_entry.dart
  3. 3
      lib/src/kdbx_format.dart
  4. 2
      lib/src/utils/print_utils.dart
  5. 8
      test/kdbx4_test.dart
  6. 2
      test/kdbx_binaries_test.dart
  7. 10
      test/kdbx_test.dart
  8. 6
      test/merge/kdbx_merge_test.dart

2
lib/kdbx.dart

@ -9,7 +9,7 @@ export 'src/kdbx_binary.dart' show KdbxBinary;
export 'src/kdbx_consts.dart'; export 'src/kdbx_consts.dart';
export 'src/kdbx_custom_data.dart'; export 'src/kdbx_custom_data.dart';
export 'src/kdbx_dao.dart' show KdbxDao; export 'src/kdbx_dao.dart' show KdbxDao;
export 'src/kdbx_entry.dart' show KdbxEntry, KdbxKey; export 'src/kdbx_entry.dart' show KdbxEntry, KdbxKey, KdbxKeyCommon;
export 'src/kdbx_file.dart'; export 'src/kdbx_file.dart';
export 'src/kdbx_format.dart' export 'src/kdbx_format.dart'
show show

41
lib/src/kdbx_entry.dart

@ -18,9 +18,44 @@ import 'package:xml/xml.dart';
final _logger = Logger('kdbx.kdbx_entry'); final _logger = Logger('kdbx.kdbx_entry');
class KdbxKeyCommon {
static const KEY_TITLE = 'Title';
static const KEY_URL = 'URL';
static const KEY_USER_NAME = 'UserName';
static const KEY_PASSWORD = 'Password';
static const KEY_OTP = 'OTPAuth';
static const KdbxKey TITLE = KdbxKey._(KEY_TITLE, 'title');
static const KdbxKey URL = KdbxKey._(KEY_URL, 'url');
static const KdbxKey USER_NAME = KdbxKey._(KEY_USER_NAME, 'username');
static const KdbxKey PASSWORD = KdbxKey._(KEY_PASSWORD, 'password');
static const KdbxKey OTP = KdbxKey._(KEY_OTP, 'otpauth');
static const List<KdbxKey> all = [
TITLE,
URL,
USER_NAME,
PASSWORD,
OTP,
];
}
// this is called during initialization of [KdbxFormat] to make sure there are
// no typos in the constant declared above.
bool kdbxKeyCommonAssertConsistency() {
assert((() {
for (final key in KdbxKeyCommon.all) {
assert(key.key.toLowerCase() == key._canonicalKey);
}
return true;
})());
return true;
}
/// Represents a case insensitive (but case preserving) key. /// Represents a case insensitive (but case preserving) key.
class KdbxKey { class KdbxKey {
KdbxKey(this.key) : _canonicalKey = key.toLowerCase(); KdbxKey(this.key) : _canonicalKey = key.toLowerCase();
const KdbxKey._(this.key, this._canonicalKey);
final String key; final String key;
final String _canonicalKey; final String _canonicalKey;
@ -290,9 +325,9 @@ class KdbxEntry extends KdbxObject {
} }
String get label => String get label =>
_plainValue(KdbxKey('Title')) ?? _plainValue(KdbxKey('URL')); _plainValue(KdbxKeyCommon.TITLE) ?? _plainValue(KdbxKeyCommon.URL);
set label(String label) => setString(KdbxKey('Title'), PlainValue(label)); set label(String label) => setString(KdbxKeyCommon.TITLE, PlainValue(label));
/// Creates a new binary and adds it to this entry. /// Creates a new binary and adds it to this entry.
KdbxBinary createBinary({ KdbxBinary createBinary({
@ -386,7 +421,7 @@ class KdbxEntry extends KdbxObject {
mergeContext.markAsMerged(this); mergeContext.markAsMerged(this);
} }
String debugLabel() => label ?? _plainValue(KdbxKey('UserName')); String debugLabel() => label ?? _plainValue(KdbxKeyCommon.USER_NAME);
@override @override
String toString() { String toString() {

3
lib/src/kdbx_format.dart

@ -4,6 +4,7 @@ import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:archive/archive.dart'; import 'package:archive/archive.dart';
import 'package:kdbx/src/kdbx_entry.dart';
import 'package:supercharged_dart/supercharged_dart.dart'; import 'package:supercharged_dart/supercharged_dart.dart';
import 'package:argon2_ffi_base/argon2_ffi_base.dart'; import 'package:argon2_ffi_base/argon2_ffi_base.dart';
import 'package:convert/convert.dart' as convert; import 'package:convert/convert.dart' as convert;
@ -470,7 +471,7 @@ class _KeysV4 {
} }
class KdbxFormat { class KdbxFormat {
KdbxFormat([this.argon2]); KdbxFormat([this.argon2]) : assert(kdbxKeyCommonAssertConsistency());
final Argon2 argon2; final Argon2 argon2;
static bool dartWebWorkaround = false; static bool dartWebWorkaround = false;

2
lib/src/utils/print_utils.dart

@ -24,7 +24,7 @@ class KdbxPrintUtils {
forceDecrypt ? value?.getText() : value?.toString(); forceDecrypt ? value?.getText() : value?.toString();
for (final entry in group.entries) { for (final entry in group.entries) {
final value = entry.getString(KdbxKey('Password')); final value = entry.getString(KdbxKeyCommon.PASSWORD);
buf.writeln('$indent `- ${entry.debugLabel()}: ' buf.writeln('$indent `- ${entry.debugLabel()}: '
'${valueToSting(value)}'); '${valueToSting(value)}');
if (allFields) { if (allFields) {

8
test/kdbx4_test.dart

@ -24,7 +24,7 @@ void main() {
final file = await kdbxFormat.read( final file = await kdbxFormat.read(
data, Credentials(ProtectedValue.fromString('asdf'))); data, Credentials(ProtectedValue.fromString('asdf')));
final firstEntry = file.body.rootGroup.entries.first; final firstEntry = file.body.rootGroup.entries.first;
final pwd = firstEntry.getString(KdbxKey('Password')).getText(); final pwd = firstEntry.getString(KdbxKeyCommon.PASSWORD).getText();
expect(pwd, 'MyPassword'); expect(pwd, 'MyPassword');
}); });
test('Reading kdbx4_keeweb', () async { test('Reading kdbx4_keeweb', () async {
@ -32,7 +32,7 @@ void main() {
final file = await kdbxFormat.read( final file = await kdbxFormat.read(
data, Credentials(ProtectedValue.fromString('asdf'))); data, Credentials(ProtectedValue.fromString('asdf')));
final firstEntry = file.body.rootGroup.entries.first; final firstEntry = file.body.rootGroup.entries.first;
final pwd = firstEntry.getString(KdbxKey('Password')).getText(); final pwd = firstEntry.getString(KdbxKeyCommon.PASSWORD).getText();
expect(pwd, 'def'); expect(pwd, 'def');
}); });
test('Reading kdbx4_keeweb modification time', () async { test('Reading kdbx4_keeweb modification time', () async {
@ -135,7 +135,7 @@ KdbxEntry _createEntry(
KdbxFile file, KdbxGroup group, String username, String password) { KdbxFile file, KdbxGroup group, String username, String password) {
final entry = KdbxEntry.create(file, group); final entry = KdbxEntry.create(file, group);
group.addEntry(entry); group.addEntry(entry);
entry.setString(KdbxKey('UserName'), PlainValue(username)); entry.setString(KdbxKeyCommon.USER_NAME, PlainValue(username));
entry.setString(KdbxKey('Password'), ProtectedValue.fromString(password)); entry.setString(KdbxKeyCommon.PASSWORD, ProtectedValue.fromString(password));
return entry; return entry;
} }

2
test/kdbx_binaries_test.dart

@ -90,7 +90,7 @@ void main() {
await TestUtil.readKdbxFile('test/keepass2binaries.kdbx'); await TestUtil.readKdbxFile('test/keepass2binaries.kdbx');
final updateEntry = (KdbxFile file) { final updateEntry = (KdbxFile file) {
final entry = fileRead.body.rootGroup.entries.first; final entry = fileRead.body.rootGroup.entries.first;
entry.setString(KdbxKey('title'), PlainValue('example')); entry.setString(KdbxKeyCommon.TITLE, PlainValue('example'));
}; };
updateEntry(fileRead); updateEntry(fileRead);
final saved = await fileRead.save(); final saved = await fileRead.save();

10
test/kdbx_test.dart

@ -74,7 +74,7 @@ void main() {
final entry = KdbxEntry.create(kdbx, rootGroup); final entry = KdbxEntry.create(kdbx, rootGroup);
rootGroup.addEntry(entry); rootGroup.addEntry(entry);
entry.setString( entry.setString(
KdbxKey('Password'), ProtectedValue.fromString('LoremIpsum')); KdbxKeyCommon.PASSWORD, ProtectedValue.fromString('LoremIpsum'));
print(kdbx.body print(kdbx.body
.generateXml(FakeProtectedSaltGenerator()) .generateXml(FakeProtectedSaltGenerator())
.toXmlString(pretty: true)); .toXmlString(pretty: true));
@ -86,7 +86,7 @@ void main() {
final file = await TestUtil.readKdbxFile('test/keepass2test.kdbx'); final file = await TestUtil.readKdbxFile('test/keepass2test.kdbx');
final first = file.body.rootGroup.entries.first; final first = file.body.rootGroup.entries.first;
expect(file.header.version.major, 3); expect(file.header.version.major, 3);
expect(first.getString(KdbxKey('Title')).getText(), 'Sample Entry'); expect(first.getString(KdbxKeyCommon.TITLE).getText(), 'Sample Entry');
final modTime = first.times.lastModificationTime.get(); final modTime = first.times.lastModificationTime.get();
expect(modTime, DateTime.utc(2020, 5, 6, 7, 31, 48)); expect(modTime, DateTime.utc(2020, 5, 6, 7, 31, 48));
}); });
@ -96,7 +96,7 @@ void main() {
{ {
final first = file.body.rootGroup.entries.first; final first = file.body.rootGroup.entries.first;
expect(file.header.version.major, 3); expect(file.header.version.major, 3);
expect(first.getString(KdbxKey('Title')).getText(), 'Sample Entry'); expect(first.getString(KdbxKeyCommon.TITLE).getText(), 'Sample Entry');
first.times.lastModificationTime.set(newModDate); first.times.lastModificationTime.set(newModDate);
} }
final saved = await file.save(); final saved = await file.save();
@ -118,7 +118,7 @@ void main() {
final entry = KdbxEntry.create(kdbx, rootGroup); final entry = KdbxEntry.create(kdbx, rootGroup);
rootGroup.addEntry(entry); rootGroup.addEntry(entry);
entry.setString( entry.setString(
KdbxKey('Password'), ProtectedValue.fromString('LoremIpsum')); KdbxKeyCommon.PASSWORD, ProtectedValue.fromString('LoremIpsum'));
return kdbx.save(); return kdbx.save();
})(); })();
@ -127,7 +127,7 @@ void main() {
final kdbx = await kdbxForamt.read(saved, credentials); final kdbx = await kdbxForamt.read(saved, credentials);
expect( expect(
kdbx.body.rootGroup.entries.first kdbx.body.rootGroup.entries.first
.getString(KdbxKey('Password')) .getString(KdbxKeyCommon.PASSWORD)
.getText(), .getText(),
'LoremIpsum'); 'LoremIpsum');
File('test.kdbx').writeAsBytesSync(saved); File('test.kdbx').writeAsBytesSync(saved);

6
test/merge/kdbx_merge_test.dart

@ -46,7 +46,7 @@ void main() {
final fileMod = await TestUtil.saveAndRead(file); final fileMod = await TestUtil.saveAndRead(file);
fileMod.body.rootGroup.entries.first fileMod.body.rootGroup.entries.first
.setString(KdbxKey('UserName'), PlainValue('changed.')); .setString(KdbxKeyCommon.USER_NAME, PlainValue('changed.'));
_logger.info('mod date: ' + _logger.info('mod date: ' +
fileMod.body.rootGroup.entries.first.times.lastModificationTime fileMod.body.rootGroup.entries.first.times.lastModificationTime
.get() .get()
@ -103,7 +103,7 @@ KdbxEntry _createEntry(
KdbxFile file, KdbxGroup group, String username, String password) { KdbxFile file, KdbxGroup group, String username, String password) {
final entry = KdbxEntry.create(file, group); final entry = KdbxEntry.create(file, group);
group.addEntry(entry); group.addEntry(entry);
entry.setString(KdbxKey('UserName'), PlainValue(username)); entry.setString(KdbxKeyCommon.USER_NAME, PlainValue(username));
entry.setString(KdbxKey('Password'), ProtectedValue.fromString(password)); entry.setString(KdbxKeyCommon.PASSWORD, ProtectedValue.fromString(password));
return entry; return entry;
} }

Loading…
Cancel
Save