diff --git a/buildSrc/src/main/kotlin/com.workos.java.examples.java-application-conventions.gradle.kts b/buildSrc/src/main/kotlin/com.workos.java.examples.java-application-conventions.gradle.kts index 5010676..bba686a 100644 --- a/buildSrc/src/main/kotlin/com.workos.java.examples.java-application-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/com.workos.java.examples.java-application-conventions.gradle.kts @@ -9,7 +9,7 @@ dependencies { implementation("gg.jte:jte:1.12.0") - implementation("com.workos:workos:2.9.1") + implementation("com.workos:workos:3.1.0-beta.user-management1") implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.0") diff --git a/java-usermanagement-example/.settings/org.eclipse.buildship.core.prefs b/java-usermanagement-example/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..b1886ad --- /dev/null +++ b/java-usermanagement-example/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,2 @@ +connection.project.dir=.. +eclipse.preferences.version=1 diff --git a/java-usermanagement-example/README.md b/java-usermanagement-example/README.md new file mode 100644 index 0000000..18eb04d --- /dev/null +++ b/java-usermanagement-example/README.md @@ -0,0 +1,78 @@ +If your SaaS product’s backend is built with Java—or a JVM-compatible language such as Kotlin, Groovy, Scala, or Clojure—and you want to incorporate WorkOS’ User Management functionality, you can do a dry-run using this example Java app. It makes use of the [WorkOS Kotlin SDK](https://github.com/workos/workos-kotlin). + +If you get stuck while following the steps below and aren't able to resolve the issue by reading our [API reference](https://workos.com/docs/reference) or [User Management Setup Guide](https://workos.com/docs/user-management/guide), please reach out to us at support@workos.com so we can help! + +## Prerequisites +A free WorkOS account, and each of these installed on your machine: +- Java version 1.8+ +- The Java Development Kit (JDK), which includes the Java Runtime Environment (JRE) + +## Clone the Java app +1. In your CLI, navigate to the directory into which you want to clone the Java example app git repo: +```bash +$ cd ~/Desktop +``` + +2. Clone the main Java example app repo: +```bash +# HTTPS +$ git clone https://github.com/workos/java-example-applications.git + +or + +# SSH +$ git clone git@github.com:workos/java-example-applications.git +``` + +3. Navigate to the cloned repo: +```bash +$ cd java-example-applications/ +``` + +## Securely store the environment variables +4. Obtain and make note of your WorkOS API key and WorkOS Client ID from the WorkOS Dashboard. The locations of these values are shown in the screenshots below. + +![Screenshot of the WorkOS dashboard showing where to locate the API key](https://assets-global.website-files.com/5f03ef1d331a69193fae6dcd/61986a545cae6987e741c044_TXlyTFBXjAfHZwhb9l-YRvpdj3LCCSXX5frveCFXh1Ywlc482yvdpKHDDRl9QKH3CXbsCwCj9Sya4DAmxvvK293sREyeTJJW8NidhsDgc5lXSU15H6cFpHIlXaAeqHXge259YQju.png) + +![Screenshot of the WorkOS dashboard showing where to locate the Client ID](https://assets-global.website-files.com/5f03ef1d331a69193fae6dcd/61986a53882d3a558ae819ee_-ZbW48EgfBtiMuTQEDAaV0UtSxw2wt6Mx-NAX5YxIdI87AZT3bI5w_7jS6tHk-TlG0aHC08AD-l_wr3v_RmUMzSyTehrLIk8D5A7hQ5UskvPVeuXec-9yf6pLTBxkm68PF3kHsqv.png) + +5. Create a .env file in the java-example-applications/ directory to store the environment variables: +```bash +$ touch .env +``` + +6. Open the new .env file with your preferred text editor and replace the placeholder values for WORKOS_API_KEY and WORKOS_CLIENT_ID. +```bash +WORKOS_API_KEY=your_api_key_here +WORKOS_CLIENT_ID=your_project_id_here +``` + +The .env file is listed in this repo's .gitignore file, so your sensitive information will not be checked into version control. This is an important consideration for keeping sensitive information such as API keys private. The WorkOS Kotlin SDK will read your API key and Client ID from the .env file. + +## Set up User Management with WorkOS +7. Create an [Organization](https://dashboard.workos.com/organizations) and an [SSO Connection](https://workos.com/docs/user-management/guide/introduction) in the Organization in your WorkOS Dashboard. +8. Copy the Organization ID from the organization that you just set up and add it to the same .env file that you created in step 6. The .env file should now look like this: +```bash +WORKOS_API_KEY=your_api_key_here +WORKOS_CLIENT_ID=your_project_id_here +WORKOS_ORGANIZATION_ID=your_organization_id_here +``` + +10. Add http://localhost:7001/callback as the default Redirect URI in the Configuration section of the WorkOS Dashboard: + +![A screenshot of te default Redirect URI in the WorkOS dashboard](https://assets-global.website-files.com/5f03ef1d331a69193fae6dcd/619d4e48d8ad3f0711f3b1e3_Screen%20Shot%202021-11-23%20at%2012.24.34%20PM.png) + +## Test the integration +9. Start the server by running this command in the CLI while you’re in the java-example-applications/ directory: +```bash +$ ./gradlew :java-usermanagement-example:run +``` + +10. Navigate to http://localhost:7001. Click the “Login” button. You’ll be prompted to sign in using the identity provider (IdP) you configured during the setup. + +11. Once you’re signed in, the webpage will display the user information for the logged-in user. + +Nice work! You just set up User Management for your Java app! + +## Need help? +If you get stuck while following the steps below and aren't able to resolve the issue by reading our [API reference](https://workos.com/docs/reference) or [User Management Setup Guide](https://workos.com/docs/user-management/guide), please reach out to us at support@workos.com so we can help! diff --git a/java-usermanagement-example/build.gradle.kts b/java-usermanagement-example/build.gradle.kts new file mode 100644 index 0000000..b5df010 --- /dev/null +++ b/java-usermanagement-example/build.gradle.kts @@ -0,0 +1,15 @@ +plugins { + id("com.workos.java.examples.java-application-conventions") +} + +repositories { + flatDir { + dirs("libs") + dirs("build/libs") + } +} + + +application { + mainClass.set("com.workos.java.examples.UserManagementApp") +} diff --git a/java-usermanagement-example/src/main/java/com/workos/java/examples/UserManagementApp.java b/java-usermanagement-example/src/main/java/com/workos/java/examples/UserManagementApp.java new file mode 100644 index 0000000..4c8a17c --- /dev/null +++ b/java-usermanagement-example/src/main/java/com/workos/java/examples/UserManagementApp.java @@ -0,0 +1,76 @@ +package com.workos.java.examples; + +import com.workos.WorkOS; +import com.workos.usermanagement.models.Identity; +import com.workos.usermanagement.models.User; +import com.workos.usermanagement.types.UserManagementProviderEnumType; +import io.github.cdimascio.dotenv.Dotenv; +import io.javalin.Javalin; +import io.javalin.http.Context; +import io.javalin.http.staticfiles.Location; +import java.util.Map; + + +public class UserManagementApp { + private final WorkOS workos; + private final String clientId; + + public UserManagementApp() { + Dotenv env = Dotenv.configure().directory("../.env").load(); + + Javalin app = Javalin.create(config -> { + config.addStaticFiles("src/resources", Location.EXTERNAL); + }).start(7001); + + workos = new WorkOS(env.get("WORKOS_API_KEY")); + clientId = env.get("WORKOS_CLIENT_ID"); + + app.get("/", this::isLoggedIn); + app.post("/login", this::login); + app.get("/callback", this::callback); + app.get("logout", this::logout); + } + + public void login(Context ctx) { + String loginMethod = ctx.formParam("login_method"); + + String url = + workos + .userManagement + .getAuthorizationUrl(clientId, "http://localhost:7001/callback") + .provider(UserManagementProviderEnumType.valueOf(loginMethod)) + .build(); + + ctx.redirect(url); + } + + public void callback(Context ctx) { + String code = ctx.queryParam("code"); + + assert code != null; + + User user = workos.userManagement.authenticateWithCode(clientId, code, null).getUser(); + Identity[] identities = workos.userManagement.getUserIdentities(user.getId()); + ctx.sessionAttribute("user", user); + ctx.sessionAttribute("identities", identities); + + ctx.redirect("/"); + } + + public void isLoggedIn(Context ctx) { + if (ctx.sessionAttribute("user") != null){ + ctx.render("user.jte", Map.of("user", ctx.sessionAttribute("user"), "identities", ctx.sessionAttribute("identities"))); + } else { + ctx.render("home.jte"); + } + } + + public void logout(Context ctx ) { + ctx.sessionAttribute("user", null); + ctx.redirect("/"); + } + + public static void main(String[] args) { + new UserManagementApp(); + } +} diff --git a/java-usermanagement-example/src/main/jte/home.jte b/java-usermanagement-example/src/main/jte/home.jte new file mode 100644 index 0000000..da8423f --- /dev/null +++ b/java-usermanagement-example/src/main/jte/home.jte @@ -0,0 +1,43 @@ + + +
+ +User | +
---|
First Name: ${user.getFirstName()} |
+
Last Name: ${user.getLastName()} |
+
Email: ${user.getEmail()} |
+
ID: ${user.getId()} |
+
Email Verified: ${user.getEmailVerified()} |
+
Identities: ${java.util.Arrays.stream(identities).map(i -> i.toString()).toList().toString()} |
+