Skip to main content

iOS SDK

The Idenplane iOS SDK provides native Swift bindings for the Idenplane Identity and Access Management server. It implements the OAuth 2.0 Authorization Code flow with PKCE (RFC 7636) using ASWebAuthenticationSession for secure, embedded authentication. Tokens are stored securely in the iOS Keychain and optional Face ID / Touch ID gating is built in.

Requirements​

RequirementVersion
Swift5.9+
iOS15.0+
macOS12.0+
Xcode15.0+

Installation​

note

The iOS SDK has no Swift Package Manager release yet. Its Package.swift lives in packages/idenplane-ios of the main repository, not at the root, and there are no iOS release tags, so SPM cannot resolve it from the repository URL. Use it as a local package.

Local Swift package​

Clone the repository next to your project:

git clone https://github.com/idenplane/idenplane.git

Then add the package by path in your Package.swift:

dependencies: [
.package(path: "../idenplane/packages/idenplane-ios")
]

and add the Idenplane product to your target:

.target(
name: "MyApp",
dependencies: [
.product(name: "Idenplane", package: "idenplane-ios")
]
)

In Xcode: File › Add Package Dependencies… › Add Local… and pick packages/idenplane-ios.


Setup​

1. Register a redirect URI​

In your Idenplane admin console, register your app's custom URL scheme as a redirect URI. For example:

com.example.myapp://callback

2. Configure URL scheme (for custom scheme redirects)​

In Info.plist:

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.example.myapp</string>
</array>
</dict>
</array>

3. Create the client​

import Idenplane

let authMe = IdenplaneClient(
serverUrl: URL(string: "https://auth.example.com")!,
realm: "my-realm",
clientId: "my-mobile-app",
redirectUri: "com.example.myapp://callback"
)

Or using the full AuthConfig:

let config = AuthConfig(
serverUrl: URL(string: "https://auth.example.com")!,
realm: "my-realm",
clientId: "my-mobile-app",
redirectUri: "com.example.myapp://callback",
scopes: ["openid", "profile", "email"],
autoRefresh: true,
refreshBuffer: 30
)
let authMe = IdenplaneClient(config: config)

Login Flow​

// In a SwiftUI view:
Button("Sign in") {
Task {
do {
try await authMe.login()
print("Logged in!")
} catch {
print("Login error: \(error.localizedDescription)")
}
}
}

login() opens an ASWebAuthenticationSession (Safari-based in-app browser) that navigates to the Idenplane authorization endpoint. When the user completes authentication, the browser redirects back to your app, and the SDK automatically exchanges the authorization code for tokens.


Authentication State​

Check if user is authenticated​

if authMe.isAuthenticated {
print("User is logged in")
}

Get the access token​

if let token = authMe.getAccessToken() {
// Attach to API requests
var request = URLRequest(url: apiURL)
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}

Fetch user info​

do {
let user = try await authMe.getUserInfo()
print("Hello, \(user.name ?? user.preferredUsername ?? "unknown")")
print("Email: \(user.email ?? "n/a")")
} catch {
print("Failed to fetch user info: \(error)")
}

Token Refresh​

Token refresh runs automatically in the background when autoRefresh: true (the default).

To refresh manually:

do {
try await authMe.refreshToken()
} catch IdenplaneError.noRefreshToken {
// Prompt user to log in again
} catch {
print("Refresh failed: \(error)")
}

Biometric Authentication​

Require Face ID or Touch ID before granting access to the token:

do {
let token = try await authMe.getAccessToken(
biometricReason: "Authenticate to access your account"
)
// token is only returned after successful biometric verification
} catch IdenplaneError.biometricAuthFailed(let reason) {
print("Biometric failed: \(reason)")
}

Checking availability​

let biometric = BiometricAuth()
switch biometric.biometryType {
case .faceID: print("Face ID available")
case .touchID: print("Touch ID available")
case .opticID: print("Optic ID available")
case .none: print("No biometrics — passcode only")
}

Logout​

await authMe.logout()
// Tokens are cleared from the Keychain and the refresh token is sent to the
// end-session endpoint. The browser's SSO cookie is not cleared, so the next
// login() can complete without a password prompt.

Error Handling​

All errors are typed as IdenplaneError:

do {
try await authMe.login()
} catch IdenplaneError.stateMismatch {
// Possible CSRF — abort
} catch IdenplaneError.serverError(let message) {
print("Server returned: \(message)")
} catch IdenplaneError.networkError(let underlying) {
print("Network: \(underlying)")
} catch {
print("Unknown error: \(error)")
}
ErrorDescription
notAuthenticatedNo valid session exists
tokenExpiredAccess token has expired
noRefreshTokenNo refresh token in storage
stateMismatchOAuth state parameter mismatch (possible CSRF)
pkceVerifierMissingPKCE verifier not found in storage
networkErrorUnderlying URLSession / transport error
serverErrorNon-2xx response from Idenplane server
biometricAuthFailedFace ID / Touch ID / passcode authentication failed
discoveryFailedCould not fetch the OIDC discovery document
callbackErrorError received in the authorization callback URL

Thread Safety​

IdenplaneClient is annotated @MainActor. All public methods must be called from the main actor. The SDK internally uses async/await for network and biometric operations.


SwiftUI Example​

A complete example using SwiftUI:

import SwiftUI
import Idenplane

@MainActor
class AuthViewModel: ObservableObject {
let client = IdenplaneClient(
serverUrl: URL(string: "https://auth.example.com")!,
realm: "demo",
clientId: "swiftui-app",
redirectUri: "com.example.swiftui://callback"
)

@Published var user: User?
@Published var errorMessage: String?

func login() {
Task {
do {
try await client.login()
user = try await client.getUserInfo()
} catch {
errorMessage = error.localizedDescription
}
}
}

func logout() {
Task {
await client.logout()
user = nil
}
}
}

struct ContentView: View {
@StateObject private var vm = AuthViewModel()

var body: some View {
VStack(spacing: 20) {
if let user = vm.user {
Text("Welcome, \(user.name ?? "User")!")
Button("Logout", action: vm.logout)
} else {
Button("Sign in with Idenplane", action: vm.login)
}
if let error = vm.errorMessage {
Text(error).foregroundColor(.red)
}
}
.padding()
}
}

Next Steps​

Quickstart Set up Idenplane and create your first authenticated app

Configuration Learn about all configuration options

API Reference Complete REST API documentation

Android SDK Idenplane SDK for Android with Kotlin


idenplane.com · GitHub · Discord