package auth; import auth.models.Token; import exceptions.PoseidonException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import play.mvc.Http; import service.UserPreferenceService; import java.util.HashMap; import java.util.Map; public class AuthenticationManager { private static final Logger logger = LoggerFactory.getLogger(AuthenticationManager.class); private final Map<GrantType, Authenticator> authenticators; public AuthenticationManager() { authenticators = new HashMap<>(); authenticators.put(GrantType.LDAP, new LDAPAuthenticator()); authenticators.put(GrantType.PASSWORD, new OAuth2PasswordAuthenticator()); authenticators.put(GrantType.REFRESH_TOKEN, new OAuth2RefreshTokenAuthenticator()); } public AuthenticationManager(Map<GrantType, Authenticator> authenticators) { this.authenticators = authenticators; } /** * Attempt to authenticate the authentication by looping through the authenticators. * Returns the token from the first authenticator which does not return null. */ public Token authenticate(Authentication authentication) { Authenticator authenticator = authenticators.get(authentication.getGrantType()); if (authenticator != null) { Token result = authenticator.authenticate(authentication); return result; } else { logger.warn("Did not find an authentication manager for grant type {}.", authentication.getGrantType()); throw new PoseidonException(Http.Status.BAD_REQUEST,"Grant type '" + authentication.getGrantType().getValue() + "' is not supported"); } } }