Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7226 anupam.sin 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.auth;
5
 
6
import in.shop2020.thrift.clients.HelperClient;
7
import in.shop2020.utils.Agent;
8
import in.shop2020.utils.HelperService.Client;
9
 
10
import java.util.ArrayList;
11
import java.util.HashMap;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16
 
17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
19
import org.apache.shiro.authc.AuthenticationException;
20
import org.apache.shiro.authc.AuthenticationInfo;
21
import org.apache.shiro.authc.AuthenticationToken;
22
import org.apache.shiro.authc.SimpleAuthenticationInfo;
23
import org.apache.shiro.authc.UnknownAccountException;
24
import org.apache.shiro.authc.UsernamePasswordToken;
25
import org.apache.shiro.authz.AuthorizationException;
26
import org.apache.shiro.authz.AuthorizationInfo;
27
import org.apache.shiro.authz.SimpleAuthorizationInfo;
28
import org.apache.shiro.realm.AuthorizingRealm;
29
import org.apache.shiro.subject.PrincipalCollection;
30
import org.apache.thrift.TException;
31
 
32
/**
33
 * @author mandeep
34
 * 
35
 *         This class is realm for fetching authentication and authorization
36
 *         details for an agent.
37
 */
38
public class InventoryAuthorizingRealm extends AuthorizingRealm {
39
    private static final Log   log = LogFactory.getLog(InventoryAuthorizingRealm.class);
40
    private static Map<Long, Agent>   agentsMapById;
41
    private static Map<String, Agent> agentsMapByEmailId;
42
 
43
    @Override
44
    protected AuthorizationInfo doGetAuthorizationInfo(
45
            PrincipalCollection principals) {
46
        // null usernames are invalid
47
        if (principals == null) {
48
            throw new AuthorizationException(
49
                    "PrincipalCollection method argument cannot be null.");
50
        }
51
 
52
        String username = (String) getAvailablePrincipal(principals);
53
        List<String> roleNames = null;
54
        Set<String> permissions = new HashSet<String>();
55
 
56
        try {
57
            Client client = new HelperClient().getClient();
58
 
59
            // Retrieve roles and permissions from database
60
            roleNames = client.getRoleNamesForAgent(username);
61
 
62
            for (String roleName : roleNames) {
63
                permissions.addAll(client
64
                        .getPermissionsForRoleName(roleName));
65
            }
66
        } catch (TException e) {
67
            throw new AuthorizationException(
68
                    "Error fetching roles' information", e);
69
        } catch (Exception e) {
70
            throw new AuthorizationException("Error creating Helper client", e);
71
        }
72
 
73
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(
74
                new HashSet<String>(roleNames));
75
        info.setStringPermissions(permissions);
76
        return info;
77
    }
78
 
79
    @Override
80
    protected AuthenticationInfo doGetAuthenticationInfo(
81
            AuthenticationToken token) throws AuthenticationException {
82
        SimpleAuthenticationInfo info = null;
83
 
84
        try {
85
            UsernamePasswordToken upToken = (UsernamePasswordToken) token;
86
            String username = upToken.getUsername();
87
 
88
            log.info("Trying to fetch password for " + username);
89
            Agent agent = getAgent(username);
90
            if (agent != null) {
91
                info = new SimpleAuthenticationInfo(username, agent.getPassword().toCharArray(), getName());
92
            } else {
93
                throw new UnknownAccountException("No account found for user ["
94
                        + username + "]");
95
            }
96
        } catch (TException e) {
97
            log.info("Could not create Helper client", e);
98
        }
99
 
100
        return info;
101
    }
102
 
103
    public static Agent getAgent(String username) throws TException {
104
        if (agentsMapByEmailId == null || !agentsMapByEmailId.containsKey(username)) {
105
            loadAgents();
106
        }
107
 
108
        return agentsMapByEmailId.get(username);
109
    }
110
 
111
    public static Agent getAgent(long agentId) throws TException {
112
        if (agentsMapById == null || !agentsMapById.containsKey(agentId)) {
113
            loadAgents();
114
        }
115
 
116
        return agentsMapById.get(agentId);
117
    }
118
 
119
    private static void loadAgents() throws TException {
120
        Client client = new HelperClient().getClient();
121
        List<in.shop2020.utils.Agent> agents = client.getAgents();
122
        Map<Long, Agent> agentsMapByIdLocal = new HashMap<Long, Agent>();
123
        Map<String, Agent> agentsMapByEmailIdLocal = new HashMap<String, Agent>();
124
 
125
        for (Agent agent : agents) {
126
            agentsMapByIdLocal.put(agent.getId(), agent);
127
            agentsMapByEmailIdLocal.put(agent.getEmailId(), agent);
128
        }
129
 
130
        synchronized(InventoryAuthorizingRealm.class) {
131
            agentsMapById = agentsMapByIdLocal;
132
            agentsMapByEmailId = agentsMapByEmailIdLocal;
133
        }
134
    }
135
 
136
    public static List<Agent> getAgents() {
137
        return new ArrayList<Agent>(agentsMapById.values());
138
    }
139
}