Class: Gemstash::ApiKeyAuthorization

Inherits:
Object
  • Object
show all
Defined in:
lib/gemstash/api_key_authorization.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ ApiKeyAuthorization

Returns a new instance of ApiKeyAuthorization.



6
7
8
# File 'lib/gemstash/api_key_authorization.rb', line 6

def initialize(key)
  @key = key
end

Class Method Details

.parse_authorization(request_env) ⇒ Object



19
20
21
22
23
# File 'lib/gemstash/api_key_authorization.rb', line 19

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, &block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/gemstash/api_key_authorization.rb', line 10

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

Instance Method Details

#check(permission) ⇒ Object



25
26
27
# File 'lib/gemstash/api_key_authorization.rb', line 25

def check(permission)
  Gemstash::Authorization.check(@key, permission)
end