Skip to content

Commit

Permalink
Move passkey exception impl to Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Mar 4, 2024
1 parent 68f562a commit 7a13bc6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:celest_core/celest_core.dart';

// TODO(dnys1): Make sealed
final class PasskeyExceptionImpl implements PasskeyException {
const PasskeyExceptionImpl({required this.message});

@override
final String message;

@override
String toString() => 'PasskeyException: $message';
}

final class PasskeyCancellationException extends PasskeyExceptionImpl {
const PasskeyCancellationException()
: super(message: 'Passkey registration was canceled by the user');
}

final class PasskeyUnknownException extends PasskeyExceptionImpl {
const PasskeyUnknownException([String? message])
: super(
message: message ??
'An unknown error occurred during passkey registration',
);
}

final class PasskeyUnsupportedException extends PasskeyExceptionImpl {
const PasskeyUnsupportedException()
: super(message: 'Passkeys are not supported on this platform');
}

final class PasskeyFailedException extends PasskeyExceptionImpl {
const PasskeyFailedException([String? message])
: super(message: message ?? 'Passkey registration failed');
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:convert';
import 'package:celest_auth/src/native/android/jni_bindings.ffi.dart'
hide Exception;
import 'package:celest_auth/src/native/android/jni_helpers.dart';
import 'package:celest_auth/src/platform/passkeys/passkey_exception_impl.dart';
import 'package:celest_auth/src/platform/passkeys/passkey_platform_impl.vm.dart';
import 'package:celest_core/celest_core.dart';
import 'package:jni/jni.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import 'dart:async';
import 'dart:convert';
import 'dart:ffi';

import 'package:celest_auth/src/platform/passkeys/passkey_platform_impl.vm.dart';
import 'package:celest_auth/src/native/darwin/celest_auth.ffi.dart';
import 'package:celest_auth/src/platform/passkeys/passkey_exception_impl.dart';
import 'package:celest_auth/src/platform/passkeys/passkey_platform_impl.vm.dart';
import 'package:celest_core/celest_core.dart';
// ignore: implementation_imports
import 'package:celest_core/src/util/globals.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'dart:typed_data';

import 'package:celest_auth/src/platform/passkeys/passkey_exception_impl.dart';
import 'package:celest_auth/src/platform/passkeys/passkey_platform_impl.web.dart';
import 'package:celest_core/celest_core.dart'
hide AuthenticatorSelectionCriteria;
Expand Down Expand Up @@ -45,7 +46,7 @@ final class PasskeyPlatformWeb extends PasskeyPlatformImpl {
PasskeyRegistrationRequest request,
) async {
if (!await isSupported) {
throw const PasskeyException(
throw const PasskeyExceptionImpl(
message: 'Passkeys are not supported in this environment',
);
}
Expand All @@ -61,7 +62,7 @@ final class PasskeyPlatformWeb extends PasskeyPlatformImpl {
for (final param in options.publicKeyCredentialParameters)
PublicKeyCredentialParameters(
alg: param.algorithm,
type: 'public-key',
type: param.type,
),
].toJS,
rp: PublicKeyCredentialRpEntity(
Expand All @@ -71,7 +72,7 @@ final class PasskeyPlatformWeb extends PasskeyPlatformImpl {
id: utf8.encode(options.userId).buffer.toJS,
displayName: options.userDisplayName,
)..name = options.userName,
attestation: 'none',
attestation: options.attestation,
authenticatorSelection: AuthenticatorSelectionCriteria(
authenticatorAttachment:
options.authenticatorSelection.authenticatorAttachment!,
Expand All @@ -93,7 +94,8 @@ final class PasskeyPlatformWeb extends PasskeyPlatformImpl {
),
) as PublicKeyCredential?;
if (credential == null) {
throw const PasskeyException(message: 'Registration was not completed');
throw const PasskeyExceptionImpl(
message: 'Registration was not completed');
}
assert(credential.type == 'public-key');
final response = credential.response as AuthenticatorAttestationResponse;
Expand All @@ -116,7 +118,7 @@ final class PasskeyPlatformWeb extends PasskeyPlatformImpl {
PasskeyAuthenticationRequest request,
) async {
if (!await isSupported) {
throw const PasskeyException(
throw const PasskeyExceptionImpl(
message: 'Passkeys are not supported in this environment',
);
}
Expand All @@ -139,7 +141,7 @@ final class PasskeyPlatformWeb extends PasskeyPlatformImpl {
),
) as PublicKeyCredential?;
if (credential == null) {
throw const PasskeyException(
throw const PasskeyExceptionImpl(
message: 'Authentication was not completed',
);
}
Expand Down Expand Up @@ -214,14 +216,14 @@ extension type RegistrationError._(JSError _) implements JSError {
// TODO(dnys1): Cover all cases
// https://github.com/MasterKale/SimpleWebAuthn/blob/634ceabdb05f4b5e56132fff7c57598caa2401a8/packages/browser/src/helpers/identifyRegistrationError.ts#L7
PasskeyException toPasskeyException() {
return PasskeyException(message: '$name: $message');
return PasskeyExceptionImpl(message: '$name: $message');
}
}

extension type AuthenticationError._(JSError _) implements JSError {
// TODO(dnys1): Cover all cases
// https://github.com/MasterKale/SimpleWebAuthn/blob/634ceabdb05f4b5e56132fff7c57598caa2401a8/packages/browser/src/helpers/identifyAuthenticationError.ts#L7
PasskeyException toPasskeyException() {
return PasskeyException(message: '$name: $message');
return PasskeyExceptionImpl(message: '$name: $message');
}
}

0 comments on commit 7a13bc6

Please sign in to comment.