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