Skip to content

Commit

Permalink
Create Auth protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Mar 3, 2024
1 parent ee9481a commit 5d400a4
Show file tree
Hide file tree
Showing 19 changed files with 418 additions and 178 deletions.
5 changes: 5 additions & 0 deletions packages/celest_auth/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
override_platforms:
firefox:
settings:
# Workaround for: https://github.com/dart-lang/test/issues/2194
executable: /Applications/Firefox.app/Contents/MacOS/firefox
4 changes: 3 additions & 1 deletion packages/celest_auth/lib/src/client/auth_client.android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import 'package:jni/jni.dart';
import 'package:logging/logging.dart';

final class AuthClientAndroid extends AuthClientPlatform {
AuthClientAndroid() : super.base() {
AuthClientAndroid({
required super.protocol,
}) : super.base() {
Jni.initDLApi();
_celestAuth.init(_applicationContext);
}
Expand Down
12 changes: 10 additions & 2 deletions packages/celest_auth/lib/src/client/auth_client.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import 'package:celest_auth/src/client/auth_client_platform.vm.dart'
if (dart.library.js_interop) 'package:celest_auth/src/client/auth_client_platform.web.dart';
import 'package:celest_core/celest_core.dart';
import 'package:meta/meta.dart';

abstract base class AuthClient {
factory AuthClient() = AuthClientPlatform;
factory AuthClient({
required AuthProtocol protocol,
}) = AuthClientPlatform;

@protected
AuthClient.base();
AuthClient.base({
required this.protocol,
});

@protected
final AuthProtocol protocol;
}
4 changes: 3 additions & 1 deletion packages/celest_auth/lib/src/client/auth_client.darwin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import 'package:celest_auth/src/client/auth_client_platform.vm.dart';
import 'package:celest_auth/src/platform/darwin/authentication_services.ffi.dart';

final class AuthClientDarwin extends AuthClientPlatform {
AuthClientDarwin() : super.base();
AuthClientDarwin({
required super.protocol,
}) : super.base();

final _authenticationServices =
AuthenticationServices(DynamicLibrary.process());
Expand Down
4 changes: 3 additions & 1 deletion packages/celest_auth/lib/src/client/auth_client.native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'dart:io';
import 'package:celest_auth/src/client/auth_client_platform.vm.dart';

final class AuthClientNative extends AuthClientPlatform {
AuthClientNative() : super.base();
AuthClientNative({
required super.protocol,
}) : super.base();

// /// Launches the given URL.
// Future<void> _launchUrl(String url) async {
Expand Down
4 changes: 3 additions & 1 deletion packages/celest_auth/lib/src/client/auth_client.web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:path/path.dart';
import 'package:web/web.dart';

final class AuthClientWeb extends AuthClientPlatform {
AuthClientWeb() : super.base();
AuthClientWeb({
required super.protocol,
}) : super.base();

String get _baseUrl {
final baseElement = document.querySelector('base') as HTMLBaseElement?;
Expand Down
15 changes: 10 additions & 5 deletions packages/celest_auth/lib/src/client/auth_client_platform.vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@ import 'package:celest_auth/src/client/auth_client.android.dart';
import 'package:celest_auth/src/client/auth_client.dart';
import 'package:celest_auth/src/client/auth_client.darwin.dart';
import 'package:celest_auth/src/client/auth_client.native.dart';
import 'package:celest_core/celest_core.dart';
// ignore: implementation_imports
import 'package:celest_core/src/util/globals.dart';
import 'package:meta/meta.dart';
import 'package:os_detect/os_detect.dart' as os;

abstract base class AuthClientPlatform extends AuthClient {
factory AuthClientPlatform() {
factory AuthClientPlatform({
required AuthProtocol protocol,
}) {
if (kIsDartNative) {
return AuthClientNative();
return AuthClientNative(protocol: protocol);
}
if (os.isIOS || os.isMacOS) {
return AuthClientDarwin();
return AuthClientDarwin(protocol: protocol);
}
if (os.isAndroid) {
return AuthClientAndroid();
return AuthClientAndroid(protocol: protocol);
}
throw UnsupportedError(
'The current platform is not supported: ${os.operatingSystem}',
);
}

@protected
AuthClientPlatform.base() : super.base();
AuthClientPlatform.base({
required super.protocol,
}) : super.base();
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import 'package:celest_auth/src/client/auth_client.dart';
import 'package:celest_auth/src/client/auth_client.web.dart';
import 'package:celest_core/celest_core.dart';
// ignore: implementation_imports
import 'package:celest_core/src/util/globals.dart';
import 'package:meta/meta.dart';
import 'package:os_detect/os_detect.dart' as os;

abstract base class AuthClientPlatform extends AuthClient {
factory AuthClientPlatform() {
factory AuthClientPlatform({
required AuthProtocol protocol,
}) {
if (kIsWeb) {
return AuthClientWeb();
return AuthClientWeb(protocol: protocol);
}
throw UnsupportedError(
'The current platform is not supported: ${os.operatingSystem}',
);
}

@protected
AuthClientPlatform.base() : super.base();
AuthClientPlatform.base({
required super.protocol,
}) : super.base();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import 'dart:async';
import 'dart:convert';

import 'package:celest_auth/src/client/passkeys/passkey_client_platform.vm.dart';
import 'package:celest_auth/src/client/passkeys/passkey_models.dart';
import 'package:celest_auth/src/platform/android/jni_bindings.ffi.dart'
hide Exception, Uri;
import 'package:celest_auth/src/platform/android/jni_helpers.dart';
import 'package:celest_core/celest_core.dart';
import 'package:jni/jni.dart';

final class PasskeyClientAndroid extends PasskeyClientPlatform {
PasskeyClientAndroid() : super.base();
PasskeyClientAndroid({
required super.protocol,
}) : super.base();

late final Activity _mainActivity =
Activity.fromRef(Jni.getCurrentActivity());
Expand Down Expand Up @@ -50,15 +52,16 @@ final class PasskeyClientAndroid extends PasskeyClientPlatform {

@override
Future<PasskeyRegistrationResponse> register(
PasskeyRegistrationOptions options,
PasskeyRegistrationRequest request,
) async {
final request = CreatePublicKeyCredentialRequest.new7(
final options = await protocol.requestRegistration(request: request);
final jRequest = CreatePublicKeyCredentialRequest.new7(
jsonEncode(options.toJson()).toJString(),
);
final responseCallback = Completer<CreatePublicKeyCredentialResponse>();
_credentialManager.createCredentialAsync(
_mainActivityContext,
request,
jRequest,
CancellationSignal(),
_threadPool,
CredentialManagerCallback<CreateCredentialResponse,
Expand Down Expand Up @@ -130,10 +133,11 @@ final class PasskeyClientAndroid extends PasskeyClientPlatform {
Future<PasskeyAuthenticationResponse> authenticate(
PasskeyAuthenticationRequest request,
) async {
final options = await protocol.requestAuthentication(request: request);
final jRequest = GetCredentialRequest_Builder()
.addCredentialOption(
GetPublicKeyCredentialOption.new3(
jsonEncode(request.toJson()).toJString(),
jsonEncode(options.toJson()).toJString(),
),
)
.build();
Expand Down
15 changes: 11 additions & 4 deletions packages/celest_auth/lib/src/client/passkeys/passkey_client.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import 'package:celest_auth/src/client/passkeys/passkey_client_platform.vm.dart'
if (dart.library.js_interop) 'package:celest_auth/src/client/passkeys/passkey_client_platform.web.dart';
import 'package:celest_auth/src/client/passkeys/passkey_models.dart';
import 'package:celest_core/celest_core.dart';
import 'package:meta/meta.dart';

abstract base class PasskeyClient {
factory PasskeyClient() = PasskeyClientPlatform;
factory PasskeyClient({
required PasskeyProtocol protocol,
}) = PasskeyClientPlatform;

@protected
PasskeyClient.base();
PasskeyClient.base({
required this.protocol,
});

@protected
final PasskeyProtocol protocol;

Future<bool> get isSupported;
Future<PasskeyRegistrationResponse> register(
PasskeyRegistrationOptions options,
PasskeyRegistrationRequest request,
);
Future<PasskeyAuthenticationResponse> authenticate(
PasskeyAuthenticationRequest request,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import 'package:celest_auth/src/client/passkeys/passkey_client_platform.vm.dart';
import 'package:celest_auth/src/client/passkeys/passkey_models.dart';
import 'package:celest_core/celest_core.dart';

final class PasskeyClientDarwin extends PasskeyClientPlatform {
PasskeyClientDarwin() : super.base();
PasskeyClientDarwin({
required super.protocol,
}) : super.base();

@override
Future<bool> get isSupported => throw UnimplementedError();

@override
Future<PasskeyRegistrationResponse> register(
PasskeyRegistrationOptions options,
) {
PasskeyRegistrationRequest request,
) async {
throw UnimplementedError();
}

Expand Down
Loading

0 comments on commit 5d400a4

Please sign in to comment.