Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods and events for email verification and password reset #228

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/kotlin/com/workos/usermanagement/UserManagementApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ import com.workos.usermanagement.builders.SendPasswordResetEmailOptionsBuilder
import com.workos.usermanagement.builders.UpdateOrganizationMembershipOptionsBuilder
import com.workos.usermanagement.models.Authentication
import com.workos.usermanagement.models.AuthenticationFactors
import com.workos.usermanagement.models.EmailVerification
import com.workos.usermanagement.models.EnrolledAuthenticationFactor
import com.workos.usermanagement.models.Identity
import com.workos.usermanagement.models.Invitation
import com.workos.usermanagement.models.Invitations
import com.workos.usermanagement.models.MagicAuth
import com.workos.usermanagement.models.OrganizationMembership
import com.workos.usermanagement.models.OrganizationMemberships
import com.workos.usermanagement.models.PasswordReset
import com.workos.usermanagement.models.RefreshAuthentication
import com.workos.usermanagement.models.User
import com.workos.usermanagement.models.Users
import com.workos.usermanagement.types.AuthenticationAdditionalOptions
import com.workos.usermanagement.types.CreateMagicAuthOptions
import com.workos.usermanagement.types.CreateOrganizationMembershipOptions
import com.workos.usermanagement.types.CreatePasswordResetOptions
import com.workos.usermanagement.types.CreateUserOptions
import com.workos.usermanagement.types.EnrolledAuthenticationFactorOptions
import com.workos.usermanagement.types.ListInvitationsOptions
Expand Down Expand Up @@ -270,9 +273,35 @@ class UserManagementApi(private val workos: WorkOS) {
)
}

/**
* Get the details of an existing email verification code.
*/
fun getEmailVerification(id: String): EmailVerification {
return workos.get("/user_management/email_verification/$id", EmailVerification::class.java)
}

/**
* Get the details of an existing password reset token.
*/
fun getPasswordReset(id: String): PasswordReset {
return workos.get("/user_management/password_reset/$id", PasswordReset::class.java)
}

/**
* Creates a password reset token that can be used to reset a user's password.
*/
fun createPasswordReset(options: CreatePasswordResetOptions): PasswordReset {
return workos.post(
"/user_management/password_reset",
PasswordReset::class.java,
RequestConfig.builder().data(options).build()
)
}

