Get started
Swift 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 client keeps the token fresh and sends both on every request. Authentication explains the credentials themselves.

Mint on your server

Your server mints a session token with its secret key, through the admin product:

import Foundation
import MountayaAdmin
import MountayaClient

// The body of your POST /session route. `userId` comes from your own sign-in, never the request.
func mintSession(for userId: String) async throws -> RestSchema.Session {
  let environment = ProcessInfo.processInfo.environment
  let server = try Mountaya(credential: .secret(key: environment["MOUNTAYA_SECRET_KEY"] ?? ""))
  let minted = try await server.admin.sessions.postSession(
    body: .init(publishable_key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "") {
      $0.user_id = userId
    })
  return minted.data
}

Answer the app with JSONEncoder().encode(session).

Keep the token fresh in the app

SessionTokenEngine asks your server for a token, and asks again before it expires. The app reads your server's address, MOUNTAYA_SESSION_URL, from Info.plist:

import Foundation
import MountayaClient

let info = Bundle.main.infoDictionary ?? [:]
let sessionURL = URL(string: info["MOUNTAYA_SESSION_URL"] as? String ?? "")

let sessions = SessionTokenEngine {
  guard let sessionURL else { throw URLError(.badURL) }
  var request = URLRequest(url: sessionURL)
  request.httpMethod = "POST"
  let (body, _) = try await URLSession.shared.data(for: request)
  return try JSONDecoder().decode(RestSchema.Session.self, from: body)
}
let mountaya = try Mountaya(
  credential: .publishable(
    key: info["MOUNTAYA_PUBLISHABLE_KEY"] as? String ?? "", sessionToken: .source(sessions)))

A request waits for the first token. Each later request sends the token the engine holds.

Open a shared itinerary

A share link carries a share token. It replaces the key, so a visitor needs no key and no session:

import Foundation
import MountayaClient

// The share link the visitor opened.
let link = URLComponents(string: "https://example.com/tour?share_token=share_your_share_token")
let shareToken = link?.queryItems?.first { $0.name == "share_token" }?.value ?? ""

let visitor = try Mountaya(credential: .share(token: shareToken))
let itinerary = try await visitor.studio.itineraries.getItinerary(
  itinerary_id: "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21")
print(itinerary.data.name)

A map built over that client draws the shared itinerary the same way.

Act as a member

A write acts as a member of your organization. Mint the session for the studio-embedded product and name the member, then write with it:

import Foundation
import MountayaAdmin
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let publishableKey = environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? ""
let server = try Mountaya(credential: .secret(key: environment["MOUNTAYA_SECRET_KEY"] ?? ""))
let session = try await server.admin.sessions.postSession(
  body: .init(publishable_key: publishableKey) {
    $0.surface = .studioEmbedded
    $0.user_id = "7a4e1c98-3b52-4f60-9d81-c05e2f7a83b1"
  }
).data

let member = try Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: .minted(session)))
let renamed = try await member.studio.itineraries.patchItinerary(
  itinerary_id: "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21",
  body: .init { $0.name = "Tour du Mont-Blanc, summer" })
print(renamed.data.name)

The publishable key needs the studio-embedded scope. Every write made under that token is that member's.

Keep the secret key out of the app

.secret(key:) exists only on macOS and Linux. An iOS, iPadOS, tvOS, watchOS, visionOS or Mac Catalyst build cannot name it, so the compiler stops a secret key from shipping in an app:

import Foundation
import MountayaClient

// Compiles for a server on macOS or Linux. In an iOS app, the compiler refuses `.secret`.
let environment = ProcessInfo.processInfo.environment
let server = try Mountaya(credential: .secret(key: environment["MOUNTAYA_SECRET_KEY"] ?? ""))

Keep the secret key in your server's environment, and never in Info.plist or an xcconfig.

Next steps

Last updated