Step-by-step guide on how to secure the frontend SDK.
Before moving to production, you must ensure nobody else can create a new connection.
Add a secret HMAC key (large, random value) in your Environment Settings tab in the Terapi UI.
Generate the HMAC signature in your backend and pass it to your frontend before you make terapi.auth calls.
The HMAC signature can be generated with the following code:
import*ascryptofrom'node:crypto';// Enforce backend authentication before generating the HMAC digest.consthmac=crypto.createHmac('sha256','');// HMAC key set in your environment settings.hmac.update(':');constdigest=hmac.digest('hex');
import hmacimport hashlib# HMAC key set in your environment settings.hmac_key =''message =':'digest = hmac.new(hmac_key.encode('utf-8'),msg=message.encode('utf-8'),digestmod=hashlib.sha256).hexdigest()
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
)
func main()
use hmac::;
use sha2::Sha256;
type HmacSha256 = Hmac;
let hmac_key = "";
let message = ":";
let mut mac = HmacSha256::new_varkey(hmac_key.as_bytes()).expect("HMAC can take key of any size");
mac.update(message.as_bytes());
let result = mac.finalize();
let digest = hex::encode(result.into_bytes());