OpenPass SDK for Unity quickstart guide

IMPORTANT: Before installation, be sure to sign up for an OpenPass account to get a client ID.

The Unity SDK guide helps you integrate OpenPass with your Unity project, enabling seamless web sign-in and sign-out processes on Android and iOS platforms. After you have configured the SDK, you can handle authentication responses optimized for Android and iOS interfaces for signing in users.

Follow these steps to install the SDK:

  1. Install the SDK.
  2. Initialize the OpenPass manager.
  3. Implement the mobile web sign-in flow.
  4. Observe token changes.
  5. Add the Sign In with OpenPass button using the web sign-in flow.

TIP: For debugging, enable additional logging.

For details on integrating your OpenPass implementation with UID2, contact your Technical Account Manager at The Trade Desk.

Installation

To add the OpenPass SDK for Unity, use one of the following options:

Use Unity's package manager

NOTE: To install the Package Manager, make sure you have the Unity Editor installed.

To install OpenPass using Unity's Package Manager, do the following:

  1. In Unity Editor, from the Window menu, select Package Manager.
  2. Click the Install (+) button, then select Install package from git URL.
  3. Enter https://github.com/openpass-sso/openpass-unity-sdk.git?path=sdk, then click Install.

Unity automatically downloads and adds the OpenPass SDK to your project.

Modify package manifest

To install OpenPass by modifying your package manifest, add the SDK to your Packages/manifest.json file:

{
  "dependencies": {
    "com.openpass.unitysdk": "https://github.com/openpass-sso/openpass-unity-sdk.git?path=sdk"
  }
}

Unity automatically downloads and adds the OpenPass SDK to your project.

Initialize the OpenPass Manager

Initialize OpenPass Manager with your OpenPass client identifier and redirect host. To access the OpenPass classes, add the using statement to the top of your C# script.

The following is a code example for initializing OpenPass Manager.

using OpenPass;

string clientId = "YOUR_CLIENT_ID";  // Replace with your actual client ID.
string redirectHost = "REDIRECT_HOST.com"; // Replace with your actual redirect host.
OpenPassManager openPassManager = new OpenPassManager(clientId, redirectHost);

Implement mobile web sign-in flow

The sign-in process for mobile is a web-based flow. Choose one of these options:

Android

The Android OS uses a custom Chrome tab for sign-in before redirecting the user to the application. Add the following entries to the application tag of the AndroidManifest.xml file using the values appropriate for your OpenPass account.

For details, see the Configure the intent and redirect section of the Android Quickstart Guide.

The following is the meta-data element to add to the manifest file:

<meta-data android:name="op_web_redirect" android:value="com.myopenpass.auth.b3044f3612f7cfdf237331d9d38d2477://openpass-android-redirect.com"/>

The following is the intent filter to add inside your main activity entry:

<intent-filter>
   <data android:scheme="com.myopenpass.auth.b3044f3612f7cfdf237331d9d38d2477" android:host="openpass-android-redirect.com" />
   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.BROWSABLE" />
   <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

iOS

The iOS option uses an embedded browser session and does not require additional steps.

Observe token changes

To be notified when OpenPass tokens are updated, subscribe to the OpenPass Manager's UserChanged event. Notification are triggered by events such as sign-in, sign-out, and token refresh.

The following is a code example for subscribing to the UserChanged event in OpenPass Manager:

OpenPassManager openPassManager = new OpenPassManager(clientId, redirectHost);
openPassManager.UserChanged += HandleUserChanged;

private void HandleUserChanged(object sender, UserChangedEventArgs e)
{
    if (e.Tokens != null)
    {
      // Do something with the tokens.
    } 
    else 
    {
      // The user is signed out.
    }
}

Launch the web sign-in flow

To handle the web sign-in process, create a WebSignInFlowClient instance. You can await the LaunchSignIn method and then access the current tokens of the manager. If an error occurs, an exception is thrown.

The following is a code example for launching the OpenPass web-based sign-in flow:

OpenPassManager openPassManager = new OpenPassManager(clientId, redirectHost);
WebSignInFlowClient webSignInFlowClient = new WebSignInFlowClient(clientId, openPassManager);
try
{
    await webSignInFlowClient.LaunchSignIn();
    // Success! Do something with openPassManager.CurrentTokens.
}
catch (AggregateException exception)
{
    // An error has occurred. See exception.InnerException.Message).
}

Sign out of the flow

To sign the user out, use the SignOut method on the OpenPass Manager instance. It removes previously saved OpenPass tokens from the key chain and the OpenPassManager.shared.openPassTokens manager, and resets OpenPass to its original post-initialized state.

The following is a code example for signing the user out:

openPassManager.SignOut();

Additional resources

For more information, see the following pages:

Developer Documentation:

External Resources: