Skip to content

Commit

Permalink
Add external connection endpoints (#61)
Browse files Browse the repository at this point in the history
* add external connection endpoints

* bump minor version
  • Loading branch information
etaylormcgregor-stytch authored Oct 10, 2024
1 parent 861986d commit b605f3a
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 5 deletions.
5 changes: 5 additions & 0 deletions stytch/src/main/kotlin/com/stytch/java/b2b/api/sso/SSO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package com.stytch.java.b2b.api.sso
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.stytch.java.b2b.api.ssoexternal.External
import com.stytch.java.b2b.api.ssoexternal.ExternalImpl
import com.stytch.java.b2b.api.ssooidc.OIDC
import com.stytch.java.b2b.api.ssooidc.OIDCImpl
import com.stytch.java.b2b.api.ssosaml.SAML
Expand Down Expand Up @@ -37,6 +39,8 @@ public interface SSO {

public val saml: SAML

public val external: External

/**
* Get all SSO Connections owned by the organization.
*/
Expand Down Expand Up @@ -168,6 +172,7 @@ internal class SSOImpl(

override val oidc: OIDC = OIDCImpl(httpClient, coroutineScope)
override val saml: SAML = SAMLImpl(httpClient, coroutineScope)
override val external: External = ExternalImpl(httpClient, coroutineScope)

override suspend fun getConnections(
data: GetConnectionsRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.stytch.java.b2b.api.ssoexternal

// !!!
// WARNING: This file is autogenerated
// Only modify code within MANUAL() sections
// or your changes may be overwritten later!
// !!!

import com.squareup.moshi.Moshi
import com.stytch.java.b2b.models.ssoexternal.CreateConnectionRequest
import com.stytch.java.b2b.models.ssoexternal.CreateConnectionRequestOptions
import com.stytch.java.b2b.models.ssoexternal.CreateConnectionResponse
import com.stytch.java.b2b.models.ssoexternal.UpdateConnectionRequest
import com.stytch.java.b2b.models.ssoexternal.UpdateConnectionRequestOptions
import com.stytch.java.b2b.models.ssoexternal.UpdateConnectionResponse
import com.stytch.java.common.InstantAdapter
import com.stytch.java.common.StytchResult
import com.stytch.java.http.HttpClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.CompletableFuture

public interface External {
/**
* Create a new External SSO Connection.
*/
public suspend fun createConnection(
data: CreateConnectionRequest,
methodOptions: CreateConnectionRequestOptions? = null,
): StytchResult<CreateConnectionResponse>

/**
* Create a new External SSO Connection.
*/
public fun createConnection(
data: CreateConnectionRequest,
methodOptions: CreateConnectionRequestOptions? = null,
callback: (StytchResult<CreateConnectionResponse>) -> Unit,
)

/**
* Create a new External SSO Connection.
*/
public fun createConnectionCompletable(
data: CreateConnectionRequest,
methodOptions: CreateConnectionRequestOptions? = null,
): CompletableFuture<StytchResult<CreateConnectionResponse>>

/**
* Updates an existing External SSO connection.
*/
public suspend fun updateConnection(
data: UpdateConnectionRequest,
methodOptions: UpdateConnectionRequestOptions? = null,
): StytchResult<UpdateConnectionResponse>

/**
* Updates an existing External SSO connection.
*/
public fun updateConnection(
data: UpdateConnectionRequest,
methodOptions: UpdateConnectionRequestOptions? = null,
callback: (StytchResult<UpdateConnectionResponse>) -> Unit,
)

/**
* Updates an existing External SSO connection.
*/
public fun updateConnectionCompletable(
data: UpdateConnectionRequest,
methodOptions: UpdateConnectionRequestOptions? = null,
): CompletableFuture<StytchResult<UpdateConnectionResponse>>
}

internal class ExternalImpl(
private val httpClient: HttpClient,
private val coroutineScope: CoroutineScope,
) : External {
private val moshi = Moshi.Builder().add(InstantAdapter()).build()

override suspend fun createConnection(
data: CreateConnectionRequest,
methodOptions: CreateConnectionRequestOptions?,
): StytchResult<CreateConnectionResponse> =
withContext(Dispatchers.IO) {
var headers = emptyMap<String, String>()
methodOptions?.let {
headers = methodOptions.addHeaders(headers)
}

val asJson = moshi.adapter(CreateConnectionRequest::class.java).toJson(data)
httpClient.post("/v1/b2b/sso/external/${data.organizationId}", asJson, headers)
}

override fun createConnection(
data: CreateConnectionRequest,
methodOptions: CreateConnectionRequestOptions?,
callback: (StytchResult<CreateConnectionResponse>) -> Unit,
) {
coroutineScope.launch {
callback(createConnection(data, methodOptions))
}
}

override fun createConnectionCompletable(
data: CreateConnectionRequest,
methodOptions: CreateConnectionRequestOptions?,
): CompletableFuture<StytchResult<CreateConnectionResponse>> =
coroutineScope.async {
createConnection(data, methodOptions)
}.asCompletableFuture()

override suspend fun updateConnection(
data: UpdateConnectionRequest,
methodOptions: UpdateConnectionRequestOptions?,
): StytchResult<UpdateConnectionResponse> =
withContext(Dispatchers.IO) {
var headers = emptyMap<String, String>()
methodOptions?.let {
headers = methodOptions.addHeaders(headers)
}

val asJson = moshi.adapter(UpdateConnectionRequest::class.java).toJson(data)
httpClient.put("/v1/b2b/sso/external/${data.organizationId}/connections/${data.connectionId}", asJson, headers)
}

override fun updateConnection(
data: UpdateConnectionRequest,
methodOptions: UpdateConnectionRequestOptions?,
callback: (StytchResult<UpdateConnectionResponse>) -> Unit,
) {
coroutineScope.launch {
callback(updateConnection(data, methodOptions))
}
}

override fun updateConnectionCompletable(
data: UpdateConnectionRequest,
methodOptions: UpdateConnectionRequestOptions?,
): CompletableFuture<StytchResult<UpdateConnectionResponse>> =
coroutineScope.async {
updateConnection(data, methodOptions)
}.asCompletableFuture()
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ public data class DangerouslyGetRequest
*/
@Json(name = "member_id")
val memberId: String,
/**
* Whether to include deleted Members in the response. Defaults to false.
*/
@Json(name = "include_deleted")
val includeDeleted: Boolean? = null,
)

/**
Expand Down
37 changes: 35 additions & 2 deletions stytch/src/main/kotlin/com/stytch/java/b2b/models/sso/SSO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ public data class Connection
public data class ConnectionImplicitRoleAssignment
@JvmOverloads
constructor(
/**
* The unique identifier of the RBAC Role, provided by the developer and intended to be human-readable.
*
* Reserved `role_id`s that are predefined by Stytch include:
*
* * `stytch_member`
* * `stytch_admin`
*
* Check out the [guide on Stytch default Roles](https://stytch.com/docs/b2b/guides/rbac/stytch-default) for a more
* detailed explanation.
*
*
*/
@Json(name = "role_id")
val roleId: String,
)
Expand Down Expand Up @@ -99,8 +112,24 @@ public data class GetConnectionsRequestOptions
public data class GroupImplicitRoleAssignment
@JvmOverloads
constructor(
/**
* The unique identifier of the RBAC Role, provided by the developer and intended to be human-readable.
*
* Reserved `role_id`s that are predefined by Stytch include:
*
* * `stytch_member`
* * `stytch_admin`
*
* Check out the [guide on Stytch default Roles](https://stytch.com/docs/b2b/guides/rbac/stytch-default) for a more
* detailed explanation.
*
*
*/
@Json(name = "role_id")
val roleId: String,
/**
* The name of the group that grants the specified role assignment.
*/
@Json(name = "group")
val group: String,
)
Expand Down Expand Up @@ -218,7 +247,7 @@ public data class SAMLGroupImplicitRoleAssignment
@Json(name = "role_id")
val roleId: String,
/**
* The name of the SAML group that grants the specified role assignment.
* The name of the group that grants the specified role assignment.
*/
@Json(name = "group")
val group: String,
Expand Down Expand Up @@ -418,7 +447,7 @@ public data class DeleteConnectionRequest
@Json(name = "organization_id")
val organizationId: String,
/**
* The ID of the SSO connection. Both SAML and OIDC connection IDs can be provided.
* The ID of the SSO connection. SAML, OIDC, and External connection IDs can be provided.
*/
@Json(name = "connection_id")
val connectionId: String,
Expand Down Expand Up @@ -488,6 +517,10 @@ public data class GetConnectionsResponse
*/
@Json(name = "oidc_connections")
val oidcConnections: List<OIDCConnection>,
/**
* The list of [External Connections](https://stytch.com/docs/b2b/api/external-connection-object) owned by this
* organization.
*/
@Json(name = "external_connections")
val externalConnections: List<Connection>,
/**
Expand Down
Loading

0 comments on commit b605f3a

Please sign in to comment.