Class OttLinkAuthenticationProvider

java.lang.Object
com.irurueta.authserver.authorization.api.web.ott.OttLinkAuthenticationProvider
All Implemented Interfaces:
org.springframework.security.authentication.AuthenticationProvider

public class OttLinkAuthenticationProvider extends Object implements org.springframework.security.authentication.AuthenticationProvider
Confirms a magic link: consumes the token Spring's JdbcOneTimeTokenService stored and signs the user in with it. It does what Spring's own provider does, except that it also reads the purpose the link was requested for (LinkTokenUsername), which Spring's provider cannot because it would look the marked user name up as it is:
  • a link requested for a sign-in answers with the user's authentication plus FACTOR_OTT;
  • a link requested as a second factor is refused unless the session that confirms it is the signed-in session of the same user, with a primary factor, and OttSecondFactorChannels allows the e-mail channel for that session (it did not sign the user in, and the user has no other confirmed second factor); it then answers with the second-factor authority of the e-mail channel, and Spring Security's authentication filter merges it with the factors the session holds.
Links are delivered by e-mail only. The account is loaded through the user service of the chain, which refuses a disabled, locked or expired account.
  • Field Details

    • oneTimeTokenService

      private final org.springframework.security.authentication.ott.OneTimeTokenService oneTimeTokenService
      Store of the link tokens.
    • userDetailsService

      private final org.springframework.security.core.userdetails.UserDetailsService userDetailsService
      Loads the account, refusing one that cannot sign in.
    • factorGrantor

      private final FactorGrantor factorGrantor
      Adds the factor authority to an authentication.
    • primaryFactorSession

      private final PrimaryFactorSession primaryFactorSession
      Rules about the session a second factor is presented in.
    • secondFactorChannels

      private final OttSecondFactorChannels secondFactorChannels
      Decides which channels may be the second factor of the user.
  • Constructor Details

    • OttLinkAuthenticationProvider

      public OttLinkAuthenticationProvider()
  • Method Details

    • authenticate

      public org.springframework.security.core.Authentication authenticate(org.springframework.security.core.Authentication authentication) throws org.springframework.security.core.AuthenticationException
      Consumes the token and builds the authentication.
      Specified by:
      authenticate in interface org.springframework.security.authentication.AuthenticationProvider
      Parameters:
      authentication - the OneTimeTokenAuthenticationToken to confirm.
      Returns:
      the authentication of the user, with the factor of the purpose of the link.
      Throws:
      org.springframework.security.core.AuthenticationException - if the token is not valid, the account cannot sign in or a second-factor link is confirmed outside the session of the user it was requested for.
    • supports

      public boolean supports(Class<?> authentication)
      Tells which requests this provider confirms.
      Specified by:
      supports in interface org.springframework.security.authentication.AuthenticationProvider
      Parameters:
      authentication - the type of the request.
      Returns:
      true for a OneTimeTokenAuthenticationToken.
    • requirePrimaryFactorSessionOf

      private void requirePrimaryFactorSessionOf(String username)
      Requires the current session to be the signed-in session of a user who still has to present a second factor.
      Parameters:
      username - the user the link was requested for.
      Throws:
      org.springframework.security.authentication.BadCredentialsException - if the session is another user's, holds no primary factor, or may not use the e-mail channel the link was delivered by as its second factor.