Sha256: 2ac31c93c97ad719f332f6b2f83376ac35be3e2de7eda200f651c073ac28c66f

Contents?: true

Size: 1.59 KB

Versions: 77

Compression:

Stored size: 1.59 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 = {}

        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
  end
end

Version data entries

77 entries across 77 versions & 1 rubygems

Version Path
forest_liana-5.4.4 app/services/forest_liana/authorization_getter.rb
forest_liana-5.4.3 app/services/forest_liana/authorization_getter.rb
forest_liana-5.4.2 app/services/forest_liana/authorization_getter.rb
forest_liana-5.4.1 app/services/forest_liana/authorization_getter.rb
forest_liana-5.4.0 app/services/forest_liana/authorization_getter.rb
forest_liana-6.0.0.pre.beta.1 app/services/forest_liana/authorization_getter.rb
forest_liana-5.3.3 app/services/forest_liana/authorization_getter.rb
forest_liana-5.3.2 app/services/forest_liana/authorization_getter.rb
forest_liana-5.3.1 app/services/forest_liana/authorization_getter.rb
forest_liana-5.3.0 app/services/forest_liana/authorization_getter.rb
forest_liana-5.2.3 app/services/forest_liana/authorization_getter.rb
forest_liana-5.2.2 app/services/forest_liana/authorization_getter.rb
forest_liana-5.2.1 app/services/forest_liana/authorization_getter.rb
forest_liana-5.2.0 app/services/forest_liana/authorization_getter.rb
forest_liana-5.1.3 app/services/forest_liana/authorization_getter.rb
forest_liana-5.1.2 app/services/forest_liana/authorization_getter.rb
forest_liana-5.1.1 app/services/forest_liana/authorization_getter.rb
forest_liana-5.1.0 app/services/forest_liana/authorization_getter.rb
forest_liana-5.0.0 app/services/forest_liana/authorization_getter.rb
forest_liana-5.0.0.pre.beta.0 app/services/forest_liana/authorization_getter.rb