List of usage examples for javax.naming.directory DirContext createSubcontext
public DirContext createSubcontext(String name, Attributes attrs) throws NamingException;
From source file:org.apache.karaf.jaas.modules.ldap.LdapCacheTest.java
@Test public void testAdminLogin() throws Exception { Properties options = ldapLoginModuleOptions(); LDAPLoginModule module = new LDAPLoginModule(); CallbackHandler cb = new CallbackHandler() { public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { for (Callback cb : callbacks) { if (cb instanceof NameCallback) { ((NameCallback) cb).setName("admin"); } else if (cb instanceof PasswordCallback) { ((PasswordCallback) cb).setPassword("admin123".toCharArray()); }/*from w w w. j a v a 2s . c om*/ } } }; Subject subject = new Subject(); module.initialize(subject, cb, null, options); assertEquals("Precondition", 0, subject.getPrincipals().size()); assertTrue(module.login()); assertTrue(module.commit()); assertEquals(2, subject.getPrincipals().size()); boolean foundUser = false; boolean foundRole = false; for (Principal pr : subject.getPrincipals()) { if (pr instanceof UserPrincipal) { assertEquals("admin", pr.getName()); foundUser = true; } else if (pr instanceof RolePrincipal) { assertEquals("admin", pr.getName()); foundRole = true; } } assertTrue(foundUser); assertTrue(foundRole); assertTrue(module.logout()); assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size()); DirContext context = new LDAPCache(new LDAPOptions(options)).open(); // Make "admin" user a member of a new "another" group // dn: cn=admin,ou=groups,dc=example,dc=com // objectClass: top // objectClass: groupOfNames // cn: admin // member: cn=admin,ou=people,dc=example,dc=com Attributes entry = new BasicAttributes(); entry.put(new BasicAttribute("cn", "another")); Attribute oc = new BasicAttribute("objectClass"); oc.add("top"); oc.add("groupOfNames"); entry.put(oc); Attribute mb = new BasicAttribute("member"); mb.add("cn=admin,ou=people,dc=example,dc=com"); entry.put(mb); context.createSubcontext("cn=another,ou=groups,dc=example,dc=com", entry); Thread.sleep(100); module = new LDAPLoginModule(); subject = new Subject(); module.initialize(subject, cb, null, options); assertEquals("Precondition", 0, subject.getPrincipals().size()); assertTrue(module.login()); assertTrue(module.commit()); assertEquals("Postcondition", 3, subject.getPrincipals().size()); }
From source file:org.apache.archiva.redback.common.ldap.role.TestLdapRoleMapper.java
private void bindUserObject(DirContext context, String cn, String dn) throws Exception { Attributes attributes = new BasicAttributes(true); BasicAttribute objectClass = new BasicAttribute("objectClass"); objectClass.add("top"); objectClass.add("inetOrgPerson"); objectClass.add("person"); objectClass.add("organizationalperson"); attributes.put(objectClass);/* ww w . j ava 2 s .c om*/ attributes.put("cn", cn); attributes.put("sn", "foo"); attributes.put("mail", cn + "@apache.org"); attributes.put("userPassword", passwordEncoder.encodePassword("foo")); attributes.put("givenName", "foo"); context.createSubcontext(dn, attributes); }
From source file:org.apache.archiva.redback.rbac.ldap.LdapRbacManagerTest.java
private void createGroup(DirContext context, String groupName, String dn, List<String> users) throws Exception { Attributes attributes = new BasicAttributes(true); BasicAttribute objectClass = new BasicAttribute("objectClass"); objectClass.add("top"); objectClass.add("groupOfUniqueNames"); attributes.put(objectClass);/*from w ww . j av a 2 s . c o m*/ attributes.put("cn", groupName); if (!users.isEmpty()) { BasicAttribute basicAttribute = new BasicAttribute("uniquemember"); for (String user : users) { basicAttribute.add("uid=" + user + "," + suffix);// dc=archiva,dc=apache,dc=org" ); } attributes.put(basicAttribute); } context.createSubcontext(dn, attributes); }
From source file:org.apache.archiva.redback.common.ldap.role.TestLdapRoleMapper.java
private void createGroup(DirContext context, String groupName, String dn, List<String> users) throws Exception { Attributes attributes = new BasicAttributes(true); BasicAttribute objectClass = new BasicAttribute("objectClass"); objectClass.add("top"); objectClass.add("groupOfUniqueNames"); attributes.put(objectClass);/*from w w w. ja va 2s .c o m*/ attributes.put("cn", groupName); BasicAttribute basicAttribute = new BasicAttribute("uniquemember"); for (String user : users) { basicAttribute.add("uid=" + user + "," + suffix);// dc=archiva,dc=apache,dc=org" ); } attributes.put(basicAttribute); context.createSubcontext(dn, attributes); }
From source file:org.wso2.carbon.connector.ldap.AddEntry.java
@Override public void connect(MessageContext messageContext) throws ConnectException { String objectClass = (String) getParameter(messageContext, LDAPConstants.OBJECT_CLASS); String attributesString = (String) getParameter(messageContext, LDAPConstants.ATTRIBUTES); String dn = (String) getParameter(messageContext, LDAPConstants.DN); OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace(LDAPConstants.CONNECTOR_NAMESPACE, LDAPConstants.NAMESPACE); OMElement result = factory.createOMElement(LDAPConstants.RESULT, ns); OMElement message = factory.createOMElement(LDAPConstants.MESSAGE, ns); try {// w ww .j a v a2 s.com DirContext context = LDAPUtils.getDirectoryContext(messageContext); String classes[] = objectClass.split(","); Attributes entry = new BasicAttributes(); Attribute obClassAttr = new BasicAttribute(LDAPConstants.OBJECT_CLASS); for (int i = 0; i < classes.length; i++) { obClassAttr.add(classes[i]); } entry.put(obClassAttr); if (StringUtils.isNotEmpty(attributesString)) { JSONObject object = new JSONObject(attributesString); Iterator keys = object.keys(); while (keys.hasNext()) { String key = (String) keys.next(); String val = object.getString(key); Attribute newAttr = new BasicAttribute(key); newAttr.add(val); entry.put(newAttr); } } try { context.createSubcontext(dn, entry); message.setText(LDAPConstants.SUCCESS); result.addChild(message); LDAPUtils.preparePayload(messageContext, result); } catch (NamingException e) { log.error("Failed to create ldap entry with dn = " + dn, e); LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.ADD_ENTRY_ERROR, e); throw new SynapseException(e); } } catch (NamingException e) { LDAPUtils.handleErrorResponse(messageContext, LDAPConstants.ErrorConstants.INVALID_LDAP_CREDENTIALS, e); throw new SynapseException(e); } catch (JSONException e) { handleException("Error while passing the JSON object", e, messageContext); } }
From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java
protected void updateSuffix(DirContext dirContext) { String ldapSuffix = getLDAPSuffix(); NamingEnumeration<SearchResult> namingEnum = null; try {//from w ww . j a v a 2 s . com try { String dn = ldapSuffix; Attributes attributes = new BasicAttributes(); Attribute objclass = new BasicAttribute("objectclass"); objclass.add("organization"); objclass.add("dcObject"); attributes.put(objclass); attributes.put("dc", "tolven"); attributes.put("o", "myOrg"); dirContext.createSubcontext(dn, attributes); logger.info("Executed a createSubContext LDAP schema for " + ldapSuffix); } catch (NamingException ex) { //For some reason the search can fail, when the suffix is available, and when not available // The only certainty is to attempt to create it for now } } finally { if (namingEnum != null) { try { namingEnum.close(); } catch (NamingException ex) { throw new RuntimeException("Could not close the naming enumeration for the ldap suffix schema", ex); } } } }
From source file:gov.medicaid.dao.impl.LDAPIdentityProviderDAOBean.java
/** * Asks the identity provider to provision a new user with the given profile and password. * * @param user the user to be provisioned * @param password the password for the user * @throws PortalServiceException for any errors encountered */// w w w .j av a 2 s . c om public void provisionUser(CMSUser user, String password) throws PortalServiceException { DirContext ctx = null; try { ctx = new InitialDirContext(env); List<Attribute> profileAttributes = mapAttributes(user); // set type Attribute oc = new BasicAttribute("objectClass"); oc.add("top"); oc.add("person"); oc.add("organizationalPerson"); oc.add("inetOrgPerson"); // build the entry BasicAttributes entry = new BasicAttributes(); for (Attribute attribute : profileAttributes) { entry.put(attribute); } // initial password entry.put(new BasicAttribute("userPassword", hash(password))); entry.put(oc); ctx.createSubcontext(MessageFormat.format(userDNPattern, user.getUsername()), entry); synchRoles(user.getUsername(), user.getRole()); } catch (NamingException e) { throw new PortalServiceException("Error while provisioning user.", e); } finally { closeContext(ctx); } }
From source file:org.wso2.carbon.connector.integration.test.ldap.LdapConnectorIntegrationTest.java
public void createSampleEntity() throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, providerUrl); env.put(Context.SECURITY_PRINCIPAL, securityPrincipal); env.put(Context.SECURITY_CREDENTIALS, securityCredentials); DirContext ctx = new InitialDirContext(env); Attributes entry = new BasicAttributes(); Attribute obClassAttr = new BasicAttribute("objectClass"); obClassAttr.add("inetOrgPerson"); entry.put(obClassAttr);/* w w w.j a va 2 s . c o m*/ Attribute mailAttr = new BasicAttribute("mail"); mailAttr.add(testUserId + "@wso2.com"); entry.put(mailAttr); Attribute passAttr = new BasicAttribute("userPassword"); passAttr.add("12345"); entry.put(passAttr); Attribute snAttr = new BasicAttribute("sn"); snAttr.add("dim"); entry.put(snAttr); Attribute cnAttr = new BasicAttribute("cn"); cnAttr.add("dim"); entry.put(cnAttr); String dn = "uid=" + testUserId + "," + userBase; ctx.createSubcontext(dn, entry); }
From source file:org.apache.archiva.redback.users.ldap.ctl.DefaultLdapController.java
private void bindUserObject(DirContext context, User user) throws NamingException { Attributes attributes = new BasicAttributes(true); BasicAttribute objectClass = new BasicAttribute("objectClass"); objectClass.add("top"); objectClass.add("inetOrgPerson"); objectClass.add("person"); objectClass.add("organizationalperson"); attributes.put(objectClass);//from w w w.ja v a 2 s. c om attributes.put("cn", user.getUsername()); attributes.put("sn", "foo"); if (StringUtils.isNotEmpty(user.getEmail())) { attributes.put("mail", user.getEmail()); } if (userConf.getBoolean(UserConfigurationKeys.LDAP_BIND_AUTHENTICATOR_ALLOW_EMPTY_PASSWORDS, false) && StringUtils.isNotEmpty(user.getPassword())) { attributes.put("userPassword", passwordEncoder.encodePassword(user.getPassword())); } attributes.put("givenName", "foo"); context.createSubcontext("cn=" + user.getUsername() + "," + this.getBaseDn(), attributes); }
From source file:CreateCorbaSchema.java
/** * Inserts object class definitions from RFC 2714 into the schema. * * This method maps the LDAP schema definitions in RFC 2714 onto the * proprietary attributes required by the Active Directory schema. * * The resulting object class definitions differ from those of RFC 2714 * in the following ways:/* ww w .j a va2s. co m*/ * * - Abstract and auxiliary classes are now defined as structural. * - The corbaObject class now inherits from corbaContainer. * - The corbaObjectReference class now inherits from corbaObject. * * The effect of these differences is that CORBA object references * cannot be mixed-in with other directory entries, they may only be * stored as stand-alone entries. * * The reason for these differences is due to the way auxiliary classes * are supported in Active Directory. Only the names of structural * classes (not auxiliary) may appear in the object class attribute of * an entry. Therefore, the abstract and auxiliary classes in the CORBA * schema definition is re-defined as structural. */ protected void insertADObjectClasses(DirContext rootCtx, DirContext schemaCtx) throws NamingException { System.out.println(" [inserting new object class definitions ...]"); String dn = schemaCtx.getNameInNamespace(); String attrID; attrID = new String("corbaContainer"); Attributes attrs1 = new BasicAttributes(); attrs1.put(new BasicAttribute("cn", attrID)); attrs1.put(new BasicAttribute("objectClass", "classSchema")); attrs1.put(new BasicAttribute("defaultHidingValue", "FALSE")); attrs1.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.10")); attrs1.put(new BasicAttribute("lDAPDisplayName", attrID)); attrs1.put(new BasicAttribute("mustContain", "cn")); attrs1.put(new BasicAttribute("objectClassCategory", "1")); attrs1.put(new BasicAttribute("systemOnly", "FALSE")); attrs1.put(new BasicAttribute("subclassOf", "top")); attrs1.put(new BasicAttribute("possSuperiors", "top")); //any superior attrs1.put(new BasicAttribute("description", "Container for a CORBA object")); schemaCtx.createSubcontext("cn=" + attrID, attrs1); System.out.println(" [" + attrID + "]"); flushADSchemaMods(rootCtx); // corbaObject relys on corbaContainer attrID = new String("corbaObject"); Attributes attrs2 = new BasicAttributes(); attrs2.put(new BasicAttribute("cn", attrID)); attrs2.put(new BasicAttribute("objectClass", "classSchema")); attrs2.put(new BasicAttribute("defaultHidingValue", "FALSE")); attrs2.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.9")); attrs2.put(new BasicAttribute("lDAPDisplayName", attrID)); Attribute coMay = new BasicAttribute("mayContain"); coMay.add("corbaRepositoryId"); coMay.add("description"); attrs2.put(coMay); attrs2.put(new BasicAttribute("objectClassCategory", "1")); attrs2.put(new BasicAttribute("systemOnly", "FALSE")); attrs2.put(new BasicAttribute("subclassOf", "corbaContainer")); attrs2.put(new BasicAttribute("description", "CORBA object representation")); schemaCtx.createSubcontext("cn=" + attrID, attrs2); System.out.println(" [" + attrID + "]"); flushADSchemaMods(rootCtx); // corbaObjectReference relys on corbaObject attrID = new String("corbaObjectReference"); Attributes attrs3 = new BasicAttributes(); attrs3.put(new BasicAttribute("cn", attrID)); attrs3.put(new BasicAttribute("objectClass", "classSchema")); attrs3.put(new BasicAttribute("defaultHidingValue", "FALSE")); attrs3.put(new BasicAttribute("governsID", "1.3.6.1.4.1.42.2.27.4.2.11")); attrs3.put(new BasicAttribute("lDAPDisplayName", attrID)); attrs3.put(new BasicAttribute("mustContain", "corbaIor")); attrs3.put(new BasicAttribute("objectClassCategory", "1")); attrs3.put(new BasicAttribute("systemOnly", "FALSE")); attrs3.put(new BasicAttribute("subclassOf", "corbaObject")); attrs3.put(new BasicAttribute("description", "CORBA interoperable object reference")); schemaCtx.createSubcontext("cn=" + attrID, attrs3); System.out.println(" [" + attrID + "]"); flushADSchemaMods(rootCtx); // finally }