use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class LdapAdapterTest method shouldPerformUpdate.
@Test
public void shouldPerformUpdate() throws Exception {
// Given
Token first = new Token("weasel", TokenType.OAUTH);
Token second = new Token("badger", TokenType.OAUTH);
Connection mockConnection = mock(Connection.class);
Result successResult = mockSuccessfulResult();
given(mockConnection.modify(any(ModifyRequest.class))).willReturn(successResult);
LdapDataLayerConfiguration config = mock(LdapDataLayerConfiguration.class);
when(config.getTokenStoreRootSuffix()).thenReturn(DN.valueOf("ou=unit-test"));
LDAPDataConversion dataConversion = new LDAPDataConversion();
LdapTokenAttributeConversion conversion = new LdapTokenAttributeConversion(dataConversion, config);
LdapAdapter adapter = new LdapAdapter(conversion, null, null);
// When
adapter.update(mockConnection, first, second);
// Then
verify(mockConnection).modify(any(ModifyRequest.class));
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class IdRepoUtils method getADAMInstanceGUID.
private static String getADAMInstanceGUID(Map attrValues) throws Exception {
try (ConnectionFactory factory = getLDAPConnection(attrValues);
Connection ld = factory.getConnection()) {
String attrName = "schemaNamingContext";
String[] attrs = { attrName };
ConnectionEntryReader res = ld.search(LDAPRequests.newSearchRequest("", SearchScope.BASE_OBJECT, "(objectclass=*)"));
if (res.hasNext()) {
SearchResultEntry entry = res.readEntry();
Attribute ldapAttr = entry.getAttribute(attrName);
if (ldapAttr != null) {
String value = ldapAttr.firstValueAsString();
int index = value.lastIndexOf("=");
if (index != -1) {
return value.substring(index + 1).trim();
}
}
}
}
return null;
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class UmaLabelsStore method read.
/**
* Reads a label from the underlying database.
* @param realm The current realm.
* @param username The user that owns the label.
* @param id The id of the label.
* @return The retrieved label details.
* @throws ResourceException Thrown if the label cannot be read.
*/
public ResourceSetLabel read(String realm, String username, String id) throws ResourceException {
try (Connection connection = getConnection()) {
SearchResultEntry entry = connection.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(getLabelDn(realm, username, id)));
Set<String> resourceSets = new HashSet<>();
final Attribute resourceSetAttribute = entry.getAttribute(RESOURCE_SET_ATTR);
if (resourceSetAttribute != null) {
for (ByteString resourceSetId : resourceSetAttribute) {
resourceSets.add(resourceSetId.toString());
}
}
return getResourceSetLabel(entry, resourceSets);
} catch (LdapException e) {
final ResultCode resultCode = e.getResult().getResultCode();
if (resultCode.equals(ResultCode.NO_SUCH_OBJECT)) {
throw new NotFoundException();
}
throw new InternalServerErrorException("Could not read", e);
}
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class UmaLabelsStore method query.
private Set<ResourceSetLabel> query(String realm, String username, Filter filter, boolean includeResourceSets) throws ResourceException {
try (Connection connection = getConnection()) {
Set<ResourceSetLabel> result = new HashSet<>();
String[] attrs;
if (includeResourceSets) {
attrs = new String[] { ID_ATTR, NAME_ATTR, TYPE_ATTR, RESOURCE_SET_ATTR };
} else {
attrs = new String[] { ID_ATTR, NAME_ATTR, TYPE_ATTR };
}
ConnectionEntryReader searchResult = connection.search(LDAPRequests.newSearchRequest(getUserDn(realm, username), SearchScope.SUBORDINATES, filter, attrs));
while (searchResult.hasNext()) {
if (searchResult.isReference()) {
debug.warning("Encountered reference {} searching for resource set labels for user {} in realm {}", searchResult.readReference(), username, realm);
} else {
final SearchResultEntry entry = searchResult.readEntry();
result.add(getResourceSetLabel(entry, getResourceSetIds(entry)));
}
}
return result;
} catch (LdapException e) {
if (e.getResult().getResultCode().equals(ResultCode.NO_SUCH_OBJECT)) {
return Collections.emptySet();
}
throw new InternalServerErrorException("Could not complete search", e);
} catch (SearchResultReferenceIOException e) {
throw new InternalServerErrorException("Shouldn't get a reference as these have been handled", e);
}
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class Step3 method validateSMHost.
/**
* Validate an Existing SM Host for Configuration Backend.
* @return
*/
public boolean validateSMHost() {
Context ctx = getContext();
String strSSL = (String) ctx.getSessionAttribute(SessionAttributeNames.CONFIG_STORE_SSL);
boolean ssl = (strSSL != null) && (strSSL.equals("SSL"));
String host = (String) ctx.getSessionAttribute(SessionAttributeNames.CONFIG_STORE_HOST);
if (host == null) {
host = "localhost";
}
String strPort = (String) ctx.getSessionAttribute(SessionAttributeNames.CONFIG_STORE_PORT);
if (strPort == null) {
strPort = getAvailablePort(50389);
}
int port = Integer.parseInt(strPort);
String bindDN = (String) ctx.getSessionAttribute(SessionAttributeNames.CONFIG_STORE_LOGIN_ID);
String rootSuffix = (String) ctx.getSessionAttribute(SessionAttributeNames.CONFIG_STORE_ROOT_SUFFIX);
String bindPwd = (String) ctx.getSessionAttribute(SessionAttributeNames.CONFIG_STORE_PWD);
if (bindDN == null) {
bindDN = "cn=Directory Manager";
}
if (rootSuffix == null) {
rootSuffix = Constants.DEFAULT_ROOT_SUFFIX;
}
try (Connection conn = getConnection(host, port, bindDN, bindPwd.toCharArray(), 5, ssl)) {
String filter = "cn=" + "\"" + rootSuffix + "\"";
String[] attrs = { "" };
conn.search(LDAPRequests.newSearchRequest(rootSuffix, SearchScope.BASE_OBJECT, filter, attrs));
writeToResponse("ok");
} catch (LdapException lex) {
if (!writeErrorToResponse(lex.getResult().getResultCode())) {
writeToResponse(getLocalizedString("cannot.connect.to.SM.datastore"));
}
} catch (Exception e) {
writeToResponse(getLocalizedString("cannot.connect.to.SM.datastore"));
}
setPath(null);
return false;
}
Aggregations