Skip to content

Commit

Permalink
refactor: new composition architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierChanth committed Nov 2, 2023
1 parent 951de34 commit 8f16edf
Show file tree
Hide file tree
Showing 49 changed files with 835 additions and 1,046 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@ import 'dart:async';
import 'dart:convert';

import 'package:dartssh2/dartssh2.dart';
import 'package:meta/meta.dart';
import 'package:noports_core/sshnp.dart';
import 'package:noports_core/utils.dart';
import 'package:path/path.dart' as path;

export 'ssh_key_utils/dart_ssh_key_util.dart';
export 'ssh_key_utils/local_ssh_key_util.dart';
export 'dart_ssh_key_util.dart';
export 'local_ssh_key_util.dart';

abstract interface class AtSshKeyUtil {
FutureOr<AtSshKeyPair> generateKeyPair({
required String identifier,
SupportedSshAlgorithm algorithm,
});

FutureOr<AtSshKeyPair> getKeyPair({
required String identifier,
});

FutureOr<dynamic> addKeyPair({
required AtSshKeyPair keyPair,
required String identifier,
});

FutureOr<dynamic> deleteKeyPair({
required String identifier,
});
}

class AtSshKeyPair {
@protected
final SSHKeyPair keyPair;
final String identifier;

Expand All @@ -34,20 +52,4 @@ class AtSshKeyPair {

String get privateKeyFileName => identifier;
String get publicKeyFileName => '$privateKeyFileName.pub';

// TODO consider adding this function
// void destroy() {
// throw UnimplementedError();
// }
}

abstract interface class AtSSHKeyUtil {
FutureOr<AtSshKeyPair> generateKeyPair({
required String identifier,
SupportedSSHAlgorithm algorithm,
});

FutureOr<AtSshKeyPair> getKeyPair({
required String identifier,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import 'package:cryptography/cryptography.dart';
import 'package:noports_core/utils.dart';
import 'package:openssh_ed25519/openssh_ed25519.dart';

class DartSSHKeyUtil implements AtSSHKeyUtil {
class DartSshKeyUtil implements AtSshKeyUtil {
static final Map<String, AtSshKeyPair> _keyPairCache = {};

@override
Future<AtSshKeyPair> generateKeyPair({
required String identifier,
SupportedSSHAlgorithm algorithm = DefaultArgs.sshAlgorithm,
SupportedSshAlgorithm algorithm = DefaultArgs.sshAlgorithm,
}) async {
AtSshKeyPair keyPair;
switch (algorithm) {
case SupportedSSHAlgorithm.rsa:
case SupportedSshAlgorithm.rsa:
keyPair = _generateRSAKeyPair(identifier);
case SupportedSSHAlgorithm.ed25519:
case SupportedSshAlgorithm.ed25519:
keyPair = await _generateEd25519KeyPair(identifier);
}
_keyPairCache[identifier] = keyPair;
Expand Down Expand Up @@ -46,4 +46,17 @@ class DartSSHKeyUtil implements AtSSHKeyUtil {
identifier: identifier,
);
}

@override
FutureOr addKeyPair({
required AtSshKeyPair keyPair,
required String identifier,
}) {
_keyPairCache[identifier] = keyPair;
}

@override
FutureOr deleteKeyPair({required String identifier}) {
_keyPairCache.remove(identifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import 'package:noports_core/utils.dart';
import 'package:path/path.dart' as path;
import 'package:posix/posix.dart' show chmod;

class LocalSshKeyUtil implements AtSSHKeyUtil {
class LocalSshKeyUtil implements AtSshKeyUtil {
static const _sshKeygenArgMap = {
SupportedSSHAlgorithm.rsa: ['-t', 'rsa', '-b', '4096'],
SupportedSSHAlgorithm.ed25519: ['-t', 'ed25519', '-a', '100'],
SupportedSshAlgorithm.rsa: ['-t', 'rsa', '-b', '4096'],
SupportedSshAlgorithm.ed25519: ['-t', 'ed25519', '-a', '100'],
};

static final Map<String, AtSshKeyPair> _keyPairCache = {};
Expand All @@ -27,13 +27,16 @@ class LocalSshKeyUtil implements AtSSHKeyUtil {

String get _defaultDirectory => sshnpHomeDirectory;

String get username => getUserName(throwIfNull: true)!;

List<File> _filesFromIdentifier({required String identifier}) {
return [
File(path.normalize(identifier)),
File(path.normalize('$identifier.pub')),
];
}

@override
Future<List<File>> addKeyPair({
required AtSshKeyPair keyPair,
required String identifier,
Expand Down Expand Up @@ -69,6 +72,7 @@ class LocalSshKeyUtil implements AtSSHKeyUtil {
return keyPair;
}

@override
Future<List<FileSystemEntity>> deleteKeyPair(
{required String identifier}) async {
var files = _filesFromIdentifier(identifier: identifier);
Expand All @@ -81,7 +85,7 @@ class LocalSshKeyUtil implements AtSSHKeyUtil {
@override
Future<AtSshKeyPair> generateKeyPair({
required String identifier,
SupportedSSHAlgorithm algorithm = DefaultArgs.sshAlgorithm,
SupportedSshAlgorithm algorithm = DefaultArgs.sshAlgorithm,
String? directory,
String? passphrase,
}) async {
Expand Down
8 changes: 4 additions & 4 deletions packages/noports_core/lib/src/common/default_args.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import 'package:noports_core/sshrv.dart';

class DefaultArgs {
static const String namespace = 'sshnp';
static const SupportedSSHAlgorithm sshAlgorithm =
SupportedSSHAlgorithm.ed25519;
static const SupportedSshAlgorithm sshAlgorithm =
SupportedSshAlgorithm.ed25519;
static const bool verbose = false;
static const SupportedSSHAlgorithm algorithm = SupportedSSHAlgorithm.ed25519;
static const SupportedSshAlgorithm algorithm = SupportedSshAlgorithm.ed25519;
static const String rootDomain = 'root.atsign.org';
static const SshrvGenerator sshrvGenerator = SSHRV.exec;
static const int localSshdPort = 22;
Expand All @@ -22,7 +22,7 @@ class DefaultArgs {
Platform.isLinux || Platform.isMacOS || Platform.isWindows;
}

class DefaultSSHNPArgs {
class DefaultSshnpArgs {
static const String device = 'default';
static const int port = 22;
static const int localPort = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:at_client/at_client.dart';
import 'package:at_utils/at_utils.dart';

abstract mixin class AtClientBindings {
mixin AtClientBindings {
AtClient get atClient;
AtSignLogger get logger;

Expand Down
8 changes: 4 additions & 4 deletions packages/noports_core/lib/src/common/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ enum SupportedSshClient {
String toString() => _cliArg;
}

enum SupportedSSHAlgorithm {
enum SupportedSshAlgorithm {
ed25519(cliArg: 'ssh-ed25519'),
rsa(cliArg: 'ssh-rsa');

final String _cliArg;
const SupportedSSHAlgorithm({required String cliArg}) : _cliArg = cliArg;
const SupportedSshAlgorithm({required String cliArg}) : _cliArg = cliArg;

factory SupportedSSHAlgorithm.fromString(String cliArg) {
return SupportedSSHAlgorithm.values.firstWhere(
factory SupportedSshAlgorithm.fromString(String cliArg) {
return SupportedSshAlgorithm.values.firstWhere(
(arg) => arg._cliArg == cliArg,
orElse: () => throw ArgumentError('Unsupported SSH algorithm: $cliArg'),
);
Expand Down

This file was deleted.

48 changes: 0 additions & 48 deletions packages/noports_core/lib/src/sshnp/brn/sshnp_ssh_key_handler.dart

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8f16edf

Please sign in to comment.