Top Level Namespace

Defined Under Namespace

Classes: FranklyClient

Instance Method Summary collapse

Instance Method Details

#generate_identity_token(app_key, app_secret, nonce, user_id = nil, role = nil) ⇒ String

This function generates an identity token suitable for a single authentication attempt of a client against the Frankly API or SDK

Parameters:

  • app_key (String)

    The key that specifies which app this client is authenticating for, this value is provided by the Frankly Console.

  • app_secret (String)

    The secret value associated the the key allowing the client to securely authenticate against the Frankly API.

  • nonce (String)

    The nonce value got from Frankly SDK or API whether the identity generation comes from an client device or app backend.

  • user_id (String) (defaults to: nil)

    This argument must be set to make the SDK operate on behalf of a specific user of the app. For backend services willing to interact with the API directly this may be omitted.

  • role (String) (defaults to: nil)

    For backend services using the Frankly API this can be set to ‘admin’ to generate a token allowing the client to get admin priviledges and perform operations that regular users are forbidden to.

Returns:

  • (String)

    The function returns the generated identity token as a string.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/frankly-ruby.rb', line 243

def generate_identity_token(app_key, app_secret, nonce, user_id = nil, role = nil)
  auth_header = {
    typ: 'JWS',
    alg: 'HS256',
    cty: 'frankly-it;v1'
  }

  auth_claims = {
    aak: app_key,
    iat: Time.now.to_i,
    exp: Time.now.to_i + 10 * 24 * 60 * 60,
    nce: nonce
  }

  auth_claims[:uid] = user_id unless user_id.nil?
  auth_claims[:role] = role unless role.nil?

  JWT.encode(auth_claims, app_secret, 'HS256', auth_header)
end