Android SDK
Native Kotlin SDK for Android applications using AppAuth for secure OAuth 2.0 PKCE authentication with encrypted storage.
Features
- ✅ OAuth 2.0 PKCE flow via AppAuth
- ✅ EncryptedSharedPreferences for secure token storage
- ✅ Kotlin Coroutines with suspend functions
- ✅ Automatic token refresh
- ✅ License and entitlement checking
- ✅ Jetpack Compose compatible
Installation
Gradle (Kotlin DSL)
dependencies {
implementation("one.optare:optareid-android:0.1.0")
}Gradle (Groovy)
dependencies {
implementation 'one.optare:optareid-android:0.1.0'
}Configuration
1. Configure Redirect URI
Add to your AndroidManifest.xml:
<activity
android:name="net.openid.appauth.RedirectUriReceiverActivity"
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="myapp" android:host="callback" />
</intent-filter>
</activity>2. Initialize SDK
import one.optare.optareid.OptareAuth
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
OptareAuth.configure(
context = applicationContext,
clientId = "your-client-id",
redirectUri = "myapp://callback"
)
}
}Usage
Login
import one.optare.optareid.OptareAuth
class LoginActivity : AppCompatActivity() {
private fun login() {
lifecycleScope.launch {
try {
val user = OptareAuth.login(this@LoginActivity)
Log.d("Auth", "Logged in as ${user.name}")
} catch (e: Exception) {
Log.e("Auth", "Login failed", e)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
lifecycleScope.launch {
OptareAuth.handleAuthResult(requestCode, resultCode, data)
}
}
}Check Authentication Status
if (OptareAuth.isAuthenticated) {
val user = OptareAuth.currentUser
println("Logged in as ${user?.name}")
}Get Access Token
// Automatically refreshes if expired
val token = OptareAuth.getAccessToken()Logout
OptareAuth.logout()License Checking
// Check if user has a product license
if (OptareAuth.hasLicense("premium-plan")) {
// Show premium features
}
// Check for specific entitlements
if (OptareAuth.hasEntitlement("ai-access")) {
// Enable AI features
}OptareUser Properties
data class OptareUser(
val id: String, // Unique user identifier
val email: String?, // Email address
val emailVerified: Boolean?, // Email verification status
val name: String?, // Display name
val image: String?, // Profile picture URL
val organizationId: String?, // Organization ID
val licenses: List<String>, // Product licenses
val entitlements: List<String>, // Feature entitlements
val roles: List<String> // User roles
)Error Handling
try {
val user = OptareAuth.login(activity)
} catch (e: OptareException.NotConfigured) {
Log.e("Auth", "SDK not configured")
} catch (e: OptareException.AuthorizationFailed) {
Log.e("Auth", "Auth failed: ${e.message}")
} catch (e: OptareException.TokenError) {
Log.e("Auth", "Token error: ${e.message}")
}Requirements
- Android API 24+ (Android 7.0)
- Kotlin 1.9+
- AndroidX Security Crypto
Dependencies
- AppAuth for Android (opens in a new tab) - OAuth 2.0 / OIDC
- EncryptedSharedPreferences (opens in a new tab) - Secure storage
- Kotlinx Serialization (opens in a new tab) - JSON parsing
See GitHub Repository (opens in a new tab) for full source code.