tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject [Bug 56488] jaas custom login module
Date Wed, 07 May 2014 19:26:42 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=56488

--- Comment #8 from dstojkov <dstojkov2002@yahoo.fr> ---
sorry it was a wrong one before

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.lobby_alpha.helper.jaas;

import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

/**
 *
 * @author dstojkov
 */
public class SimpleLoginModule implements LoginModule {
    private Set<String> lobbyUsers;
    private List<String> lobbyGroups;
    private Subject subject;
    private CallbackHandler callbackHandler;

    private Principal simpleUser;
    private boolean committed;

    public SimpleLoginModule() {
        System.out.println("creation SimpleLoginModule");
        this.lobbyUsers = new TreeSet<>();
        this.lobbyGroups = new ArrayList<>();
        this.simpleUser = null;
        this.committed = false;
        dataInit();
    }

    @Override
    public void initialize(Subject subject, CallbackHandler callbackHandler,
Map<String, ?> sharedState, Map<String, ?> options) {
        System.out.println("debut initialise");
        this.subject = subject;
        this.callbackHandler = callbackHandler;

        System.out.println("debut sharedState");
        for(String tmp : sharedState.keySet()) {
            System.out.println("key : " + tmp + " value : " +
sharedState.get(tmp));
        }
        System.out.println("fin sharedState");
        System.out.println("debut options");
        for(String tmp : options.keySet()) {
            System.out.println("key : " + tmp + " value : " +
options.get(tmp));
        }
        System.out.println("fin options");
        System.out.println("fin initialise");
    }

    @Override
    public boolean login() throws LoginException {
        System.out.println("debut login");
        //boolean ret = false;

        Callback[] callbacks = {new NameCallback("username"), new
PasswordCallback("password", true)};
        try {
            callbackHandler.handle(callbacks);
        } catch(IOException | UnsupportedCallbackException ex) {
            throw new LoginException(ex.getMessage());
        }
        System.out.println("callbacks[0] : " + callbacks[0]);
        System.out.println("callbacks[1] : " + callbacks[1]);

        String name = ((NameCallback)callbacks[0]).getName();
        String password =
String.valueOf(((PasswordCallback)callbacks[1]).getPassword());
        System.out.println("debut lobbyUsers");
        for(String tmp : lobbyUsers) {
            System.out.println("tmp : " + tmp);
        }
        System.out.println("fin lobbyUsers");
        System.out.println("name : -" + name + "-");
        System.out.println("password : -" + password + "-");
        System.out.println("" + !this.lobbyUsers.contains(name));
        System.out.println("" + !name.equals(password));
        if(name.equals("") || !this.lobbyUsers.contains(name) ||
!name.equals(password)) {
            throw new FailedLoginException("bad credentials");
        }
        this.simpleUser = new SimpleUser(name);
        //this.simpleUser = new GenericPrincipal(name, password,
Arrays.asList("user"));
        System.out.println("fin login");
        return true;
    }

    @Override
    public boolean commit() throws LoginException {
        if(this.simpleUser == null) {
            return false;
        }
        System.out.println("debut commit");
        System.out.println("debut Principals");
        for(Principal tmp : subject.getPrincipals()) {
            System.out.println("tmp : " + tmp);
        }
        System.out.println("fin Principals");
        subject.getPrincipals().add(this.simpleUser);
        subject.getPrincipals().add(new SimpleRole("user"));
        System.out.println("fin commit");
        this.committed = true;
        return true;
    }

    @Override
    public boolean abort() throws LoginException {
        System.out.println("debut abort");
        if(this.simpleUser == null) {
            return false;
        }
        if(this.committed) {
            logout();
        } else {
            this.committed = false;
            this.simpleUser = null;
        }
        System.out.println("fin abort");
        return true;
    }

    @Override
    public boolean logout() throws LoginException {
        System.out.println("debut logout");
        subject.getPrincipals().remove(this.simpleUser);
        committed = false;
        this.simpleUser = null;
        System.out.println("fin logout");
        return true;
    }

    private void dataInit() {
        lobbyUsers.add("user21");
        lobbyUsers.add("user22");
        lobbyUsers.add("user23");
        lobbyUsers.add("user24");
        lobbyUsers.add("user25");
        lobbyUsers.add("user26");
        lobbyUsers.add("user27");
        lobbyUsers.add("user28");
        lobbyUsers.add("user29");
        lobbyUsers.add("user30");
        lobbyUsers.add("user31");
        lobbyUsers.add("user32");
        this.lobbyGroups.add("user");
    }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message