public DirContextOperations authenticate(Authentication authentication) { DirContextAdapter ctx = new DirContextAdapter(); ctx.setAttributeValue("ou", "FROM_ENTRY"); String username = authentication.getName(); String password = (String) authentication.getCredentials(); if (username.equals("ben") && password.equals("benspassword")) { ctx.setDn(new DistinguishedName( "cn=ben,ou=people,dc=springframework,dc=org")); ctx.setAttributeValue("userPassword", "{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ="); return ctx; } else if (username.equals("jen") && password.equals("")) { ctx.setDn(new DistinguishedName( "cn=jen,ou=people,dc=springframework,dc=org")); return ctx; } throw new BadCredentialsException("Authentication failed."); } }
@Test public void mappingBackToContextMatchesOriginalData() { DirContextAdapter ctx1 = createUserContext(); DirContextAdapter ctx2 = new DirContextAdapter(); ctx1.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" }); ctx2.setDn(new DistinguishedName("ignored=ignored")); InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)) .createUserDetails(); p.populateContext(ctx2); assertThat(ctx2).isEqualTo(ctx1); }
@Test public void copyMatchesOriginalData() { DirContextAdapter ctx1 = createUserContext(); DirContextAdapter ctx2 = new DirContextAdapter(); ctx2.setDn(new DistinguishedName("ignored=ignored")); ctx1.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" }); InetOrgPerson p = (InetOrgPerson) (new InetOrgPerson.Essence(ctx1)) .createUserDetails(); InetOrgPerson p2 = (InetOrgPerson) new InetOrgPerson.Essence(p) .createUserDetails(); p2.populateContext(ctx2); assertThat(ctx2).isEqualTo(ctx1); }
private DirContextAdapter createUserContext() { DirContextAdapter ctx = new DirContextAdapter(); ctx.setDn(new DistinguishedName("ignored=ignored")); ctx.setAttributeValue("uid", "ghengis"); ctx.setAttributeValue("userPassword", "pillage"); ctx.setAttributeValue("carLicense", "HORS1"); ctx.setAttributeValue("cn", "Ghengis Khan"); ctx.setAttributeValue("description", "Scary"); ctx.setAttributeValue("destinationIndicator", "West"); ctx.setAttributeValue("displayName", "Ghengis McCann"); ctx.setAttributeValue("givenName", "Ghengis"); ctx.setAttributeValue("homePhone", "+467575436521"); ctx.setAttributeValue("initials", "G"); ctx.setAttributeValue("employeeNumber", "00001"); ctx.setAttributeValue("homePostalAddress", "Steppes"); ctx.setAttributeValue("mail", "ghengis@mongolia"); ctx.setAttributeValue("mobile", "always"); ctx.setAttributeValue("o", "Hordes"); ctx.setAttributeValue("ou", "Horde1"); ctx.setAttributeValue("postalAddress", "On the Move"); ctx.setAttributeValue("postalCode", "Changes Frequently"); ctx.setAttributeValue("roomNumber", "Yurt 1"); ctx.setAttributeValue("roomNumber", "Yurt 1"); ctx.setAttributeValue("sn", "Khan"); ctx.setAttributeValue("street", "Westward Avenue"); ctx.setAttributeValue("telephoneNumber", "+442075436521"); return ctx; }