Class: ForestLiana::AuthorizationGetter

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/authorization_getter.rb

Instance Method Summary collapse

Constructor Details

#initialize(rendering_id, use_google_authentication, auth_data, two_factor_registration) ⇒ AuthorizationGetter

Returns a new instance of AuthorizationGetter.



3
4
5
6
7
8
9
10
11
# File 'app/services/forest_liana/authorization_getter.rb', line 3

def initialize(rendering_id, use_google_authentication, auth_data, two_factor_registration)
  @rendering_id = rendering_id
  @use_google_authentication = use_google_authentication
  @auth_data = auth_data
  @two_factor_registration = two_factor_registration

  @route = "/liana/v2/renderings/#{rendering_id}"
  @route += use_google_authentication ? "/google-authorization" : "/authorization"
end

Instance Method Details

#performObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/forest_liana/authorization_getter.rb', line 13

def perform
  begin
    if @use_google_authentication
      headers = { 'forest-token' => @auth_data[:forest_token] }
    else
      headers = { 'email' => @auth_data[:email], 'password' => @auth_data[:password] }
    end

    query_parameters = {}

    if @two_factor_registration
      query_parameters['two-factor-registration'] = true
    end

    response = ForestLiana::ForestApiRequester
      .get(@route, query: query_parameters, headers: headers)

    if response.is_a?(Net::HTTPOK)
      body = JSON.parse(response.body)
      user = body['data']['attributes']
      user['id'] = body['data']['id']
      user
    else
      if @use_google_authentication
        raise "Cannot authorize the user using this google account. Forest API returned an #{Errors::HTTPErrorHelper.format(response)}"
      else
        raise "Cannot authorize the user using this email/password. Forest API returned an #{Errors::HTTPErrorHelper.format(response)}"
      end
    end
  rescue
    raise ForestLiana::Errors::HTTP401Error
  end
end