class RippleKeycloak::Client

Attributes

access_token[RW]
access_token_expiry[RW]
configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/ripple_keycloak/client.rb, line 15
def configure
  @configuration = Configuration.new
  yield(configuration)
  auth
  'Authenticated successfully'
end
delete_formatted(resource, json: true, authed: true, **options) click to toggle source
# File lib/ripple_keycloak/client.rb, line 51
def delete_formatted(resource, json: true, authed: true, **options)
  if authed
    options = add_auth_header(options)
    resource = "admin/realms/#{realm}/" + resource
  end
  options = format_as_json(options) if json

  return_or_raise delete("#{base_uri}/#{resource}", options)
end
get_formatted(resource, authed: true, **options) click to toggle source
# File lib/ripple_keycloak/client.rb, line 42
def get_formatted(resource, authed: true, **options)
  if authed
    options = add_auth_header(options)
    resource = "admin/realms/#{realm}/" + resource
  end

  return_or_raise get("#{base_uri}/#{resource}", options)
end
post_formatted(resource, json: true, authed: true, **options) click to toggle source
# File lib/ripple_keycloak/client.rb, line 22
def post_formatted(resource, json: true, authed: true, **options)
  if authed
    options = add_auth_header(options)
    resource = "admin/realms/#{realm}/" + resource
  end
  options = format_as_json(options) if json

  return_or_raise post("#{base_uri}/#{resource}", options)
end
put_formatted(resource, json: true, authed: true, **options) click to toggle source
# File lib/ripple_keycloak/client.rb, line 32
def put_formatted(resource, json: true, authed: true, **options)
  if authed
    options = add_auth_header(options)
    resource = "admin/realms/#{realm}/" + resource
  end
  options = format_as_json(options) if json

  return_or_raise put("#{base_uri}/#{resource}", options)
end

Private Class Methods

add_auth_header(options) click to toggle source
# File lib/ripple_keycloak/client.rb, line 111
def add_auth_header(options)
  add_header(options, 'Authorization', "Bearer #{token}")
end
add_header(options, header, value) click to toggle source
# File lib/ripple_keycloak/client.rb, line 115
def add_header(options, header, value)
  options[:headers] ||= {}
  options[:headers] = options[:headers].merge({ "#{header}": value })
  options
end
auth() click to toggle source
# File lib/ripple_keycloak/client.rb, line 81
def auth
  response = post_formatted(
    "realms/#{realm}/protocol/openid-connect/token",
    body: { grant_type: 'client_credentials', client_id: client_id, client_secret: client_secret },
    json: false, authed: false
  )
  update_token_fields response
  access_token
end
base_uri() click to toggle source
# File lib/ripple_keycloak/client.rb, line 96
def base_uri
  "#{base_url}/auth"
end
error_handler() click to toggle source
# File lib/ripple_keycloak/client.rb, line 100
def error_handler
  ErrorHandler
end
format_as_json(options) click to toggle source
# File lib/ripple_keycloak/client.rb, line 69
def format_as_json(options)
  options = add_header(options, 'Content-Type', 'application/json')
  options[:body] = options[:body].to_json
  options
end
return_or_raise(response) click to toggle source
# File lib/ripple_keycloak/client.rb, line 75
def return_or_raise(response)
  return response if [200, 201, 204].include? response.code

  raise_error response
end
token() click to toggle source
# File lib/ripple_keycloak/client.rb, line 104
def token
  now = Time.now
  return auth if access_token_expiry < now

  access_token
end
update_token_fields(response) click to toggle source
# File lib/ripple_keycloak/client.rb, line 91
def update_token_fields(response)
  @access_token = response['access_token']
  @access_token_expiry = Time.now + response['expires_in']
end

Public Instance Methods

delete(resource, body = {}) click to toggle source
# File lib/ripple_keycloak/client.rb, line 134
def delete(resource, body = {})
  self.class.delete_formatted(resource, body: body)
end
get(resource) click to toggle source
# File lib/ripple_keycloak/client.rb, line 130
def get(resource)
  self.class.get_formatted(resource)
end
post(resource, body) click to toggle source
# File lib/ripple_keycloak/client.rb, line 122
def post(resource, body)
  self.class.post_formatted(resource, body: body)
end
put(resource, body) click to toggle source
# File lib/ripple_keycloak/client.rb, line 126
def put(resource, body)
  self.class.put_formatted(resource, body: body)
end