@Override public void clearCredentials() throws RealmUnavailableException { try { Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute(algorithmAttributeName)); attributes.put(new BasicAttribute(hashAttributeName)); attributes.put(new BasicAttribute(seedAttributeName)); attributes.put(new BasicAttribute(sequenceAttributeName)); context.modifyAttributes(distinguishedName, DirContext.REMOVE_ATTRIBUTE, attributes); } catch (NoSuchAttributeException e) { // ignore if already clear } catch (NamingException e) { throw log.ldapRealmCredentialClearingFailed(distinguishedName, e); } } }
private Attributes buildAttributes( String dn, String[] attributes, String[] values, String multValuedSeparator ) { Attributes attrs = new javax.naming.directory.BasicAttributes( true ); int nrAttributes = attributes.length; for ( int i = 0; i < nrAttributes; i++ ) { if ( !Utils.isEmpty( values[i] ) ) { // We have a value String value = values[i].trim(); if ( multValuedSeparator != null && value.indexOf( multValuedSeparator ) > 0 ) { Attribute attr = new javax.naming.directory.BasicAttribute( attributes[i] ); for ( String attribute : value.split( multValuedSeparator ) ) { attr.add( attribute ); } attrs.put( attr ); } else { attrs.put( attributes[i], value ); } } } return attrs; }
@Override public void persistCredential(final Credential credential) throws RealmUnavailableException { OneTimePassword password = credential.castAndApply(PasswordCredential.class, c -> c.getPassword(OneTimePassword.class)); try { Attributes attributes = new BasicAttributes(); attributes.put(algorithmAttributeName, password.getAlgorithm()); attributes.put(hashAttributeName, ByteIterator.ofBytes(password.getHash()).base64Encode().drainToString()); attributes.put(seedAttributeName, password.getSeed()); attributes.put(sequenceAttributeName, Integer.toString(password.getSequenceNumber())); context.modifyAttributes(distinguishedName, DirContext.REPLACE_ATTRIBUTE, attributes); } catch (NamingException e) { throw log.ldapRealmCredentialPersistingFailed(credential.toString(), distinguishedName, e); } }
@Override public void clearCredentials() throws RealmUnavailableException { try { Attributes attributes = new BasicAttributes(); attributes.put(new BasicAttribute(userPasswordAttributeName)); context.modifyAttributes(distinguishedName, DirContext.REMOVE_ATTRIBUTE, attributes); } catch (NoSuchAttributeException e) { // ignore if already clear } catch (NamingException e) { throw log.ldapRealmCredentialClearingFailed(distinguishedName, e); } } }
@Override public void persistCredential(final Credential credential) throws RealmUnavailableException { // TODO - We probably need some better resolution here of the existing attributes - i.e. different types we would want to add, same type we would want to replace. try { byte[] composedPassword = UserPasswordPasswordUtil.composeUserPassword(credential.castAndApply(PasswordCredential.class, PasswordCredential::getPassword)); Assert.assertNotNull(composedPassword); Attributes attributes = new BasicAttributes(); attributes.put(userPasswordAttributeName, composedPassword); context.modifyAttributes(distinguishedName, DirContext.REPLACE_ATTRIBUTE, attributes); } catch (NamingException | IOException e) { throw log.ldapRealmCredentialPersistingFailed(credential.toString(), distinguishedName, e); } }
private static Attributes mockAttributes(NameValues... namedValues) throws NamingException { Attributes attributes = new BasicAttributes(); for (NameValues namedValue : namedValues) { Attribute attr = new BasicAttribute(namedValue.name); for (String value : namedValue.values) { attr.add(value); } attributes.put(attr); } return attributes; }
@Test public void testFamilyNameAttributeNameMapping() throws Exception { attrs.put("lastName", "Lastnamerton"); adapter = new DirContextAdapter(attrs, new LdapName("cn=marissa,ou=Users,dc=test,dc=com")); mapper.setFamilyNameAttributeName("lastName"); ExtendedLdapUserImpl ldapUserDetails = getExtendedLdapUser(); Assert.assertThat(ldapUserDetails.getFamilyName(), is("Lastnamerton")); }
@Test public void testGivenNameAttributeNameMapping() throws Exception { attrs.put("givenName", "Marissa"); adapter = new DirContextAdapter(attrs, new LdapName("cn=marissa,ou=Users,dc=test,dc=com")); mapper.setGivenNameAttributeName("givenName"); ExtendedLdapUserImpl ldapUserDetails = getExtendedLdapUser(); Assert.assertThat(ldapUserDetails.getGivenName(), is("Marissa")); }
@Test public void testPhoneNumberAttributeNameMapping() throws Exception { attrs.put("phoneNumber", "8675309"); adapter = new DirContextAdapter(attrs, new LdapName("cn=marissa,ou=Users,dc=test,dc=com")); mapper.setPhoneNumberAttributeName("phoneNumber"); ExtendedLdapUserImpl ldapUserDetails = getExtendedLdapUser(); Assert.assertThat(ldapUserDetails.getPhoneNumber(), is("8675309")); }
SearchResult searchResult = getSearchResult().next(); Attributes results = searchResult.getAttributes(); results.put( "dn", searchResult.getNameInNamespace() ); return results; } catch ( Exception e ) {
private void addConstantAttributes(String names, String values, Attributes attribs) { String[] arrNames = names.split(","); String[] arrValues = values.split(","); if (arrNames.length != arrValues.length) { throw new IllegalArgumentException( String.format("Inconsintent constant attributes: %s; %s", names, values)); } for (int i = 0; i < arrNames.length; i++) { attribs.put(new BasicAttribute(arrNames[i], arrValues[i])); } }
private Attributes storeTo(ConfigurationChanges.ModifiedObject ldapObj, String tsuid, ImageReaderParam param, Attributes attrs) { attrs.put("objectclass", "dcmImageReader"); attrs.put("dicomTransferSyntax", tsuid); attrs.put("dcmIIOFormatName", param.formatName); LdapUtils.storeNotNullOrDef(ldapObj, attrs, "dcmJavaClassName", param.className, null); LdapUtils.storeNotNullOrDef(ldapObj, attrs, "dcmPatchJPEGLS", param.patchJPEGLS, null); LdapUtils.storeNotEmpty(ldapObj, attrs, "dcmImageReadParam", param.getImageReadParams()); return attrs; }
public static Attributes attrs(String objectclass, String attrID, String attrVal) { Attributes attrs = new BasicAttributes(true); // case-ignore attrs.put("objectclass", objectclass); storeNotNullOrDef(attrs, attrID, attrVal, null); return attrs; }
public static void storeNotNullOrDef(ConfigurationChanges.ModifiedObject ldapObj, Attributes attrs, String attrID, TimeZone val, TimeZone defVal) { if (val != null && !val.equals(defVal)) { attrs.put(attrID, val.getID()); if (ldapObj != null) { ConfigurationChanges.ModifiedAttribute attribute = new ConfigurationChanges.ModifiedAttribute(attrID); attribute.addValue(val); ldapObj.add(attribute); } } }
public static Attribute storeInt(ConfigurationChanges.ModifiedObject ldapObj, Attributes attrs, String attrID, int val) { if (ldapObj != null) { ConfigurationChanges.ModifiedAttribute attribute = new ConfigurationChanges.ModifiedAttribute(attrID); attribute.addValue(val); ldapObj.add(attribute); } return attrs.put(attrID, Integer.toString(val)); }
public static <T> void storeNotNullOrDef(ConfigurationChanges.ModifiedObject ldapObj, Attributes attrs, String attrID, T val, T defVal) { if (val != null && !val.equals(defVal)) { attrs.put(attrID, LdapUtils.toString(val)); if (ldapObj != null) { ConfigurationChanges.ModifiedAttribute attribute = new ConfigurationChanges.ModifiedAttribute(attrID); attribute.addValue(val); ldapObj.add(attribute); } } }
public static void storeConnRefs(ConfigurationChanges.ModifiedObject ldapObj, Attributes attrs, Collection<Connection> conns, String deviceDN) { if (!conns.isEmpty()) { attrs.put(LdapUtils.connRefs(conns, deviceDN)); if (ldapObj != null) { ConfigurationChanges.ModifiedAttribute attribute = new ConfigurationChanges.ModifiedAttribute("dicomNetworkConnectionReference"); for (Connection conn : conns) attribute.addValue(LdapUtils.dnOf(conn, deviceDN)); ldapObj.add(attribute); } } }
private static void storeNotEmpty(ConfigurationChanges.ModifiedObject ldapObj, Attributes attrs, String attrID, byte[]... vals) { if (vals != null && vals.length > 0) { attrs.put(attr(attrID, vals)); if (ldapObj != null) { ConfigurationChanges.ModifiedAttribute attribute = new ConfigurationChanges.ModifiedAttribute(attrID); for (byte[] val : vals) attribute.addValue(val.length + " bytes"); ldapObj.add(attribute); } } }
private Attributes storeTo(ConfigurationChanges.ModifiedObject ldapObj, AuditRecordRepository arr, String deviceDN, Attributes attrs) { attrs.put(new BasicAttribute("objectclass", "dcmAuditRecordRepository")); LdapUtils.storeConnRefs(ldapObj, attrs, arr.getConnections(), deviceDN); LdapUtils.storeNotNullOrDef(ldapObj, attrs, "dicomInstalled", arr.getInstalled(), null); return attrs; }
public static <T> void storeNotEmpty(ConfigurationChanges.ModifiedObject ldapObj, Attributes attrs, String attrID, T[] vals, T... defVals) { if (vals.length > 0 && !LdapUtils.equals(vals, defVals)) { attrs.put(LdapUtils.attr(attrID, vals)); if (ldapObj != null) { ConfigurationChanges.ModifiedAttribute attribute = new ConfigurationChanges.ModifiedAttribute(attrID); for (T val : vals) attribute.addValue(val); ldapObj.add(attribute); } } }