module Keycloak

Constants

KEYCLOAK_JSON_FILE
VERSION

Attributes

auth_server_url[RW]
generate_request_exception[RW]
installation_file[RW]
keycloak_controller[RW]
proc_external_attributes[RW]
proxy[RW]
realm[RW]

Public Class Methods

explode_exception() click to toggle source
# File lib/keycloak.rb, line 19
def self.explode_exception
  if Keycloak.generate_request_exception == nil
    Keycloak.generate_request_exception = false
  end
  Keycloak.generate_request_exception
end
installation_file=(file = nil) click to toggle source
# File lib/keycloak.rb, line 30
def self.installation_file=(file = nil)
  raise InstallationFileNotFound unless file.instance_of?(String) && File.exists?(file)
  @installation_file = file || KEYCLOAK_JSON_FILE
end

Private Class Methods

generic_request(access_token, uri, query_parameters, body_parameter, method) click to toggle source
# File lib/keycloak.rb, line 827
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) click to toggle source
# File lib/keycloak.rb, line 884
def self.rescue_response(response)
  case response.code
  when 200..399
    if response.body.empty?
      true
    else
      response.body
    end
  when 400..499
    begin
      response.return!
    rescue RestClient::ExceptionWithResponse => err
      raise ActionController::RoutingError.new(err.response)
    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