Sha256: 523e00e63ce7e52990d4a7bb7b90d7bb093eb7df8bb41b6c1ccd654228738ae5

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

module ForestLiana
  class AuthorizationGetter
    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

    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 = { 'renderingId' => @rendering_id }

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

        response = 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 Errors::HTTP401Error
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
forest_liana-2.13.0 app/services/forest_liana/authorization_getter.rb
forest_liana-2.12.0 app/services/forest_liana/authorization_getter.rb