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:


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:

  1. Build the Payload

    • Use TruvideoSdk.generatePayload() to create a dynamic payload string.

  2. Sign the Payload

    • Hash the payload using HMAC-SHA256 with your Secret Key.

  3. Encode the Signature

    • Convert the hash into a hexadecimal string or base64 format.

  4. Send the Signature

    • Include the signature in your authentication request.


import 'package:crypto/crypto.dart';

String? toSha256String({required String secret, required String payload}) {
  try {
     // Convert secret and payload to byte arrays
     List<int> secretBytes = utf8.encode(secret);
     List<int> payloadBytes = utf8.encode(payload);
     var hmacSha256 = Hmac(sha256, secretBytes);
     var macData = hmacSha256.convert(payloadBytes);
     return macData.toString();
   } catch (e) {
     debugPrint("Error generating SHA256 HMAC: $e");
     return null;
   }
 }
  1. Test Connection

  • Run tests to confirm successful integration.

import 'package:truvideo_core_sdk/truvideo_core_sdk.dart';

Future<void> authenticate() async {
  try {
      bool isAuthenticated = await TruvideoCoreSdk.isAuthenticated();
      bool isAuthExpired = await TruvideoCoreSdk.isAuthenticationExpired();
      String payload = await TruvideoCoreSdk.generatePayload();
      String signature = toSha256String(secret: secretKey, payload: payload) ?? '';

      // Authenticate with generated payload and signature
      if (!isAuthenticated || isAuthExpired ) {
        await TruvideoCoreSdk.authenticate(
          apiKey: apiKey,
          signature: signature,
          payload: payload,
          externalId: "",
          );
      }
      await TruvideoCoreSdk.initAuthentication();
    } catch (e) {
      debugPrint('Authentication failed: $e');
    }
}

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?