Subversion Repositories SmartDukaan

Rev

Rev 3128 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.serving.auth;

import in.shop2020.crm.Agent;
import in.shop2020.crm.CRMService.Client;
import in.shop2020.thrift.clients.CRMServiceClient;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.thrift.TException;

/**
 * @author mandeep
 * 
 * This class is realm for fetching authentication and authorization details for an agent.
 */
public class CRMRealm extends AuthorizingRealm {
    private static final Log log = LogFactory.getLog(CRMRealm.class);

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //null usernames are invalid
        if (principals == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }

        String username = (String) getAvailablePrincipal(principals);
        List<String> roleNames = null;
        Set<String> permissions = null;

        try {
            Client crmServiceClient = new CRMServiceClient().getClient();

            // Retrieve roles and permissions from database
            roleNames = crmServiceClient.getRoleNamesForAgent(username);

//            // XXX - we might not need permissions!
//            if (permissionsLookupEnabled) {
//                permissions = crmServiceClient.getPermissionsForRoleName(roleNames.get(0));
//            }
        } catch (TException e) {
            throw new AuthorizationException("Error fetching roles' information", e);
        } catch (Exception e) {
            throw new AuthorizationException("Error creating CRM client", e);
        }

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(new HashSet<String>(roleNames));
        info.setStringPermissions(permissions);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException
    {
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();
        SimpleAuthenticationInfo info = null;

        log.info("Trying to fetch passowrd for " + username);
        try {
            Client crmServiceClient = new CRMServiceClient().getClient();
            Agent agent = crmServiceClient.getAgentByEmailId(username);

            if (agent != null) {
                info = new SimpleAuthenticationInfo(username, agent.getPassword().toCharArray(), getName());
            }
            else {
                throw new UnknownAccountException("No account found for user [" + username + "]");
            }
        }
        catch (Exception e) {
            String error = "Error while creating CRM client";
            log.error(error, e);
            throw new AuthenticationException(error, e);
        }

        return info;
    }
}