Skip to content

Commit e23dc69

Browse files
authored
Add methods and events for email verification and password reset (#228)
1 parent 529cb8b commit e23dc69

16 files changed

+498
-11
lines changed

src/main/kotlin/com/workos/usermanagement/UserManagementApi.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ import com.workos.usermanagement.builders.SendPasswordResetEmailOptionsBuilder
1616
import com.workos.usermanagement.builders.UpdateOrganizationMembershipOptionsBuilder
1717
import com.workos.usermanagement.models.Authentication
1818
import com.workos.usermanagement.models.AuthenticationFactors
19+
import com.workos.usermanagement.models.EmailVerification
1920
import com.workos.usermanagement.models.EnrolledAuthenticationFactor
2021
import com.workos.usermanagement.models.Identity
2122
import com.workos.usermanagement.models.Invitation
2223
import com.workos.usermanagement.models.Invitations
2324
import com.workos.usermanagement.models.MagicAuth
2425
import com.workos.usermanagement.models.OrganizationMembership
2526
import com.workos.usermanagement.models.OrganizationMemberships
27+
import com.workos.usermanagement.models.PasswordReset
2628
import com.workos.usermanagement.models.RefreshAuthentication
2729
import com.workos.usermanagement.models.User
2830
import com.workos.usermanagement.models.Users
2931
import com.workos.usermanagement.types.AuthenticationAdditionalOptions
3032
import com.workos.usermanagement.types.CreateMagicAuthOptions
3133
import com.workos.usermanagement.types.CreateOrganizationMembershipOptions
34+
import com.workos.usermanagement.types.CreatePasswordResetOptions
3235
import com.workos.usermanagement.types.CreateUserOptions
3336
import com.workos.usermanagement.types.EnrolledAuthenticationFactorOptions
3437
import com.workos.usermanagement.types.ListInvitationsOptions
@@ -270,9 +273,35 @@ class UserManagementApi(private val workos: WorkOS) {
270273
)
271274
}
272275

