/*
 Created as part of the StratusLab project (http://stratuslab.eu),
 co-funded by the European Commission under the Grant Agreement
 INSFO-RI-261552.

 Copyright (c) 2010, Centre Nationale de la Recherche Scientifique

 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 eu.stratuslab.authn;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;

import org.eclipse.jetty.http.security.Credential;
import org.eclipse.jetty.plus.jaas.spi.AbstractLoginModule;
import org.eclipse.jetty.plus.jaas.spi.UserInfo;

public class CertLoginModule extends AbstractLoginModule {

    final private static AtomicReference<AuthnData> authnUsersRef = new AtomicReference<AuthnData>();

    static {
        authnUsersRef.set(new AuthnData(null));
    }

    @Override
    public UserInfo getUserInfo(String username) throws Exception {

        Credential credential = createUserCredential(username);
        List<String> roles = getUserRoles(username);

        return new UserInfo(username, credential, roles);
    }

    @Override
    public void initialize(Subject subject, CallbackHandler callbackHandler,
            Map sharedState, Map options) {

        super.initialize(subject, callbackHandler, sharedState, options);

        authnUsersRef.set(new AuthnData(options.get("file")));
    }

    private Credential createUserCredential(String username) {
        AuthnData data = authnUsersRef.get();
        return new BooleanCredential(data.isValidUser(username));
    }

    private List<String> getUserRoles(String username) {
        AuthnData data = authnUsersRef.get();
        return data.groups(username);
    }

    @SuppressWarnings("serial")
    private static class BooleanCredential extends Credential {

        final private boolean valid;

        public BooleanCredential(boolean valid) {
            this.valid = valid;
        }

        @Override
        public boolean check(Object credentials) {
            return valid;
        }
    }

}

@@ -874,16 +874,124 @@
 ference;
+%0Aimport java.util.logging.ConsoleHandler;%0Aimport java.util.logging.Handler;%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -1458,32 +1458,331 @@
 (null));%0A    %7D%0A%0A
+    final private static Logger logger;%0A    static %7B%0A        logger = Logger.getLogger(%22eu.stratuslab.authn%22);%0A        for (Handler h : logger.getHandlers()) %7B%0A            logger.removeHandler(h);%0A        %7D%0A%0A        Handler handler = new ConsoleHandler();%0A        logger.addHandler(handler);%0A    %7D%0A%0A
     @Override%0A  
@@ -2023,32 +2023,67 @@
  roles);%0A    %7D%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     @Override%0A  
@@ -2402,32 +2402,32 @@
 ing username) %7B%0A
-
         AuthnDat
@@ -2448,32 +2448,94 @@
 UsersRef.get();%0A
+        logger.warning(%22TRYING TO CREATE USER: %22 + username);%0A
         return n

add logging to certificate checker