Android
Steps to integrate the zenda AutoAuth on Android using ready to use code snippets.
Add zenda Auto-Auth Code Snippet to your project.
package <packageName>;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import org.json.JSONException;
import org.json.JSONObject;
/**
* The ZendaAuth class provides methods for interacting with the Zenda authentication system.
* It allows users to open the Zenda application using encrypted tokens and handles authentication-related operations.
*
* @author zenda <[email protected]>
* @version 1.0
* @since 1.0
*/
public class ZendaAuth {
/**
* Opens the Zenda application using the provided context and token.
* Validates the context and token, decodes the Base64 token, and then opens the Zenda application.
*
* @param context the application context.
* @param token the encrypted token to be used for authentication.
* @throws NullPointerException if the context parameter is null.
* @throws IllegalArgumentException if the token parameter is null or invalid.
*/
public static void Open(final Context context, final String token) throws NullPointerException, IllegalArgumentException {
// Validate context
if (context == null)
throw new NullPointerException("Context parameter is null");
// Validate token
if (token == null)
throw new IllegalArgumentException("Token parameter is null");
final AuthData authData;
try {
// Decode base64 string
String json = DecodeBase64(token);
// Convert json string in to class object
authData = AuthData.FromJson(json);
} catch (JSONException e) {
throw new IllegalArgumentException("Token parameter is invalid");
}
// Open Zenda
Open(context, authData);
}
/**
* Decodes a Base64 encoded string.
* @param base64 the Base64 encoded string to decode.
* @return the decoded string.
*/
private static String DecodeBase64(final String base64) {
// Decode the base64 encoded string
byte[] decodedBytes = Base64.decode(base64, Base64.DEFAULT);
// Convert the decoded bytes to a string
String decodedString = new String(decodedBytes);
return decodedString;
}
/**
* Opens the Zenda application using the provided authentication data.
* If the Zenda application is not installed, it redirects to the Play Store.
*
* @param context the application context.
* @param authData the authentication data containing the URLs for redirection.
*/
private static void Open(final Context context, final AuthData authData) {
// Open Zenda Application if the Zenda Application is installed
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authData.getAppRedirectURL()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Handles the specific case for Android 12 (API level 31) to ensure the splash screen icon is shown.
// This is necessary due to changes in how splash screens are managed in Android 12.
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.S) {
Bundle options = ActivityOptions.makeBasic().toBundle();
options.putInt("android.activity.splashScreenStyle", 1);
context.startActivity(intent, options);
} else {
context.startActivity(intent);
}
} catch (Exception e) {
// Open Zenda application in Play Store if the Zenda Application isn't installed
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authData.getPlayStoreRedirectURL()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
/**
* A class representing the authentication data required to open the Zenda application.
*/
private static class AuthData {
private final String appRedirectURL;
private final String playStoreRedirectURL;
/**
* Constructs a new AuthData instance.
*
* @param appRedirectURL the URL to redirect to the app.
* @param playStoreRedirectURL the URL to redirect to the Play Store if the app is not installed.
*/
public AuthData(String appRedirectURL, String playStoreRedirectURL) {
this.appRedirectURL = appRedirectURL;
this.playStoreRedirectURL = playStoreRedirectURL;
}
/**
* Creates an AuthData instance from a JSON string.
*
* @param json the JSON string containing the authentication data.
* @return an AuthData instance.
* @throws JSONException if the JSON string cannot be parsed.
*/
private static AuthData FromJson(String json) throws JSONException {
JSONObject jsonObject = new JSONObject(json);
String appRedirectURL = jsonObject.getString("appRedirectURL");
String playStoreRedirectURL = jsonObject.getString("playStoreRedirectURL");
return new AuthData(appRedirectURL, playStoreRedirectURL);
}
/**
* Returns the URL to redirect to the app.
*
* @return the app redirect URL.
*/
public String getAppRedirectURL() {
return appRedirectURL;
}
/**
* Returns the URL to redirect to the Play Store if the app is not installed.
*
* @return the Play Store redirect URL.
*/
public String getPlayStoreRedirectURL() {
return playStoreRedirectURL;
}
}
}
Request the Get token REST API using POST method from server side, which responds with 2 properties, they are referenceToken, expiresAt. Call the Open zenda authentication method along with the received properties.
Request the redirection to the zenda app.
Last updated