Commit 5ce00859 authored by o@immerda.ch's avatar o@immerda.ch
Browse files

automatically send token to the api backend if available

parent 057c4b78
class AppPasswordsController < ApplicationController class AppPasswordsController < ApplicationController
include ApiBackend
def show def show
begin begin
res = ApiBackend::app_passwords(current_user) res = api.app_passwords
puts res puts res
@passwords = res['res'] @passwords = res['res']
if session[:app_pw] if session[:app_pw]
...@@ -23,15 +25,15 @@ class AppPasswordsController < ApplicationController ...@@ -23,15 +25,15 @@ class AppPasswordsController < ApplicationController
def edit def edit
if params[:app_name] if params[:app_name]
begin begin
res = ApiBackend::app_password_create( res = api.app_password_create(
current_user, params[:password], params[:app_name]) params[:password], params[:app_name])
session[:app_pw] = res['app_pw'] session[:app_pw] = res['app_pw']
rescue ApiBackend::ApiError => e rescue ApiBackend::ApiError => e
flash[:notice] = e.api_msg || :failed flash[:notice] = e.api_msg || :failed
end end
elsif params[:delete] elsif params[:delete]
begin begin
ApiBackend::app_password_delete(current_user, params[:delete], api_token) api.app_password_delete(params[:delete])
rescue ApiBackend::ApiError rescue ApiBackend::ApiError
flash[:notice] = :failed flash[:notice] = :failed
end end
......
...@@ -135,5 +135,4 @@ class ApplicationController < ActionController::Base ...@@ -135,5 +135,4 @@ class ApplicationController < ActionController::Base
def api_token def api_token
session[:api_token] session[:api_token]
end end
helper_method :api_token
end end
...@@ -9,281 +9,296 @@ module ApiBackend ...@@ -9,281 +9,296 @@ module ApiBackend
end end
end end
def self.sanitize(parts) class TheBackendImplementation
parts.map{|v| URI.encode(v)}.join("/") def sanitize(parts)
end parts.map{|v| URI.encode(v)}.join("/")
end
def self.post(what, data) def initialize(controller)
res = RestClient::Resource.new("#{IAPI::Endpoint}/#{sanitize(what)}", @controller = controller
:ssl_client_cert => IAPI::Cert, end
:ssl_client_key => IAPI::Key, def session
:ssl_ca_file => IAPI::CA, @controller.session
:verify_ssl => OpenSSL::SSL::VERIFY_PEER) end
begin
response = res.post(data.to_json, content_type: :json) def merge_token(data)
res = JSON.parse(response) unless session[:user_id]
if !res || res['result'] != "success" return data
puts "Expected success from iapi, but got #{res}" end
raise ApiError.new("blocked #{res}") auth = {"token" => session[:api_token]}
if Admin::Enabled
auth["admin_email"] = session[:user_id]
else
auth["email"] = session[:user_id]
end end
res auth.merge(data)
rescue RestClient::BadRequest => e
raise ApiError.new("400", JSON.parse(e.response)['errors'])
rescue => e
raise ApiError.new("#{e}")
end end
end
def self.get(what, data) def post(what, data = {})
res = RestClient::Resource.new("#{IAPI::Endpoint}/#{sanitize(what)}", data = merge_token(data)
:ssl_client_cert => IAPI::Cert, res = RestClient::Resource.new("#{IAPI::Endpoint}/#{sanitize(what)}",
:ssl_client_key => IAPI::Key, :ssl_client_cert => IAPI::Cert,
:ssl_ca_file => IAPI::CA, :ssl_client_key => IAPI::Key,
:verify_ssl => OpenSSL::SSL::VERIFY_PEER) :ssl_ca_file => IAPI::CA,
begin :verify_ssl => OpenSSL::SSL::VERIFY_PEER)
response = res.get(:params => data) begin
res = JSON.parse(response) response = res.post(data.to_json, content_type: :json)
if !res['result'] == "success" res = JSON.parse(response)
puts "Expected success from iapi, but got #{res}" if !res || res['result'] != "success"
raise ApiError.new("blocked #{res}") puts "Expected success from iapi, but got #{res}"
raise ApiError.new("blocked #{res}")
end
res
rescue RestClient::BadRequest => e
raise ApiError.new("400", JSON.parse(e.response)['errors'])
rescue => e
raise ApiError.new("#{e}")
end end
res
rescue RestClient::BadRequest => e
raise ApiError.new("400", JSON.parse(e.response)['errors'])
rescue => e
raise ApiError.new("#{e}")
end end
end
def self.account_properties(email, token) def get(what, data = {})
get(['users', 'properties'], {'email' => email, 'token' => token}) data = merge_token(data)
end puts data
res = RestClient::Resource.new("#{IAPI::Endpoint}/#{sanitize(what)}",
:ssl_client_cert => IAPI::Cert,
:ssl_client_key => IAPI::Key,
:ssl_ca_file => IAPI::CA,
:verify_ssl => OpenSSL::SSL::VERIFY_PEER)
begin
response = res.get(:params => data)
res = JSON.parse(response)
if !res['result'] == "success"
puts "Expected success from iapi, but got #{res}"
raise ApiError.new("blocked #{res}")
end
res
rescue RestClient::BadRequest => e
raise ApiError.new("400", JSON.parse(e.response)['errors'])
rescue => e
raise ApiError.new("#{e}")
end
end
def self.app_passwords(email) def account_properties
get(['users', 'app_passwords'], {'email' => email}) get(['users', 'properties'])
end end
def self.app_password_delete(email, pw_name, token) def app_passwords
post(['users', 'app_password_delete'], {'email' => email, get(['users', 'app_passwords'])
'token' => token, end
'pw_name' => pw_name})
end
def self.app_password_create(email, pw, pw_name) def app_password_delete(pw_name)
post(['users', 'app_password_create'], {'email' => email, post(['users', 'app_password_delete'], {'pw_name' => pw_name})
'password' => pw, end
'pw_name' => pw_name})
end
def self.user_info(admin_email, token, email, include_deleted=false) def app_password_create(pw, pw_name)
get(['users_admin', 'info'], { post(['users', 'app_password_create'], {'password' => pw,
'admin_email' => admin_email, 'pw_name' => pw_name})
'token' => token, end
'email' => email,
'include_deleted' => include_deleted})
end
def self.user_update(admin_email, token, email, action, value) def check_invite(token)
post(['users_admin', 'update'], { get(['invites', 'validate'], {'token' => token})
'admin_email' => admin_email, end
'token' => token,
'email' => email,
'action' => action,
'value' => value})
end
def self.check_invite(token) def valid_new_email?(email)
get(['invites', 'validate'], {'token' => token}) get(['users', 'check_valid_new_email'], {'email' => email})
end end
def self.valid_new_email?(email) def list_domains
get(['users', 'check_valid_new_email'], {'email' => email}) get(['domains', 'list'], {})['domains']
end end
def self.list(admin_email, token, filter = nil, domain = nil, mailbox = nil, deleted = nil) def get_domain(domain)
get(['users_admin', 'list'], { get(['domains', 'get'], {'domain' => domain})
'admin_email' => admin_email, end
'token' => token,
'filter' => filter,
'domain' => domain,
'mailbox' => mailbox,
'deleted' => deleted,})
end
def self.list_public_domains def update_domain(domain, values)
@public_domains ||= get(['domains', 'list_public'], {})['domains'] post(['domains', 'update'], {'domain' => domain, 'values' => values})
end end
def self.list_domains def create_domain(domain, values)
get(['domains', 'list'], {})['domains'] post(['domains', 'create'], {'domain' => domain, 'values' => values})
end end
def self.get_domain(domain) def delete_domain(domain)
get(['domains', 'get'], {'domain' => domain}) post(['domains', 'delete'], {'domain' => domain})
end end
def storagehosts
get(['mail', 'storagehosts'], {})
end
def self.update_domain(domain, values) def jabber_get_jid(jid, host = nil, include_deleted = false)
post(['domains', 'update'], {'domain' => domain, 'values' => values}) get(['jabber', 'info'], {
end 'jid' => jid,
'host' => host,
'include_deleted' => include_deleted,
})
end
def self.create_domain(domain, values) def jabber_delete_jid(jid, password)
post(['domains', 'create'], {'domain' => domain, 'values' => values}) post(['jabber', 'delete'], {
end 'jid' => jid,
'account_password' => password,
})
end
def self.delete_domain(domain) def jabber_create(jid_alias = nil, domain = nil)
post(['domains', 'delete'], {'domain' => domain}) post(['jabber', 'create'], {
end 'jid_alias' => jid_alias,
def self.storagehosts 'domain' => domain,
get(['mail', 'storagehosts'], {}) })
end end
def self.jabber_get_jid(email, jid, host = nil, include_deleted = false) def jabber_reset_password(jid)
get(['jabber', 'info'], { post(['jabber', 'reset_password'], {
'email' => email, 'jid' => jid,
'jid' => jid, })
'host' => host, end
'include_deleted' => include_deleted,
})
end
def self.jabber_delete_jid(email, jid, password) def jabber_purge_jid(jid)
post(['jabber', 'delete'], { post(['jabber', 'purge'], {
'email' => email, 'jid' => jid,
'jid' => jid, })
'account_password' => password, end
})
end
def self.jabber_create(email, jid_alias = nil, domain = nil) def jabber_get_jids_admin(filter)
post(['jabber', 'create'], { get(['jabber', 'list_admin'], filter)
'email' => email, end
'jid_alias' => jid_alias,
'domain' => domain,
})
end
def self.jabber_reset_password(email, jid) def jabber_get_jids
post(['jabber', 'reset_password'], { get(['jabber', 'list'])
'email' => email, end
'jid' => jid,
})
end
def self.jabber_purge_jid(jid) def jabber_domains_public
post(['jabber', 'purge'], { get(['jabber_domains', 'list_public'])['domains']
'jid' => jid, end
}) def jabber_domains
end get(['jabber_domains', 'list'])['domains']
end
def jabber_get_domain(domain)
get(['jabber_domains', 'get'],{
'domain' => domain,
})
end
def self.jabber_get_jids(filter = {}) def jabber_create_domain(domain, host, ispublic = false)
get(['jabber', 'list'], filter) post(['jabber_domains', 'create'],{
end 'domain' => domain,
'host' => host,
'ispublic' => ispublic,
})
end
def self.jabber_domains_public def jabber_delete_domain(domain)
get(['jabber_domains', 'list_public'], {})['domains'] post(['jabber_domains', 'delete'],{
end 'domain' => domain,
def self.jabber_domains })
get(['jabber_domains', 'list'], {})['domains'] end
end
def self.jabber_get_domain(domain)
get(['jabber_domains', 'get'],{
'domain' => domain,
})
end
def self.jabber_create_domain(domain, host, ispublic = false) def jabber_update_domain(domain, host, ispublic = false)
post(['jabber_domains', 'create'],{ post(['jabber_domains', 'update'],{
'domain' => domain, 'domain' => domain,
'host' => host, 'host' => host,
'ispublic' => ispublic, 'ispublic' => ispublic,
}) })
end end
def self.jabber_get_jabberhosts def jabber_get_jabberhosts
get(['jabber_domains', 'jabberhosts'], {})['hosts'] get(['jabber_domains', 'jabberhosts'], {})['hosts']
end end
def self.jabber_update_domain(domain, host, ispublic = false) def change_password(old, pw)
post(['jabber_domains', 'update'],{ post(['users', 'change_password'], {"current_password" => old, "new_password" => pw})
'domain' => domain, end
'host' => host,
'ispublic' => ispublic,
})
end
def self.jabber_delete_domain(domain) def delete_account(pw)
post(['jabber_domains', 'delete'],{ post(['users', 'delete_account'], {"password" => pw})
'domain' => domain, end
})
end
def self.change_password(user, old, pw) def mail_crypt_enable(pw)
post(['users', 'change_password'], {"email"=>user, "current_password" => old, "new_password" => pw}) post(['users', 'enable_mail_crypt'], {"password" => pw})
end end
def self.delete_account(user, pw) def enable_totp(name)
post(['users', 'delete_account'], {"email"=> user, "password" => pw}) post(['users', 'enable_totp'], {'name' => name})
end end
def self.mail_crypt_enable(user, pw) def get_totp_names
post(['users', 'enable_mail_crypt'], {"email"=> user, "password" => pw}) get(['users', 'totp_names'])
end end
def self.enable_totp(user, name) def delete_totp(name)
post(['users', 'enable_totp'], {"email"=> user, 'name' => name}) post(['users', 'delete_totp'], {'name' => name })
end end
def self.get_totp_names(user) def mail_crypt_token(pw)
get(['users', 'totp_names'], {"email" => user }) post(['users', 'generate_recovery_token'], {"password" => pw})
end end
def self.delete_totp(user, name) def set_recovery_email(recovery, email_token, pass = nil)
post(['users', 'delete_totp'], {"email" => user, 'name' => name }) post(['users', 'set_recovery_email'], {"recovery_email" => recovery, 'email_recovery_token' => email_token, 'password' => pass})
end end
def self.mail_crypt_token(user, pw)
post(['users', 'generate_recovery_token'], {"email"=> user, "password" => pw})
end
def self.set_recovery_email(email, recovery, email_token, pass = nil) def create_new_mailbox(email, pw, mail_crypt_enabled, invite, recovery_email, recovery_token)
post(['users', 'set_recovery_email'], {"email"=> email, "recovery_email" => recovery, 'email_recovery_token' => email_token, 'password' => pass}) post(['users', 'create_new_mailbox'], {"email" => email,
end "password" => pw,
"mail_crypt_enabled" => mail_crypt_enabled,
"invite" => invite,
"recovery_email" => recovery_email,
"keep_recovery_token" => recovery_token == 'yes',
"email_recovery_token" => recovery_token == 'email',
"language" => I18n.locale,
})
end
def self.generate_invites(num, expire, allow_admin_account, domains=nil) def generate_invites(num, expire, allow_admin_account, domains=nil)
post(['invites', 'generate'], {"number" => num, "expire" => expire, "domains" => domains, 'allow_admin_account' => allow_admin_account}) post(['invites', 'generate'], {"number" => num, "expire" => expire, "domains" => domains, 'allow_admin_account' => allow_admin_account})
end end
def self.create_new_mailbox(email, pw, mail_crypt_enabled, invite, recovery_email, recovery_token) def user_info(email, include_deleted=false)
post(['users', 'create_new_mailbox'], {"email" => email, get(['users_admin', 'info'], {
"password" => pw, 'email' => email,
"mail_crypt_enabled" => mail_crypt_enabled, 'include_deleted' => include_deleted})
"invite" => invite, end
"recovery_email" => recovery_email,
"keep_recovery_token" => recovery_token == 'yes',
"email_recovery_token" => recovery_token == 'email',
"language" => I18n.locale,
})
end
def self.create_new_mailbox_admin(admin_email, token, email, pw, forward, mail_crypt_enabled, recovery_email, keep_recovery_token, storagehost) def user_update(email, action, value)
post(['users_admin', 'create_new_mailbox'], { post(['users_admin', 'update'], {
'admin_email' => admin_email, 'email' => email,
'token' => token, 'action' => action,
"email" => email, 'value' => value})
"password" => pw, end
"forward" => forward,
"storagehost" => storagehost,
"mail_crypt_enabled" => mail_crypt_enabled, def list(filter = nil, domain = nil, mailbox = nil, deleted = nil)
"recovery_email" => recovery_email, get(['users_admin', 'list'], {
"keep_recovery_token" => keep_recovery_token, 'filter' => filter,
}) 'domain' => domain,
'mailbox' => mailbox,
'deleted' => deleted,})
end
def create_new_mailbox_admin(email, pw, forward, mail_crypt_enabled, recovery_email, keep_recovery_token, storagehost)
post(['users_admin', 'create_new_mailbox'], {
"email" => email,
"password" => pw,
"forward" => forward,
"storagehost" => storagehost,
"mail_crypt_enabled" => mail_crypt_enabled,
"recovery_email" => recovery_email,
"keep_recovery_token" => keep_recovery_token,
})
end
def create_new_forward_admin(email, forwards)
post(['users_admin', 'create_new_forward'], {
"email" => email,
"forwards" => forwards})
end
end end
def self.create_new_forward_admin(email, forwards) def api
post(['users_admin', 'create_new_forward'], { @the_api ||= TheBackendImplementation.new(self)
'admin_email' => admin_email,
'token' => token,
"email" => email,