Android

For Android with AppAuth SDK

This quickstart guide serves as an integration example for our trusted partners so that you will be able to test and/or evaluate LoginID’s authentication service for your own needs.

Create your client API Keys

To obtain our client API keys among with other credentials you will need for our AppAuth SDK, please contact support@loginid.io

Install AppAuth SDK for your Build Environment

The following instructions are for your Android Studio environment. AppAuth SDK GitHhub can be found here.

This integration required Chrome browser running on Android devices.

Add AppAuth SDK to your Android Project

Add the following packages to your Android app module build.gradle dependencies

implementation 'net.openid:appauth:0.7.1'

Setup Custom URL Scheme for Oauth2 Redirect

The custom URI scheme = lid-[unique-id] part of your callbackURL. Add the following line to your Android app module build.gradle

manifestPlaceholders = [
'appAuthRedirectScheme': ''
]

Example:

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "api.login.oidc_test_app1"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
manifestPlaceholders = [
'appAuthRedirectScheme': 'lid-ec64bfaf-ad17-49f1-88ef-7a3df4197c48'
]
}
...
...
}

Configure OIDC Service Endpoints

Kotlin
Java
Kotlin
val serviceConfig = AuthorizationServiceConfiguration(
Uri.parse("https://oauth2.usw1.api.loginid.io/oauth2/auth"), // authorization endpoint
Uri.parse("https://oauth2.usw1.api.loginid.io/oauth2/token")) // token endpoint
Java
final AuthorizationServiceConfiguration serviceConfig = new AuthorizationServiceConfiguration(
Uri.parse("https://oauth2.sandbox-usw1.api.loginid.io/oauth2/auth"), // authorization endpoint
Uri.parse("https://oauth2.sandbox-usw1.api.loginid.io/oauth2/token")); // token endpoint

Configure authorization request in Activity's authorization event such as OnClickListener()

Kotlin
Java
Kotlin
val clientID = ""
val clientSecret = ""
val redirectURI = Uri.parse("://oauth2redirect")
// ...
// ...
// ...
// FIDO2 Oauth2 is currently worked on Android Chrome browser
val appAuthConfig: AppAuthConfiguration = net.openid.appauth.AppAuthConfiguration.Builder()
.setBrowserMatcher(BrowserWhitelist(VersionedBrowserMatcher.CHROME_CUSTOM_TAB))
.build()
authService = AuthorizationService(this, appAuthConfig)
val authRequestBuilder = AuthorizationRequest.Builder(
serviceConfig, // the authorization service configuration
clientID, // the client ID, typically pre-registered and static
ResponseTypeValues.CODE, // the response_type value: we want a code
redirectURI) // the redirect URI to which the auth response is sent
val authIntent = authService!!.getAuthorizationRequestIntent(authRequestBuilder.build())
startActivityForResult(authIntent, RC_AUTH)
Java
String clientID = "";
String clientSecret = "";
String redirectURI = Uri.parse("://oauth2redirect");
// ...
// ...
// ...
// FIDO2 Oauth2 is currently worked on Android Chrome browser
AppAuthConfiguration appAuthConfig = new AppAuthConfiguration.Builder()
.setBrowserMatcher(new BrowserWhitelist(VersionedBrowserMatcher.CHROME_CUSTOM_TAB))
.build();
authService = new AuthorizationService(this, appAuthConfig);
AuthorizationRequest.Builder authRequestBuilder = new AuthorizationRequest.Builder(
serviceConfig, // the authorization service configuration
clientID, // the client ID, typically pre-registered and static
ResponseTypeValues.CODE, // the response_type value: we want a code
redirectURI); // the redirect URI to which the auth response is sent
Intent authIntent = authService.getAuthorizationRequestIntent(authRequestBuilder.build());
startActivityForResult(authIntent, RC_AUTH);

Setup Oauth2 redirect handler in Activity's onActivityResult method

Kotlin
Java
Kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_AUTH) {
val resp = data?.let { AuthorizationResponse.fromIntent(it) }
val ex = AuthorizationException.fromIntent(data)
if (resp != null) {
// authorization completed
val request = resp.createTokenExchangeRequest()
val clientAuthentication: ClientAuthentication = ClientSecretBasic(clientSecret)
val callback = TokenResponseCallback { response, ex ->
if (response != null) {
// handle success response here
Log.e(TAG, "access token is " + response.accessToken)
} else {
// handle failure response here
Log.e(TAG, " error " + ex!!.errorDescription)
}
}
authService!!.performTokenRequest(
request,
clientAuthentication,
callback)
} else {
// authorization failed, check ex for more details
Log.e(TAG, " error " + ex!!.errorDescription)
}
} else {
// ...
// ...
}
}
Java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_AUTH) {
AuthorizationResponse resp = AuthorizationResponse.fromIntent(data);
AuthorizationException ex = AuthorizationException.fromIntent(data);
if (resp != null) {
// authorization completed
TokenRequest request = resp.createTokenExchangeRequest();
ClientAuthentication clientAuthentication = new ClientSecretBasic(clientSecret);
AuthorizationService.TokenResponseCallback callback = new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable AuthorizationException ex) {
if(response != null){
// handle success response here
Log.e(TAG,"access token is: " +response.accessToken);
} else {
// handle failure response here
Log.e(TAG, "Error: " + ex.errorDescription);
}
}
};
authService.performTokenRequest(
request,
clientAuthentication,
callback);
} else {
// authorization failed, check ex for more details
Log.e(TAG, " error " + ex.errorDescription);
}
} else {
// ...
// ...
}
}