mirror of
https://github.com/internetee/registry.git
synced 2025-08-01 07:26:22 +02:00
added tokens
This commit is contained in:
parent
c2969fa877
commit
7e0c6b3afb
13 changed files with 94 additions and 62 deletions
3
Gemfile
3
Gemfile
|
@ -104,3 +104,6 @@ gem 'pghero'
|
||||||
gem 'pg_query', '>= 0.9.0'
|
gem 'pg_query', '>= 0.9.0'
|
||||||
gem 'newrelic_rpm'
|
gem 'newrelic_rpm'
|
||||||
gem 'newrelic-infinite_tracing'
|
gem 'newrelic-infinite_tracing'
|
||||||
|
|
||||||
|
# token
|
||||||
|
gem 'jwt'
|
||||||
|
|
|
@ -275,6 +275,7 @@ GEM
|
||||||
activesupport (>= 4.2)
|
activesupport (>= 4.2)
|
||||||
aes_key_wrap
|
aes_key_wrap
|
||||||
bindata
|
bindata
|
||||||
|
jwt (2.3.0)
|
||||||
kaminari (1.2.1)
|
kaminari (1.2.1)
|
||||||
activesupport (>= 4.1.0)
|
activesupport (>= 4.1.0)
|
||||||
kaminari-actionview (= 1.2.1)
|
kaminari-actionview (= 1.2.1)
|
||||||
|
@ -560,6 +561,7 @@ DEPENDENCIES
|
||||||
iso8601 (= 0.13.0)
|
iso8601 (= 0.13.0)
|
||||||
jquery-rails
|
jquery-rails
|
||||||
jquery-ui-rails (= 6.0.1)
|
jquery-ui-rails (= 6.0.1)
|
||||||
|
jwt
|
||||||
kaminari
|
kaminari
|
||||||
lhv!
|
lhv!
|
||||||
mime-types-data
|
mime-types-data
|
||||||
|
|
|
@ -101,14 +101,5 @@ module Admin
|
||||||
|
|
||||||
invoices.where(account_activities: { created_at: date_from..date_until })
|
invoices.where(account_activities: { created_at: date_from..date_until })
|
||||||
end
|
end
|
||||||
|
|
||||||
# def send_invoice_data_to_billing_system
|
|
||||||
# add_invoice_instance = EisBilling::AddDeposits.new(@invoice)
|
|
||||||
# result = add_invoice_instance.send_invoice
|
|
||||||
|
|
||||||
# link = JSON.parse(result.body)['everypay_link']
|
|
||||||
|
|
||||||
# @invoice.update(payment_link: link)
|
|
||||||
# end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,14 +1,51 @@
|
||||||
module EisBilling
|
module EisBilling
|
||||||
class BaseController < ApplicationController
|
class BaseController < ApplicationController
|
||||||
# load_and_authorize_resource
|
protect_from_forgery with: :null_session
|
||||||
|
|
||||||
skip_authorization_check # Temporary solution
|
skip_authorization_check # Temporary solution
|
||||||
skip_before_action :verify_authenticity_token # Temporary solution
|
# skip_before_action :verify_authenticity_token # Temporary solution
|
||||||
end
|
before_action :authorized
|
||||||
|
|
||||||
protected
|
def encode_token(payload)
|
||||||
|
JWT.encode(payload, ENV['secret_word'])
|
||||||
|
end
|
||||||
|
|
||||||
def logger
|
def auth_header
|
||||||
@logger ||= Rails.logger
|
# { Authorization: 'Bearer <token>' }
|
||||||
|
request.headers['Authorization']
|
||||||
|
end
|
||||||
|
|
||||||
|
def decoded_token
|
||||||
|
if auth_header
|
||||||
|
token = auth_header.split(' ')[1]
|
||||||
|
# header: { 'Authorization': 'Bearer <token>' }
|
||||||
|
begin
|
||||||
|
JWT.decode(token, ENV['secret_word'], true, algorithm: 'HS256')
|
||||||
|
rescue JWT::DecodeError
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def accessable_service
|
||||||
|
if decoded_token
|
||||||
|
decoded_token[0]['data'] == ENV['secret_access_word']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def logged_in?
|
||||||
|
!!accessable_service
|
||||||
|
end
|
||||||
|
|
||||||
|
def authorized
|
||||||
|
render json: { message: 'Access denied' }, status: :unauthorized unless logged_in?
|
||||||
|
end
|
||||||
|
|
||||||
|
def logger
|
||||||
|
Rails.logger
|
||||||
|
end
|
||||||
|
|
||||||
|
def logger
|
||||||
|
@logger ||= Rails.logger
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,7 +12,6 @@ class Registrar
|
||||||
|
|
||||||
if @invoice
|
if @invoice
|
||||||
flash[:notice] = t(:please_pay_the_following_invoice)
|
flash[:notice] = t(:please_pay_the_following_invoice)
|
||||||
# send_invoice_data_to_billing_system
|
|
||||||
redirect_to [:registrar, @invoice]
|
redirect_to [:registrar, @invoice]
|
||||||
else
|
else
|
||||||
flash[:alert] = @deposit.errors.full_messages.join(', ')
|
flash[:alert] = @deposit.errors.full_messages.join(', ')
|
||||||
|
@ -22,15 +21,6 @@ class Registrar
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# def send_invoice_data_to_billing_system
|
|
||||||
# add_invoice_instance = EisBilling::AddDeposits.new(@invoice)
|
|
||||||
# result = add_invoice_instance.send_invoice
|
|
||||||
|
|
||||||
# link = JSON.parse(result.body)['everypay_link']
|
|
||||||
|
|
||||||
# @invoice.update(payment_link: link)
|
|
||||||
# end
|
|
||||||
|
|
||||||
def deposit_params
|
def deposit_params
|
||||||
params.require(:deposit).permit(:amount, :description)
|
params.require(:deposit).permit(:amount, :description)
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,21 +41,14 @@ class Invoice < ApplicationRecord
|
||||||
attribute :vat_rate, ::Type::VatRate.new
|
attribute :vat_rate, ::Type::VatRate.new
|
||||||
|
|
||||||
def set_invoice_number
|
def set_invoice_number
|
||||||
# last_no = Invoice.order(number: :desc).limit(1).pick(:number)
|
|
||||||
|
|
||||||
# if last_no && last_no >= Setting.invoice_number_min.to_i
|
|
||||||
# self.number = last_no + 1
|
|
||||||
# else
|
|
||||||
# self.number = Setting.invoice_number_min.to_i
|
|
||||||
# end
|
|
||||||
|
|
||||||
# return if number <= Setting.invoice_number_max.to_i
|
|
||||||
|
|
||||||
# errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
|
|
||||||
# logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
|
|
||||||
# throw(:abort)
|
|
||||||
result = EisBilling::GetInvoiceNumber.send_invoice
|
result = EisBilling::GetInvoiceNumber.send_invoice
|
||||||
|
|
||||||
|
if JSON.parse(result.body)['code'] == '403'
|
||||||
|
errors.add(:base, I18n.t('cannot get access'))
|
||||||
|
logger.error('PROBLEM WITH TOKEN')
|
||||||
|
throw(:abort)
|
||||||
|
end
|
||||||
|
|
||||||
if JSON.parse(result.body)['error'] == 'out of range'
|
if JSON.parse(result.body)['error'] == 'out of range'
|
||||||
errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
|
errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
|
||||||
logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
|
logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
|
||||||
|
|
|
@ -27,7 +27,7 @@ module EisBilling
|
||||||
|
|
||||||
def send_request(json_obj:)
|
def send_request(json_obj:)
|
||||||
http = EisBilling::Base.base_request(url: invoice_generator_url)
|
http = EisBilling::Base.base_request(url: invoice_generator_url)
|
||||||
http.post(invoice_generator_url, json_obj.to_json, HEADERS)
|
http.post(invoice_generator_url, json_obj.to_json, EisBilling::Base.headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoice_generator_url
|
def invoice_generator_url
|
||||||
|
|
|
@ -1,12 +1,5 @@
|
||||||
module EisBilling
|
module EisBilling
|
||||||
class Base
|
class Base
|
||||||
# crypt = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base[0..31])
|
|
||||||
# irb(main):047:0> encrypted_data = crypt.encrypt_and_sign('PLEASE CREATE INVOICE')
|
|
||||||
# =>
|
|
||||||
# irb(main):048:0> decrypted_back = crypt.decrypt_and_verify(encrypted_data)
|
|
||||||
# =>
|
|
||||||
TOKEN = 'Bearer WA9UvDmzR9UcE5rLqpWravPQtdS8eDMAIynzGdSOTw==--9ZShwwij3qmLeuMJ--NE96w2PnfpfyIuuNzDJTGw=='.freeze
|
|
||||||
|
|
||||||
BASE_URL = ''
|
BASE_URL = ''
|
||||||
if Rails.env.staging?
|
if Rails.env.staging?
|
||||||
BASE_URL = ENV['eis_billing_system_base_url_staging']
|
BASE_URL = ENV['eis_billing_system_base_url_staging']
|
||||||
|
@ -16,11 +9,8 @@ module EisBilling
|
||||||
|
|
||||||
INITIATOR = 'registry'
|
INITIATOR = 'registry'
|
||||||
|
|
||||||
HEADERS = {
|
SECRET_WORD = ENV['secret_word']
|
||||||
'Authorization' => 'Bearer foobar',
|
SECRET_ACCESS_WORD = ENV['secret_access_word']
|
||||||
'Content-Type' => 'application/json',
|
|
||||||
'Accept' => TOKEN
|
|
||||||
}
|
|
||||||
|
|
||||||
def self.base_request(url:)
|
def self.base_request(url:)
|
||||||
uri = URI(url)
|
uri = URI(url)
|
||||||
|
@ -33,5 +23,20 @@ module EisBilling
|
||||||
|
|
||||||
http
|
http
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.generate_token
|
||||||
|
JWT.encode(payload, ENV['secret_word'])
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.payload
|
||||||
|
{ data: ENV['secret_access_word'] }
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.headers
|
||||||
|
{
|
||||||
|
'Authorization' => "Bearer #{generate_token}",
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ module EisBilling
|
||||||
|
|
||||||
def self.send_request
|
def self.send_request
|
||||||
http = EisBilling::Base.base_request(url: invoice_number_generator_url)
|
http = EisBilling::Base.base_request(url: invoice_number_generator_url)
|
||||||
http.post(invoice_number_generator_url, nil, HEADERS)
|
http.post(invoice_number_generator_url, nil, EisBilling::Base.headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.invoice_number_generator_url
|
def self.invoice_number_generator_url
|
||||||
|
|
|
@ -14,11 +14,11 @@ module EisBilling
|
||||||
|
|
||||||
def self.send_request
|
def self.send_request
|
||||||
http = EisBilling::Base.base_request(url: reference_number_generator_url)
|
http = EisBilling::Base.base_request(url: reference_number_generator_url)
|
||||||
http.post(reference_number_generator_url, obj_data.to_json, HEADERS)
|
http.post(reference_number_generator_url, obj_data.to_json, EisBilling::Base.headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.reference_number_generator_url
|
def self.reference_number_generator_url
|
||||||
"#{BASE_URL}/api/v1/invoice_generator/reference_number_generator"
|
"#{EisBilling::Base::BASE_URL}/api/v1/invoice_generator/reference_number_generator"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,7 @@ module EisBilling
|
||||||
}
|
}
|
||||||
|
|
||||||
http = EisBilling::Base.base_request(url: directo_url)
|
http = EisBilling::Base.base_request(url: directo_url)
|
||||||
http.post(directo_url, prepared_data.to_json, HEADERS)
|
http.post(directo_url, prepared_data.to_json, EisBilling::Base.headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.directo_url
|
def self.directo_url
|
||||||
|
|
|
@ -34,7 +34,7 @@ module EisBilling
|
||||||
prepared_data[:items] = items
|
prepared_data[:items] = items
|
||||||
|
|
||||||
http = EisBilling::Base.base_request(url: e_invoice_url)
|
http = EisBilling::Base.base_request(url: e_invoice_url)
|
||||||
http.post(e_invoice_url, prepared_data.to_json, HEADERS)
|
http.post(e_invoice_url, prepared_data.to_json, EisBilling::Base.headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.e_invoice_url
|
def self.e_invoice_url
|
||||||
|
|
|
@ -46,11 +46,6 @@ end
|
||||||
def base_request(url:, json_obj:)
|
def base_request(url:, json_obj:)
|
||||||
uri = URI(url)
|
uri = URI(url)
|
||||||
http = Net::HTTP.new(uri.host, uri.port)
|
http = Net::HTTP.new(uri.host, uri.port)
|
||||||
headers = {
|
|
||||||
'Authorization' => 'Bearer foobar',
|
|
||||||
'Content-Type' => 'application/json',
|
|
||||||
'Accept' => TOKEN
|
|
||||||
}
|
|
||||||
|
|
||||||
unless Rails.env.development?
|
unless Rails.env.development?
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
|
@ -59,3 +54,19 @@ def base_request(url:, json_obj:)
|
||||||
|
|
||||||
http.post(url, json_obj.to_json, headers)
|
http.post(url, json_obj.to_json, headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def generate_token
|
||||||
|
JWT.encode(payload, ENV['secret_word'])
|
||||||
|
end
|
||||||
|
|
||||||
|
def payload
|
||||||
|
{ data: ENV['secret_access_word'] }
|
||||||
|
end
|
||||||
|
|
||||||
|
def headers
|
||||||
|
{
|
||||||
|
'Authorization' => "Bearer #{generate_token}",
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue