const crypto = require ( 'crypto' ) ; const { default : SignJWT } = require ( 'jose/dist/node/cjs/jwt/sign' ) ; const auth = require ( '../../auth.json' ) ; const twilio = require ( 'twilio' ) ; export function uppercaseFirstLetter ( string ) { return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 ) ; } const APP_ID = auth . HIFI_APP_ID ; const APP_SECRET = auth . HIFI_APP_SECRET ; const SECRET_KEY_FOR_SIGNING = crypto . createSecretKey ( Buffer . from ( APP_SECRET , "utf8" ) ) ; const JWT_EXPIRATION_MS = 15 * 60 * 1000 ; export async function generateHiFiJWT ( userID , spaceName , isAdmin = false ) { let hiFiJWT ; try { let jwtArgs = { "user_id" : userID , "app_id" : APP_ID , "space_name" : spaceName , "admin" : isAdmin , "exp" : Math . round ( ( Date . now ( ) + JWT_EXPIRATION_MS ) / 1000 ) } ; hiFiJWT = await new SignJWT ( jwtArgs ) . setProtectedHeader ( { alg : 'HS256' , typ : 'JWT' } ) . sign ( SECRET_KEY_FOR_SIGNING ) ; return hiFiJWT ; } catch ( error ) { console . error ( `Couldn't create JWT! Error:${ error }` ) ; return ; } } export function generateTwilioAccessToken ( providedUserID , spaceName ) { if ( ! auth . TWILIO_ACCOUNT_SID || auth . TWILIO_ACCOUNT_SID . length === 0 || ! auth . TWILIO_API_KEY_SID || auth . TWILIO_API_KEY_SID . length === 0 || ! auth . TWILIO_API_KEY_SECRET || auth . TWILIO_API_KEY_SECRET . length === 0 ) { return "" ; } const AccessToken = twilio . jwt . AccessToken ; const VideoGrant = AccessToken . VideoGrant ; let accessToken = new AccessToken ( auth . TWILIO_ACCOUNT_SID , auth . TWILIO_API_KEY_SID , auth . TWILIO_API_KEY_SECRET ) ; accessToken . identity = providedUserID ; let grant = new VideoGrant ( ) ; grant . room = spaceName ; accessToken . addGrant ( grant ) ; return accessToken . toJwt ( ) ; }