Skip to content

Commit

Permalink
Change MicrosoftRequest to ProviderInformationRequest (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifer-stytch authored Mar 28, 2024
1 parent 2f91a7f commit 0c096c0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.stytch.java.b2b.models.organizationsmembersoauthproviders.GoogleResponse
import com.stytch.java.b2b.models.organizationsmembersoauthproviders.MicrosoftRequest
import com.stytch.java.b2b.models.organizationsmembersoauthproviders.MicrosoftResponse
import com.stytch.java.b2b.models.organizationsmembersoauthproviders.ProviderInformationRequest
import com.stytch.java.common.InstantAdapter
import com.stytch.java.common.StytchResult
import com.stytch.java.http.HttpClient
Expand All @@ -34,7 +34,7 @@ public interface OAuthProviders {
* To force a refresh token to be issued, pass the `?provider_prompt=consent` query param into the
* [Start Google OAuth flow](https://stytch.com/docs/b2b/api/oauth-google-start) endpoint.
*/
public suspend fun google(data: MicrosoftRequest): StytchResult<GoogleResponse>
public suspend fun google(data: ProviderInformationRequest): StytchResult<GoogleResponse>

/**
* Retrieve the saved Google access token and ID token for a member. After a successful OAuth login, Stytch will save the
Expand All @@ -47,7 +47,7 @@ public interface OAuthProviders {
* [Start Google OAuth flow](https://stytch.com/docs/b2b/api/oauth-google-start) endpoint.
*/
public fun google(
data: MicrosoftRequest,
data: ProviderInformationRequest,
callback: (StytchResult<GoogleResponse>) -> Unit,
)

Expand All @@ -61,15 +61,15 @@ public interface OAuthProviders {
* To force a refresh token to be issued, pass the `?provider_prompt=consent` query param into the
* [Start Google OAuth flow](https://stytch.com/docs/b2b/api/oauth-google-start) endpoint.
*/
public fun googleCompletable(data: MicrosoftRequest): CompletableFuture<StytchResult<GoogleResponse>>
public fun googleCompletable(data: ProviderInformationRequest): CompletableFuture<StytchResult<GoogleResponse>>

/**
* Retrieve the saved Microsoft access token and ID token for a member. After a successful OAuth login, Stytch will save
* the
* issued access token and ID token from the identity provider. If a refresh token has been issued, Stytch will refresh the
* access token automatically.
*/
public suspend fun microsoft(data: MicrosoftRequest): StytchResult<MicrosoftResponse>
public suspend fun microsoft(data: ProviderInformationRequest): StytchResult<MicrosoftResponse>

/**
* Retrieve the saved Microsoft access token and ID token for a member. After a successful OAuth login, Stytch will save
Expand All @@ -78,7 +78,7 @@ public interface OAuthProviders {
* access token automatically.
*/
public fun microsoft(
data: MicrosoftRequest,
data: ProviderInformationRequest,
callback: (StytchResult<MicrosoftResponse>) -> Unit,
)

Expand All @@ -88,7 +88,7 @@ public interface OAuthProviders {
* issued access token and ID token from the identity provider. If a refresh token has been issued, Stytch will refresh the
* access token automatically.
*/
public fun microsoftCompletable(data: MicrosoftRequest): CompletableFuture<StytchResult<MicrosoftResponse>>
public fun microsoftCompletable(data: ProviderInformationRequest): CompletableFuture<StytchResult<MicrosoftResponse>>
}

internal class OAuthProvidersImpl(
Expand All @@ -97,36 +97,36 @@ internal class OAuthProvidersImpl(
) : OAuthProviders {
private val moshi = Moshi.Builder().add(InstantAdapter()).build()

override suspend fun google(data: MicrosoftRequest): StytchResult<GoogleResponse> =
override suspend fun google(data: ProviderInformationRequest): StytchResult<GoogleResponse> =
withContext(Dispatchers.IO) {
var headers = emptyMap<String, String>()

val asJson = moshi.adapter(MicrosoftRequest::class.java).toJson(data)
val asJson = moshi.adapter(ProviderInformationRequest::class.java).toJson(data)
val type = Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java)
val adapter: JsonAdapter<Map<String, Any>> = moshi.adapter(type)
val asMap = adapter.fromJson(asJson) ?: emptyMap()
httpClient.get("/v1/b2b/organizations/${data.organizationId}/members/${data.memberId}/oauth_providers/google", asMap, headers)
}

override fun google(
data: MicrosoftRequest,
data: ProviderInformationRequest,
callback: (StytchResult<GoogleResponse>) -> Unit,
) {
coroutineScope.launch {
callback(google(data))
}
}

override fun googleCompletable(data: MicrosoftRequest): CompletableFuture<StytchResult<GoogleResponse>> =
override fun googleCompletable(data: ProviderInformationRequest): CompletableFuture<StytchResult<GoogleResponse>> =
coroutineScope.async {
google(data)
}.asCompletableFuture()

override suspend fun microsoft(data: MicrosoftRequest): StytchResult<MicrosoftResponse> =
override suspend fun microsoft(data: ProviderInformationRequest): StytchResult<MicrosoftResponse> =
withContext(Dispatchers.IO) {
var headers = emptyMap<String, String>()

val asJson = moshi.adapter(MicrosoftRequest::class.java).toJson(data)
val asJson = moshi.adapter(ProviderInformationRequest::class.java).toJson(data)
val type = Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java)
val adapter: JsonAdapter<Map<String, Any>> = moshi.adapter(type)
val asMap = adapter.fromJson(asJson) ?: emptyMap()
Expand All @@ -138,15 +138,15 @@ internal class OAuthProvidersImpl(
}

override fun microsoft(
data: MicrosoftRequest,
data: ProviderInformationRequest,
callback: (StytchResult<MicrosoftResponse>) -> Unit,
) {
coroutineScope.launch {
callback(microsoft(data))
}
}

override fun microsoftCompletable(data: MicrosoftRequest): CompletableFuture<StytchResult<MicrosoftResponse>> =
override fun microsoftCompletable(data: ProviderInformationRequest): CompletableFuture<StytchResult<MicrosoftResponse>> =
coroutineScope.async {
microsoft(data)
}.asCompletableFuture()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,6 @@ public data class GoogleResponse
val refreshToken: String? = null,
)

/**
* Request type for `OAuthProviders.google`, `OAuthProviders.microsoft`.
*/
@JsonClass(generateAdapter = true)
public data class MicrosoftRequest
@JvmOverloads
constructor(
/**
* Globally unique UUID that identifies a specific Organization. The `organization_id` is critical to perform operations
* on an Organization, so be sure to preserve this value.
*/
@Json(name = "organization_id")
val organizationId: String,
/**
* Globally unique UUID that identifies a specific Member. The `member_id` is critical to perform operations on a Member,
* so be sure to preserve this value.
*/
@Json(name = "member_id")
val memberId: String,
/**
* Whether to return the refresh token Stytch has stored for the OAuth Provider. Defaults to false. **Important:** If your
* application exchanges the refresh token, Stytch may not be able to automatically refresh access tokens in the future.
*/
@Json(name = "include_refresh_token")
val includeRefreshToken: Boolean? = null,
)

/**
* Response type for `OAuthProviders.microsoft`.
*/
Expand Down Expand Up @@ -155,3 +128,30 @@ public data class MicrosoftResponse
@Json(name = "refresh_token")
val refreshToken: String? = null,
)

/**
* Request type for `OAuthProviders.google`, `OAuthProviders.microsoft`.
*/
@JsonClass(generateAdapter = true)
public data class ProviderInformationRequest
@JvmOverloads
constructor(
/**
* Globally unique UUID that identifies a specific Organization. The `organization_id` is critical to perform operations
* on an Organization, so be sure to preserve this value.
*/
@Json(name = "organization_id")
val organizationId: String,
/**
* Globally unique UUID that identifies a specific Member. The `member_id` is critical to perform operations on a Member,
* so be sure to preserve this value.
*/
@Json(name = "member_id")
val memberId: String,
/**
* Whether to return the refresh token Stytch has stored for the OAuth Provider. Defaults to false. **Important:** If your
* application exchanges the refresh token, Stytch may not be able to automatically refresh access tokens in the future.
*/
@Json(name = "include_refresh_token")
val includeRefreshToken: Boolean? = null,
)
2 changes: 1 addition & 1 deletion stytch/src/main/kotlin/com/stytch/java/common/Version.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.stytch.java.common

internal const val VERSION = "3.1.0"
internal const val VERSION = "4.0.0"
2 changes: 1 addition & 1 deletion version.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "3.1.0"
version = "4.0.0"

0 comments on commit 0c096c0

Please sign in to comment.