1 26 27 package org.objectweb.jonas.security.auth.spi; 28 29 import java.security.Principal ; 30 import java.security.acl.Group ; 31 import java.util.ArrayList ; 32 import java.util.Enumeration ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 import javax.security.auth.Subject ; 38 import javax.security.auth.callback.CallbackHandler ; 39 import javax.security.auth.login.LoginException ; 40 import javax.security.auth.spi.LoginModule ; 41 42 import org.objectweb.security.context.SecurityContext; 43 import org.objectweb.security.context.SecurityCurrent; 44 45 46 51 public class ClientLoginModule implements LoginModule { 52 53 56 private Subject subject = null; 57 58 61 private Map options = null; 62 63 66 private String principalName = null; 67 68 71 private ArrayList principalRoles = null; 72 73 76 private boolean globalContext = false; 77 78 86 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) { 87 this.subject = subject; 88 this.options = options; 89 principalRoles = new ArrayList (); 90 } 91 92 93 99 public boolean login() throws LoginException { 100 String useGlobalCtx = (String ) options.get("globalCtx"); 102 if ((useGlobalCtx != null) && (Boolean.valueOf(useGlobalCtx).booleanValue())) { 103 globalContext = true; 104 } 105 return true; 106 } 107 108 109 116 public boolean commit() throws LoginException { 117 118 Set principals = subject.getPrincipals(Principal .class); 120 Iterator iterator = principals.iterator(); 121 while (iterator.hasNext()) { 122 Principal principal = (Principal ) iterator.next(); 123 if (!(principal instanceof Group )) { 124 principalName = principal.getName(); 125 } 126 } 127 128 if (principalName == null) { 130 throw new LoginException ("There was no previous login module. This login module can only be used in addition to another module which perform the authentication."); 131 } 132 133 principals = subject.getPrincipals(Group .class); 135 iterator = principals.iterator(); 136 while (iterator.hasNext()) { 137 Group group = (Group ) iterator.next(); 138 Enumeration e = group.members(); 139 while (e.hasMoreElements()) { 140 Principal p = (Principal ) e.nextElement(); 141 principalRoles.add(p.getName()); 142 } 143 } 144 145 SecurityContext ctx = new SecurityContext(principalName, principalRoles); 147 SecurityCurrent current = SecurityCurrent.getCurrent(); 148 if (globalContext) { 149 current.setGlobalSecurityContext(ctx); 150 } else { 151 current.setSecurityContext(ctx); 152 } 153 154 return true; 155 } 156 157 158 165 public boolean abort() throws LoginException { 166 167 return true; 169 } 170 171 177 public boolean logout() throws LoginException { 178 179 SecurityContext ctx = new SecurityContext(); 181 SecurityCurrent current = SecurityCurrent.getCurrent(); 182 current.setSecurityContext(ctx); 183 184 return true; 185 186 } 187 188 } 189 | Popular Tags |