/**
* Send a password reset email and change the user’s password.
*/
@Deprecated("Please use `createPasswordReset` instead. This method will be removed in a future major version.")
fun sendPasswordResetEmail(email: String, passwordResetUrl: String) {
return workos.post(
"/user_management/password_reset/send",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.workos.usermanagement.builders

import com.workos.usermanagement.types.CreatePasswordResetOptions

/**
* Builder for options when creating a password reset token.
*
* @param email The email address of the user.
*/
class CreatePasswordResetOptionsBuilder @JvmOverloads constructor(
private var email: String,
) {
/**
* Generates the CreatePasswordResetOptions object.
*/
fun build(): CreatePasswordResetOptions {
return CreatePasswordResetOptions(
email = this.email,
)
}

/**
* @suppress
*/
companion object {
@JvmStatic
fun create(email: String): CreatePasswordResetOptionsBuilder {
return CreatePasswordResetOptionsBuilder(email)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.workos.usermanagement.models

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty

/**
* An email verification code that allows the recipient to verify their email
*
* @param id The unique ID of the email verification code.
* @param userId The unique ID of the user.
* @param email The email address of the user.
* @param expiresAt The timestamp when the email verification code will expire.
* @param code The email verification code.
* @param createdAt The timestamp when the email verification code was created.
* @param updatedAt The timestamp when the email verification code was last updated.
*/
data class EmailVerification @JsonCreator constructor(
@JsonProperty("id")
val id: String,

@JsonProperty("user_id")
val userId: String,

@JsonProperty("email")
val email: String,

@JsonProperty("expires_at")
val expiresAt: String,

@JsonProperty("code")
val code: String,

@JsonProperty("created_at")
val createdAt: String,

@JsonProperty("updated_at")
val updatedAt: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.workos.usermanagement.models

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty

/**
* An email verification code that allows the recipient to verify their email
*
* @param id The unique ID of the email verification code.
* @param userId The unique ID of the user.
* @param email The email address of the user.
* @param expiresAt The timestamp when the email verification code will expire.
* @param createdAt The timestamp when the email verification code was created.
* @param updatedAt The timestamp when the email verification code was last updated.
*/
data class EmailVerificationEventData @JsonCreator constructor(
@JsonProperty("id")
val id: String,

@JsonProperty("user_id")
val userId: String,

@JsonProperty("email")
val email: String,

@JsonProperty("expires_at")
val expiresAt: String,

@JsonProperty("created_at")
val createdAt: String,

@JsonProperty("updated_at")
val updatedAt: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.workos.usermanagement.types.InvitationStateEnumType
* @param token The token of an invitation.
* @param acceptInvitationUrl The URL where the user can accept the invitation.
* @param organizationId The ID of the organization.
* @param inviterUserId The ID of the user that invited the recipient, if provided.
* @param createdAt The timestamp when the invitation was created.
* @param updatedAt The timestamp when the invitation was last updated.
*/
Expand Down Expand Up @@ -49,6 +50,9 @@ data class Invitation @JsonCreator constructor(
@JsonProperty("organization_id")
val organizationId: String? = null,

@JsonProperty("inviter_user_id")
val inviterUserId: String? = null,

@JsonProperty("created_at")
val createdAt: String,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.workos.usermanagement.types.InvitationStateEnumType
* @param revokedAt The timestamp when the invitation was revoked.
* @param expiresAt The timestamp when the invitation will expire.
* @param organizationId The ID of the organization.
* @param inviterUserId The ID of the user that invited the recipient, if provided.
* @param createdAt The timestamp when the invitation was created.
* @param updatedAt The timestamp when the invitation was last updated.
*/
Expand All @@ -41,6 +42,9 @@ data class InvitationEventData @JsonCreator constructor(
@JsonProperty("organization_id")
val organizationId: String? = null,

@JsonProperty("inviter_user_id")
val inviterUserId: String? = null,

@JsonProperty("created_at")
val createdAt: String,

Expand Down
38 changes: 38 additions & 0 deletions src/main/kotlin/com/workos/usermanagement/models/PasswordReset.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.workos.usermanagement.models

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty

/**
* A password reset token that allows the recipient to reset their password
*
* @param id The unique ID of the password reset token.
* @param userId The unique ID of the user.
* @param email The email address of the user.
* @param passwordResetToken The token for password reset.
* @param passwordResetUrl The URL where the user can reset their password.
* @param expiresAt The timestamp when the password reset token will expire.
* @param createdAt The timestamp when the password reset token was created.
*/
data class PasswordReset @JsonCreator constructor(
@JsonProperty("id")
val id: String,

@JsonProperty("user_id")
val userId: String,

@JsonProperty("email")
val email: String,

@JsonProperty("password_reset_token")
val passwordResetToken: String,

@JsonProperty("password_reset_url")
val passwordResetUrl: String,

@JsonProperty("expires_at")
val expiresAt: String,

@JsonProperty("created_at")
val createdAt: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.workos.usermanagement.models

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty

/**
* A password reset token that allows the recipient to reset their password
*
* @param id The unique ID of the password reset token.
* @param userId The unique ID of the user.
* @param email The email address of the user.
* @param expiresAt The timestamp when the password reset token will expire.
* @param createdAt The timestamp when the password reset token was created.
*/
data class PasswordResetEventData @JsonCreator constructor(
@JsonProperty("id")
val id: String,

@JsonProperty("user_id")
val userId: String,

@JsonProperty("email")
val email: String,

@JsonProperty("expires_at")
val expiresAt: String,

@JsonProperty("created_at")
val createdAt: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.workos.usermanagement.types

import com.fasterxml.jackson.annotation.JsonProperty

class CreatePasswordResetOptions(
/**
* The email address of the user.
*/
@JsonProperty("email")
val email: String,
) {
init {
require(email.isNotBlank()) { "Email is required" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.workos.webhooks.models

import com.workos.usermanagement.models.EmailVerificationEventData

/**
* Webhook Event for `email_verification.*` events.
*/
class EmailVerificationEvent(
@JvmField
override val id: String,

@JvmField
override val event: EventType,

@JvmField
override val data: EmailVerificationEventData,

@JvmField
override val createdAt: String
) : WebhookEvent(id, event, data, createdAt)
12 changes: 11 additions & 1 deletion src/main/kotlin/com/workos/webhooks/models/EventType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ enum class EventType(
*/
DirectoryGroupDeleted("dsync.group.deleted"),

/**
* Triggers when a user is required to verify their email.
*/
EmailVerificationCreated("email_verification.created"),

/**
* Triggers when a user is invited to sign up or to join an organization.
*/
Expand All @@ -104,5 +109,10 @@ enum class EventType(
/**
* Triggers when an organization membership is updated.
*/
OrganizationMembershipUpdated("organization_membership.updated")
OrganizationMembershipUpdated("organization_membership.updated"),

/**
* Triggers when a user requests to reset their password.
*/
PasswordResetCreated("password_reset.created")
}
20 changes: 20 additions & 0 deletions src/main/kotlin/com/workos/webhooks/models/PasswordResetEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.workos.webhooks.models

import com.workos.usermanagement.models.PasswordResetEventData

/**
* Webhook Event for `password_reset.*` events.
*/
class PasswordResetEvent(
@JvmField
override val id: String,

@JvmField
override val event: EventType,

@JvmField
override val data: PasswordResetEventData,

@JvmField
override val createdAt: String
) : WebhookEvent(id, event, data, createdAt)
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import com.workos.directorysync.models.Directory
import com.workos.directorysync.models.Group
import com.workos.directorysync.models.User
import com.workos.sso.models.Connection
import com.workos.usermanagement.models.EmailVerificationEventData
import com.workos.usermanagement.models.InvitationEventData
import com.workos.usermanagement.models.MagicAuthEventData
import com.workos.usermanagement.models.OrganizationMembership
import com.workos.usermanagement.models.PasswordResetEventData

/**
* Custom JSON deserializer for [com.workos.webhooks.models.WebhookEvent] events.
Expand Down Expand Up @@ -51,11 +53,13 @@ class WebhookJsonDeserializer : JsonDeserializer<WebhookEvent>() {
EventType.DirectoryGroupDeleted -> DirectoryGroupDeletedEvent(id, eventType, deserializeData(data, Group::class.java), createdAt)
EventType.DirectoryGroupUserAdded -> DirectoryGroupUserAddedEvent(id, eventType, deserializeData(data, DirectoryGroupUserEvent::class.java), createdAt)
EventType.DirectoryGroupUserRemoved -> DirectoryGroupUserRemovedEvent(id, eventType, deserializeData(data, DirectoryGroupUserEvent::class.java), createdAt)
EventType.EmailVerificationCreated -> EmailVerificationEvent(id, eventType, deserializeData(data, EmailVerificationEventData::class.java), createdAt)
EventType.InvitationCreated -> InvitationEvent(id, eventType, deserializeData(data, InvitationEventData::class.java), createdAt)
EventType.MagicAuthCreated -> MagicAuthEvent(id, eventType, deserializeData(data, MagicAuthEventData::class.java), createdAt)
EventType.OrganizationMembershipCreated -> OrganizationMembershipEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt)
EventType.OrganizationMembershipDeleted -> OrganizationMembershipEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt)
EventType.OrganizationMembershipUpdated -> OrganizationMembershipEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt)
EventType.PasswordResetCreated -> PasswordResetEvent(id, eventType, deserializeData(data, PasswordResetEventData::class.java), createdAt)
}
}

Expand Down
Loading