class Gemstash::ApiKeyAuthorization

Authorize actions via an API key and Gemstash::Authorization.

Public Class Methods

new(key) click to toggle source
# File lib/gemstash/api_key_authorization.rb, line 8
def initialize(key)
  @key = key
end
parse_authorization(request_env) click to toggle source
# File lib/gemstash/api_key_authorization.rb, line 21
def self.parse_authorization(request_env)
  http_auth = Rack::Auth::Basic::Request.new(request_env)
  return http_auth.credentials.first if http_auth.provided? && http_auth.basic?

  request_env["HTTP_AUTHORIZATION"]
end
protect(app) { || ... } click to toggle source
# File lib/gemstash/api_key_authorization.rb, line 12
def self.protect(app, &block)
  key = parse_authorization(app.request.env)
  app.auth = new(key)
  yield
rescue Gemstash::NotAuthorizedError => e
  app.headers["WWW-Authenticate"] = "Basic realm=\"Gemstash Private Gems\""
  app.halt 401, e.message
end

Public Instance Methods

check(permission) click to toggle source
# File lib/gemstash/api_key_authorization.rb, line 28
def check(permission)
  Gemstash::Authorization.check(@key, permission)
end