ForgeRock SDKs 3.3

Use centralized login

The following diagram shows a sample OAuth 2.0 flow that the ForgeRock SDK uses for your native app:

OAuth 2.0 for Native Apps

Note that the SDKs also support PKCE.

ForgeRock Android SDK apps

This section describes how to configure your ForgeRock Android SDK application to use centralized login by leveraging the AppAuth library:

  1. Add the build dependency to the build.gradle file:

    implementation 'net.openid:appauth:0.7.1'
  2. Configure the app to allow AppAuth to capture authorization redirects.

    Add the custom scheme your app will use to your build.gradle file:

    android.defaultConfig.manifestPlaceholders = [
        'appAuthRedirectScheme': 'com.example.sdkapp'
    ]
  3. When using a custom scheme, you can easily configure AppAuth to capture all redirects using this custom scheme through a manifest placeholder instead:

    1. Configure the redirect URI by adding an <intent-filter> for AppAuth’s RedirectUriReceiverActivity to your AndroidManifest.xml:

      <activity
         android:name="net.openid.appauth.RedirectUriReceiverActivity"
         tools:node="replace">
          <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.forgerock.android"/>
          </intent-filter>
      </activity>
    2. Add the following to the strings.xml file:

      <string name="forgerock_oauth_redirect_uri" translatable="false">com.forgerock.android:/oauth2redirect</string>
    3. In the Identity Cloud admin UI, update the Application Sign-in-URLs to match the value:

      com.forgerock.android:/oauth2redirect

    For more information, see Capturing the authorization redirect.

  4. For Android 11 or higher, add the following to the AndroidManfest.xml file:

    <queries>
         <intent>
             <action android:name="android.intent.action.VIEW" />
             <category android:name="android.intent.category.BROWSABLE" />
             <data android:scheme="https" />
         </intent>
     </queries>

    For more information, see Open URLs in a browser or other app.

  5. Configure your application to use browser mode:

    // Use FRUser.browser() to enable browser mode:
    FRUser.browser().login(context, new FRListener<FRUser>());
    
    // Use standard SDK interface to retrieve an AccessToken:
    FRUser.getCurrentUser().getAccessToken()
    
    // Use standard SDK interface to logout a user:
    FRUser.getCurrentUser().logout()

    The SDK uses the OAuth 2.0 parameters you configured in your application. You can amend the example code above to customize the integration with AppAuth; for example, adding OAuth 2.0 or OpenID Connect parameters, and browser colors:

     FRUser.browser().appAuthConfigurer()
         .authorizationRequest(r -> {
             // Add a login hint parameter about the user:
             r.setLoginHint("demo@example.com");
             // Request that the user re-authenticates:
             r.setPrompt("login");
         })
         .customTabsIntent(t -> {
             // Customize the browser:
             t.setShowTitle(true);
             t.setToolbarColor(getResources().getColor(R.color.colorAccent));
         }).done()
         .login(this, new FRListener<FRUser>() {
             @Override
             public void onSuccess(FRUser result) {
                 //success
             }
    
             @Override
             public void onException(Exception e) {
                 //failed
             }
         });

ForgeRock iOS SDK apps

This section describes how to configure your ForgeRock iOS SDK application to use centralized login:

  1. If your application targets iOS 10 and earlier, configure a custom URL scheme:

    1. In Xcode, in the Project Navigator, double-click your application to open the Project pane.

    2. On the Info tab, in the URL Types panel, configure your custom URL scheme:

      Custom URL Scheme
    3. Add the custom URL scheme to the Redirection URIs property of your OAuth 2.0 client:

      OAuth 2.0 Redirection URI
    4. In your AppDelegate.swift file, call the validateBrowserLogin() function:

      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {
      
        // Method invoked when the app is opened with Custom URL Scheme.
        // For example, a redirect from SFSafariViewController with an authorization code
        func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
      
          // Parse and validate URL, extract authorization code, and continue the flow:
          Browser.validateBrowserLogin(url)
        }
      }
  2. To enable centralized login, add code similar to the following to your app:

    //  BrowserBuilder
    let browserBuilder = FRUser.browser()
    browserBuilder.set(presentingViewController: self)
    browserBuilder.setCustomParam(key: "custom_key", value: "custom_val")
    
    //  Browser
    let browser = browserBuilder.build()
    
    // Login
    browser.login{ (user, error) in
      if let error = error {
        // Handle error
      }
      else if let user = user {
        // Handle authenticated status
      }
    }

ForgeRock JavaScript SDK apps

This section describes how to configure your ForgeRok JavaScript SDK application with centralized login:

  1. To initiate authentication by redirecting to the centralized login UI, add a login property that specifies how authentication happens in your app:

    const tokens = TokenManager.getTokens({
      forceRenew: false, // Immediately return stored tokens, if they exist
      login: 'redirect' // Redirect to AM or the web app that handles authentication
    });

    Supported values are as follows:

    Setting Description

    redirect

    Your app uses a redirect to ForgeRock® Access Management (AM), or another web application, to handle authentication.

    embedded

    Your app handles authentication natively, using SDK functionality.

    If you do not specify a value, embedded is assumed, for backwards-compatibility.

  2. When the user is returned to your app, complete the OAuth 2.0 flow by passing in the code and state values that were returned.

    Use the query property to complete the flow:

    const tokens = TokenManager.getTokens({
      query: {
        code: 'lFJQYdoQG1u7nUm8 ... ', // Authorization code from redirect URL
        state: 'MTY2NDkxNTQ2Nde3D ... ', // State from redirect URL
      },
    });
Copyright © 2010-2022 ForgeRock, all rights reserved.