Azure Active Directory Authentication Library (ADAL) for Android

repository·dev·Indexed 41 days ago

https://github.com/azuread/azure-activedirectory-library-for-android

A library for authenticating Android applications with Microsoft Azure AD Work Accounts, supporting OAuth2, multi-resource refresh tokens, and encrypted caching. Note: ADAL for Android was deprecated as of June 30, 2023; developers are recommended to migrate to MSAL for Android.

Tokens
8K
Snippets
25
Records
35
Agent score
52%

What's inside ADAL for Android

  1. Quick Start: Build and Run with Gradle

    dev

    To build and run the included sample application using Gradle, follow these steps:

    1. Clone the repository.
    2. Set up an emulator with SDK 23.
    3. Navigate to the root folder of the cloned repository.
    4. Connect your test device and run: ./gradlew :userappwithbroker:installDebug
    5. The app 'Fancy ADAL Test App' will be installed.
    6. Open the app, select an authority, and click Acquire Token to test authentication.

    Note: Configuration values like authority, clientId, redirectUri, and resource are defined in userappwithbroker/src/main/java/com/microsoft/aad/adal/example/userappwithbroker/Constants.java.

    ./gradlew :userappwithbroker:installDebug
  2. Quick Start: Integrate ADAL for Android

    dev

    To integrate the Microsoft Azure Active Directory Authentication Library (ADAL) into your Android project, follow these high-level steps:

    1. Add Library Reference: Add the ADAL library as an Android library dependency in your project.
    2. Configure Manifest: Add required permissions (INTERNET, ACCESS_NETWORK_STATE) and register the com.microsoft.aad.adal.AuthenticationActivity in your AndroidManifest.xml.
    3. Azure AD Registration:
      • Register your Web API service in Azure AD and note the APP ID URI.
      • Register your native client app in Azure AD, granting it permission to the Web API, and note the clientId and redirectUri.
    4. Initialize AuthenticationContext: Create an instance of AuthenticationContext in your main Activity.
    5. Handle Activity Results: Override onActivityResult in your Activity to pass results back to the AuthenticationContext.
    6. Acquire Tokens: Use acquireToken with an AuthenticationCallback to perform interactive logins.
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application ...>
        <activity
            android:name="com.microsoft.aad.adal.AuthenticationActivity" >
        </activity>
    </application>
  3. Quick Start: Build and Run with Maven

    dev

    To build and run the sample application using Maven, follow these steps:

    1. Clone the repository.
    2. Ensure Maven prerequisites are met (Maven 3.1.1+, Android SDK, etc.).
    3. Set up an emulator with SDK 19.
    4. Navigate to the root folder and run: mvn clean install
    5. Change directory to the Quick Start sample: cd userappwithbroker/
    6. Run the deployment command: mvn android:deploy android:run
    mvn clean install
    cd userappwithbroker/
    mvn android:deploy android:run
  4. Configure and Use Broker Authentication

    dev

    Broker authentication allows using Microsoft Intune's Company Portal or the Azure Authenticator App.

    Requirements:

    1. Enable Broker: Explicitly enable it using AuthenticationSettings.INSTANCE.setUseBroker(true);.
    2. Redirect URI: Register a special redirect URI in the format msauth://packagename/Base64UrlencodedSignature. You can retrieve this via mContext.getBrokerRedirectUri.
    3. Permissions:
      • Target SDK < 23: Requires GET_ACCOUNTS, USE_CREDENTIALS, and MANAGE_ACCOUNTS in the manifest.
      • Target SDK >= 23: USE_CREDENTIALS and MANAGE_ACCOUNTS are deprecated. GET_ACCOUNTS is a 'dangerous' permission and must be requested at runtime.
    4. Error Handling: If you receive DEVELOPER_BROKER_PERMISSIONS_MISSING, ensure you are using the latest version of ADAL and the broker app.

    Get Broker User: You can check if a valid broker account exists using mContext.getBrokerUser().

    // Enable broker usage
    AuthenticationSettings.INSTANCE.setUseBroker(true);
    
    // Get the current broker user
    String brokerAccount = mContext.getBrokerUser();
  5. Install ADAL for Android via Gradle

    dev

    To include the ADAL AAR package in your Android Studio project, add mavenCentral() to your repositories and include the dependency in your build.gradle file.

    Note: If your app already includes libraries like Gson or the Surface Duo SDK, you may need to exclude them from the ADAL compile task to avoid conflicts.

    repositories {
        mavenCentral()
        maven {
            // Surface Duo SDK dependency.
            url 'https://pkgs.dev.azure.com/MicrosoftDeviceSDK/DuoSDK-Public/_packaging/Duo-SDK-Feed%40Local/maven/v1'
        }
    }
    dependencies {
        // your dependencies here...
        compile('com.microsoft.aad:adal:3.0.1') {
            // if your app includes android support
            // libraries, Gson or Surface Duo SDK 
            // in its dependencies, exclude that
            // groupId from ADAL's compile task by 
            // un-commenting the appropriate line below
    
            // exclude group: 'com.android.support'
            // exclude group: 'com.google.code.gson'
            // exclude group: 'com.microsoft.device.display'
        }
    }
  6. Handle session cookies in WebView

    dev

    Because Android's WebView does not automatically clear session cookies when an app is closed, you may need to manually clear them to ensure a clean state for subsequent authentication attempts.

    CookieSyncManager.createInstance(getApplicationContext());
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeSessionCookie();
    CookieSyncManager.getInstance().sync();
  7. Understand token caching and SSO seeding

    dev

    ADAL manages tokens in a local cache to support seamless authentication.

    Seeding the cache for SSO: When performing a refresh token exchange, if the response contains an IdToken and a resource is specified, ADAL creates a TokenCacheItem containing:

    • The raw IdToken
    • UserInfo (extracted from the IdToken)
    • The tenantId

    This item is then updated in the cache via mTokenCacheAccessor.updateCachedItemWithResult. This process ensures that subsequent authentication requests can leverage the identity information for Single Sign-On (SSO) without re-authenticating the user.