Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.stytch.java.b2b.api.sessions
// !!!
// 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.sessions.AuthenticateRequest
import com.stytch.java.b2b.models.sessions.AuthenticateResponse
import com.stytch.java.b2b.models.sessions.AuthorizationCheck
import com.stytch.java.b2b.models.sessions.ExchangeRequest
import com.stytch.java.b2b.models.sessions.ExchangeResponse
import com.stytch.java.b2b.models.sessions.GetJWKSRequest
import com.stytch.java.b2b.models.sessions.GetJWKSResponse
import com.stytch.java.b2b.models.sessions.GetRequest
import com.stytch.java.b2b.models.sessions.GetResponse
import com.stytch.java.b2b.models.sessions.MemberSession
import com.stytch.java.b2b.models.sessions.MigrateRequest
import com.stytch.java.b2b.models.sessions.MigrateResponse
import com.stytch.java.b2b.models.sessions.RevokeRequest
import com.stytch.java.b2b.models.sessions.RevokeResponse
import com.stytch.java.common.InstantAdapter
import com.stytch.java.common.JWTException
import com.stytch.java.common.JwtOptions
import com.stytch.java.common.ParseJWTClaimsOptions
import com.stytch.java.common.PolicyCache
import com.stytch.java.common.StytchException
import com.stytch.java.common.StytchResult
import com.stytch.java.common.StytchSessionClaim
import com.stytch.java.common.parseJWTClaims
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 org.jose4j.jwk.HttpsJwks
import java.time.Instant
import java.util.concurrent.CompletableFuture
public interface Sessions {
/**
* Retrieves all active Sessions for a Member.
*/
public suspend fun get(data: GetRequest): StytchResult
/**
* Retrieves all active Sessions for a Member.
*/
public fun get(
data: GetRequest,
callback: (StytchResult) -> Unit,
)
/**
* Retrieves all active Sessions for a Member.
*/
public fun getCompletable(data: GetRequest): CompletableFuture>
/**
* Authenticates a Session and updates its lifetime by the specified `session_duration_minutes`. If the
* `session_duration_minutes` is not specified, a Session will not be extended. This endpoint requires either a
* `session_jwt` or `session_token` be included in the request. It will return an error if both are present.
*
* You may provide a JWT that needs to be refreshed and is expired according to its `exp` claim. A new JWT will be
* returned if both the signature and the underlying Session are still valid.
*
* If an `authorization_check` object is passed in, this method will also check if the Member is authorized to perform the
* given action on the given Resource in the specified Organization. A Member is authorized if their Member Session
* contains a Role, assigned [explicitly or implicitly](https://stytch.com/docs/b2b/guides/rbac/role-assignment), with
* adequate permissions.
* In addition, the `organization_id` passed in the authorization check must match the Member's Organization.
*
* If the Member is not authorized to perform the specified action on the specified Resource, or if the
* `organization_id` does not match the Member's Organization, a 403 error will be thrown.
* Otherwise, the response will contain a list of Roles that satisfied the authorization check.
*/
public suspend fun authenticate(data: AuthenticateRequest): StytchResult
/**
* Authenticates a Session and updates its lifetime by the specified `session_duration_minutes`. If the
* `session_duration_minutes` is not specified, a Session will not be extended. This endpoint requires either a
* `session_jwt` or `session_token` be included in the request. It will return an error if both are present.
*
* You may provide a JWT that needs to be refreshed and is expired according to its `exp` claim. A new JWT will be
* returned if both the signature and the underlying Session are still valid.
*
* If an `authorization_check` object is passed in, this method will also check if the Member is authorized to perform the
* given action on the given Resource in the specified Organization. A Member is authorized if their Member Session
* contains a Role, assigned [explicitly or implicitly](https://stytch.com/docs/b2b/guides/rbac/role-assignment), with
* adequate permissions.
* In addition, the `organization_id` passed in the authorization check must match the Member's Organization.
*
* If the Member is not authorized to perform the specified action on the specified Resource, or if the
* `organization_id` does not match the Member's Organization, a 403 error will be thrown.
* Otherwise, the response will contain a list of Roles that satisfied the authorization check.
*/
public fun authenticate(
data: AuthenticateRequest,
callback: (StytchResult) -> Unit,
)
/**
* Authenticates a Session and updates its lifetime by the specified `session_duration_minutes`. If the
* `session_duration_minutes` is not specified, a Session will not be extended. This endpoint requires either a
* `session_jwt` or `session_token` be included in the request. It will return an error if both are present.
*
* You may provide a JWT that needs to be refreshed and is expired according to its `exp` claim. A new JWT will be
* returned if both the signature and the underlying Session are still valid.
*
* If an `authorization_check` object is passed in, this method will also check if the Member is authorized to perform the
* given action on the given Resource in the specified Organization. A Member is authorized if their Member Session
* contains a Role, assigned [explicitly or implicitly](https://stytch.com/docs/b2b/guides/rbac/role-assignment), with
* adequate permissions.
* In addition, the `organization_id` passed in the authorization check must match the Member's Organization.
*
* If the Member is not authorized to perform the specified action on the specified Resource, or if the
* `organization_id` does not match the Member's Organization, a 403 error will be thrown.
* Otherwise, the response will contain a list of Roles that satisfied the authorization check.
*/
public fun authenticateCompletable(data: AuthenticateRequest): CompletableFuture>
/**
* Revoke a Session and immediately invalidate all its tokens. To revoke a specific Session, pass either the
* `member_session_id`, `session_token`, or `session_jwt`. To revoke all Sessions for a Member, pass the `member_id`.
*/
public suspend fun revoke(data: RevokeRequest): StytchResult
/**
* Revoke a Session and immediately invalidate all its tokens. To revoke a specific Session, pass either the
* `member_session_id`, `session_token`, or `session_jwt`. To revoke all Sessions for a Member, pass the `member_id`.
*/
public fun revoke(
data: RevokeRequest,
callback: (StytchResult) -> Unit,
)
/**
* Revoke a Session and immediately invalidate all its tokens. To revoke a specific Session, pass either the
* `member_session_id`, `session_token`, or `session_jwt`. To revoke all Sessions for a Member, pass the `member_id`.
*/
public fun revokeCompletable(data: RevokeRequest): CompletableFuture>
/**
* Use this endpoint to exchange a Member's existing session for another session in a different Organization. This can be
* used to accept an invite, but not to create a new member via domain matching.
*
* To create a new member via domain matching, use the
* [Exchange Intermediate Session](https://stytch.com/docs/b2b/api/exchange-intermediate-session) flow instead.
*
* Only Email Magic Link, OAuth, and SMS OTP factors can be transferred between sessions. Other authentication factors,
* such as password factors, will not be transferred to the new session.
* Any OAuth Tokens owned by the Member will not be transferred to the new Organization.
* SMS OTP factors can be used to fulfill MFA requirements for the target Organization if both the original and target
* Member have the same phone number and the phone number is verified for both Members.
*
* If the Member is required to complete MFA to log in to the Organization, the returned value of `member_authenticated`
* will be `false`, and an `intermediate_session_token` will be returned.
* The `intermediate_session_token` can be passed into the
* [OTP SMS Authenticate endpoint](https://stytch.com/docs/b2b/api/authenticate-otp-sms) to complete the MFA step and
* acquire a full member session.
* The `intermediate_session_token` can also be used with the
* [Exchange Intermediate Session endpoint](https://stytch.com/docs/b2b/api/exchange-intermediate-session) or the
* [Create Organization via Discovery endpoint](https://stytch.com/docs/b2b/api/create-organization-via-discovery) to join
* a different Organization or create a new one.
* The `session_duration_minutes` and `session_custom_claims` parameters will be ignored.
*/
public suspend fun exchange(data: ExchangeRequest): StytchResult
/**
* Use this endpoint to exchange a Member's existing session for another session in a different Organization. This can be
* used to accept an invite, but not to create a new member via domain matching.
*
* To create a new member via domain matching, use the
* [Exchange Intermediate Session](https://stytch.com/docs/b2b/api/exchange-intermediate-session) flow instead.
*
* Only Email Magic Link, OAuth, and SMS OTP factors can be transferred between sessions. Other authentication factors,
* such as password factors, will not be transferred to the new session.
* Any OAuth Tokens owned by the Member will not be transferred to the new Organization.
* SMS OTP factors can be used to fulfill MFA requirements for the target Organization if both the original and target
* Member have the same phone number and the phone number is verified for both Members.
*
* If the Member is required to complete MFA to log in to the Organization, the returned value of `member_authenticated`
* will be `false`, and an `intermediate_session_token` will be returned.
* The `intermediate_session_token` can be passed into the
* [OTP SMS Authenticate endpoint](https://stytch.com/docs/b2b/api/authenticate-otp-sms) to complete the MFA step and
* acquire a full member session.
* The `intermediate_session_token` can also be used with the
* [Exchange Intermediate Session endpoint](https://stytch.com/docs/b2b/api/exchange-intermediate-session) or the
* [Create Organization via Discovery endpoint](https://stytch.com/docs/b2b/api/create-organization-via-discovery) to join
* a different Organization or create a new one.
* The `session_duration_minutes` and `session_custom_claims` parameters will be ignored.
*/
public fun exchange(
data: ExchangeRequest,
callback: (StytchResult) -> Unit,
)
/**
* Use this endpoint to exchange a Member's existing session for another session in a different Organization. This can be
* used to accept an invite, but not to create a new member via domain matching.
*
* To create a new member via domain matching, use the
* [Exchange Intermediate Session](https://stytch.com/docs/b2b/api/exchange-intermediate-session) flow instead.
*
* Only Email Magic Link, OAuth, and SMS OTP factors can be transferred between sessions. Other authentication factors,
* such as password factors, will not be transferred to the new session.
* Any OAuth Tokens owned by the Member will not be transferred to the new Organization.
* SMS OTP factors can be used to fulfill MFA requirements for the target Organization if both the original and target
* Member have the same phone number and the phone number is verified for both Members.
*
* If the Member is required to complete MFA to log in to the Organization, the returned value of `member_authenticated`
* will be `false`, and an `intermediate_session_token` will be returned.
* The `intermediate_session_token` can be passed into the
* [OTP SMS Authenticate endpoint](https://stytch.com/docs/b2b/api/authenticate-otp-sms) to complete the MFA step and
* acquire a full member session.
* The `intermediate_session_token` can also be used with the
* [Exchange Intermediate Session endpoint](https://stytch.com/docs/b2b/api/exchange-intermediate-session) or the
* [Create Organization via Discovery endpoint](https://stytch.com/docs/b2b/api/create-organization-via-discovery) to join
* a different Organization or create a new one.
* The `session_duration_minutes` and `session_custom_claims` parameters will be ignored.
*/
public fun exchangeCompletable(data: ExchangeRequest): CompletableFuture>
/**
* Migrate a session from an external OIDC compliant endpoint. Stytch will call the external UserInfo endpoint defined in
* your Stytch Project settings in the [Dashboard](/dashboard), and then perform a lookup using the `session_token`. If
* the response contains a valid email address, Stytch will attempt to match that email address with a Member in your
* Organization and create a Stytch Session.
*/
public suspend fun migrate(data: MigrateRequest): StytchResult
/**
* Migrate a session from an external OIDC compliant endpoint. Stytch will call the external UserInfo endpoint defined in
* your Stytch Project settings in the [Dashboard](/dashboard), and then perform a lookup using the `session_token`. If
* the response contains a valid email address, Stytch will attempt to match that email address with a Member in your
* Organization and create a Stytch Session.
*/
public fun migrate(
data: MigrateRequest,
callback: (StytchResult) -> Unit,
)
/**
* Migrate a session from an external OIDC compliant endpoint. Stytch will call the external UserInfo endpoint defined in
* your Stytch Project settings in the [Dashboard](/dashboard), and then perform a lookup using the `session_token`. If
* the response contains a valid email address, Stytch will attempt to match that email address with a Member in your
* Organization and create a Stytch Session.
*/
public fun migrateCompletable(data: MigrateRequest): CompletableFuture>
/**
* Get the JSON Web Key Set (JWKS) for a project.
*
* JWKS are rotated every ~6 months. Upon rotation, new JWTs will be signed using the new key set, and both key sets will
* be returned by this endpoint for a period of 1 month.
*
* JWTs have a set lifetime of 5 minutes, so there will be a 5 minute period where some JWTs will be signed by the old
* JWKS, and some JWTs will be signed by the new JWKS. The correct JWKS to use for validation is determined by matching
* the `kid` value of the JWT and JWKS.
*
* If you're using one of our [backend SDKs](https://stytch.com/docs/b2b/sdks), the JWKS roll will be handled for you.
*
* If you're using your own JWT validation library, many have built-in support for JWKS rotation, and you'll just need to
* supply this API endpoint. If not, your application should decide which JWKS to use for validation by inspecting the
* `kid` value.
*/
public suspend fun getJWKS(data: GetJWKSRequest): StytchResult
/**
* Get the JSON Web Key Set (JWKS) for a project.
*
* JWKS are rotated every ~6 months. Upon rotation, new JWTs will be signed using the new key set, and both key sets will
* be returned by this endpoint for a period of 1 month.
*
* JWTs have a set lifetime of 5 minutes, so there will be a 5 minute period where some JWTs will be signed by the old
* JWKS, and some JWTs will be signed by the new JWKS. The correct JWKS to use for validation is determined by matching
* the `kid` value of the JWT and JWKS.
*
* If you're using one of our [backend SDKs](https://stytch.com/docs/b2b/sdks), the JWKS roll will be handled for you.
*
* If you're using your own JWT validation library, many have built-in support for JWKS rotation, and you'll just need to
* supply this API endpoint. If not, your application should decide which JWKS to use for validation by inspecting the
* `kid` value.
*/
public fun getJWKS(
data: GetJWKSRequest,
callback: (StytchResult) -> Unit,
)
/**
* Get the JSON Web Key Set (JWKS) for a project.
*
* JWKS are rotated every ~6 months. Upon rotation, new JWTs will be signed using the new key set, and both key sets will
* be returned by this endpoint for a period of 1 month.
*
* JWTs have a set lifetime of 5 minutes, so there will be a 5 minute period where some JWTs will be signed by the old
* JWKS, and some JWTs will be signed by the new JWKS. The correct JWKS to use for validation is determined by matching
* the `kid` value of the JWT and JWKS.
*
* If you're using one of our [backend SDKs](https://stytch.com/docs/b2b/sdks), the JWKS roll will be handled for you.
*
* If you're using your own JWT validation library, many have built-in support for JWKS rotation, and you'll just need to
* supply this API endpoint. If not, your application should decide which JWKS to use for validation by inspecting the
* `kid` value.
*/
public fun getJWKSCompletable(data: GetJWKSRequest): CompletableFuture>
// MANUAL(authenticateJWT_interface)(INTERFACE_METHOD)
// ADDIMPORT: import com.stytch.java.b2b.models.sessions.AuthorizationCheck
// ADDIMPORT: import com.stytch.java.b2b.models.sessions.MemberSession
// ADDIMPORT: import com.stytch.java.common.JWTException
// ADDIMPORT: import com.stytch.java.common.ParseJWTClaimsOptions
// ADDIMPORT: import com.stytch.java.common.StytchSessionClaim
// ADDIMPORT: import com.stytch.java.common.parseJWTClaims
/** Parse a JWT and verify the signature, preferring local verification over remote.
*
* If maxTokenAgeSeconds is set, remote verification will be forced if the JWT was issued at
* (based on the "iat" claim) more than that many seconds ago.
*
* To force remote validation for all tokens, set maxTokenAgeSeconds to zero or use the
* authenticate method instead.
*/
public suspend fun authenticateJwt(
jwt: String,
maxTokenAgeSeconds: Int?,
authorizationCheck: AuthorizationCheck? = null,
): StytchResult
/** Parse a JWT and verify the signature, preferring local verification over remote.
*
* If maxTokenAgeSeconds is set, remote verification will be forced if the JWT was issued at
* (based on the "iat" claim) more than that many seconds ago.
*
* To force remote validation for all tokens, set maxTokenAgeSeconds to zero or use the
* authenticate method instead.
*/
public fun authenticateJwt(
jwt: String,
maxTokenAgeSeconds: Int?,
authorizationCheck: AuthorizationCheck? = null,
callback: (StytchResult) -> Unit,
)
/** Parse a JWT and verify the signature, preferring local verification over remote.
*
* If maxTokenAgeSeconds is set, remote verification will be forced if the JWT was issued at
* (based on the "iat" claim) more than that many seconds ago.
*
* To force remote validation for all tokens, set maxTokenAgeSeconds to zero or use the
* authenticate method instead.
*/
public fun authenticateJwtCompletable(
jwt: String,
maxTokenAgeSeconds: Int?,
authorizationCheck: AuthorizationCheck? = null,
): CompletableFuture>
/** Parse a JWT and verify the signature locally (without calling /authenticate in the API).
*
* If maxTokenAgeSeconds is set, this will return an error if the JWT was issued (based on the "iat"
* claim) more than maxTokenAge seconds ago.
*
* If maxTokenAgeSeconds is explicitly set to zero, all tokens will be considered too old,
* even if they are otherwise valid.
*
* The value for leeway is the maximum allowable difference when comparing
* timestamps. It defaults to zero.
*/
public suspend fun authenticateJwtLocal(
jwt: String,
maxTokenAgeSeconds: Int?,
authorizationCheck: AuthorizationCheck? = null,
leeway: Int = 0,
): StytchResult
/** Parse a JWT and verify the signature locally (without calling /authenticate in the API).
*
* If maxTokenAgeSeconds is set, this will return an error if the JWT was issued (based on the "iat"
* claim) more than maxTokenAge seconds ago.
*
* If maxTokenAgeSeconds is explicitly set to zero, all tokens will be considered too old,
* even if they are otherwise valid.
*
* The value for leeway is the maximum allowable difference when comparing
* timestamps. It defaults to zero.
*/
public fun authenticateJwtLocal(
jwt: String,
maxTokenAgeSeconds: Int?,
authorizationCheck: AuthorizationCheck? = null,
leeway: Int = 0,
callback: (StytchResult) -> Unit,
)
/** Parse a JWT and verify the signature locally (without calling /authenticate in the API).
*
* If maxTokenAgeSeconds is set, this will return an error if the JWT was issued (based on the "iat"
* claim) more than maxTokenAge seconds ago.
*
* If maxTokenAgeSeconds is explicitly set to zero, all tokens will be considered too old,
* even if they are otherwise valid.
*
* The value for leeway is the maximum allowable difference when comparing
* timestamps. It defaults to zero.
*/
public suspend fun authenticateJwtLocalCompletable(
jwt: String,
maxTokenAgeSeconds: Int?,
authorizationCheck: AuthorizationCheck? = null,
leeway: Int = 0,
): CompletableFuture>
// ENDMANUAL(authenticateJWT_interface)
}
internal class SessionsImpl(
private val httpClient: HttpClient,
private val coroutineScope: CoroutineScope,
private val jwksClient: HttpsJwks,
private val jwtOptions: JwtOptions,
private val policyCache: PolicyCache,
) : Sessions {
private val moshi = Moshi.Builder().add(InstantAdapter()).build()
override suspend fun get(data: GetRequest): StytchResult =
withContext(Dispatchers.IO) {
var headers = emptyMap()
val asJson = moshi.adapter(GetRequest::class.java).toJson(data)
val type = Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java)
val adapter: JsonAdapter