Commit e5ca92d7 authored by Nina's avatar Nina
Browse files

Extract api daemon routes into own files

parent c8ce7153
......@@ -9,7 +9,11 @@ require 'sinatra/json'
require 'sinatra/namespace'
require 'thin'
require 'schleuder'
require 'schleuder-api-daemon/routes/status'
require 'schleuder-api-daemon/routes/version'
require 'schleuder-api-daemon/routes/list'
require 'schleuder-api-daemon/routes/subscription'
require 'schleuder-api-daemon/routes/key'
%w[tls_cert_file tls_key_file].each do |config_key|
path = Conf.api[config_key]
......@@ -20,8 +24,6 @@ require 'schleuder'
end
class SchleuderApiDaemon < Sinatra::Base
register Sinatra::Namespace
configure do
set :server, :thin
set :port, Schleuder::Conf.api['port'] || 4443
......@@ -58,14 +60,6 @@ class SchleuderApiDaemon < Sinatra::Base
'Not found'
end
get '/status.json' do
json status: :ok
end
get '/version.json' do
json version: Schleuder::VERSION
end
helpers do
def valid_credentials?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
......@@ -210,205 +204,6 @@ class SchleuderApiDaemon < Sinatra::Base
end
end
namespace '/lists' do
get '.json' do
json List.all, include: :subscriptions
end
post '.json' do
listname = parsed_body['email']
fingerprint = parsed_body['fingerprint']
adminaddress = parsed_body['adminaddress']
adminfingerprint = parsed_body['adminfingerprint']
adminkey = parsed_body['adminkey']
list, messages = ListBuilder.new({email: listname, fingerprint: fingerprint}, adminaddress, adminfingerprint, adminkey).run
if list.nil?
client_error(messages, 422)
elsif ! list.valid?
client_error(list, 422)
else
set_x_messages(messages)
body json(list)
end
end
get '/configurable_attributes.json' do
json(List.configurable_attributes) + "\n"
end
post '/send_list_key_to_subscriptions.json' do
json(result: list.send_list_key_to_subscriptions)
end
get '/new.json' do
json List.new
end
get '/:id.json' do |id|
json list(id)
end
put '/:id.json' do |id|
list = list(id)
if list.update(parsed_body)
204
else
client_error(list)
end
end
patch '/:id.json' do |id|
list = list(id)
if list.update(parsed_body)
204
else
client_error(list)
end
end
delete '/:id.json' do |id|
list = list(id)
if list.destroy
200
else
client_error(list)
end
end
end
namespace '/subscriptions' do
get '.json' do
filterkeys = Subscription.configurable_attributes + [:list_id, :email]
filter = params.select do |param|
filterkeys.include?(param.to_sym)
end
logger.debug "Subscription filter: #{filter.inspect}"
if filter['list_id'] && ! is_an_integer?(filter['list_id'])
# Value is an email-address
if list = List.where(email: filter['list_id']).first
filter['list_id'] = list.id
else
status 404
return json(errors: 'No such list')
end
end
json Subscription.where(filter)
end
post '.json' do
begin
list = list(requested_list_id)
# We don't have to care about nil-values, subscribe() does that for us.
sub, msgs = list.subscribe(
parsed_body['email'],
parsed_body['fingerprint'],
parsed_body['admin'],
parsed_body['delivery_enabled'],
find_key_material
)
set_x_messages(msgs)
logger.debug "subcription: #{sub.inspect}"
if sub.valid?
logger.debug "Subscribed: #{sub.inspect}"
# TODO: why redirect instead of respond with result?
redirect to("/subscriptions/#{sub.id}.json"), 201
else
client_error(sub, 422)
end
rescue ActiveRecord::RecordNotUnique
logger.error "Already subscribed"
status 422
json errors: {email: ['is already subscribed']}
end
end
get '/configurable_attributes.json' do
json(Subscription.configurable_attributes) + "\n"
end
get '/new.json' do
json Subscription.new
end
get '/:id.json' do |id|
json subscription(id)
end
put '/:id.json' do |id|
sub = subscription(id)
list = sub.list
args = find_attributes_from_body(%w[email fingerprint admin delivery_enabled])
fingerprint, messages = list.import_key_and_find_fingerprint(find_key_material)
set_x_messages(messages)
# For an already existing subscription, only update fingerprint if a
# new one has been selected from the upload.
if fingerprint.present?
args["fingerprint"] = fingerprint
end
if sub.update(args)
200
else
client_error(sub, 422)
end
end
patch '/:id.json' do |id|
sub = subscription(id)
if sub.update(parsed_body)
200
else
client_error(sub)
end
end
delete '/:id.json' do |id|
if sub = subscription(id).destroy
200
else
client_error(sub)
end
end
end
namespace '/keys' do
get '.json' do
keys = list.keys.sort_by(&:email).map do |key|
key_to_hash(key)
end
json keys
end
post '.json' do
input = parsed_body['keymaterial']
if ! input.match('BEGIN PGP')
input = Base64.decode64(input)
end
json list(requested_list_id).import_key(input)
end
get '/check_keys.json' do
json result: list.check_keys
end
get '/:fingerprint.json' do |fingerprint|
if key = list.key(fingerprint)
json key_to_hash(key, true)
else
404
end
end
delete '/:fingerprint.json' do |fingerprint|
if list.delete_key(fingerprint)
200
else
404
end
end
end
def self.run!
super do |server|
server.ssl = true
......
class SchleuderApiDaemon < Sinatra::Base
register Sinatra::Namespace
namespace '/keys' do
get '.json' do
keys = list.keys.sort_by(&:email).map do |key|
key_to_hash(key)
end
json keys
end
post '.json' do
input = parsed_body['keymaterial']
if ! input.match('BEGIN PGP')
input = Base64.decode64(input)
end
json list(requested_list_id).import_key(input)
end
get '/check_keys.json' do
json result: list.check_keys
end
get '/:fingerprint.json' do |fingerprint|
if key = list.key(fingerprint)
json key_to_hash(key, true)
else
404
end
end
delete '/:fingerprint.json' do |fingerprint|
if list.delete_key(fingerprint)
200
else
404
end
end
end
end
class SchleuderApiDaemon < Sinatra::Base
register Sinatra::Namespace
namespace '/lists' do
get '.json' do
json List.all, include: :subscriptions
end
post '.json' do
listname = parsed_body['email']
fingerprint = parsed_body['fingerprint']
adminaddress = parsed_body['adminaddress']
adminfingerprint = parsed_body['adminfingerprint']
adminkey = parsed_body['adminkey']
list, messages = ListBuilder.new({email: listname, fingerprint: fingerprint}, adminaddress, adminfingerprint, adminkey).run
if list.nil?
client_error(messages, 422)
elsif ! list.valid?
client_error(list, 422)
else
set_x_messages(messages)
body json(list)
end
end
get '/configurable_attributes.json' do
json(List.configurable_attributes) + "\n"
end
post '/send_list_key_to_subscriptions.json' do
json(result: list.send_list_key_to_subscriptions)
end
get '/new.json' do
json List.new
end
get '/:id.json' do |id|
json list(id)
end
put '/:id.json' do |id|
list = list(id)
if list.update(parsed_body)
204
else
client_error(list)
end
end
patch '/:id.json' do |id|
list = list(id)
if list.update(parsed_body)
204
else
client_error(list)
end
end
delete '/:id.json' do |id|
list = list(id)
if list.destroy
200
else
client_error(list)
end
end
end
end
class SchleuderApiDaemon < Sinatra::Base
get '/status.json' do
json status: :ok
end
end
class SchleuderApiDaemon < Sinatra::Base
register Sinatra::Namespace
namespace '/subscriptions' do
get '.json' do
filterkeys = Subscription.configurable_attributes + [:list_id, :email]
filter = params.select do |param|
filterkeys.include?(param.to_sym)
end
logger.debug "Subscription filter: #{filter.inspect}"
if filter['list_id'] && ! is_an_integer?(filter['list_id'])
# Value is an email-address
if list = List.where(email: filter['list_id']).first
filter['list_id'] = list.id
else
status 404
return json(errors: 'No such list')
end
end
json Subscription.where(filter)
end
post '.json' do
begin
list = list(requested_list_id)
# We don't have to care about nil-values, subscribe() does that for us.
sub, msgs = list.subscribe(
parsed_body['email'],
parsed_body['fingerprint'],
parsed_body['admin'],
parsed_body['delivery_enabled'],
find_key_material
)
set_x_messages(msgs)
logger.debug "subcription: #{sub.inspect}"
if sub.valid?
logger.debug "Subscribed: #{sub.inspect}"
# TODO: why redirect instead of respond with result?
redirect to("/subscriptions/#{sub.id}.json"), 201
else
client_error(sub, 422)
end
rescue ActiveRecord::RecordNotUnique
logger.error "Already subscribed"
status 422
json errors: {email: ['is already subscribed']}
end
end
get '/configurable_attributes.json' do
json(Subscription.configurable_attributes) + "\n"
end
get '/new.json' do
json Subscription.new
end
get '/:id.json' do |id|
json subscription(id)
end
put '/:id.json' do |id|
sub = subscription(id)
list = sub.list
args = find_attributes_from_body(%w[email fingerprint admin delivery_enabled])
fingerprint, messages = list.import_key_and_find_fingerprint(find_key_material)
set_x_messages(messages)
# For an already existing subscription, only update fingerprint if a
# new one has been selected from the upload.
if fingerprint.present?
args["fingerprint"] = fingerprint
end
if sub.update(args)
200
else
client_error(sub, 422)
end
end
patch '/:id.json' do |id|
sub = subscription(id)
if sub.update(parsed_body)
200
else
client_error(sub)
end
end
delete '/:id.json' do |id|
if sub = subscription(id).destroy
200
else
client_error(sub)
end
end
end
end
class SchleuderApiDaemon < Sinatra::Base
get '/version.json' do
json version: Schleuder::VERSION
end
end
require_relative 'api_daemon_spec_helper'
describe 'status' do
it 'returns status code 200' do
authorize!
get '/status.json'
expect(last_response.status).to be 200
end
end
require_relative 'api_daemon_spec_helper'
describe 'version' do
it 'returns the current schleuder version' do
authorize!
get '/version.json'
expect(last_response.status).to be 200
expect(last_response.body).to eq "{\"version\":\"#{Schleuder::VERSION}\"}"
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment