Rev 3128 | Rev 3390 | 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.CRMClient;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 CRMAuthorizingRealm extends AuthorizingRealm {private static final Log log = LogFactory.getLog(CRMAuthorizingRealm.class);@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {//null usernames are invalidif (principals == null) {throw new AuthorizationException("PrincipalCollection method argument cannot be null.");}String username = (String) getAvailablePrincipal(principals);List<String> roleNames = null;Set<String> permissions = new HashSet<String>();try {Client crmServiceClient = new CRMClient().getClient();// Retrieve roles and permissions from databaseroleNames = crmServiceClient.getRoleNamesForAgent(username);for (String roleName : roleNames) {permissions.addAll(crmServiceClient.getPermissionsForRoleName(roleName));}} 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;}@Overrideprotected 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 CRMClient().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;}}