-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* User Management example * Add support for user identities * usermanagement -> user_management * Delete unneeded files * follow convention for module naming * Update java-usermanagement-example/README.md Co-authored-by: Michael Hadley <m@mthadley.com> * Update SDK version --------- Co-authored-by: Michael Hadley <m@mthadley.com>
- Loading branch information
1 parent
4e510d0
commit a8a8bc3
Showing
14 changed files
with
685 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
java-usermanagement-example/.settings/org.eclipse.buildship.core.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
connection.project.dir=.. | ||
eclipse.preferences.version=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
76 changes: 76 additions & 0 deletions
76
java-usermanagement-example/src/main/java/com/workos/java/examples/UserManagementApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<link rel="stylesheet" href="main.css"> | ||
|
||
<body class="height-100vh"> | ||
<div class="logged_in_nav"> | ||
<div class="flex"> | ||
<div> | ||
<img src="images/workos-logo-with-text.png" alt="workos logo"> | ||
</div> | ||
|
||
</div> | ||
<div class="flex"> | ||
<a href="https://workos.com/docs" target="_blank"><button class='button nav-item'>Documentation</button></a> | ||
<a href="https://workos.com/docs/reference" target="_blank"><button class='button nav-item'>API | ||
Reference</button></a> | ||
<a href="https://workos.com/blog" target="_blank"><button | ||
class='button nav-item blog-nav-button'>Blog</button></a> | ||
<a href="https://workos.com/" target="_blank"><button class='button button-outline'>WorkOS</button></a> | ||
</div> | ||
</div> | ||
<div class="flex flex_column height-80vh"> | ||
<div class='flex height-40vh'> | ||
<div class="card height-315 width-335"> | ||
<form method="POST" action="/login" class="mb-0"> | ||
<div class='flex_column'> | ||
<div> | ||
<span>Log in</span> | ||
</div> | ||
<hr style="width:100%; margin-top: 15px; margin-bottom: 20px;"> | ||
<button id="Google" name="login_method" value="GoogleOAuth" class="card login_button google_button"> | ||
<span>Google OAuth</span> | ||
</button> | ||
<button id="Microsoft" name="login_method" value="MicrosoftOAuth" class="card login_button microsoft_button"> | ||
<span>Microsoft OAuth</span> | ||
</button> | ||
<button id="SAML" name="login_method" value="saml" class="card login_button saml_button mb-0"> | ||
<span>Enterprise SAML</span> | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
@param com.workos.usermanagement.models.User user | ||
@param com.workos.usermanagement.models.Identity[] identities | ||
|
||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> | ||
<link rel="stylesheet" href="main.css"> | ||
|
||
<div class="logged_in_nav"> | ||
<div class="flex"> | ||
<div> | ||
<img src="images/workos-logo-with-text.png" alt="workos logo"> | ||
</div> | ||
|
||
</div> | ||
<div class="flex"> | ||
<a href="https://workos.com/docs" target="_blank"><button class='button nav-item'>Documentation</button></a> | ||
<a href="https://workos.com/docs/reference" target="_blank"><button class='button nav-item'>API | ||
Reference</button></a> | ||
<a href="https://workos.com/blog" target="_blank"><button | ||
class='button nav-item blog-nav-button'>Blog</button></a> | ||
<a href="https://workos.com/" target="_blank"><button class='button button-outline'>WorkOS</button></a> | ||
</div> | ||
</div> | ||
<div class='flex'> | ||
<div class="logged_in_div_right"> | ||
<div class="flex_column"> | ||
<table> | ||
<tr> | ||
<th>User</th> | ||
</tr> | ||
<tr> | ||
<td>First Name: <code>${user.getFirstName()}</code></td> | ||
</tr> | ||
<tr> | ||
<td>Last Name: <code>${user.getLastName()}</code></td> | ||
</tr> | ||
<tr> | ||
<td>Email: <code>${user.getEmail()}</code></td> | ||
</tr> | ||
<tr> | ||
<td>ID: <code>${user.getId()}</code></td> | ||
</tr> | ||
<tr> | ||
<td>Email Verified: <code>${user.getEmailVerified()}</code></td> | ||
</tr> | ||
<tr> | ||
<td>Identities: <code>${java.util.Arrays.stream(identities).map(i -> i.toString()).toList().toString()}</code></td> | ||
</tr> | ||
</table> | ||
<div class="flex width-40vw"> | ||
<a href="/logout"><button class='button'>Logout</button></a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.24 KB
java-usermanagement-example/src/resources/images/microsoft-button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+32.9 KB
java-usermanagement-example/src/resources/images/workos-logo-with-text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.