package com.eldermoraes.ch05.authentication; import javax.enterprise.context.ApplicationScoped; import javax.security.enterprise.AuthenticationException; import javax.security.enterprise.AuthenticationStatus; import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism; import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext; import java.util.Arrays; import java.util.HashSet; import javax.security.enterprise.credential.CallerOnlyCredential; import javax.security.enterprise.credential.Credential; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @ApplicationScoped public class AuthenticationMechanism implements HttpAuthenticationMechanism { @Override public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) throws AuthenticationException { if (httpMessageContext.isAuthenticationRequest()) { Credential credential = httpMessageContext.getAuthParameters().getCredential(); if (!(credential instanceof CallerOnlyCredential)) { throw new IllegalStateException("Invalid mechanism"); } CallerOnlyCredential callerOnlyCredential = (CallerOnlyCredential) credential; if ("user".equals(callerOnlyCredential.getCaller())) { return httpMessageContext.notifyContainerAboutLogin(callerOnlyCredential.getCaller(), new HashSet<>(Arrays.asList("role1","role2"))); } else{ throw new AuthenticationException(); } } return httpMessageContext.doNothing(); } }