Skip to content

Commit d646ece

Browse files
authored
Add events and API changes for invitations and Magic Auth (#221)
* Add events and API changes for invitations and Magic Auth * Fix failing test * Rename event data
1 parent 1e23b03 commit d646ece

File tree

14 files changed

+454
-11
lines changed

14 files changed

+454
-11
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import com.workos.usermanagement.models.EnrolledAuthenticationFactor
2020
import com.workos.usermanagement.models.Identity
2121
import com.workos.usermanagement.models.Invitation
2222
import com.workos.usermanagement.models.Invitations
23+
import com.workos.usermanagement.models.MagicAuth
2324
import com.workos.usermanagement.models.OrganizationMembership
2425
import com.workos.usermanagement.models.OrganizationMemberships
2526
import com.workos.usermanagement.models.RefreshAuthentication
2627
import com.workos.usermanagement.models.User
2728
import com.workos.usermanagement.models.Users
2829
import com.workos.usermanagement.types.AuthenticationAdditionalOptions
30+
import com.workos.usermanagement.types.CreateMagicAuthOptions
2931
import com.workos.usermanagement.types.CreateOrganizationMembershipOptions
3032
import com.workos.usermanagement.types.CreateUserOptions
3133
import com.workos.usermanagement.types.EnrolledAuthenticationFactorOptions
@@ -213,10 +215,29 @@ class UserManagementApi(private val workos: WorkOS) {
213215
.toString()
214216
}
215217

218+
/**
219+
* Get the details of an existing Magic Auth code.
220+
*/
221+
fun getMagicAuth(id: String): MagicAuth {
222+
return workos.get("/user_management/magic_auth/$id", MagicAuth::class.java)
223+
}
224+
225+
/**
226+
* Creates a Magic Auth code that can be used to authenticate into your app.
227+
*/
228+
fun createMagicAuth(options: CreateMagicAuthOptions): MagicAuth {
229+
return workos.post(
230+
"/user_management/magic_auth",
231+
MagicAuth::class.java,
232+
RequestConfig.builder().data(options).build()
233+
)
234+
}
235+
216236
/**
217237
* Sends a one-time authentication code to the user’s email address. The code
218238
* expires in 10 minutes. To verify the code, authenticate the user with Magic Auth.
219239
*/
240+
@Deprecated("Please use `createMagicAuth` instead. This method will be removed in a future major version.")
220241
fun sendMagicAuthCode(email: String) {
221242
workos.post(
222243
"/user_management/magic_auth/send",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.workos.usermanagement.builders
2+
3+
import com.workos.usermanagement.types.CreateMagicAuthOptions
4+
5+
/**
6+
* Builder for options when creating a Magic Auth code.
7+
*
8+
* @param email The email address of the user.
9+
* @param invitationToken The token of an invitation, if required.
10+
*/
11+
class CreateMagicAuthOptionsBuilder @JvmOverloads constructor(
12+
private var email: String,
13+
private var invitationToken: String? = null,
14+
) {
15+
/**
16+
* Invitation Token
17+
*/
18+
fun invitationToken(value: String) = apply { invitationToken = value }
19+
20+
/**
21+
* Generates the CreateMagicAuthOptions object.
22+
*/
23+
fun build(): CreateMagicAuthOptions {
24+
return CreateMagicAuthOptions(
25+
email = this.email,
26+
invitationToken = this.invitationToken,
27+
)
28+
}
29+
30+
/**
31+
* @suppress
32+
*/
33+
companion object {
34+
@JvmStatic
35+
fun create(email: String): CreateMagicAuthOptionsBuilder {
36+
return CreateMagicAuthOptionsBuilder(email)
37+
}
38+
}
39+
}

src/main/kotlin/com/workos/usermanagement/models/Invitation.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 token The token of an invitation.
19+
* @param acceptInvitationUrl The URL where the user can accept the invitation.
1920
* @param organizationId The ID of the organization.
2021
* @param createdAt The timestamp when the invitation was created.
2122
* @param updatedAt The timestamp when the invitation was last updated.
@@ -42,6 +43,9 @@ data class Invitation @JsonCreator constructor(
4243
@JsonProperty("token")
4344
val token: String,
4445

46+
@JsonProperty("accept_invitation_url")
47+
val acceptInvitationUrl: String,
48+
4549
@JsonProperty("organization_id")
4650
val organizationId: String? = null,
4751

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.workos.usermanagement.models
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator
4+
import com.fasterxml.jackson.annotation.JsonProperty
5+
import com.workos.usermanagement.types.InvitationStateEnumType
6+
7+
/**
8+
* An email invitation allows the recipient to sign up for your app and join a
9+
* specific organization. When an invitation is accepted, a user and a
10+
* corresponding organization membership are created.
11+
*
12+
* @param id The unique ID of the invitation.
13+
* @param email The email address of the user.
14+
* @param state The state of the invitation (see enum values in [InvitationStateEnumType]).
15+
* @param acceptedAt The timestamp when the invitation was accepted.
16+
* @param revokedAt The timestamp when the invitation was revoked.
17+
* @param expiresAt The timestamp when the invitation will expire.
18+
* @param organizationId The ID of the organization.
19+
* @param createdAt The timestamp when the invitation was created.
20+
* @param updatedAt The timestamp when the invitation was last updated.
21+
*/
22+
data class InvitationEventData @JsonCreator constructor(
23+
@JsonProperty("id")
24+
val id: String,
25+
26+
@JsonProperty("email")
27+
val email: String,
28+
29+
@JsonProperty("state")
30+
val state: InvitationStateEnumType,
31+
32+
@JsonProperty("accepted_at")
33+
val acceptedAt: String? = null,
34+
35+
@JsonProperty("revoked_at")
36+
val revokedAt: String? = null,
37+
38+
@JsonProperty("expires_at")
39+
val expiresAt: String,
40+
41+
@JsonProperty("organization_id")
42+
val organizationId: String? = null,
43+
44+
@JsonProperty("created_at")
45+
val createdAt: String,
46+
47+
@JsonProperty("updated_at")
48+
val updatedAt: String
49+
)
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 Magic Auth code that allows the recipient to authenticate to your app
8+
*
9+
* @param id The unique ID of the Magic Auth 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 Magic Auth code will expire.
13+
* @param code The Magic Auth code.
14+
* @param createdAt The timestamp when the Magic Auth code was created.
15+
* @param updatedAt The timestamp when the Magic Auth code was last updated.
16+
*/
17+
data class MagicAuth @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+
* A Magic Auth code that allows the recipient to authenticate to your app
8+
*
9+
* @param id The unique ID of the Magic Auth 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 Magic Auth code will expire.
13+
* @param createdAt The timestamp when the Magic Auth code was created.
14+
* @param updatedAt The timestamp when the Magic Auth code was last updated.
15+
*/
16+
data class MagicAuthEventData @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+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.workos.usermanagement.types
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty
4+
5+
class CreateMagicAuthOptions(
6+
/**
7+
* The email address of the user.
8+
*/
9+
@JsonProperty("email")
10+
val email: String,
11+
12+
/**
13+
* The token of an invitation, if required.
14+
*/
15+
@JsonProperty("invitation_token")
16+
val invitationToken: String? = null,
17+
) {
18+
init {
19+
require(email.isNotBlank()) { "Email is required" }
20+
}
21+
}

src/main/kotlin/com/workos/webhooks/models/EventType.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,21 @@ enum class EventType(
8181
*/
8282
DirectoryGroupDeleted("dsync.group.deleted"),
8383

84+
/**
85+
* Triggers when a user is invited to sign up or to join an organization.
86+
*/
87+
InvitationCreated("invitation.created"),
88+
8489
/**
8590
* Triggers when an organization membership is created.
8691
*/
8792
OrganizationMembershipCreated("organization_membership.created"),
8893

94+
/**
95+
* Triggers when a user initiates Magic Auth and an authentication code is created.
96+
*/
97+
MagicAuthCreated("magic_auth.created"),
98+
8999
/**
90100
* Triggers when an organization membership is deleted.
91101
*/
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.InvitationEventData
4+
5+
/**
6+
* Webhook Event for `invitation.*` events.
7+
*/
8+
class InvitationEvent(
9+
@JvmField
10+
override val id: String,
11+
12+
@JvmField
13+
override val event: EventType,
14+
15+
@JvmField
16+
override val data: InvitationEventData,
17+
18+
@JvmField
19+
override val createdAt: String
20+
) : WebhookEvent(id, event, data, createdAt)
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.MagicAuthEventData
4+
5+
/**
6+
* Webhook Event for `magic_auth.*` events.
7+
*/
8+
class MagicAuthEvent(
9+
@JvmField
10+
override val id: String,
11+
12+
@JvmField
13+
override val event: EventType,
14+
15+
@JvmField
16+
override val data: MagicAuthEventData,
17+
18+
@JvmField
19+
override val createdAt: String
20+
) : WebhookEvent(id, event, data, createdAt)

0 commit comments

Comments
 (0)