franta-hg@2: package cz.frantovo.jaas.sql;
franta-hg@2:
franta-hg@2: import com.sun.appserv.security.AppservPasswordLoginModule;
franta-hg@6: import com.sun.enterprise.security.auth.realm.NoSuchUserException;
franta-hg@6: import java.util.ArrayList;
franta-hg@4: import java.util.Arrays;
franta-hg@6: import java.util.Enumeration;
franta-hg@6: import java.util.List;
franta-hg@4: import java.util.logging.Level;
franta-hg@4: import java.util.logging.Logger;
franta-hg@4: import javax.security.auth.login.LoginException;
franta-hg@2:
franta-hg@2: /**
franta-hg@4: * Přihlašovací modul pro SQL doménu.
franta-hg@6: *
franta-hg@6: * TODO: později bude potomkem
franta-hg@6: * com.sun.appserv.security.AbstractLoginModule
franta-hg@6: *
franta-hg@2: * @author fiki
franta-hg@2: */
franta-hg@2: public class SQLLoginModul extends AppservPasswordLoginModule {
franta-hg@4:
franta-hg@6: /**
franta-hg@6: * viz konfigurace v login.conf
franta-hg@6: */
franta-hg@4: public static final String VÝCHOZÍ_JAAS_KONTEXT = "sqlRealm";
franta-hg@4: private static final Logger log = Logger.getLogger(SQLLoginModul.class.getName());
franta-hg@4:
franta-hg@4: @Override
franta-hg@4: protected void authenticateUser() throws LoginException {
franta-hg@6: if (_currentRealm instanceof SQLRealm) {
franta-hg@6: try {
franta-hg@6: SQLRealm sqlRealm = (SQLRealm) _currentRealm;
franta-hg@6: sqlRealm.ověřUživatele(_username, _passwd);
franta-hg@6:
franta-hg@6: String skupiny[] = poleSkupin(sqlRealm.getGroupNames(_username));
franta-hg@6: log.log(Level.INFO, "Uživatel {0} byl úspěšně ověřen a je členem těchto skupin: {1}", new Object[]{_username, Arrays.toString(skupiny)});
franta-hg@6: commitUserAuthentication(skupiny);
franta-hg@6: } catch (NoSuchUserException nue) {
franta-hg@6: log.log(Level.SEVERE, "Uživatele se podařilo ověřit, ale při pokusu o zjištění jeho skupin došlo k chybě. Uživatel: " + _username, nue);
franta-hg@6: throw new LoginException("Neexistující uživatel: " + _username);
franta-hg@6: }
franta-hg@4: } else {
franta-hg@4: throw new LoginException("Špatný realm: " + _currentRealm + " Očekávám: SQLRealm.");
franta-hg@4: }
franta-hg@4: }
franta-hg@6:
franta-hg@6: /**
franta-hg@6: * Převede výčet na pole
franta-hg@6: */
franta-hg@6: private static String[] poleSkupin(Enumeration e) {
franta-hg@6: List l = new ArrayList<>();
franta-hg@6: while (e.hasMoreElements()) {
franta-hg@6: l.add(e.nextElement());
franta-hg@6: }
franta-hg@6: return l.toArray(new String[0]);
franta-hg@6: }
franta-hg@2: }