Subversion Repositories SmartDukaan

Rev

Rev 3128 | Rev 3390 | Go to most recent revision | Details | Compare with Previous | 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
 */
3206 mandeep.dh 34
public class CRMAuthorizingRealm extends AuthorizingRealm {
35
    private static final Log log = LogFactory.getLog(CRMAuthorizingRealm.class);
3090 mandeep.dh 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;
3206 mandeep.dh 46
        Set<String> permissions = new HashSet<String>();
3090 mandeep.dh 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
 
3206 mandeep.dh 54
            for (String roleName : roleNames) {
55
                permissions.addAll(crmServiceClient.getPermissionsForRoleName(roleName));
56
            }
3090 mandeep.dh 57
        } catch (TException e) {
58
            throw new AuthorizationException("Error fetching roles' information", e);
59
        } catch (Exception e) {
60
            throw new AuthorizationException("Error creating CRM client", e);
61
        }
62
 
63
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(new HashSet<String>(roleNames));
64
        info.setStringPermissions(permissions);
65
        return info;
66
    }
67
 
68
    @Override
69
    protected AuthenticationInfo doGetAuthenticationInfo(
70
            AuthenticationToken token) throws AuthenticationException
71
    {
72
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
73
        String username = upToken.getUsername();
74
        SimpleAuthenticationInfo info = null;
75
 
76
        log.info("Trying to fetch passowrd for " + username);
77
        try {
3128 rajveer 78
            Client crmServiceClient = new CRMClient().getClient();
3090 mandeep.dh 79
            Agent agent = crmServiceClient.getAgentByEmailId(username);
80
 
81
            if (agent != null) {
82
                info = new SimpleAuthenticationInfo(username, agent.getPassword().toCharArray(), getName());
83
            }
84
            else {
85
                throw new UnknownAccountException("No account found for user [" + username + "]");
86
            }
87
        }
88
        catch (Exception e) {
89
            String error = "Error while creating CRM client";
90
            log.error(error, e);
91
            throw new AuthenticationException(error, e);
92
        }
93
 
94
        return info;
95
    }
96
}