1 17 18 package org.apache.geronimo.security.bridge; 19 20 import java.security.Principal ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import javax.security.auth.Subject ; 25 26 import org.apache.geronimo.security.ContextManager; 27 import org.apache.geronimo.security.IdentificationPrincipal; 28 29 30 33 public class MappingUserPasswordBridgeTest extends AbstractBridgeTest { 34 private static final String SOURCE_USER_1 = "sourceUser1"; 35 private static final String SOURCE_USER_2 = "sourceUser2"; 36 private static final String SOURCE_PRINCIPAL_1 = "sourcePrincipal1"; 37 private static final String SOURCE_PRINCIPAL_2 = "sourcePrincipal2"; 38 private static final String SOURCE_PASSWORD_1 = "sourcePassword1"; 39 private static final String SOURCE_PASSWORD_2 = "sourcePassword2"; 40 41 private TestMappingBridge bridge; 42 43 protected void setUp() throws Exception { 44 super.setUp(); 45 bridge = new TestMappingBridge(); 46 bridge.setTargetRealm(TestLoginModule.JAAS_NAME); 47 bridge.setPrincipalSourceType(TestPrincipalPrincipal.class); 48 bridge.setPrincipalTargetCallbackName("Resource Principal"); 49 Map principalMap = new HashMap (); 50 principalMap.put(SOURCE_PRINCIPAL_1, AbstractBridgeTest.USER); 51 principalMap.put(SOURCE_PRINCIPAL_2, "no-one"); 52 bridge.setPrincipalMap(principalMap); 53 bridge.setUserNameSourceType(TestUserNamePrincipal.class); 54 bridge.setUserNameTargetCallbackName("User Name"); 55 Map userNameMap = new HashMap (); 56 userNameMap.put(SOURCE_USER_1, AbstractBridgeTest.USER); 57 userNameMap.put(SOURCE_USER_2, "no-one"); 58 bridge.setUserNameMap(userNameMap); 59 bridge.setPasswordSourceType(TestPasswordPrincipal.class); 60 Map passwordMap = new HashMap (); 61 passwordMap.put(SOURCE_PASSWORD_1, AbstractBridgeTest.PASSWORD.toCharArray()); 62 passwordMap.put(SOURCE_PASSWORD_2, "no-password".toCharArray()); 63 bridge.setPasswordMap(passwordMap); 64 } 65 66 public void testMapping() throws Exception { 67 Subject subject = new Subject (); 68 subject.getPrincipals().add(new TestPrincipalPrincipal(SOURCE_PRINCIPAL_1)); 69 subject.getPrincipals().add(new TestUserNamePrincipal(SOURCE_USER_1)); 70 subject.getPrincipals().add(new TestPasswordPrincipal(SOURCE_PASSWORD_1)); 71 Subject targetSubject = bridge.mapSubject(subject); 72 assertTrue("expected non-null client subject", targetSubject != null); 73 Set set = targetSubject.getPrincipals(IdentificationPrincipal.class); 74 assertEquals("client subject should have one ID principal", set.size(), 1); 75 IdentificationPrincipal idp = (IdentificationPrincipal)set.iterator().next(); 76 targetSubject = ContextManager.getRegisteredSubject(idp.getId()); 77 checkValidSubject(targetSubject); 78 } 79 80 public void testInsufficientSourcePrincipals() throws Exception { 81 Subject subject = new Subject (); 82 subject.getPrincipals().add(new TestPrincipalPrincipal(SOURCE_PRINCIPAL_1)); 83 subject.getPrincipals().add(new TestPasswordPrincipal(SOURCE_PASSWORD_1)); 84 try { 85 bridge.mapSubject(subject); 86 fail(); 87 } catch (Throwable e) { 88 } 89 } 90 91 public void testNotInMap() throws Exception { 92 Subject subject = new Subject (); 93 subject.getPrincipals().add(new TestPrincipalPrincipal(SOURCE_PRINCIPAL_1 + "xxx")); 94 subject.getPrincipals().add(new TestUserNamePrincipal(SOURCE_USER_1)); 95 subject.getPrincipals().add(new TestPasswordPrincipal(SOURCE_PASSWORD_1)); 96 try { 97 bridge.mapSubject(subject); 98 fail(); 99 } catch (Throwable e) { 100 } 101 } 102 103 public static class TestPrincipalPrincipal implements Principal { 104 private String name; 105 106 public TestPrincipalPrincipal(String name) { 107 this.name = name; 108 } 109 110 public String getName() { 111 return name; 112 } 113 } 114 115 public static class TestUserNamePrincipal implements Principal { 116 private String name; 117 118 public TestUserNamePrincipal(String name) { 119 this.name = name; 120 } 121 122 public String getName() { 123 return name; 124 } 125 } 126 127 public static class TestPasswordPrincipal implements Principal { 128 private String name; 129 130 public TestPasswordPrincipal(String name) { 131 this.name = name; 132 } 133 134 public String getName() { 135 return name; 136 } 137 } 138 139 public static class TestMappingBridge extends AbstractPrincipalMappingUserPasswordRealmBridge { 140 141 public void setPrincipalMap(Map principalMap) { 142 this.principalMap.clear(); 143 this.principalMap.putAll(principalMap); 144 } 145 146 public void setUserNameMap(Map userNameMap) { 147 this.userNameMap.clear(); 148 this.userNameMap.putAll(userNameMap); 149 } 150 151 public void setPasswordMap(Map passwordMap) { 152 this.passwordMap.clear(); 153 this.passwordMap.putAll(passwordMap); 154 } 155 156 } 157 } 158 | Popular Tags |