All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.stytch.java.b2b.api.organizationsmembersoauthproviders.OrganizationsMembersOAuthProviders.kt Maven / Gradle / Ivy

There is a newer version: 7.3.0
Show newest version
package com.stytch.java.b2b.api.organizationsmembersoauthproviders

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

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.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
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 OAuthProviders {
    /**
     * Retrieve the saved Google 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.
     *
     * __Note:__ Google does not issue a refresh token on every login, and refresh tokens may expire if unused.
     * 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: ProviderInformationRequest): StytchResult

    /**
     * Retrieve the saved Google 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.
     *
     * __Note:__ Google does not issue a refresh token on every login, and refresh tokens may expire if unused.
     * 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 google(
        data: ProviderInformationRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * Retrieve the saved Google 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.
     *
     * __Note:__ Google does not issue a refresh token on every login, and refresh tokens may expire if unused.
     * 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: ProviderInformationRequest): CompletableFuture>

    /**
     * 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: ProviderInformationRequest): StytchResult

    /**
     * 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 fun microsoft(
        data: ProviderInformationRequest,
        callback: (StytchResult) -> Unit,
    )

    /**
     * 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 fun microsoftCompletable(data: ProviderInformationRequest): CompletableFuture>
}

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

    override suspend fun google(data: ProviderInformationRequest): StytchResult =
        withContext(Dispatchers.IO) {
            var headers = emptyMap()

            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> = 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: ProviderInformationRequest,
        callback: (StytchResult) -> Unit,
    ) {
        coroutineScope.launch {
            callback(google(data))
        }
    }

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

    override suspend fun microsoft(data: ProviderInformationRequest): StytchResult =
        withContext(Dispatchers.IO) {
            var headers = emptyMap()

            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> = moshi.adapter(type)
            val asMap = adapter.fromJson(asJson) ?: emptyMap()
            httpClient.get(
                "/v1/b2b/organizations/${data.organizationId}/members/${data.memberId}/oauth_providers/microsoft",
                asMap,
                headers,
            )
        }

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

    override fun microsoftCompletable(data: ProviderInformationRequest): CompletableFuture> =
        coroutineScope.async {
            microsoft(data)
        }.asCompletableFuture()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy