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 iOS Integration Guide »
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.
Swift: HMAC-SHA256 Utility
Use the following Swift extension to generate the signature:
import Foundation
import CommonCrypto
extension String {
/// Generates an HMAC-SHA256 hash using a secret key.
func toSha256String(using key: String) -> String {
let hmac256 = CCHmacAlgorithm(kCCHmacAlgSHA256)
var macData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
key.withCString { keyCString in
withCString { msgCString in
macData.withUnsafeMutableBytes { macDataBytes in
guard let keyBytes = UnsafeRawPointer(keyCString)?.assumingMemoryBound(to: UInt8.self),
let msgBytes = UnsafeRawPointer(msgCString)?.assumingMemoryBound(to: UInt8.self) else {
return
}
CCHmac(
hmac256,
keyBytes, Int(strlen(keyCString)),
msgBytes, Int(strlen(msgCString)),
macDataBytes.bindMemory(to: UInt8.self).baseAddress
)
}
}
}
return macData.map { String(format: "%02x", $0) }.joined()
}
}
Step 3: Authenticate with the SDK
Now, use the authenticate()
method to start the session.
func authenticate() async {
let isAuthenticated = TruvideoSdk.isAuthenticated
let isAuthenticationExpired = TruvideoSdk.isAuthenticationExpired
let apiKey = "your-api-key"
let secretKey = "your-secret-key"
do {
let payload = try TruvideoSdk.generatePayload()
/// The payload string is transformed into an encrypted string using the SHA256 algorithm.
let signature = payload.toSha256String(using: secretKey)
if try isAuthenticated() || isAuthenticationExpired() {
/// Initialize a session when the user is not authenticated.
/// - Parameters:
/// - API_Key : Provided by TruVideo team
/// - Payload : generated by sdk TruvideoSdk.generatePayload() every time you have to create new payload
/// - Signature: encrypted string payload using the SHA256 algorithm with "secret key"
/// - Secret_Key: secret key is also provided by TruVideo team
try await TruvideoSdk.authenticate(
apiKey: apiKey,
payload: payload,
signature: signature
)
}
/// Initialize a session when the user is already authenticated.
try await TruvideoSdk.initAuthentication()
} catch {
print("Error")
}
}
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?