The SkylinkSDKs for mobile offer a secure method to connect to room without having to expose the App Secret in the native code.
- This method is recommended for production applications and eliminates the need to include the AppKey and Secret in your native code.
- This generation should ideally be done on an external application server.
Sample credentials generation using NodeJS
/** * Get the SkylinkConnectionString * @param roomName [Required] Name of the room * @param apiKey [Required] API Key * @param secret [Required] API secret * @param startTime [Required] Room Start Time * @param duration [Required] Duration of the room in Hours * @returns SkylinkConnectionString to use with the SDK */ function getSkylinkConnectionString(roomName, apiKey, secret, startTime, duration) { // Get ISO Timestamp of the date var dateString = startTime.toISOString(); // Generate Hmac and URI encode var credentials = encodeURIComponent(generateHmac(roomName + "_" + duration + "_" + dateString, secret)); // Form the connection string return apiKey + "/" + roomName + "/" + dateString + "/" + duration + "?cred=" + credentials; } /** * Get the signature/digest of a supplied input string * @param data [Required] The String to encode * @param secret [Required] API Secret * @returns Str with encoded digest of the input string */ function generateHmac(data, secret) { var crypto = require("crypto"); return crypto.createHmac("sha1", secret).update(data).digest("base64"); }
Sample credentials generation using Java
private static final String TAG = Utils.class.getName(); private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; public static final String TIME_ZONE_UTC = "UTC"; public static final String ISO_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"; /** * Returns the SkylinkConnectionString * * @param roomName Name of the room * @param apiKey API Key * @param secret API secret * @param startTime Room Start Time * @param duration Duration of the room in Hours * @return */ public static String getSkylinkConnectionString(String roomName, String apiKey, String secret, Date startTime, int duration) { Log.d(TAG, "Room name " + roomName); Log.d(TAG, "API Key " + apiKey); Log.d(TAG, "startTime " + startTime); Log.d(TAG, "duration " + duration); // Convert the date in to ISO format String dateString = Utils.getISOTimeStamp(startTime); // Compute RFC 2104-compliant HMAC signature String cred = calculateRFC2104HMAC(roomName + "_" + duration + "_" + dateString, secret); try { cred = URLEncoder.encode(cred, "UTF-8"); } catch (UnsupportedEncodingException e) { Log.e(TAG, e.getMessage(), e); } return apiKey + "/" + roomName + "/" + dateString + "/" + duration + "?cred=" + cred; } /** * Computes RFC 2104-compliant HMAC signature. * * @param data data The data to be signed. * @param key The signing key. * @return The Base64-encoded RFC 2104-compliant HMAC signature. */ public static String calculateRFC2104HMAC(String data, String key) { String result = null; try { // Get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes()); // Base64-encode the hmac result = Base64 .encodeToString(rawHmac, android.util.Base64.DEFAULT); } catch (Exception e) { Log.e(TAG, "Failed to generate HMAC : " + e.getMessage(), e); } return result.substring(0, result.length() - 1); } /** * Returns the date in ISO time format * * @param date * @return ISO timestamp */ public static String getISOTimeStamp(Date date) { TimeZone tz = TimeZone.getTimeZone(TIME_ZONE_UTC); DateFormat df = new SimpleDateFormat(ISO_TIME_FORMAT); df.setTimeZone(tz); return df.format(date); }
Sample credentials generation using PHP
Thanks to Emil Romanus for this snippet /* ---------- */ date_default_timezone_set("UTC"); /* or deal manually with possible timezone offsets */ define(SKYLINK_API_SECRET, "xxxxxx"); /* ----------- */ $room_name = "???" $duration = 24; $stamp = time(); /* unix timestamp */ $stamp = date("c", $stamp); $stamp = substr($stamp, 0, -6); /* remove timezone offset, i.e. '+00:00' */ $stamp = $stamp . '.0Z'; $cred_base = $room_name.'_'.sprintf('%.6f', $duration).'_'.$stamp; $cred = hash_hmac("sha1", $cred_base, SKYLINK_API_SECRET, true); $cred = base64_encode($cred); /* ----------- */
Other articles you may be interested in: Authenticating your Application Key to start a connection