Passed
Push — master ( 33ca92...23b088 )
by Ahmad
10:28
created

BbbApi.retrieve_provider_info()   B

Complexity

Conditions 8

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 3
Metric Value
cc 8
dl 0
loc 38
rs 7.1013
c 4
b 0
f 3
1
# frozen_string_literal: true
2
3
module BbbApi
4
  RETURNCODE_SUCCESS = "SUCCESS"
5
6
  def bbb_endpoint
7
    Rails.configuration.bigbluebutton_endpoint
8
  end
9
10
  def bbb_secret
11
    Rails.configuration.bigbluebutton_secret
12
  end
13
14
  # Sets a BigBlueButtonApi object for interacting with the API.
15
  def bbb(user_provider)
16
    if Rails.configuration.loadbalanced_configuration
17
      user_domain = retrieve_provider_info(user_provider)
18
19
      BigBlueButton::BigBlueButtonApi.new(remove_slash(user_domain["apiURL"]), user_domain["secret"], "0.8")
20
    else
21
      BigBlueButton::BigBlueButtonApi.new(remove_slash(bbb_endpoint), bbb_secret, "0.8")
22
    end
23
  end
24
25
  # Rereives info from the loadbalanced in regards to a Provider (or tenant).
26
  def retrieve_provider_info(provider, api = 'api', route = 'getUser')
27
    # Include Omniauth accounts under the Greenlight provider.
28
    raise "Provider not included." if !provider || provider.empty?
29
30
    cached_provider = Rails.cache.fetch("#{provider}/#{route}")
31
    # Return cached result if the value exists and cache is enabled
32
    return cached_provider if !cached_provider.nil? && Rails.configuration.enable_cache
33
34
    # Build the URI.
35
    uri = encode_bbb_url(
36
      Rails.configuration.loadbalancer_endpoint + api + '/',
37
      Rails.configuration.loadbalancer_secret,
38
      { name: provider },
39
      route
40
    )
41
42
    logger.info uri
43
44
    # Make the request.
45
    http = Net::HTTP.new(uri.host, uri.port)
46
    http.use_ssl = (uri.scheme == 'https')
47
    response = http.get(uri.request_uri)
48
49
    # Parse XML.
50
    doc = XmlSimple.xml_in(response.body, 'ForceArray' => false)
51
52
    raise doc['message'] unless response.is_a?(Net::HTTPSuccess)
53
54
    # Return the user credentials if the request succeeded on the loadbalancer.
55
    Rails.cache.fetch("#{provider}/#{route}", expires_in: 1.hours) do
56
      doc['user']
57
    end
58
59
    return doc['user'] if doc['returncode'] == 'SUCCESS'
60
61
    raise "User with provider #{provider} does not exist." if doc['messageKey'] == 'noSuchUser'
62
    raise "API call #{url} failed with #{doc['messageKey']}."
63
  end
64
65
  # Builds a request to retrieve credentials from the load balancer.
66
  def encode_bbb_url(base_url, secret, params, route = 'getUser')
67
    encoded_params = params.to_param
68
    string = route + encoded_params + secret
69
    checksum = OpenSSL::Digest.digest('sha1', string).unpack1('H*')
70
71
    URI.parse("#{base_url}#{route}?#{encoded_params}&checksum=#{checksum}")
72
  end
73
74
  # Removes trailing forward slash from a URL.
75
  def remove_slash(s)
76
    s.nil? ? nil : s.chomp("/")
77
  end
78
end
79