Class ResolveFederatedLoginUseCase

java.lang.Object
com.irurueta.authserver.authorization.application.social.ResolveFederatedLoginUseCase

@Service @Transactional public class ResolveFederatedLoginUseCase extends Object
Use case that resolves a sign-in with an external provider to a local account. The provider's stable subject, never its e-mail address, finds the account. The identity is locked first (FederatedIdentityRepository.lockSubject(com.irurueta.authserver.authorization.domain.social.IdentityProvider, java.lang.String)), so two concurrent first sign-ins of the same person are serialized and the second one finds the link of the first instead of failing on the unique constraint. In order:
  1. an identity that is already linked signs in its account, unless the account is disabled;
  2. otherwise the provider must vouch for the e-mail address: an unverified address never links and never provisions;
  3. an account that has that address and has verified it too is linked to the identity, unless it is disabled: a disabled account is refused before anything is linked or audited;
  4. an address that belongs to an account that never verified it is refused, so a pre-registered account cannot capture the victim's sign-in;
  5. otherwise a new, enabled account is provisioned, whose password is a random one nobody knows.
  • Field Details

    • MAX_USERNAME_LENGTH

      static final int MAX_USERNAME_LENGTH
      Longest username the users table accepts.
      See Also:
    • federatedIdentityRepository

      private final FederatedIdentityRepository federatedIdentityRepository
      Port that stores the links.
    • userAccountRepository

      private final UserAccountRepository userAccountRepository
      Port that stores the accounts.
    • generatedPasswordRepository

      private final GeneratedPasswordRepository generatedPasswordRepository
      Port that remembers the accounts that only hold a generated password.
    • passwordHasher

      private final PasswordHasher passwordHasher
      Port that hashes the generated password.
    • tokenGenerator

      private final TokenGenerator tokenGenerator
      Port that generates the unguessable secret the generated password is made of.
    • federatedLinkingPolicy

      private final FederatedLinkingPolicy federatedLinkingPolicy
      Rules of linking and provisioning.
    • recordSecurityEventUseCase

      private final RecordSecurityEventUseCase recordSecurityEventUseCase
      Use case that records the audit events.
    • clock

      private final Clock clock
      Source of the current time.
  • Constructor Details

    • ResolveFederatedLoginUseCase

      public ResolveFederatedLoginUseCase()
  • Method Details

    • execute

      public FederatedLogin execute(IdentityProvider provider, String subject, String email, boolean emailVerified)
      Resolves a sign-in.
      Parameters:
      provider - provider that authenticated the user.
      subject - stable identifier the provider assigned to the user.
      email - e-mail address the provider reported, or null.
      emailVerified - whether the provider says it verified the address.
      Returns:
      the account the sign-in resolved to and how it was found.
      Throws:
      FederatedLoginRefusedException - if the identity is not linked and the provider does not vouch for a usable e-mail address, the address belongs to an unverified account, or the account is disabled.
    • signIn

      private FederatedLogin signIn(FederatedIdentity identity, String email, Instant now)
      Signs in the account of an identity that is already linked, remembering the sign-in.
      Parameters:
      identity - the stored link.
      email - e-mail address the provider reported now, or null.
      now - current instant.
      Returns:
      the resolution.
    • requireMaySignIn

      private void requireMaySignIn(UserAccount account)
      Refuses an account that may not sign in, before anything is linked, updated or audited.
      Parameters:
      account - the account the sign-in resolved to.
      Throws:
      FederatedLoginRefusedException - if the account is disabled.
    • link

      private FederatedIdentity link(UserAccount account, IdentityProvider provider, String subject, String email, Instant now)
      Links an identity to an account and audits it.
      Parameters:
      account - the account.
      provider - provider of the identity.
      subject - subject of the identity.
      email - e-mail address the provider reported.
      now - current instant.
      Returns:
      the stored link.
    • provision

      private UserAccount provision(IdentityProvider provider, String email)
      Creates an enabled account from the provider's verified e-mail address. Its password is the hash of a random secret that is discarded, and the account is marked as having no password of its own.
      Parameters:
      provider - provider that vouched for the address.
      email - verified e-mail address.
      Returns:
      the new account.