Class: Bosh::OpenstackRegistry::ApiController

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/openstack_registry/api_controller.rb

Instance Method Summary collapse

Constructor Details

#initializeApiController

Returns a new instance of ApiController.



38
39
40
41
42
43
44
45
46
# File 'lib/openstack_registry/api_controller.rb', line 38

def initialize
  super
  @logger = Bosh::OpenstackRegistry.logger

  @users = Set.new
  @users << [Bosh::OpenstackRegistry.http_user,
             Bosh::OpenstackRegistry.http_password]
  @server_manager = ServerManager.new
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/openstack_registry/api_controller.rb', line 55

def authorized?
  @auth ||= Rack::Auth::Basic::Request.new(request.env)
  @auth.provided? &&
    @auth.basic? &&
    @auth.credentials &&
    @users.include?(@auth.credentials)
end

#json(payload) ⇒ Object



63
64
65
# File 'lib/openstack_registry/api_controller.rb', line 63

def json(payload)
  Yajl::Encoder.encode(payload)
end

#protected!Object



48
49
50
51
52
53
# File 'lib/openstack_registry/api_controller.rb', line 48

def protected!
  unless authorized?
    headers("WWW-Authenticate" => 'Basic realm="OpenStack Registry"')
    halt(401, json("status" => "access_denied"))
  end
end