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.



8
9
10
# File 'lib/gemstash/api_key_authorization.rb', line 8

def initialize(key)
  @key = key
end

Class Method Details

.parse_authorization(request_env) ⇒ Object



21
22
23
24
25
26
# 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, &block) ⇒ Object



12
13
14
15
16
17
18
19
# 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

Instance Method Details

#check(permission) ⇒ Object



28
29
30
# File 'lib/gemstash/api_key_authorization.rb', line 28

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