Skip to content

Commit

Permalink
Including Advanced User Search API client
Browse files Browse the repository at this point in the history
bacon: test
resolves OKTA-78337
  • Loading branch information
larsjohansen-okta committed Feb 11, 2016
1 parent 874dcfd commit 05f6c97
Show file tree
Hide file tree
Showing 23 changed files with 824 additions and 280 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ hs_err_pid*
.idea/uiDesigner.xml

# Maven:
target/maven-archiver
target/surefire-reports
target

# Gradle:
.idea/gradle.xml
Expand Down
56 changes: 27 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@ To build and install:

1. Clone the repo
2. Navigate to the repo directory. It should contain pom.xml
2. `mvn -Dmaven.test.skip=true install`

To run the build with tests:

1. Set the following environment variables:
* OKTA_TEST_URL
* OKTA_TEST_KEY
* OKTA_TEST_ADMIN_NAME
* OKTA_TEST_ADMIN_PASSWORD
2. `mvn build`

3. Build with tests `mvn install` or without tests `mvn -Dmaven.test.skip=true install`

###Client configuration
```java
Expand Down Expand Up @@ -44,13 +34,13 @@ String status = result.getStatus();
This client is used to perform CRUD operations on user objects
(http://developer.okta.com/docs/api/resources/users.html).
```java
UsersApiClient usersClient = new UsersApiClient(oktaSettings);
UserApiClient userApiClient = new UserApiClient(oktaSettings);

