Module: Keycloak

Defined in:
lib/keycloak.rb,
lib/keycloak/version.rb,
lib/keycloak/exceptions.rb

Defined Under Namespace

Modules: Admin, Client, Internal Classes: KeycloakException, ProcCookieTokenNotDefined, ProcExternalAttributesNotDefined, UserLoginNotFound

Constant Summary collapse

VERSION =
"2.2.6"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.generate_request_exceptionObject

Returns the value of attribute generate_request_exception.



10
11
12
# File 'lib/keycloak.rb', line 10

def generate_request_exception
  @generate_request_exception
end

.keycloak_controllerObject

Returns the value of attribute keycloak_controller.



10
11
12
# File 'lib/keycloak.rb', line 10

def keycloak_controller
  @keycloak_controller
end

Returns the value of attribute proc_cookie_token.



10
11
12
# File 'lib/keycloak.rb', line 10

def proc_cookie_token
  @proc_cookie_token
end

.proc_external_attributesObject

Returns the value of attribute proc_external_attributes.



10
11
12
# File 'lib/keycloak.rb', line 10

def proc_external_attributes
  @proc_external_attributes
end

.proxyObject

Returns the value of attribute proxy.



10
11
12
# File 'lib/keycloak.rb', line 10

def proxy
  @proxy
end

Class Method Details

.explode_exceptionObject



15
16
17
18
19
20
# File 'lib/keycloak.rb', line 15

def self.explode_exception
  if Keycloak.generate_request_exception == nil
    Keycloak.generate_request_exception = true
  end
  Keycloak.generate_request_exception
end

.generic_request(access_token, uri, query_parameters, body_parameter, method) ⇒ Object



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'lib/keycloak.rb', line 737

def self.generic_request(access_token, uri, query_parameters, body_parameter, method)
  Keycloak::Client.verify_setup
  final_url = uri

  header = {'Content-Type' => 'application/x-www-form-urlencoded',
            'Authorization' => "Bearer #{access_token}"}

  if query_parameters
    parameters = URI.encode_www_form(query_parameters)
    final_url = final_url << '?' << parameters
  end

  case method.upcase
  when 'GET'
    _request = -> do
      RestClient.get(final_url, header){|response, request, result|
        rescue_response(response)
      }
    end
  when 'POST', 'PUT'
    header["Content-Type"] = 'application/json'
    parameters = JSON.generate body_parameter
    _request = -> do
      case method.upcase
      when 'POST'
        RestClient.post(final_url, parameters, header){|response, request, result|
          rescue_response(response)
        }
      else
        RestClient.put(final_url, parameters, header){|response, request, result|
          rescue_response(response)
        }
      end
    end
  when 'DELETE'
    _request = -> do
      if body_parameter
        header["Content-Type"] = 'application/json'
        parameters = JSON.generate body_parameter
        RestClient::Request.execute(method: :delete, url: final_url,
                      payload: parameters, headers: header) { |response, request, result|
          rescue_response(response)
        }
      else
        RestClient.delete(final_url, header) { |response, request, result|
          rescue_response(response)
        }
      end
    end
  else
    raise
  end

  _request.call

end

.rescue_response(response) ⇒ Object



794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/keycloak.rb', line 794

def self.rescue_response(response)
  case response.code
  when 200..399
    if response.body.empty?
      true
    else
      response.body
    end
  else
    if Keycloak.explode_exception
      response.return!
    else
      begin
        response.return!
      rescue RestClient::ExceptionWithResponse => err
        err.response
      rescue StandardError => e
        e.message
      end
    end
  end
end