Class: Alula::TokenExchange

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/resources/token_exchange.rb

Overview

Helper class to generate OAuth access tokens (core API) and JWT tokens (VSP API)

Defined Under Namespace

Classes: ImpersonatedToken

Class Method Summary collapse

Class Method Details

.fetch_video_token(payload) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/alula/resources/token_exchange.rb', line 24

def fetch_video_token(payload)
  cache_key = generate_cache_key(payload)
  cached_token, expiry = retrieve_cached_token(cache_key)

  if cached_token && Time.now.to_i < expiry
    jwt_token = cached_token
  else
    jwt_token = build_jwt_token(payload)
    expiry = Time.now.to_i + JwtHelper::ONE_HOUR # 1 hour expiry
    store_token_in_cache(cache_key, jwt_token, expiry)
  end

  jwt_token
end

.token_for_user(user_id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/alula/resources/token_exchange.rb', line 9

def token_for_user(user_id)
  url = '/rest/v1/oauth/accesstokens'
  payload = { data: { attributes: { userId: user_id } } }
  opts = {}

  response = Alula::Client.request(:post, url, payload, opts)

  if response.ok?
    ImpersonatedToken.new(response.data['data']['attributes'])
  else
    error_class = AlulaError.for_response(response)
    raise error_class
  end
end