Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3090 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.auth;
5
 
6
import in.shop2020.crm.Agent;
7
import in.shop2020.crm.CRMService.Client;
3128 rajveer 8
import in.shop2020.thrift.clients.CRMClient;
3090 mandeep.dh 9
 
10
import java.util.HashSet;
11
import java.util.List;
12
import java.util.Set;
13
 
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.apache.shiro.authc.AuthenticationException;
17
import org.apache.shiro.authc.AuthenticationInfo;
18
import org.apache.shiro.authc.AuthenticationToken;
19
import org.apache.shiro.authc.SimpleAuthenticationInfo;
20
import org.apache.shiro.authc.UnknownAccountException;
21
import org.apache.shiro.authc.UsernamePasswordToken;
22
import org.apache.shiro.authz.AuthorizationException;
23
import org.apache.shiro.authz.AuthorizationInfo;
24
import org.apache.shiro.authz.SimpleAuthorizationInfo;
25
import org.apache.shiro.realm.AuthorizingRealm;
26
import org.apache.shiro.subject.PrincipalCollection;
27
import org.apache.thrift.TException;
28
 
29
/**
30
 * @author mandeep
31
 * 
32
 * This class is realm for fetching authentication and authorization details for an agent.
33
 */
34
public class CRMRealm extends AuthorizingRealm {
35
    private static final Log log = LogFactory.getLog(CRMRealm.class);
36
 
37
    @Override
38
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
39
        //null usernames are invalid
40
        if (principals == null) {
41
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
42
        }
43
 
44
        String username = (String) getAvailablePrincipal(principals);
45
        List<String> roleNames = null;
46
        Set<String> permissions = null;
47
 
48
        try {
3128 rajveer 49
            Client crmServiceClient = new CRMClient().getClient();
3090 mandeep.dh 50
 
51
            // Retrieve roles and permissions from database
52
            roleNames = crmServiceClient.getRoleNamesForAgent(username);
53
 
54
//            // XXX - we might not need permissions!
55
//            if (permissionsLookupEnabled) {
56
//                permissions = crmServiceClient.getPermissionsForRoleName(roleNames.get(0));
57
//            }
58
        } catch (TException e) {
59
            throw new AuthorizationException("Error fetching roles' information", e);
60
        } catch (Exception e) {
61
            throw new AuthorizationException("Error creating CRM client", e);
62
        }
63
 
64
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(new HashSet<String>(roleNames));
65
        info.setStringPermissions(permissions);
66
        return info;
67
    }
68
 
69
    @Override
70
    protected AuthenticationInfo doGetAuthenticationInfo(
71
            AuthenticationToken token) throws AuthenticationException
72
    {
73
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
74
        String username = upToken.getUsername();
75
        SimpleAuthenticationInfo info = null;
76
 
77
        log.info("Trying to fetch passowrd for " + username);
78
        try {
3128 rajveer 79
            Client crmServiceClient = new CRMClient().getClient();
3090 mandeep.dh 80
            Agent agent = crmServiceClient.getAgentByEmailId(username);
81
 
82
            if (agent != null) {
83
                info = new SimpleAuthenticationInfo(username, agent.getPassword().toCharArray(), getName());
84
            }
85
            else {
86
                throw new UnknownAccountException("No account found for user [" + username + "]");
87
            }
88
        }
89
        catch (Exception e) {
90
            String error = "Error while creating CRM client";
91
            log.error(error, e);
92
            throw new AuthenticationException(error, e);
93
        }
94
 
95
        return info;
96
    }
97
}