Sign-in in the Swift SDK
Sign an app in with the owner's approval, make a Server from that sign-in or a key, ask again for more access, and confirm it's the owner.
A Server is an address and a credential. A working copy, Folders and Client each take one. Authorization signs an app in with the owner's approval and keeps the sign-in in the keychain, so the app never sees the owner's password.
Sign in
import Marfa
let auth = try Authorization(
server: serverURL,
client: .init(
name: "Shoebox",
redirect: URL(string: "com.example.shoebox:/signed-in"),
scopes: ["core.file:read", "core.file:write"]),
service: "com.example.shoebox")
let credential: Credential
if let kept = try auth.credential() {
credential = kept
} else {
credential = try await auth.signIn(presentingFrom: window)
}
let server = try Server(url: serverURL, credential: credential)
let copy = try await WorkingCopy.open(store: storeURL, server: server)Make a Server with | For |
|---|---|
Server(url:credential:) | A sign-in from Authorization. Throws SignInError.otherServer, .notSignedIn, or .notAServer for an address holding a user or password. |
Server(url:key:) | An API key. Throws AddressError.userInfo for an address holding a user or password. |
Server.fromEnvironment() | MARFA_API_URL and MARFA_API_KEY, or nil when neither is set. Throws Server.EnvironmentError when only one is set, or when the address names no server. |
Server.description never shows the key. Show plainHTTPOrigin, set for plain http to another machine, as a warning. Every request names the contract the build was made for, and a server on another contract answers 412 contract_mismatch, which throws contractMismatch.
redirectis a private-use address: a reverse domain name you control, with a path and no host, claimed inCFBundleURLTypes.scopesare what the app asks for, andpermissions:adds instance permissions. The owner may approve fewer: checkcredential.grants(_:)before offering a control. On a credential a sign-in returned,scopesLeftOutlists each scope the approval left out, in the order the app named them.- The SDK reads sign-in metadata only at
/.well-known/oauth-authorization-server, and refuses anissueror endpoint outside the instance's origin.
Authorization call | Does |
|---|---|
credential() | The kept sign-in, or nil. |
sign, signIn(using:) | The browser sign-in, from a window or SwiftUI's webAuthenticationSession. |
startDeviceSignIn() | A DeviceSignIn for a device with no browser: show its userCode and verificationURL, then wait on its credential(). |
signOut() | Deletes the sign-in, then revokes it. Returns .endedAtServer, .nothingKept or .notConfirmed. |
A sign-in throws a SignInError: .canceled, .denied or .expired for the person's choice, .stateMismatch or .issuerMismatch for an approval it didn't ask for, .notSignedIn, .otherServer, .notAServer or .noRedirect for an unusable address or keychain, .refused, .unavailable or .unreadable for the server's answer, .discoveryTransport(reason:) with .dnsFailure, .connectionFailed or .untrustedCertificate when its sign-in metadata can't be fetched, .heldElsewhere when another process held the keychain, and .keptSignInChanged when another sign-in was kept meanwhile.
Once the server ends a sign-in, calls throw signedOut. Connect an app with OAuth explains the flow.
Sign in again for more access
To ask for more, make a new Authorization and sign in with it. Give it the same server and service, and a client with the same redirect that names the scopes the app needs now. The new sign-in reuses the app's OAuth client when the server still knows it and the flow and the redirect are unchanged. The owner's approval then widens the same grant, so the app keeps its webhooks, jobs, app.<vendor> namespace and the keys it minted.
Credential.clientIDis the ID of the app's OAuth client. The server reports it as an app's maker, such as a webhook'smaker.id, so compare the two to find what the app made. A sign-in that reuses the client keeps the same ID.- A new client is registered when the server no longer knows the kept one, such as after a reset or a restore, or when the flow or the redirect changed. That sign-in ends the grant it replaces, leaving one approval among the connected apps. Where
Credential.replacedGrantStandsistrue, the server didn't confirm the end: tell the person to remove the old one. - The approval can offer a scope the client didn't register, such as a type the owner made since. It leaves out a scope the instance can't grant, and an
app.<vendor>namespace the client's registration doesn't cover. The device flow refuses those withSignInError.refusedand the codeinvalid_scope. - A sign-in on the kept client throws
keptSignInChangedwhen a sign-in on another client was kept while it ran, in this process or another sharing the keychain. It keeps nothing, and the keychain holds the other sign-in.
A renewal whose new tokens the keychain can't keep signs the app out, and calls throw signedOut. The grant, and what the app made under it, stand at the server. The next sign-in registers a new client, which doesn't reach what the old client made. The old grant stays until the owner removes it from the connected apps.
Confirm it's you
Some acts need a sign-in from the last fifteen minutes: restoring an archive or backup, moving the sign-in address, changing allow_private_addresses, changing where files and backups go, and adding, changing or removing a type's guidance.
let server = try Server(url: serverURL, credential: credential).confirming(from: { window })A server made with confirming(from:) or confirming(using:) signs the owner in again and resends the call, one sheet at a time. Without it, or at a second challenge, the call throws confirmationRequired. A key can't make these calls and throws appSignInRequired, except guidance: a key the owner made writes it, and a key an app made throws forbidden.
The rules are in the keys and sign-in chapter of the contract.