1 5 6 package org.exoplatform.services.security.test; 7 8 9 import javax.security.auth.Subject ; 10 import javax.security.auth.callback.*; 11 import javax.security.auth.login.LoginContext ; 12 import org.exoplatform.container.PortalContainer; 13 import org.exoplatform.container.RootContainer; 14 import org.exoplatform.services.log.LogService; 15 import org.exoplatform.services.organization.OrganizationService; 16 import org.exoplatform.services.organization.User; 17 import org.exoplatform.services.security.SecurityService; 18 import org.exoplatform.services.security.UserPrincipal; 19 import org.exoplatform.test.BasicTestCase; 20 import java.io.IOException ; 21 import java.security.Principal ; 22 import java.security.acl.Group ; 23 import java.util.Enumeration ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import org.exoplatform.services.security.impl.mock.MockCallbackHandler; 27 import java.io.File ; 28 29 34 public class TestLoginModule extends BasicTestCase { 35 protected static SecurityService service_; 36 protected static OrganizationService organizationService; 37 38 public TestLoginModule(String name) { 39 super(name); 40 } 41 42 protected String getDescription() { 43 return "test JAAS Login module"; 44 } 45 46 protected void setUp() throws Exception { 47 if (service_ == null) { 48 System.setProperty("java.security.auth.login.config", "src/resource/login.conf" ); 49 RootContainer rootContainer = RootContainer.getInstance(); 50 PortalContainer manager = rootContainer.getPortalContainer("default"); 51 PortalContainer.setInstance(manager) ; 52 service_ = (SecurityService) manager.getComponentInstanceOfType(SecurityService.class); 53 organizationService = 54 (OrganizationService) manager.getComponentInstanceOfType(OrganizationService.class); 55 User user = organizationService.createUserInstance(); 56 user.setUserName("login"); 57 user.setUserName("login"); 58 user.setPassword("password"); 59 organizationService.createUser(user); 60 ((LogService) manager.getComponentInstanceOfType(LogService.class)). 61 setLogLevel("org.exoplatform.services.security", LogService.DEBUG, false); 62 } 63 } 64 65 public void testLogin() throws Exception { 66 MockCallbackHandler handler = new MockCallbackHandler("login", "password@default"); 67 LoginContext loginContext = new LoginContext ("eXo", handler); 68 loginContext.login(); 69 assertNotNull(loginContext.getSubject()); 70 Subject subject = service_.getSubject("login"); 71 assertNotNull(loginContext.getSubject()); 72 assertSame(loginContext.getSubject(), subject); 73 Set principals = subject.getPrincipals(UserPrincipal.class); 74 UserPrincipal userPrincipal = ((UserPrincipal) principals.iterator().next()); 75 assertEquals("login", userPrincipal.getName()); 76 Iterator p = subject.getPrincipals(Group .class).iterator(); 77 if (p.hasNext()) { 78 Group roles = (Group ) p.next(); 79 Enumeration roleEnum = roles.members(); 80 Principal princ = (Principal) roleEnum.nextElement(); 81 assertTrue(roles.isMember(princ)); 83 } 84 loginContext.logout(); 85 assertNull(service_.getSubject("login")); 86 } 87 } 88 | Popular Tags |