276+
/**
277+
* Get the details of an existing email verification code.
278+
*/
279+
fun getEmailVerification(id: String): EmailVerification {
280+
return workos.get("/user_management/email_verification/$id", EmailVerification::class.java)
281+
}
282+
283+
/**
284+
* Get the details of an existing password reset token.
285+
*/
286+
fun getPasswordReset(id: String): PasswordReset {
287+
return workos.get("/user_management/password_reset/$id", PasswordReset::class.java)
288+
}
289+
290+
/**
291+
* Creates a password reset token that can be used to reset a user's password.
292+
*/
293+
fun createPasswordReset(options: CreatePasswordResetOptions): PasswordReset {
294+
return workos.post(
295+
"/user_management/password_reset",
296+
PasswordReset::class.java,
297+
RequestConfig.builder().data(options).build()
298+
)
299+
}
300+
273301
/**
274302
* Send a password reset email and change the user’s password.
275303
*/
304+
@Deprecated("Please use `createPasswordReset` instead. This method will be removed in a future major version.")
276305
fun sendPasswordResetEmail(email: String, passwordResetUrl: String) {
277306
return workos.post(
278307
"/user_management/password_reset/send",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.workos.usermanagement.builders
2+
3+
import com.workos.usermanagement.types.CreatePasswordResetOptions
4+
5+
/**
6+
* Builder for options when creating a password reset token.
7+
*
8+
* @param email The email address of the user.
9+
*/
10+
class CreatePasswordResetOptionsBuilder @JvmOverloads constructor(
11+
private var email: String,
12+
) {
13+
/**
14+
* Generates the CreatePasswordResetOptions object.
15+
*/
16+
fun build(): CreatePasswordResetOptions {
17+
return CreatePasswordResetOptions(
18+
email = this.email,
19+
)
20+
}
21+
22+
/**
23+
* @suppress
24+
*/
25+
companion object {
26+
@JvmStatic
27+
fun create(email: String): CreatePasswordResetOptionsBuilder {
28+
return CreatePasswordResetOptionsBuilder(email)
29+
}
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.workos.usermanagement.models
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator
4+
import com.fasterxml.jackson.annotation.JsonProperty
5+
6+
/**
7+
* An email verification code that allows the recipient to verify their email
8+
*
9+
* @param id The unique ID of the email verification code.
10+
* @param userId The unique ID of the user.
11+
* @param email The email address of the user.
12+
* @param expiresAt The timestamp when the email verification code will expire.
13+
* @param code The email verification code.
14+
* @param createdAt The timestamp when the email verification code was created.
15+
* @param updatedAt The timestamp when the email verification code was last updated.
16+
*/
17+
data class EmailVerification @JsonCreator constructor(
18+
@JsonProperty("id")
19+
val id: String,
20+
21+
@JsonProperty("user_id")
22+
val userId: String,
23+
24+
@JsonProperty("email")
25+
val email: String,
26+
27+
@JsonProperty("expires_at")
28+
val expiresAt: String,
29+
30+
@JsonProperty("code")
31+
val code: String,
32+
33+
@JsonProperty("created_at")
34+
val createdAt: String,
35+
36+
@JsonProperty("updated_at")
37+
val updatedAt: String
38+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.workos.usermanagement.models
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator
4+
import com.fasterxml.jackson.annotation.JsonProperty
5+
6+
/**
7+
* An email verification code that allows the recipient to verify their email
8+
*
9+
* @param id The unique ID of the email verification code.
10+
* @param userId The unique ID of the user.
11+
* @param email The email address of the user.
12+
* @param expiresAt The timestamp when the email verification code will expire.
13+
* @param createdAt The timestamp when the email verification code was created.
14+
* @param updatedAt The timestamp when the email verification code was last updated.
15+
*/
16+
data class EmailVerificationEventData @JsonCreator constructor(
17+
@JsonProperty("id")
18+
val id: String,
19+
20+
@JsonProperty("user_id")
21+
val userId: String,
22+
23+
@JsonProperty("email")
24+
val email: String,
25+
26+
@JsonProperty("expires_at")
27+
val expiresAt: String,
28+
29+
@JsonProperty("created_at")
30+
val createdAt: String,
31+
32+
@JsonProperty("updated_at")
33+
val updatedAt: String
34+
)

src/main/kotlin/com/workos/usermanagement/models/Invitation.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.workos.usermanagement.types.InvitationStateEnumType
1818
* @param token The token of an invitation.
1919
* @param acceptInvitationUrl The URL where the user can accept the invitation.
2020
* @param organizationId The ID of the organization.
21+
* @param inviterUserId The ID of the user that invited the recipient, if provided.
2122
* @param createdAt The timestamp when the invitation was created.
2223
* @param updatedAt The timestamp when the invitation was last updated.
2324
*/
@@ -49,6 +50,9 @@ data class Invitation @JsonCreator constructor(
4950
@JsonProperty("organization_id")
5051
val organizationId: String? = null,
5152

53+
@JsonProperty("inviter_user_id")
54+
val inviterUserId: String? = null,
55+
5256
@JsonProperty("created_at")
5357
val createdAt: String,
5458

src/main/kotlin/com/workos/usermanagement/models/InvitationEventData.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.workos.usermanagement.types.InvitationStateEnumType
1616
* @param revokedAt The timestamp when the invitation was revoked.
1717
* @param expiresAt The timestamp when the invitation will expire.
1818
* @param organizationId The ID of the organization.
19+
* @param inviterUserId The ID of the user that invited the recipient, if provided.
1920
* @param createdAt The timestamp when the invitation was created.
2021
* @param updatedAt The timestamp when the invitation was last updated.
2122
*/
@@ -41,6 +42,9 @@ data class InvitationEventData @JsonCreator constructor(
4142
@JsonProperty("organization_id")
4243
val organizationId: String? = null,
4344

45+
@JsonProperty("inviter_user_id")
46+
val inviterUserId: String? = null,
47+
4448
@JsonProperty("created_at")
4549
val createdAt: String,
4650

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.workos.usermanagement.models
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator
4+
import com.fasterxml.jackson.annotation.JsonProperty
5+
6+
/**
7+
* A password reset token that allows the recipient to reset their password
8+
*
9+
* @param id The unique ID of the password reset token.
10+
* @param userId The unique ID of the user.
11+
* @param email The email address of the user.
12+
* @param passwordResetToken The token for password reset.
13+
* @param passwordResetUrl The URL where the user can reset their password.
14+
* @param expiresAt The timestamp when the password reset token will expire.
15+
* @param createdAt The timestamp when the password reset token was created.
16+
*/
17+
data class PasswordReset @JsonCreator constructor(
18+
@JsonProperty("id")
19+
val id: String,
20+
21+
@JsonProperty("user_id")
22+
val userId: String,
23+
24+
@JsonProperty("email")
25+
val email: String,
26+
27+
@JsonProperty("password_reset_token")
28+
val passwordResetToken: String,
29+
30+
@JsonProperty("password_reset_url")
31+
val passwordResetUrl: String,
32+
33+
@JsonProperty("expires_at")
34+
val expiresAt: String,
35+
36+
@JsonProperty("created_at")
37+
val createdAt: String
38+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.workos.usermanagement.models
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator
4+
import com.fasterxml.jackson.annotation.JsonProperty
5+
6+
/**
7+
* A password reset token that allows the recipient to reset their password
8+
*
9+
* @param id The unique ID of the password reset token.
10+
* @param userId The unique ID of the user.
11+
* @param email The email address of the user.
12+
* @param expiresAt The timestamp when the password reset token will expire.
13+
* @param createdAt The timestamp when the password reset token was created.
14+
*/
15+
data class PasswordResetEventData @JsonCreator constructor(
16+
@JsonProperty("id")
17+
val id: String,
18+
19+
@JsonProperty("user_id")
20+
val userId: String,
21+
22+
@JsonProperty("email")
23+
val email: String,
24+
25+
@JsonProperty("expires_at")
26+
val expiresAt: String,
27+
28+
@JsonProperty("created_at")
29+
val createdAt: String
30+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.workos.usermanagement.types
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty
4+
5+
class CreatePasswordResetOptions(
6+
/**
7+
* The email address of the user.
8+
*/
9+
@JsonProperty("email")
10+
val email: String,
11+
) {
12+
init {
13+
require(email.isNotBlank()) { "Email is required" }
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.workos.webhooks.models
2+
3+
import com.workos.usermanagement.models.EmailVerificationEventData
4+
5+
/**
6+
* Webhook Event for `email_verification.*` events.
7+
*/
8+
class EmailVerificationEvent(
9+
@JvmField
10+
override val id: String,
11+
12+
@JvmField
13+
override val event: EventType,
14+
15+
@JvmField
16+
override val data: EmailVerificationEventData,
17+
18+
@JvmField
19+
override val createdAt: String
20+
) : WebhookEvent(id, event, data, createdAt)

0 commit comments

Comments
 (0)