// Create a new user
// First Name, Last Name, Email and Login are required. Password is optional.
// The boolean variable is for activate which means that activate the user as soon as
// it is created.
User newUser = usersClient.createUser(
User newUser = userApiClient.createUser(
"First",
"Last",
"login@example.com",
Expand All @@ -75,44 +65,52 @@ user.setProfile(userProfile);
user.setCredentials(loginCredentials);

// true is for activate user as soon as it is created
userClient.createUser(user, true);
userApiClient.createUser(user, true);

// Read/Search
// There are plenty of methods for reading users.
// 1. Search user when user ID/loginName/loginShortName is known
User user = usersClient.getUser("ID/loginName/loginShortName");
User user = userApiClient.getUser("ID/loginName/loginShortName");

// 2. Search user using filters. You can query the API for searching a user
// with the help of filters mentioned at - http://developer.okta.com/docs/api/resources/users.html#filters
// Example - search for first name. Returns a list of users matching that query
FilterBuilder filterBuilder = new FilterBuilder("profile.firstName eq \"" + firstName + "\"");
List<User> users = userClient.getUsersWithFilter(filterBuilder);
List<User> users = userApiClient.getUsersWithFilter(filterBuilder);

// 3. Advanced search provides the option to filter on any user profile attribute, any custom defined
// profile attribute, as well as the following top-level attributes: id, status, created, activated,
// statusChanged and lastUpdated. The advanced search performs a case insensitive filter against all fields
// specified in the search parameter. Note that the results might not yet be up to date, as the most up to date
// data can be delayed up to a few seconds, so use for convenience.
FilterBuilder filterBuilder = new FilterBuilder("profile.flightNumber eq \"A415\"");
List<User> users = userApiClient.getUsersWithSearch(filterBuilder);

// 3. Search users only on firstName, lastName or email
// 4. Search users only on firstName, lastName or email
// The parameter passed is searched in the attributes - firstName, lastName and email of all Users.
List<User> users = userClient.getUsersWithQuery("firstName/lastName/email");
List<User> users = userApiClient.getUsersWithQuery("firstName/lastName/email");

// Update
newUser.getProfile().setLastName("NewLast");
usersClient.updateUser(newUser);
userApiClient.updateUser(newUser);

// Delete (for Users this is the same as deactivate)
usersClient.deleteUser(newUser.getId());
userApiClient.deleteUser(newUser.getId());
```

###Paging
```java
PagedResults<User> pagedResults = usersClient.getUsersPagedResultsWithLimit(10);
PagedResults<User> pagedResults = userApiClient.getUsersPagedResultsWithLimit(10);

int counter = 0;
do {
if(!pagedResults.isFirstPage()) {
pagedResults = usersClient.getUsersPagedResultsByUrl(pagedResults.getNextUrl());
}

for(User user : pagedResults.getResult()) {
while (true) {
for (User user : pagedResults.getResult()) {
// Do something with user
}

if (!pagedResults.isLastPage()) {
pagedResults = userApiClient.getUsersPagedResultsByUrl(pagedResults.getNextUrl());
} else {
break;
}
}
while(!pagedResults.isLastPage());
```
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
<slf4j-api.version>1.7.10</slf4j-api.version>
<testng.version>6.8.17</testng.version>
<org.hamcrest.version>1.3</org.hamcrest.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -57,6 +58,12 @@
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/okta/sdk/clients/AppGroupApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
import java.util.Map;

public class AppGroupApiClient extends JsonApiClient {

public AppGroupApiClient(ApiClientConfiguration config) {
super(config);
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/apps%s", this.apiVersion, relativePath);
}

////////////////////////////////////////////
// COMMON METHODS
////////////////////////////////////////////
Expand Down Expand Up @@ -113,4 +109,10 @@ public PagedResults<AppGroup> getAppGroupsPagedResultsAfterCursorWithLimit(Strin
public PagedResults<AppGroup> getAppGroupsPagedResultsWithUrl(String url) throws IOException {
return new PagedResults<AppGroup>(getAppGroupsApiResponseWithUrl(url));
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/apps%s", this.apiVersion, relativePath);
}

}
12 changes: 7 additions & 5 deletions src/main/java/com/okta/sdk/clients/AppInstanceApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
import java.util.Map;

public class AppInstanceApiClient extends JsonApiClient {

public AppInstanceApiClient(ApiClientConfiguration config) {
super(config);
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/apps%s", this.apiVersion, relativePath);
}

////////////////////////////////////////////
// COMMON METHODS
////////////////////////////////////////////
Expand Down Expand Up @@ -196,4 +192,10 @@ private FilterBuilder getGroupFilter(String... groups) {
private FilterBuilder getUserFilter(String... users) {
return Utils.getFilter(Filters.AppInstance.USER_ID, users);
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/apps%s", this.apiVersion, relativePath);
}

}
10 changes: 5 additions & 5 deletions src/main/java/com/okta/sdk/clients/AuthApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public AuthApiClient(ApiClientConfiguration config) {
super(config);
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v1/authn%s", relativePath);
}

////////////////////////////////////////////
// COMMON METHODS
////////////////////////////////////////////
Expand Down Expand Up @@ -206,4 +201,9 @@ public AuthResult cancelTransaction(String stateToken, String relayState) throws
return post(getEncodedPath("/cancel"), params, new TypeReference<AuthResult>() { });
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v1/authn%s", relativePath);
}

}
11 changes: 6 additions & 5 deletions src/main/java/com/okta/sdk/clients/EventApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public EventApiClient(ApiClientConfiguration config) {
super(config);
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/events%s", this.apiVersion, relativePath);
}

////////////////////////////////////////////
// COMMON METHODS
////////////////////////////////////////////
Expand Down Expand Up @@ -148,4 +143,10 @@ public PagedResults<Event> getEventsPagedResultsAfterCursorWithLimit(String afte
public PagedResults<Event> getEventsPagedResultsWithUrl(String url) throws IOException {
return new PagedResults<Event>(getEventsApiResponseWithUrl(url));
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/events%s", this.apiVersion, relativePath);
}

}
14 changes: 8 additions & 6 deletions src/main/java/com/okta/sdk/clients/FactorsAdminApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public FactorsAdminApiClient(ApiClientConfiguration config) {
super(config);
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/org%s", this.apiVersion, relativePath);
}

////////////////////////////////////////////
// COMMON METHODS
////////////////////////////////////////////
Expand All @@ -39,10 +34,17 @@ public OrgAuthFactor activateOrgFactor(String orgAuthFactorId) throws IOExceptio
}

public OrgAuthFactor activateOrgFactor(String orgAuthFactorId, OrgAuthFactor orgAuthFactor) throws IOException {
return post(getEncodedPath("/factors/%s/lifecycle/activate", orgAuthFactorId), orgAuthFactor, new TypeReference<OrgAuthFactor>() { });
return post(getEncodedPath("/factors/%s/lifecycle/activate", orgAuthFactorId), orgAuthFactor, new TypeReference<OrgAuthFactor>() {
});
}

public OrgAuthFactor deActivateOrgFactor(String orgAuthFactorId) throws IOException {
return post(getEncodedPath("/factors/%s/lifecycle/deactivate", orgAuthFactorId), null, new TypeReference<OrgAuthFactor>() { });
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/org%s", this.apiVersion, relativePath);
}

}
11 changes: 6 additions & 5 deletions src/main/java/com/okta/sdk/clients/FactorsApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public FactorsApiClient(ApiClientConfiguration config) {
super(config);
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/users%s", this.apiVersion, relativePath);
}

////////////////////////////////////////////
// COMMON METHODS
////////////////////////////////////////////
Expand Down Expand Up @@ -127,4 +122,10 @@ public Factor activateFactorDevice(String userId, String userFactorId, String de
verification.setPassCode(passCode);
return post(getEncodedPath("/%s/factors/%s/devices/%s/lifecycle/activate", userId, userFactorId, deviceId), verification, new TypeReference<Factor>() { });
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/users%s", this.apiVersion, relativePath);
}

}
20 changes: 12 additions & 8 deletions src/main/java/com/okta/sdk/clients/SessionApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public SessionApiClient(ApiClientConfiguration config) {
super(config);
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/sessions%s", this.apiVersion, relativePath);
}

////////////////////////////////////////////
// COMMON METHODS
////////////////////////////////////////////
Expand Down Expand Up @@ -58,7 +53,8 @@ public Session createSessionWithCredentialsAndCookieTokenUrl(String username, St
public Session createSessionWithSessionToken(String sessionToken) throws IOException {
Map<String, Object> params = new HashMap<String, Object>();
params.put("sessionToken", sessionToken);
return post(getEncodedPath("/"), params, new TypeReference<Session>() { });
return post(getEncodedPath("/"), params, new TypeReference<Session>() {
});
}

public Session createSessionWithSessionTokenAndAdditionalFields(String sessionToken, String additionalFields) throws IOException {
Expand All @@ -68,14 +64,22 @@ public Session createSessionWithSessionTokenAndAdditionalFields(String sessionTo
}

public Session validateSession(String sessionId) throws IOException {
return get(getEncodedPath("/%s", sessionId), new TypeReference<Session>() { });
return get(getEncodedPath("/%s", sessionId), new TypeReference<Session>() {
});
}

public Session extendSession(String sessionId) throws IOException {
return put(getEncodedPath("/%s", sessionId), new TypeReference<Session>() { });
return put(getEncodedPath("/%s", sessionId), new TypeReference<Session>() {
});
}

public void clearSession(String sessionId) throws IOException {
delete(getEncodedPath("/%s", sessionId));
}

@Override
protected String getFullPath(String relativePath) {
return String.format("/api/v%d/sessions%s", this.apiVersion, relativePath);
}

}
Loading

0 comments on commit 05f6c97

Please sign in to comment.