mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-27 10:28:41 -05:00
Add basic HTTP authentication and /token routes
- the only initial backend option for auth is LDAP
This commit is contained in:
parent
331ac33b53
commit
e447b754c3
3 changed files with 134 additions and 0 deletions
|
|
@ -173,6 +173,74 @@ module Vmpooler
|
|||
JSON.pretty_generate(result)
|
||||
end
|
||||
|
||||
get "#{api_prefix}/token/:token/?" do
|
||||
content_type :json
|
||||
|
||||
status 404
|
||||
result = { 'ok' => false }
|
||||
|
||||
if Vmpooler::API.settings.config[:auth]
|
||||
status 401
|
||||
|
||||
protected!
|
||||
|
||||
token = Vmpooler::API.settings.redis.hgetall('vmpooler__token__' + params[:token])
|
||||
|
||||
if not token.nil? and not token.empty?
|
||||
status 200
|
||||
result = { 'ok' => true, params[:token] => token }
|
||||
else
|
||||
status 404
|
||||
end
|
||||
end
|
||||
|
||||
JSON.pretty_generate(result)
|
||||
end
|
||||
|
||||
delete "#{api_prefix}/token/:token/?" do
|
||||
content_type :json
|
||||
|
||||
status 404
|
||||
result = { 'ok' => false }
|
||||
|
||||
if Vmpooler::API.settings.config[:auth]
|
||||
status 401
|
||||
|
||||
protected!
|
||||
|
||||
if Vmpooler::API.settings.redis.del('vmpooler__token__' + params[:token]).to_i > 0
|
||||
status 200
|
||||
result['ok'] = true
|
||||
end
|
||||
end
|
||||
|
||||
JSON.pretty_generate(result)
|
||||
end
|
||||
|
||||
post "#{api_prefix}/token" do
|
||||
content_type :json
|
||||
|
||||
status 404
|
||||
result = { 'ok' => false }
|
||||
|
||||
if Vmpooler::API.settings.config[:auth]
|
||||
status 401
|
||||
|
||||
protected!
|
||||
|
||||
o = [('a'..'z'), ('0'..'9')].map(&:to_a).flatten
|
||||
result['token'] = o[rand(25)] + (0...31).map { o[rand(o.length)] }.join
|
||||
|
||||
Vmpooler::API.settings.redis.hset('vmpooler__token__' + result['token'], 'user', @auth.username)
|
||||
Vmpooler::API.settings.redis.hset('vmpooler__token__' + result['token'], 'timestamp', Time.now)
|
||||
|
||||
status 200
|
||||
result['ok'] = true
|
||||
end
|
||||
|
||||
JSON.pretty_generate(result)
|
||||
end
|
||||
|
||||
get "#{api_prefix}/vm/?" do
|
||||
content_type :json
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue