Interface AuthenticatorSelector

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface AuthenticatorSelector
The object in charge of selecting the authenticator to be used to perform an operation.

The SDK does not provide implementations of this interface. The implementation must be done by the user of the SDK.

See Also:
  • Method Details

    • selectAuthenticator

      void selectAuthenticator(AuthenticatorSelectionContext context, AuthenticatorSelectionHandler handler)
      The authenticator selection interaction. The implementing class must ask the user to choose one of the authenticators exposed by the AuthenticatorSelectionContext and provide the choice to the AuthenticatorSelectionHandler.

      Note, that in the case of transaction confirmation (which can be considered a special case of authentication) the implementing classes must present the contents of the transaction (if any) to the user for verification (see AuthenticatorSelectionContext.transactionConfirmationData()).

      This method will be invoked in the main/UI thread.

      Basic implementation layout:

       import java.util.ArrayList;
       import java.util.List;
      
       [...]
      
           @Override
           public void selectAuthenticator(AuthenticatorSelectionContext context, AuthenticatorSelectionHandler handler) {
               List<Authenticator> validAuthenticators = getValidAuthenticators(context);
               if (validAuthenticators.isEmpty()) {
                   // Display a message informing the user that there are no valid authenticators available and that we cannot
                   // proceed with  the operation. When the user discards the message, cancel the operation invoking:
                   //   handler.cancel();
               } else if (validAuthenticators.size() == 1) {
                   // There is just one valid authenticator available. We can decide simply to not interact with the user and
                   // return the available authenticator.
                   handler.aaid(validAuthenticators.get(0).aaid());
               } else {
                   // More than one authenticator available, display them to the user to pick one. If one is picked, provide
                   // it invoking:
                   //   Authenticator chosenAuthenticator = ...
                   //   handler.aaid(chosenAuthenticator.aaid());
      
                   // If the user cancels the operation, cancel it invoking:
                   // handler.cancel();
               }
           }
      
           private static List<Authenticator> getValidAuthenticators(AuthenticatorSelectionContext context) {
               List<Authenticator> validAuthenticators = new ArrayList<>();
               for (Authenticator authenticator : context.authenticators()) {
                   if (isValid(authenticator, context)) {
                       validAuthenticators.add(authenticator);
                   }
               }
               return validAuthenticators;
           }
      
           private static boolean isValid(Authenticator authenticator, AuthenticatorSelectionContext context) {
               String username = context.account().username();
               Registration registration = authenticator.registration();
               // This method will filter the authenticators that are no supported by the hardware, those
               // whose registration status does not correspond to the operation and in the case of registration
               // and authentication, those that are compliant with the server policy.
               if (isRegistrationOperation()) {
                       return !registration.isRegistered(username) && authenticator.isSupportedByHardware() &&
                              context.isPolicyCompliant(authenticator.aaid());
               }
               if (isAuthenticationOperation()) {
                   return registration.isRegistered(username) && authenticator.isSupportedByHardware() &&
                       context.isPolicyCompliant(authenticator.aaid());
               }
               // Bug
               throw new IllegalStateException();
           }
       
      Parameters:
      context - the object containing the list of existing authenticators
      handler - the object that is notified of the selection result