Get started
Kotlin SDK
Get started

Authentication in the SDK

Authentication in the SDK

An app holds a publishable key and a session token your server mints. The server holds the secret key, which no Android or iOS build can even name.

Mint on your server

On the JVM, a client over the secret key mints a token for the member your own sign-in vouches for:

import com.mountaya.admin.admin
import com.mountaya.admin.generated.AdminSchema
import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema

// `server` is built with Credential.Secret. `userId` is the member your own sign-in vouches for.
suspend fun mintFor(server: Mountaya, publishableKey: String, userId: String): RestSchema.Session =
    server.admin.sessions
        .postSession(AdminSchema.SessionCreateParams(publishable_key = publishableKey) { user_id = userId })
        .data

Answer the app with that Session as JSON. The server quickstart serves it.

Keep the token fresh in the app

SessionTokenEngine mints through your server and refreshes before each token expires. Hand it to the client as the token's source:

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mountaya.client.Credential
import com.mountaya.client.Mountaya
import com.mountaya.client.SessionToken
import com.mountaya.client.SessionTokenEngine
import com.mountaya.client.generated.RestSchema
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import java.net.HttpURLConnection
import java.net.URI

class TrailViewModel : ViewModel() {
    private val sessions = SessionTokenEngine(viewModelScope, mint = { fetchSession() })

    val mountaya = Mountaya(Credential.Publishable(BuildConfig.MOUNTAYA_PUBLISHABLE_KEY, SessionToken.Source(sessions)))

    override fun onCleared() = mountaya.close()

    // Your server answers the Session that postSession minted.
    private suspend fun fetchSession(): RestSchema.Session =
        withContext(Dispatchers.IO) {
            val connection = URI.create(BuildConfig.MOUNTAYA_SESSION_URL).toURL().openConnection() as HttpURLConnection
            connection.requestMethod = "POST"
            connection.doOutput = true
            connection.setRequestProperty("Content-Type", "application/json")
            connection.outputStream.use { it.write("{}".encodeToByteArray()) }
            val body = connection.inputStream.use { it.readBytes().decodeToString() }
            Json { ignoreUnknownKeys = true }.decodeFromString(RestSchema.Session.serializer(), body)
        }
}

A request made before the first token waits for it. Call sessions.refresh() after a sign-in, and sessions.disable() on a sign-out.

Open a shared itinerary

A visitor holding a share link reads the shared resource with its token in place of the key:

import com.mountaya.client.Credential
import com.mountaya.client.Mountaya

// The token at the end of the share link the visitor opened.
suspend fun openShared(shareToken: String = "share_your_share_token") {
    val visitor = Mountaya(Credential.Share(shareToken))
    val itinerary = visitor.studio.itineraries.getItinerary("0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
    println(itinerary.data.name)
    visitor.close()
}

Act as a member

A write, a collaboration stream or an agent run acts as a member. Mint its token for that member and for Embedded Studio:

import com.mountaya.admin.admin
import com.mountaya.admin.generated.AdminSchema
import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema

suspend fun mintWriter(server: Mountaya, publishableKey: String): RestSchema.Session =
    server.admin.sessions
        .postSession(
            AdminSchema.SessionCreateParams(publishable_key = publishableKey) {
                user_id = "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1"
                surface = RestSchema.SessionSurface.StudioEmbedded
            },
        ).data

The app's client then writes as that member:

import com.mountaya.client.Mountaya
import com.mountaya.client.generated.RestSchema

suspend fun rename(mountaya: Mountaya) {
    val renamed =
        mountaya.studio.itineraries.patchItinerary(
            "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21",
            body = RestSchema.ItineraryUpdateParams { name = "Tour du Mont-Blanc, summer" },
        )
    println(renamed.data.name)
}

Keep the secret key out of the app

Credential.Secret and Credential(secretKey = …) are declared on the JVM alone. An Android or iOS build has no symbol to call, so a secret key cannot ship in an app by mistake.

Next steps

Last updated