module MasterApiKey::ApiGatekeeper

Public Instance Methods

api_group() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 24
def api_group
  nil
end

Protected Instance Methods

authorize_action(authorizers = nil) { || ... } click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 40
def authorize_action(authorizers = nil)
  if user_authenticated?
    raise ArgumentError, "MasterApiKey: Didn't define an api group name" unless self.api_group.present?

    if authorized_with_group? and (authorizers.nil? or passes_authorizers?(authorizers))
      yield if block_given?
    else
      on_forbidden_request
    end
  else
    on_authentication_failure
  end
end
on_authentication_failure() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 54
def on_authentication_failure
  head(:unauthorized)
end
on_forbidden_request() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 58
def on_forbidden_request
  head(:forbidden)
end
passes_authorizers?(authorizers) click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 30
def passes_authorizers?(authorizers)
  method_definitions = authorizers.respond_to?(:inject) ? authorizers : [authorizers]
  method_definitions.inject(true) do |is_authorized, authorizer|
    was_authorized = is_authorized
    is_authorized &= self.send(authorizer)
    log_failed_authorization(authorizer, is_authorized, was_authorized)
    is_authorized
  end
end
read_authorizer() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 68
def read_authorizer
  @api_key.read_access
end
write_authorizer() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 64
def write_authorizer
  @api_key.write_access
end

Private Instance Methods

api_token() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 94
def api_token
  header('X-API-TOKEN')
end
authorized_with_group?() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 80
def authorized_with_group?
  is_authorized = @api_key.group.casecmp(self.api_group.to_s) == 0
  log_failed_authorization(:authorized_with_group?, is_authorized, true)
  is_authorized
end
header(header) click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 98
def header(header)
  request.headers[header]
end
log_failed_authorization(authorizer, is_authorized, was_authorized) click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 74
def log_failed_authorization(authorizer, is_authorized, was_authorized)
  unless was_authorized == is_authorized
    Rails.logger.info "#{authorizer} failed for user of api token #{@api_key.api_token}"
  end
end
user_api_key() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 90
def user_api_key
  @api_key.present? ? @api_key : (@api_key = MasterApiKey::ApiKey.find_by_api_token(api_token))
end
user_authenticated?() click to toggle source
# File lib/master_api_key/api_gatekeeper.rb, line 86
def user_authenticated?
  api_token.present? and user_api_key.present?
end