Authenticate
The authenticate
function is the foundation of the Truvideo SDK. It securely initiates a session and prepares the SDK for capturing, uploading, and editing media.
This guide will walk you through integrating authentication into your project using the Truvideo Core Module.
Authentication Integration Guide
Prerequisites
Before you begin:
Make sure the Core module is integrated. See Installation
Request your API Key and Secret Key from Truvideo.
Get the API key and Secret Key from Truvideo Support
Step-by-Step Authentication Flow
Step 1: Prepare Your Credentials
Ensure you have:
API Key
Secret Key
External Id (If provided)
These are needed to sign each authentication request securely.
Step 2: Generate a Secure Signature
The SDK requires a hashed signature to validate each request. Follow this process to generate one:
Build the Payload
Use
TruvideoSdk.GeneratePayload()
to create a dynamic payload string.
Sign the Payload
Hash the payload using HMAC-SHA256 with your
Secret Key
.
Encode the Signature
Convert the hash into a hexadecimal string or base64 format.
Send the Signature
Include the signature in your authentication request.
Imports
#if IOS
using UIKit;
using Foundation;
using TruvideoCameraiOS;
#endif
#if ANDROID
using Application = Android.App.Application;
using TruVideoCoreAndroidBinding;
using TruVideoCoreLib = TruVideoCoreAndroid.DotnetCoreTruvideo;
#endif
Async Callback Wrapper
To get callbacks from native wrapper is needed
#if ANDROID
private Task<T> ExecuteCallbackAsync<T>(Action<CoreCallback> action)
{
var tcs = new TaskCompletionSource<T>();
action.Invoke(new CoreCallback(
success => MainThread.BeginInvokeOnMainThread(() =>
tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
));
return tcs.Task;
}
#endif
#if IOS
private Task<T> ExecuteCallbackAsync<T>(Action<Action<string, NSError>> nativeCall) {
var tcs = new TaskCompletionSource<T>();
nativeCall((nsResult, error) => {
if (error != null)
{
tcs.SetException(new Exception(error.LocalizedDescription));
}
else {
// Convert NSString to string
string result = nsResult.ToString();
tcs.TrySetResult((T)Convert.ChangeType(result, typeof(T)));
// tcs.SetResult(result);
}
});
return tcs.Task;
}
#endif
using System.Security.Cryptography;
using System.Text;
namespace TruVideoSampleApp;
public static class HashHelper
{
public static string? ToSha256String(string signature, string payload)
{
try
{
var keyBytes = Encoding.UTF8.GetBytes(signature);
var payloadBytes = Encoding.UTF8.GetBytes(payload);
using var hmac = new HMACSHA256(keyBytes);
var hashBytes = hmac.ComputeHash(payloadBytes);
// Convert to hex string
var hexString = new StringBuilder();
foreach (var b in hashBytes) hexString.Append(b.ToString("x2")); // formats byte as lowercase hex
return hexString.ToString();
}
catch (Exception ex)
{
// Log or handle exception as needed
Console.WriteLine(ex);
return null;
}
}
}
Test Connection
Run tests to confirm successful integration.
#if ANDROID
string _init = await ExecuteCallbackAsync<string>(cb => TruVideoCoreLib.InitAppInitializer(Application.Context, cb));
// Step 1: Check if authenticated
bool _isAuthenticate = await ExecuteCallbackAsync<bool>(cb => TruVideoCoreLib.IsAuthenticated(cb));
// Step 2: Check if authentication is expired
bool _isAuthenticationExpired = await ExecuteCallbackAsync<bool>(cb => TruVideoCoreLib.IsAuthenticationExpired(cb));
// Step 3: Generate payload
string _payload = await ExecuteCallbackAsync<string>(cb => TruVideoCoreLib.GeneratePayload(cb));
if (!_isAuthenticate || _isAuthenticationExpired)
{
// Step 4: Generate SHA-256 signature
string _signature = HashHelper.ToSha256String(_secretKey, payload);
// Step 5: Authenticate with API Key, Payload, and Signature
string authResult = await ExecuteCallbackAsync<string>(cb => TruVideoCoreLib.Authenticate(_apiKey, _payload, _signature, "", cb));
MainThread.BeginInvokeOnMainThread(() => AuthenticateButtonLabel.Text = $"Result: {authResult}");
}
// Step 6: Initialize Authentication
string _initAuthResult = await ExecuteCallbackAsync<string>(cb => TruVideoCoreLib.InitAuthentication(cb));
MainThread.BeginInvokeOnMainThread(() => AuthenticateButtonLabel.Text = $"Result: {_initAuthResult}");
#elif IOS
await Task.Run(async () =>
{
// Step 1: Check if authenticated
bool _isAuthenticate = await ExecuteCallbackAsync<bool>(cb => TruvideoCore.Shared.IsAuthenticatedWithCompletionHandler(cb));
//_isAuthenticate = Convert.ToBoolean(isAuthenticate);
// Step 2: Check if authentication is expired
bool _isAuthenticationExpired = await ExecuteCallbackAsync<bool>(cb => TruvideoCore.Shared.IsAuthenticationExpiredWithCompletionHandler(cb));
// Step 3: Generate payload
string payload = await ExecuteCallbackAsync<string>(cb => TruvideoCore.Shared.GeneratePayload(cb));
if (!_isAuthenticate || _isAuthenticationExpired)
{
// Step 4: Generate SHA-256 signature
string signature = HashHelper.ToSha256String(_secretKey, payload);
// Step 5: Authenticate with API Key, Payload, and Signature
string authResult = await ExecuteCallbackAsync<string>(cb => TruvideoCore.Shared.AuthenticateWithApiKey(apiKey, payload, "", signature, cb));
}
// Step 6: Initialize Authentication
string initAuthResult = await ExecuteCallbackAsync<string>(cb => TruvideoCore.Shared.InitAuthenticationWithCompletionHandler(cb));
MainThread.BeginInvokeOnMainThread(() =>
{
ActivityIndicator.IsRunning = false;
AuthenticateButtonLabel.Text = " Authentication Success";
//CheckAuthentication(); // Refresh UI after authentication
});
});
#endif
Final Steps: Test Your Integration
After implementing the authentication:
Run your application to verify that the SDK session initializes successfully.
Monitor the logs for any issues related to signatures or tokens.
Once confirmed, proceed to explore media capture, uploading, and editing features.
Thank you for using the Truvideo SDK. You're now authenticated and ready to explore all features!
Last updated
Was this helpful?