Skip to main content

Android SDK

The Idenplane Android SDK provides native Kotlin bindings for the Idenplane Identity and Access Management server. It implements the OAuth 2.0 Authorization Code flow with PKCE (RFC 7636) using Chrome Custom Tabs for secure, embedded authentication. Tokens are stored securely with EncryptedSharedPreferences (AES-256-GCM) and optional biometric gating is available via BiometricPrompt.

Requirements​

RequirementVersion
Kotlin1.9+
Android minSdk24 (Android 7.0)
Android targetSdk34 (Android 14)
AGP8.x

Installation​

The SDK is published to Maven Central as com.idenplane:idenplane-android.

mavenCentral() is part of the default Gradle repositories — no extra repository setup is needed:

dependencies {
implementation("com.idenplane:idenplane-android:1.0.0")
}

Required permissions in AndroidManifest.xml​

<uses-permission android:name="android.permission.INTERNET" />

Setup​

1. Register a redirect URI​

In your Idenplane admin console, register a custom scheme redirect URI for your app:

com.example.myapp://callback

2. Add an intent filter to your Activity​

In AndroidManifest.xml, register the Activity that will handle the OAuth callback:

<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="com.example.myapp"
android:host="callback" />
</intent-filter>
</activity>

3. Create the client​

import com.idenplane.sdk.AuthConfig
import com.idenplane.sdk.IdenplaneClient

val authMe = IdenplaneClient(
context = applicationContext,
serverUrl = "https://auth.example.com",
realm = "my-realm",
clientId = "my-android-app",
redirectUri = "com.example.myapp://callback"
)

Or using AuthConfig for more options:

val config = AuthConfig(
serverUrl = "https://auth.example.com",
realm = "my-realm",
clientId = "my-android-app",
redirectUri = "com.example.myapp://callback",
scopes = listOf("openid", "profile", "email"),
autoRefresh = true,
refreshBuffer = 30
)
val authMe = IdenplaneClient(applicationContext, config)

Login Flow​

Initiate login​

// In your Activity or Fragment:
binding.signInButton.setOnClickListener {
lifecycleScope.launch {
try {
authMe.login(this@MainActivity)
// Browser opens — wait for the redirect callback
} catch (e: IdenplaneException) {
showError(e.message)
}
}
}

Handle the redirect callback​

In your Activity, override onNewIntent (and optionally onResume):

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
handleAuthCallback(intent)
}

override fun onResume() {
super.onResume()
handleAuthCallback(intent)
}

private fun handleAuthCallback(intent: Intent?) {
lifecycleScope.launch {
try {
val handled = authMe.handleRedirectIntent(intent)
if (handled) {
// Login successful — navigate to home screen
navigateToHome()
}
} catch (e: IdenplaneException.StateMismatch) {
showError("Security error: state mismatch")
} catch (e: IdenplaneException.CallbackError) {
showError("Login failed: ${e.message}")
}
}
}

Authentication State​

Check if user is authenticated​

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

Get the access token​

val token = authMe.getAccessToken()
if (token != null) {
// Attach to API requests
val request = Request.Builder()
.url(apiUrl)
.header("Authorization", "Bearer $token")
.build()
}

Fetch user info​

lifecycleScope.launch {
try {
val user = authMe.getUserInfo()
println("Hello, ${user.name ?: user.preferredUsername ?: "User"}")
println("Email: ${user.email}")
} catch (e: IdenplaneException.NotAuthenticated) {
redirectToLogin()
}
}

Token Refresh​

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

To refresh manually:

lifecycleScope.launch {
try {
authMe.refreshToken()
} catch (e: IdenplaneException.NoRefreshToken) {
// Prompt user to log in again
redirectToLogin()
} catch (e: IdenplaneException) {
showError("Refresh failed: ${e.message}")
}
}

Biometric Authentication​

Require fingerprint, face, or device credentials before accessing the token:

lifecycleScope.launch {
try {
val token = authMe.getAccessToken(
activity = this@MainActivity,
title = "Verify your identity"
)
// token is non-null only after successful biometric auth
useToken(token)
} catch (e: IdenplaneException.BiometricAuthFailed) {
showError("Biometric failed: ${e.message}")
}
}

Standalone biometric prompt​

val biometric = BiometricAuth(activity)

