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