Skip to content

lmuhlha/okta-sdk-java

 
 

Repository files navigation

Maven Central License Support

okta-sdk-java

This SDK is in EA, so all existing features are supported by Okta in a production setting.

This version of the Okta Java SDK supports CRUD (Create, Read, Update, Delete) operations for the following resource:

  • User
  • Group
  • Group Membership Rules

Usage

Dependencies

The only compile time dependency you will need is okta-sdk-api. You will also need to add the implementation dependencies too: okta-sdk-impl and okta-sdk-httpclient.

<dependency>
    <groupId>com.okta.sdk</groupId>
    <artifactId>okta-sdk-api</artifactId>
    <version>${okta.version}</version>
</dependency>
<dependency>
    <groupId>com.okta.sdk</groupId>
    <artifactId>okta-sdk-impl</artifactId>
    <version>${okta.version}</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.okta.sdk</groupId>
    <artifactId>okta-sdk-httpclient</artifactId>
    <version>${okta.version}</version>
    <scope>runtime</scope>
</dependency>

SNAPSHOT Dependencies

Snapshots are deployed off of the 'master' branch to OSSRH and can be consumed using the following repository configured for Apache Maven or Gradle:

https://oss.sonatype.org/content/repositories/snapshots/

Client configuration

There are a few ways to configure the client, but the easiest way is to create a ~/.okta/okta.yamlfile and set the token and orgUrl values:

okta:
  client:
    token: <your-api-token>
    orgUrl: https://dev-123456.oktapreview.com

Creating a Client

Once you create your okta.yaml file, you can create a client with a couple of lines:

// Instantiate a builder for your Client. If needed, settings like Proxy and Caching can be defined here.
ClientBuilder builder = Clients.builder();

// No need to define anything else; build the Client instance. The ClientCredential information will be automatically found
// in pre-defined locations.
Client client = builder.build();

Client CRUD Operations

The client is used to perform CRUD operations against Okta's management APIs.

Create a group:

UserGroup group = GroupBuilder.instance()
        .setName("my-user-group-" + UUID.randomUUID().toString())
        .setDescription("Quickstart created Group")
        .buildAndCreate(client);

// print a couple of the attributes
println("Group: '" + group.getId() + "' was last updated on: " + group.getLastUpdated());

Create a User Account:

String email = "joe.coder+" + UUID.randomUUID().toString() + "@example.com";

User user = UserBuilder.instance()
    .setEmail(email)
    .setFirstName("Joe")
    .setLastName("Coder")
    .setPassword("Password1")
    .setSecurityQuestion("Favorite security question?")
    .setSecurityQuestionAnswer("None of them!")
    .putProfileProperty("division", "Seven") // key/value pairs predefined in the user profile schema
    .setActive(true)
    .buildAndCreate(client);

String userId = user.getId();
println("User created with ID: " + userId);

Add user to the newly created group:

user.addToGroup(group.getId());

User lookup by ID or email:

// You can look up user by ID
println("User lookup by ID: "+ client.getUser(userId).getProfile().getLogin());

// or by Email
println("User lookup by Email: "+ client.getUser(email).getProfile().getLogin());

Paging

Paging is handled automatically when iterating over a any collection.

// get the list of users
UserList users = client.listUsers();

// get the first user in the collection
println("First user in collection: " + users.iterator().next().getProfile().getEmail());

// or loop through all of them (paging is automatic)
int ii = 0;
for (User tmpUser : users) {
    println("["+ ii++ +"] User: " + tmpUser.getProfile().getEmail());
}

Build this Project

To build and install:

  1. Clone the repo
  2. Navigate to the repo directory. It should contain pom.xml
  3. Build with tests mvn install

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 72.4%
  • Groovy 22.6%
  • HTML 4.2%
  • Shell 0.8%