if (biometric.isBiometricAvailable) {
lifecycleScope.launch {
try {
biometric.authenticate(
title = "Confirm identity",
subtitle = "Access your secure data",
description = "Use your fingerprint or face to continue"
)
// Proceed with sensitive operation
} catch (e: IdenplaneException.BiometricAuthFailed) {
showError(e.message)
}
}
}

Logout​

lifecycleScope.launch {
authMe.logout()
// Tokens are cleared locally and the refresh token is revoked server-side.
redirectToLogin()
}

Passing the current Activity also clears the browser-side session (a silent back-channel call alone can't touch cookies set during login) — requires that a valid ID token was stored from a prior login, and opens a Custom Tab showing a blank response the user has to dismiss (deliberately not redirected back to the app, to avoid it being misread as a login callback):

lifecycleScope.launch {
authMe.logout(this@MainActivity)
redirectToLogin()
}

Lifecycle Management​

Cancel background auto-refresh jobs when the client is no longer needed:

override fun onDestroy() {
super.onDestroy()
authMe.destroy()
}

Error Handling​

All SDK errors are subclasses of IdenplaneException:

try {
authMe.login(activity)
} catch (e: IdenplaneException.StateMismatch) {
// Possible CSRF — abort and clear state
} catch (e: IdenplaneException.ServerError) {
showError("Server: ${e.message}")
} catch (e: IdenplaneException.NetworkError) {
showError("Network: ${e.message}")
} catch (e: IdenplaneException) {
showError(e.message)
}
ExceptionDescription
NotAuthenticatedNo valid session exists
TokenExpiredAccess token has expired
NoRefreshTokenNo refresh token in storage
StateMismatchOAuth state parameter mismatch (possible CSRF)
PkceVerifierMissingPKCE verifier missing from storage
NetworkErrorHTTP / IO error
ServerErrorNon-2xx response from the Idenplane server
CallbackErrorError present in the redirect callback URI
DiscoveryFailedOIDC discovery document fetch failed
BiometricAuthFailedBiometric / device credential authentication failed
LoginCancelledUser cancelled the login flow

ViewModel Example​

A complete example using Android Jetpack ViewModel:

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.idenplane.sdk.IdenplaneClient
import com.idenplane.sdk.IdenplaneException
import com.idenplane.sdk.User
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class AuthViewModel(private val authMe: IdenplaneClient) : ViewModel() {

private val _user = MutableStateFlow<User?>(null)
val user: StateFlow<User?> = _user

private val _error = MutableStateFlow<String?>(null)
val error: StateFlow<String?> = _error

fun login(activity: androidx.fragment.app.FragmentActivity) {
viewModelScope.launch {
runCatching { authMe.login(activity) }
.onFailure { _error.value = it.message }
}
}

fun handleCallback(intent: android.content.Intent?) {
viewModelScope.launch {
runCatching {
val handled = authMe.handleRedirectIntent(intent)
if (handled) {
_user.value = authMe.getUserInfo()
}
}.onFailure { _error.value = it.message }
}
}

fun logout() {
viewModelScope.launch {
authMe.logout()
_user.value = null
}
}
}

Jetpack Compose​

Compose support is optional — its dependencies are compileOnly in the SDK, so plain View-based apps don't pull in the Compose runtime. To use it, add Compose to your own app:

dependencies {
implementation(platform("androidx.compose:compose-bom:2024.02.00"))
implementation("androidx.compose.runtime:runtime")
}
import com.idenplane.sdk.rememberIdenplaneClient
import com.idenplane.sdk.collectAuthStateAsState

@Composable
fun App(config: AuthConfig) {
val authMe = rememberIdenplaneClient(LocalContext.current, config)
val isAuthenticated by authMe.collectAuthStateAsState()

if (isAuthenticated) HomeScreen() else LoginScreen(authMe)
}

rememberIdenplaneClient scopes the client to the composition and calls destroy() when it leaves. collectAuthStateAsState reacts to login/logout/refresh outcomes — it does not poll for token expiry on its own, so still call isAuthenticated or getAccessToken() directly where up-to-the-second accuracy matters.


Next Steps​

Quickstart Set up Idenplane and create your first authenticated app

Configuration Learn about all configuration options

API Reference Complete REST API documentation

JavaScript SDK Core SDK for non-React applications


idenplane.com · GitHub · Discord