/**
 * Copyright © 2016-2018 Jesse Gallagher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package security;

import com.darwino.commons.httpclnt.HttpClient;
import com.darwino.j2ee.servlet.authentication.handler.FormAuthHandler;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
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 javax.security.enterprise.credential.UsernamePasswordCredential;
import javax.security.enterprise.identitystore.CredentialValidationResult;
import javax.security.enterprise.identitystore.IdentityStore;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ApplicationScoped
public class DarwinoHttpAuthenticationMechanism implements HttpAuthenticationMechanism {

    @Inject
    IdentityStore identityStore;
    
    @Override
    public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) throws AuthenticationException {
        FormAuthHandler handler = new FormAuthHandler();
        try {
            HttpClient.Authenticator auth = handler.readAuthentication(request, response);
            if(auth instanceof HttpClient.BasicAuthenticator) {
                HttpClient.BasicAuthenticator cred = (HttpClient.BasicAuthenticator)auth;
                CredentialValidationResult result = identityStore.validate(new UsernamePasswordCredential(cred.getUserName(), cred.getPassword()));
                httpMessageContext.notifyContainerAboutLogin(result.getCallerPrincipal(), result.getCallerGroups());
            }
        } catch (IOException | ServletException e) {
            throw new AuthenticationException(e);
        }
        return httpMessageContext.doNothing();
    }
}

@@ -659,22 +659,159 @@
 lnt.Http
-Client
+Base;%0Aimport com.darwino.commons.httpclnt.HttpClient;%0Aimport com.darwino.commons.util.StringUtil;%0Aimport com.darwino.commons.util.io.Base64Util
 ;%0Aimport
@@ -1722,17 +1722,16 @@
 anism %7B%0A
-%0A
     @Inj
@@ -1964,16 +1964,986 @@
 ption %7B%0A
+        // Check for basic auth first%0A        String authHeaderB64 = request.getHeader(HttpBase.HEADER_AUTHORIZATION);%0A        if(StringUtil.isNotEmpty(authHeaderB64) && authHeaderB64.startsWith(%22Basic %22)) %7B%0A            String authHeader = new String(Base64Util.decodeBase64(authHeaderB64.substring(authHeaderB64.indexOf(' ') + 1)));%0A            int i = authHeader.indexOf(':');%0A            if (i %3E 0) %7B%0A                String userName = authHeader.substring(0, i);%0A                String password = i == authHeader.length()-1 ? %22%22 : authHeader.substring(i+1);%0A                CredentialValidationResult result = identityStore.validate(new UsernamePasswordCredential(userName, password));%0A                return httpMessageContext.notifyContainerAboutLogin(result.getCallerPrincipal(), result.getCallerGroups());%0A            %7D else %7B%0A                return httpMessageContext.responseUnauthorized();%0A            %7D%0A        %7D%0A%0A        // Failing that, check for form auth%0A
         
@@ -3410,24 +3410,31 @@
             
+return 
 httpMessageC
@@ -3585,24 +3585,57 @@
 eption e) %7B%0A
+            e.printStackTrace();%0A
             
@@ -3668,24 +3668,24 @@
 ception(e);%0A
-
         %7D%0A  
@@ -3674,32 +3674,33 @@
 n(e);%0A        %7D%0A
+%0A
         return h

Add HTTP Basic auth support