1 25 package org.objectweb.jonas.security.realm.factory; 26 27 import java.rmi.AccessException ; 28 import java.rmi.RemoteException ; 29 import java.util.ArrayList ; 30 31 import javax.rmi.PortableRemoteObject ; 32 33 import org.objectweb.jonas.security.SecurityService; 34 import org.objectweb.jonas.security.auth.JGroup; 35 import org.objectweb.jonas.security.auth.JPrincipal; 36 import org.objectweb.jonas.security.auth.JRole; 37 import org.objectweb.jonas.security.auth.JSubject; 38 import org.objectweb.jonas.security.realm.principals.User; 39 import org.objectweb.jonas.service.ServiceManager; 40 41 46 public class JResourceRemoteImpl extends PortableRemoteObject implements JResourceRemote { 47 48 51 private static SecurityService securityService = null; 52 53 57 public JResourceRemoteImpl() throws RemoteException { 58 super(); 59 } 60 61 70 public JSubject authenticate(String principalName, char[] arrayPass, String resourceName) throws RemoteException { 71 if (securityService == null) { 73 try { 74 securityService = (SecurityService) ServiceManager.getInstance().getSecurityService(); 75 } catch (Exception e) { 76 throw createChainedAccessException( 77 "Cannot retrieve security service. Check that the security service is running", e); 78 } catch (Error e) { 79 throw createChainedAccessException( 80 "Cannot retrieve security service. Check that the security service is running", e); 81 } 82 } 83 84 if (resourceName == null) { 85 throw new AccessException ("The 'resourceName' parameter is required and cannot be null."); 86 } 87 88 JResource jResource = null; 90 try { 91 jResource = securityService.getJResource(resourceName); 92 } catch (Exception e) { 93 throw createChainedAccessException("The resource '" + resourceName + "' is not available.", e); 94 } 95 96 if (jResource == null) { 97 throw new AccessException ("The resource '" + resourceName + "' is not available."); 98 } 99 100 User user = null; 102 try { 103 user = jResource.findUser(principalName); 104 } catch (Exception jre) { 105 throw createChainedAccessException("Can not find the user", jre); 107 } 108 if (user == null) { 110 throw new AccessException ("User '" + principalName + "' not found."); 111 } 112 113 boolean validated = jResource.isValidUser(user, new String (arrayPass)); 115 if (!validated) { 116 throw new AccessException ("The password for the user '" + principalName + "' is not valid"); 117 } 118 119 ArrayList principalRoles = null; 121 try { 122 principalRoles = jResource.getArrayListCombinedRoles(user); 123 } catch (JResourceException jre) { 124 throw createChainedAccessException(jre.getMessage(), jre); 125 } 126 127 JGroup group = new JGroup("Roles"); 128 129 String [] roles = new String [principalRoles.size()]; 131 roles = (String []) principalRoles.toArray(roles); 132 int size = principalRoles.size(); 133 for (int i = 0; i < size; i++) { 134 group.addMember(new JRole(roles[i])); 135 } 136 137 return new JSubject(new JPrincipal(principalName), group); 139 140 } 141 142 149 private static AccessException createChainedAccessException(String msg, Throwable t) { 150 AccessException ae = new AccessException (msg); 151 ae.initCause(t); 152 return ae; 153 } 154 155 } 156 | Popular Tags |