From 6e82d8be9e85838a7b1788413e4f9af57a7d917d Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 28 Jul 2021 12:11:38 +0300 Subject: [PATCH 01/15] added api endpoint for registrar login --- .../repp/v1/registrar/login_controller.rb | 22 +++++++++++++++++++ config/routes.rb | 5 +++++ 2 files changed, 27 insertions(+) create mode 100644 app/controllers/repp/v1/registrar/login_controller.rb diff --git a/app/controllers/repp/v1/registrar/login_controller.rb b/app/controllers/repp/v1/registrar/login_controller.rb new file mode 100644 index 000000000..501c6129c --- /dev/null +++ b/app/controllers/repp/v1/registrar/login_controller.rb @@ -0,0 +1,22 @@ +module Repp + module V1 + module Registrar + class LoginController < BaseController + api :GET, 'repp/v1/registrar/login' + desc 'check login user and return data' + + def index + @login = current_user + + # rubocop:disable Style/AndOr + render_success(data: nil) and return unless @login + # rubocop:enable Style/AndOr + + data = @login.as_json() + + render_success(data: data) + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 1a3b394d1..a7f9b4302 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,6 +72,11 @@ Rails.application.routes.draw do get '/all_notifications', to: 'notifications#all_notifications' end end + resource :login, only: [:index] do + collection do + get '/', to: 'login#index' + end + end resources :nameservers do collection do put '/', to: 'nameservers#update' From 64cb2c35f31cb606d9a890b04c2015aacc40403f Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 28 Jul 2021 12:36:08 +0300 Subject: [PATCH 02/15] added test --- .../repp/v1/registrar/login_controller.rb | 2 +- .../repp/v1/registrar/login_test.rb | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/integration/repp/v1/registrar/login_test.rb diff --git a/app/controllers/repp/v1/registrar/login_controller.rb b/app/controllers/repp/v1/registrar/login_controller.rb index 501c6129c..e487d480e 100644 --- a/app/controllers/repp/v1/registrar/login_controller.rb +++ b/app/controllers/repp/v1/registrar/login_controller.rb @@ -12,7 +12,7 @@ module Repp render_success(data: nil) and return unless @login # rubocop:enable Style/AndOr - data = @login.as_json() + data = @login.as_json render_success(data: data) end diff --git a/test/integration/repp/v1/registrar/login_test.rb b/test/integration/repp/v1/registrar/login_test.rb new file mode 100644 index 000000000..d210a7848 --- /dev/null +++ b/test/integration/repp/v1/registrar/login_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class ReppV1LoginTest < ActionDispatch::IntegrationTest + def setup + @user = users(:api_bestnames) + token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}") + token = "Basic #{token}" + + @auth_headers = { 'Authorization' => token } + end + + def test_valid_login + get '/repp/v1/registrar/login', headers: @auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal json[:data][:username], @user.username + assert_equal json[:data][:identity_code], @user.identity_code + end + + def test_invalid_login + token = Base64.encode64("#{@user.username}:0066600") + token = "Basic #{token}" + + auth_headers = { 'Authorization' => token } + + get '/repp/v1/registrar/login', headers: auth_headers + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :unauthorized + assert_equal json[:message], 'Invalid authorization information' + end +end From 68ff1c1e64672786fd001c16f7e3d7c27c761261 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 28 Jul 2021 14:19:13 +0300 Subject: [PATCH 03/15] Changed tests --- app/controllers/repp/v1/registrar/login_controller.rb | 2 +- test/integration/repp/v1/registrar/login_test.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/repp/v1/registrar/login_controller.rb b/app/controllers/repp/v1/registrar/login_controller.rb index e487d480e..7ecddd1db 100644 --- a/app/controllers/repp/v1/registrar/login_controller.rb +++ b/app/controllers/repp/v1/registrar/login_controller.rb @@ -6,7 +6,7 @@ module Repp desc 'check login user and return data' def index - @login = current_user + @login = current_user.registrar # rubocop:disable Style/AndOr render_success(data: nil) and return unless @login diff --git a/test/integration/repp/v1/registrar/login_test.rb b/test/integration/repp/v1/registrar/login_test.rb index d210a7848..354e66406 100644 --- a/test/integration/repp/v1/registrar/login_test.rb +++ b/test/integration/repp/v1/registrar/login_test.rb @@ -3,6 +3,7 @@ require 'test_helper' class ReppV1LoginTest < ActionDispatch::IntegrationTest def setup @user = users(:api_bestnames) + @registrar = @user.registrar token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}") token = "Basic #{token}" @@ -14,8 +15,8 @@ class ReppV1LoginTest < ActionDispatch::IntegrationTest json = JSON.parse(response.body, symbolize_names: true) assert_response :ok - assert_equal json[:data][:username], @user.username - assert_equal json[:data][:identity_code], @user.identity_code + assert_equal json[:data][:email], @registrar.email + assert_equal json[:data][:id], @registrar.id end def test_invalid_login From 36e9aac1d3004c784794b9b950f281d39fc950a3 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 28 Jul 2021 14:47:43 +0300 Subject: [PATCH 04/15] changed response --- app/controllers/repp/v1/registrar/login_controller.rb | 2 +- test/integration/repp/v1/registrar/login_test.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/repp/v1/registrar/login_controller.rb b/app/controllers/repp/v1/registrar/login_controller.rb index 7ecddd1db..e487d480e 100644 --- a/app/controllers/repp/v1/registrar/login_controller.rb +++ b/app/controllers/repp/v1/registrar/login_controller.rb @@ -6,7 +6,7 @@ module Repp desc 'check login user and return data' def index - @login = current_user.registrar + @login = current_user # rubocop:disable Style/AndOr render_success(data: nil) and return unless @login diff --git a/test/integration/repp/v1/registrar/login_test.rb b/test/integration/repp/v1/registrar/login_test.rb index 354e66406..d210a7848 100644 --- a/test/integration/repp/v1/registrar/login_test.rb +++ b/test/integration/repp/v1/registrar/login_test.rb @@ -3,7 +3,6 @@ require 'test_helper' class ReppV1LoginTest < ActionDispatch::IntegrationTest def setup @user = users(:api_bestnames) - @registrar = @user.registrar token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}") token = "Basic #{token}" @@ -15,8 +14,8 @@ class ReppV1LoginTest < ActionDispatch::IntegrationTest json = JSON.parse(response.body, symbolize_names: true) assert_response :ok - assert_equal json[:data][:email], @registrar.email - assert_equal json[:data][:id], @registrar.id + assert_equal json[:data][:username], @user.username + assert_equal json[:data][:identity_code], @user.identity_code end def test_invalid_login From f6e46b8fe6c869bd990a579a4b0ac19fcb3a5aad Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Thu, 29 Jul 2021 12:50:01 +0300 Subject: [PATCH 05/15] change endpoint --- ...login_controller.rb => accreditation_info_controller.rb} | 4 ++-- config/routes.rb | 4 ++-- .../registrar/{login_test.rb => accreditaion_info_test.rb} | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) rename app/controllers/repp/v1/registrar/{login_controller.rb => accreditation_info_controller.rb} (77%) rename test/integration/repp/v1/registrar/{login_test.rb => accreditaion_info_test.rb} (79%) diff --git a/app/controllers/repp/v1/registrar/login_controller.rb b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb similarity index 77% rename from app/controllers/repp/v1/registrar/login_controller.rb rename to app/controllers/repp/v1/registrar/accreditation_info_controller.rb index e487d480e..5859b8acf 100644 --- a/app/controllers/repp/v1/registrar/login_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb @@ -1,8 +1,8 @@ module Repp module V1 module Registrar - class LoginController < BaseController - api :GET, 'repp/v1/registrar/login' + class AccreditationInfoController < BaseController + api :GET, 'repp/v1/registrar/accreditation_info' desc 'check login user and return data' def index diff --git a/config/routes.rb b/config/routes.rb index a7f9b4302..21827911e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,9 +72,9 @@ Rails.application.routes.draw do get '/all_notifications', to: 'notifications#all_notifications' end end - resource :login, only: [:index] do + resource :accreditation_info, only: [:index] do collection do - get '/', to: 'login#index' + get '/', to: 'accreditation_info#index' end end resources :nameservers do diff --git a/test/integration/repp/v1/registrar/login_test.rb b/test/integration/repp/v1/registrar/accreditaion_info_test.rb similarity index 79% rename from test/integration/repp/v1/registrar/login_test.rb rename to test/integration/repp/v1/registrar/accreditaion_info_test.rb index d210a7848..c2a23d2c0 100644 --- a/test/integration/repp/v1/registrar/login_test.rb +++ b/test/integration/repp/v1/registrar/accreditaion_info_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class ReppV1LoginTest < ActionDispatch::IntegrationTest +class ReppV1AccreditationInfoTest < ActionDispatch::IntegrationTest def setup @user = users(:api_bestnames) token = Base64.encode64("#{@user.username}:#{@user.plain_text_password}") @@ -10,7 +10,7 @@ class ReppV1LoginTest < ActionDispatch::IntegrationTest end def test_valid_login - get '/repp/v1/registrar/login', headers: @auth_headers + get '/repp/v1/registrar/accreditation_info', headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) assert_response :ok @@ -24,7 +24,7 @@ class ReppV1LoginTest < ActionDispatch::IntegrationTest auth_headers = { 'Authorization' => token } - get '/repp/v1/registrar/login', headers: auth_headers + get '/repp/v1/registrar/accreditation_info', headers: auth_headers json = JSON.parse(response.body, symbolize_names: true) assert_response :unauthorized From d56c61bf8488225317e4bffb822f1e7f19af5fd6 Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Thu, 28 Oct 2021 09:49:15 +0300 Subject: [PATCH 06/15] resolve structure conflict --- .../accreditation_info_controller.rb | 10 ++- .../accreditation_results_controller.rb | 75 +++++++++++++++++++ config/routes.rb | 5 +- .../20210729131100_add_field_to_user.rb | 6 ++ .../20210729134625_add_column_to_user.rb | 5 ++ db/structure.sql | 11 ++- .../v1/registrar/accreditaion_info_test.rb | 4 +- 7 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 app/controllers/repp/v1/registrar/accreditation_results_controller.rb create mode 100644 db/migrate/20210729131100_add_field_to_user.rb create mode 100644 db/migrate/20210729134625_add_column_to_user.rb diff --git a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb index 5859b8acf..3851ded65 100644 --- a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb @@ -2,17 +2,23 @@ module Repp module V1 module Registrar class AccreditationInfoController < BaseController - api :GET, 'repp/v1/registrar/accreditation_info' + api :GET, 'repp/v1/registrar/accreditation/get_info' desc 'check login user and return data' def index @login = current_user + registrar = current_user.registrar + + # name = registrar.name + # reg_no = registrar.reg_no # rubocop:disable Style/AndOr render_success(data: nil) and return unless @login # rubocop:enable Style/AndOr - data = @login.as_json + data = @login.as_json(only: %i[id username name reg_no uuid roles accreditation_date accreditation_expire_date]) + data[:registrar_name] = registrar.name + data[:registrar_reg_no] = registrar.reg_no render_success(data: data) end diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb new file mode 100644 index 000000000..a959159c8 --- /dev/null +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -0,0 +1,75 @@ +module Repp + module V1 + module Registrar + class AccreditationResultsController < ActionController::API + before_action :authenticate_admin + + # api :POST, 'repp/v1/registrar/push_results' + api :GET, 'repp/v1/registrar/accreditation/push_results' + desc 'added datetime results' + + def index + @login = @current_user + + # rubocop:disable Style/AndOr + render_success(data: nil) and return unless @login + # rubocop:enable Style/AndOr + + data = @login + render_success(data: data) + end + + # def create + # @login = current_user + # registrar = current_user.registrar + + # rubocop:disable Style/AndOr + # render_success(data: nil) and return unless @login + # rubocop:enable Style/AndOr + + # user = ApiUser.find(params[:user_id]) + # user.accreditation_date = Date.now + # user.save + + + + # data = @login.as_json(only: %i[id username name reg_no uuid roles accreditation_date accreditation_expire_date]) + # data[:registrar_name] = registrar.name + # data[:registrar_reg_no] = registrar.reg_no + + # render_success(data: data) + # end + + private + + def authenticate_admin + # TODO: ADD MORE CONDITIONS FOR ACCR ADMIN REQUESTS + username, password = Base64.urlsafe_decode64(basic_token).split(':') + @current_user ||= User.find_by(username: username, plain_text_password: password) + + return if @current_user + # return if @current_user.roles.include? "admin" + + raise(ArgumentError) + rescue NoMethodError, ArgumentError + @response = { code: 2202, message: 'Invalid authorization information' } + render(json: @response, status: :unauthorized) + end + + def basic_token + pattern = /^Basic / + header = request.headers['Authorization'] + header = header.gsub(pattern, '') if header&.match(pattern) + header.strip + end + + def render_success(code: nil, message: nil, data: nil) + @response = { code: code || 1000, message: message || 'Command completed successfully', + data: data || {} } + + render(json: @response, status: :ok) + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 21827911e..a1e3d58e8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,9 +72,10 @@ Rails.application.routes.draw do get '/all_notifications', to: 'notifications#all_notifications' end end - resource :accreditation_info, only: [:index] do + resource :accreditation, only: [:index] do collection do - get '/', to: 'accreditation_info#index' + get '/get_info', to: 'accreditation_info#index' + get '/push_results', to: 'accreditation_results#index' end end resources :nameservers do diff --git a/db/migrate/20210729131100_add_field_to_user.rb b/db/migrate/20210729131100_add_field_to_user.rb new file mode 100644 index 000000000..38efcea49 --- /dev/null +++ b/db/migrate/20210729131100_add_field_to_user.rb @@ -0,0 +1,6 @@ +class AddFieldToUser < ActiveRecord::Migration[6.1] + def change + add_column :users, :accreditation_date, :datetime + add_column :users, :accreditation_expire_date, :datetime + end +end diff --git a/db/migrate/20210729134625_add_column_to_user.rb b/db/migrate/20210729134625_add_column_to_user.rb new file mode 100644 index 000000000..c2131d3fb --- /dev/null +++ b/db/migrate/20210729134625_add_column_to_user.rb @@ -0,0 +1,5 @@ +class AddColumnToUser < ActiveRecord::Migration[6.1] + def change + add_column :users, :uuid, :uuid, default: 'gen_random_uuid()' + end +end diff --git a/db/structure.sql b/db/structure.sql index fdfacff95..d718d0d33 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2582,7 +2582,10 @@ CREATE TABLE public.users ( remember_created_at timestamp without time zone, failed_attempts integer DEFAULT 0 NOT NULL, locked_at timestamp without time zone, - legacy_id integer + legacy_id integer, + accreditation_date timestamp without time zone, + accreditation_expire_date timestamp without time zone, + uuid uuid DEFAULT public.gen_random_uuid() ); @@ -5230,6 +5233,6 @@ INSERT INTO "schema_migrations" (version) VALUES ('20210616112332'), ('20210629074044'), ('20210628090353'), -('20210708131814'); - - +('20210708131814'), +('20210729131100'), +('20210729134625'); diff --git a/test/integration/repp/v1/registrar/accreditaion_info_test.rb b/test/integration/repp/v1/registrar/accreditaion_info_test.rb index c2a23d2c0..237fa319a 100644 --- a/test/integration/repp/v1/registrar/accreditaion_info_test.rb +++ b/test/integration/repp/v1/registrar/accreditaion_info_test.rb @@ -10,7 +10,7 @@ class ReppV1AccreditationInfoTest < ActionDispatch::IntegrationTest end def test_valid_login - get '/repp/v1/registrar/accreditation_info', headers: @auth_headers + get '/repp/v1/registrar/accreditation/get_info', headers: @auth_headers json = JSON.parse(response.body, symbolize_names: true) assert_response :ok @@ -24,7 +24,7 @@ class ReppV1AccreditationInfoTest < ActionDispatch::IntegrationTest auth_headers = { 'Authorization' => token } - get '/repp/v1/registrar/accreditation_info', headers: auth_headers + get '/repp/v1/registrar/accreditation/get_info', headers: auth_headers json = JSON.parse(response.body, symbolize_names: true) assert_response :unauthorized From 8a1967bdb9670f5f7e88508abcab439e32beb68a Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 6 Aug 2021 12:26:41 +0300 Subject: [PATCH 07/15] implement api endpoint for record results --- .../accreditation_info_controller.rb | 26 ++++-- .../accreditation_results_controller.rb | 80 ++++++++----------- config/routes.rb | 2 +- .../v1/registrar/accreditaion_info_test.rb | 4 +- .../registrar/accreditation_results_test.rb | 49 ++++++++++++ 5 files changed, 104 insertions(+), 57 deletions(-) create mode 100644 test/integration/repp/v1/registrar/accreditation_results_test.rb diff --git a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb index 3851ded65..cd86ce9ed 100644 --- a/app/controllers/repp/v1/registrar/accreditation_info_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_info_controller.rb @@ -6,22 +6,32 @@ module Repp desc 'check login user and return data' def index - @login = current_user + login = current_user registrar = current_user.registrar - # name = registrar.name - # reg_no = registrar.reg_no - # rubocop:disable Style/AndOr - render_success(data: nil) and return unless @login + render_success(data: nil) and return unless login # rubocop:enable Style/AndOr - data = @login.as_json(only: %i[id username name reg_no uuid roles accreditation_date accreditation_expire_date]) - data[:registrar_name] = registrar.name - data[:registrar_reg_no] = registrar.reg_no + data = set_values_to_data(login: login, registrar: registrar) render_success(data: data) end + + private + + def set_values_to_data(login:, registrar:) + data = login.as_json(only: %i[id + username + name + uuid + roles + accreditation_date + accreditation_expire_date]) + data[:registrar_name] = registrar.name + data[:registrar_reg_no] = registrar.reg_no + data + end end end end diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index a959159c8..b0ed06c4c 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -2,65 +2,51 @@ module Repp module V1 module Registrar class AccreditationResultsController < ActionController::API - before_action :authenticate_admin + before_action :authenticate_shared_key - # api :POST, 'repp/v1/registrar/push_results' - api :GET, 'repp/v1/registrar/accreditation/push_results' + TEMPARY_SECRET_KEY = 'tempary-secret-key'.freeze + + api :POST, 'repp/v1/registrar/accreditation/push_results' desc 'added datetime results' - def index - @login = @current_user + def create + username = params[:accreditation_result][:username] + result = params[:accreditation_result][:result] - # rubocop:disable Style/AndOr - render_success(data: nil) and return unless @login - # rubocop:enable Style/AndOr - - data = @login - render_success(data: data) + record_accreditation_result(username, result) if result + rescue ActiveRecord::RecordNotFound + record_not_found(username) end - # def create - # @login = current_user - # registrar = current_user.registrar - - # rubocop:disable Style/AndOr - # render_success(data: nil) and return unless @login - # rubocop:enable Style/AndOr - - # user = ApiUser.find(params[:user_id]) - # user.accreditation_date = Date.now - # user.save - - - - # data = @login.as_json(only: %i[id username name reg_no uuid roles accreditation_date accreditation_expire_date]) - # data[:registrar_name] = registrar.name - # data[:registrar_reg_no] = registrar.reg_no - - # render_success(data: data) - # end - private - def authenticate_admin - # TODO: ADD MORE CONDITIONS FOR ACCR ADMIN REQUESTS - username, password = Base64.urlsafe_decode64(basic_token).split(':') - @current_user ||= User.find_by(username: username, plain_text_password: password) + def record_accreditation_result(username, result) + user = ApiUser.find_by(username: username) - return if @current_user - # return if @current_user.roles.include? "admin" + raise ActiveRecord::RecordNotFound if user.nil? - raise(ArgumentError) - rescue NoMethodError, ArgumentError - @response = { code: 2202, message: 'Invalid authorization information' } - render(json: @response, status: :unauthorized) + user.accreditation_date = DateTime.current + + return unless user.save + + render_success(data: { user: user, + result: result, + message: 'Accreditation info successfully added' }) end - def basic_token - pattern = /^Basic / - header = request.headers['Authorization'] - header = header.gsub(pattern, '') if header&.match(pattern) - header.strip + def authenticate_shared_key + api_key = "Basic #{TEMPARY_SECRET_KEY}" + render_failed unless api_key == request.authorization + end + + def record_not_found(username) + @response = { code: 2303, message: "Object '#{username}' does not exist" } + render(json: @response) + end + + def render_failed + @response = { code: 2202, message: 'Invalid authorization information' } + render(json: @response, status: :unauthorized) end def render_success(code: nil, message: nil, data: nil) diff --git a/config/routes.rb b/config/routes.rb index a1e3d58e8..e69e5affe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -75,7 +75,7 @@ Rails.application.routes.draw do resource :accreditation, only: [:index] do collection do get '/get_info', to: 'accreditation_info#index' - get '/push_results', to: 'accreditation_results#index' + post '/push_results', to: 'accreditation_results#create' end end resources :nameservers do diff --git a/test/integration/repp/v1/registrar/accreditaion_info_test.rb b/test/integration/repp/v1/registrar/accreditaion_info_test.rb index 237fa319a..4efba5d38 100644 --- a/test/integration/repp/v1/registrar/accreditaion_info_test.rb +++ b/test/integration/repp/v1/registrar/accreditaion_info_test.rb @@ -15,7 +15,9 @@ class ReppV1AccreditationInfoTest < ActionDispatch::IntegrationTest assert_response :ok assert_equal json[:data][:username], @user.username - assert_equal json[:data][:identity_code], @user.identity_code + assert json[:data][:roles].include? 'super' + assert_equal json[:data][:registrar_name], 'Best Names' + assert_equal json[:data][:registrar_reg_no], '1234' end def test_invalid_login diff --git a/test/integration/repp/v1/registrar/accreditation_results_test.rb b/test/integration/repp/v1/registrar/accreditation_results_test.rb new file mode 100644 index 000000000..8d2338ecf --- /dev/null +++ b/test/integration/repp/v1/registrar/accreditation_results_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class ReppV1AccreditationResultsTest < ActionDispatch::IntegrationTest + TEMPARY_SECRET_KEY = 'tempary-secret-key'.freeze + + def setup + @user = users(:api_bestnames) + + token = "Basic #{TEMPARY_SECRET_KEY}" + + @auth_headers = { 'Authorization' => token } + end + + def test_should_return_valid_response + post '/repp/v1/registrar/accreditation/push_results', + headers: @auth_headers, + params: {accreditation_result: {username: @user.username, result: true} } + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + assert_equal json[:data][:user][:username], @user.username + assert_equal json[:data][:result], "true" + assert_equal json[:data][:message], "Accreditation info successfully added" + end + + def test_should_return_valid_response_invalid_authorization + post '/repp/v1/registrar/accreditation/push_results', + headers: { 'Authorization' => 'Basic tempary-secret-ke'}, + params: {accreditation_result: {username: @user.username, result: true} } + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :unauthorized + + assert_equal json[:code], 2202 + assert_equal json[:message], 'Invalid authorization information' + end + + def test_should_return_valid_response_record_exception + post '/repp/v1/registrar/accreditation/push_results', + headers: @auth_headers, + params: {accreditation_result: { username: "chungachanga", result: true} } + json = JSON.parse(response.body, symbolize_names: true) + + assert_response :ok + + assert_equal json[:code], 2303 + assert_equal json[:message], "Object 'chungachanga' does not exist" + end +end From 17850b53d75a61a26ada4910a97d53d3723a3a8b Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 13 Aug 2021 09:58:44 +0300 Subject: [PATCH 08/15] added secret key --- .../repp/v1/registrar/accreditation_results_controller.rb | 4 ++-- .../repp/v1/registrar/accreditation_results_test.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index b0ed06c4c..32a3cd425 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -4,7 +4,7 @@ module Repp class AccreditationResultsController < ActionController::API before_action :authenticate_shared_key - TEMPARY_SECRET_KEY = 'tempary-secret-key'.freeze + TEMPORARY_SECRET_KEY = 'temporary-secret-key'.freeze api :POST, 'repp/v1/registrar/accreditation/push_results' desc 'added datetime results' @@ -35,7 +35,7 @@ module Repp end def authenticate_shared_key - api_key = "Basic #{TEMPARY_SECRET_KEY}" + api_key = "Basic #{TEMPORARY_SECRET_KEY}" render_failed unless api_key == request.authorization end diff --git a/test/integration/repp/v1/registrar/accreditation_results_test.rb b/test/integration/repp/v1/registrar/accreditation_results_test.rb index 8d2338ecf..11047f2c1 100644 --- a/test/integration/repp/v1/registrar/accreditation_results_test.rb +++ b/test/integration/repp/v1/registrar/accreditation_results_test.rb @@ -1,12 +1,12 @@ require 'test_helper' class ReppV1AccreditationResultsTest < ActionDispatch::IntegrationTest - TEMPARY_SECRET_KEY = 'tempary-secret-key'.freeze + TEMPORARY_SECRET_KEY = 'temporary-secret-key'.freeze def setup @user = users(:api_bestnames) - token = "Basic #{TEMPARY_SECRET_KEY}" + token = "Basic #{TEMPORARY_SECRET_KEY}" @auth_headers = { 'Authorization' => token } end @@ -25,7 +25,7 @@ class ReppV1AccreditationResultsTest < ActionDispatch::IntegrationTest def test_should_return_valid_response_invalid_authorization post '/repp/v1/registrar/accreditation/push_results', - headers: { 'Authorization' => 'Basic tempary-secret-ke'}, + headers: { 'Authorization' => 'Basic temporary-secret-ke'}, params: {accreditation_result: {username: @user.username, result: true} } json = JSON.parse(response.body, symbolize_names: true) From bccd3d51c16c17c354ff940331e150b4bb3c2a2d Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Thu, 19 Aug 2021 10:41:27 +0300 Subject: [PATCH 09/15] changes paths in routes --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index e69e5affe..e64c78ba5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,8 @@ require_dependency 'epp_constraint' require 'sidekiq/web' Rails.application.routes.draw do + get 'practice/index' + get 'practice/contact' # https://github.com/internetee/epp_proxy#translation-of-epp-calls namespace :epp do constraints(EppConstraint.new(:session)) do From a87fd6a8b735379d9b589f44575a28b727bdf9da Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Thu, 26 Aug 2021 18:22:19 +0300 Subject: [PATCH 10/15] added api for accr center --- .../accreditation_center/auth_controller.rb | 65 +++++++++++++++++++ .../accreditation_center/base_controller.rb | 37 +++++++++++ .../contacts_controller.rb | 20 ++++++ .../domains_controller.rb | 20 ++++++ config/application.yml.sample | 3 + config/routes.rb | 7 ++ .../api/accreditation_center/contacts_test.rb | 16 +++++ .../api/accreditation_center/domains_test.rb | 23 +++++++ 8 files changed, 191 insertions(+) create mode 100644 app/controllers/api/v1/accreditation_center/auth_controller.rb create mode 100644 app/controllers/api/v1/accreditation_center/base_controller.rb create mode 100644 app/controllers/api/v1/accreditation_center/contacts_controller.rb create mode 100644 app/controllers/api/v1/accreditation_center/domains_controller.rb create mode 100644 test/integration/api/accreditation_center/contacts_test.rb create mode 100644 test/integration/api/accreditation_center/domains_test.rb diff --git a/app/controllers/api/v1/accreditation_center/auth_controller.rb b/app/controllers/api/v1/accreditation_center/auth_controller.rb new file mode 100644 index 000000000..f89c4c931 --- /dev/null +++ b/app/controllers/api/v1/accreditation_center/auth_controller.rb @@ -0,0 +1,65 @@ +require 'serializers/repp/domain' + +module Api + module V1 + module AccreditationCenter + class AuthController < ::Api::V1::AccreditationCenter::BaseController + before_action :authenticate_user + + def index + login = @current_user + registrar = @current_user.registrar + + # rubocop:disable Style/AndOr + render_success(data: nil) and return unless login + # rubocop:enable Style/AndOr + + data = set_values_to_data(login: login, registrar: registrar) + + render_success(data: data) + end + + private + + def authenticate_user + username, password = Base64.urlsafe_decode64(basic_token).split(':') + @current_user ||= ApiUser.find_by(username: username, plain_text_password: password) + + return if @current_user + + raise(ArgumentError) + rescue NoMethodError, ArgumentError + @response = { code: 2202, message: 'Invalid authorization information' } + render(json: @response, status: :unauthorized) + end + + def basic_token + pattern = /^Basic / + header = request.headers['Authorization'] + header = header.gsub(pattern, '') if header&.match(pattern) + header.strip + end + + def set_values_to_data(login:, registrar:) + data = login.as_json(only: %i[id + username + name + uuid + roles + accreditation_date + accreditation_expire_date]) + data[:registrar_name] = registrar.name + data[:registrar_reg_no] = registrar.reg_no + data + end + + def render_success(code: nil, message: nil, data: nil) + @response = { code: code || 1000, message: message || 'Command completed successfully', + data: data || {} } + + render(json: @response, status: :ok) + end + end + end + end +end diff --git a/app/controllers/api/v1/accreditation_center/base_controller.rb b/app/controllers/api/v1/accreditation_center/base_controller.rb new file mode 100644 index 000000000..75e94919a --- /dev/null +++ b/app/controllers/api/v1/accreditation_center/base_controller.rb @@ -0,0 +1,37 @@ +require 'auth_token/auth_token_decryptor' + +module Api + module V1 + module AccreditationCenter + class BaseController < ActionController::API + before_action :check_ip_whitelist + + rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error + rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error + rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception| + error = {} + error[parameter_missing_exception.param] = ['parameter is required'] + response = { errors: [error] } + render json: response, status: :unprocessable_entity + end + + private + + def check_ip_whitelist + allowed_ips = ENV['accr_center_api_auth_allowed_ips'].to_s.split(',').map(&:strip) + return if allowed_ips.include?(request.ip) || Rails.env.development? + + render json: { errors: [{ base: ['Not authorized'] }] }, status: :unauthorized + end + + def show_not_found_error + render json: { errors: [{ base: ['Not found'] }] }, status: :not_found + end + + def show_invalid_record_error(exception) + render json: { errors: exception.record.errors }, status: :bad_request + end + end + end + end +end diff --git a/app/controllers/api/v1/accreditation_center/contacts_controller.rb b/app/controllers/api/v1/accreditation_center/contacts_controller.rb new file mode 100644 index 000000000..7463aef54 --- /dev/null +++ b/app/controllers/api/v1/accreditation_center/contacts_controller.rb @@ -0,0 +1,20 @@ +require 'serializers/repp/domain' + +module Api + module V1 + module AccreditationCenter + class ContactsController < ::Api::V1::AccreditationCenter::BaseController + def show + @contact = Contact.find_by(code: params[:id]) + + if @contact + render json: { contact: Serializers::Repp::Contact.new(@contact, + show_address: false).to_json }, status: :found + else + render json: { errors: 'Contact not found' }, status: :not_found + end + end + end + end + end +end diff --git a/app/controllers/api/v1/accreditation_center/domains_controller.rb b/app/controllers/api/v1/accreditation_center/domains_controller.rb new file mode 100644 index 000000000..d51420568 --- /dev/null +++ b/app/controllers/api/v1/accreditation_center/domains_controller.rb @@ -0,0 +1,20 @@ +require 'serializers/repp/domain' + +module Api + module V1 + module AccreditationCenter + class DomainsController < ::Api::V1::AccreditationCenter::BaseController + def show + @domain = Domain.find_by(name: params[:name]) + + if @domain + render json: { domain: Serializers::Repp::Domain.new(@domain, + sponsored: true).to_json }, status: :found + else + render json: { errors: 'Domain not found' }, status: :not_found + end + end + end + end + end +end diff --git a/config/application.yml.sample b/config/application.yml.sample index aa86325f1..8133382c9 100644 --- a/config/application.yml.sample +++ b/config/application.yml.sample @@ -90,6 +90,9 @@ sk_digi_doc_service_name: 'Testimine' registrant_api_base_url: registrant_api_auth_allowed_ips: '127.0.0.1, 0.0.0.0' #ips, separated with commas +# Accreditation Center API +accr_center_api_auth_allowed_ips: '127.0.0.1, 0.0.0.0' #ips, separated with commas + # Shared key for REST-WHOIS Bounces API incl. CERT rwhois_bounces_api_shared_key: testkey diff --git a/config/routes.rb b/config/routes.rb index e64c78ba5..19a19af27 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -126,6 +126,13 @@ Rails.application.routes.draw do resources :companies, only: %i[index] end + namespace :accreditation_center do + resource :domains, only: [ :show ], param: :name + resource :contacts, only: [ :show ], param: :id + # resource :auth, only: [ :index ] + get 'auth', to: 'auth#index' + end + resources :auctions, only: %i[index show update], param: :uuid resources :contact_requests, only: %i[create update], param: :id resources :bounces, only: %i[create] diff --git a/test/integration/api/accreditation_center/contacts_test.rb b/test/integration/api/accreditation_center/contacts_test.rb new file mode 100644 index 000000000..b3ca27e3f --- /dev/null +++ b/test/integration/api/accreditation_center/contacts_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' + +class ContactsTest < ApplicationIntegrationTest + def setup + super + + @contact = contacts(:john) + end + + def test_return_code_error_if_valid_domain_name + get "/api/v1/accreditation_center/contacts/?id=Alyosha" + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:errors], "Contact not found" + end +end \ No newline at end of file diff --git a/test/integration/api/accreditation_center/domains_test.rb b/test/integration/api/accreditation_center/domains_test.rb new file mode 100644 index 000000000..0e6b49ccd --- /dev/null +++ b/test/integration/api/accreditation_center/domains_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' + +class DomainsTest < ApplicationIntegrationTest + def setup + super + + @domain = domains(:shop) + end + + def test_get_domain_info + get "/api/v1/accreditation_center/domains/?name=#{@domain.name}" + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:domain][:name], @domain.name + end + + def test_return_code_error_if_valid_domain_name + get "/api/v1/accreditation_center/domains/?name=some.ee" + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:errors], "Domain not found" + end +end \ No newline at end of file From ece988fca1fc1afcf23da2a53703755f762288e9 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 27 Aug 2021 11:09:49 +0300 Subject: [PATCH 11/15] refactoring --- .../accreditation_center/auth_controller.rb | 84 +++++++++---------- .../accreditation_center/base_controller.rb | 2 +- .../contacts_controller.rb | 5 +- .../domains_controller.rb | 3 +- .../api/accreditation_center/auth_test.rb | 33 ++++++++ .../api/accreditation_center/contacts_test.rb | 21 +++-- 6 files changed, 95 insertions(+), 53 deletions(-) create mode 100644 test/integration/api/accreditation_center/auth_test.rb diff --git a/app/controllers/api/v1/accreditation_center/auth_controller.rb b/app/controllers/api/v1/accreditation_center/auth_controller.rb index f89c4c931..1b6f207dd 100644 --- a/app/controllers/api/v1/accreditation_center/auth_controller.rb +++ b/app/controllers/api/v1/accreditation_center/auth_controller.rb @@ -7,58 +7,58 @@ module Api before_action :authenticate_user def index - login = @current_user - registrar = @current_user.registrar + login = @current_user + registrar = @current_user.registrar - # rubocop:disable Style/AndOr - render_success(data: nil) and return unless login - # rubocop:enable Style/AndOr + # rubocop:disable Style/AndOr + render_success(data: nil) and return unless login + # rubocop:enable Style/AndOr - data = set_values_to_data(login: login, registrar: registrar) + data = set_values_to_data(login: login, registrar: registrar) - render_success(data: data) - end + render_success(data: data) + end - private + private - def authenticate_user - username, password = Base64.urlsafe_decode64(basic_token).split(':') - @current_user ||= ApiUser.find_by(username: username, plain_text_password: password) + def authenticate_user + username, password = Base64.urlsafe_decode64(basic_token).split(':') + @current_user ||= ApiUser.find_by(username: username, plain_text_password: password) - return if @current_user + return if @current_user - raise(ArgumentError) - rescue NoMethodError, ArgumentError - @response = { code: 2202, message: 'Invalid authorization information' } - render(json: @response, status: :unauthorized) - end + raise(ArgumentError) + rescue NoMethodError, ArgumentError + @response = { code: 2202, message: 'Invalid authorization information' } + render(json: @response, status: :unauthorized) + end - def basic_token - pattern = /^Basic / - header = request.headers['Authorization'] - header = header.gsub(pattern, '') if header&.match(pattern) - header.strip - end + def basic_token + pattern = /^Basic / + header = request.headers['Authorization'] + header = header.gsub(pattern, '') if header&.match(pattern) + header.strip + end - def set_values_to_data(login:, registrar:) - data = login.as_json(only: %i[id - username - name - uuid - roles - accreditation_date - accreditation_expire_date]) - data[:registrar_name] = registrar.name - data[:registrar_reg_no] = registrar.reg_no - data - end - - def render_success(code: nil, message: nil, data: nil) - @response = { code: code || 1000, message: message || 'Command completed successfully', - data: data || {} } + def set_values_to_data(login:, registrar:) + data = login.as_json(only: %i[id + username + name + uuid + roles + accreditation_date + accreditation_expire_date]) + data[:registrar_name] = registrar.name + data[:registrar_reg_no] = registrar.reg_no + data + end - render(json: @response, status: :ok) - end + def render_success(code: nil, message: nil, data: nil) + @response = { code: code || 1000, message: message || 'Command completed successfully', + data: data || {} } + + render(json: @response, status: :ok) + end end end end diff --git a/app/controllers/api/v1/accreditation_center/base_controller.rb b/app/controllers/api/v1/accreditation_center/base_controller.rb index 75e94919a..4a3cb1fa5 100644 --- a/app/controllers/api/v1/accreditation_center/base_controller.rb +++ b/app/controllers/api/v1/accreditation_center/base_controller.rb @@ -17,7 +17,7 @@ module Api private - def check_ip_whitelist + def check_ip_whitelist allowed_ips = ENV['accr_center_api_auth_allowed_ips'].to_s.split(',').map(&:strip) return if allowed_ips.include?(request.ip) || Rails.env.development? diff --git a/app/controllers/api/v1/accreditation_center/contacts_controller.rb b/app/controllers/api/v1/accreditation_center/contacts_controller.rb index 7463aef54..0ab8665e3 100644 --- a/app/controllers/api/v1/accreditation_center/contacts_controller.rb +++ b/app/controllers/api/v1/accreditation_center/contacts_controller.rb @@ -1,4 +1,4 @@ -require 'serializers/repp/domain' +require 'serializers/repp/contact' module Api module V1 @@ -9,7 +9,8 @@ module Api if @contact render json: { contact: Serializers::Repp::Contact.new(@contact, - show_address: false).to_json }, status: :found + show_address: false).to_json }, + status: :found else render json: { errors: 'Contact not found' }, status: :not_found end diff --git a/app/controllers/api/v1/accreditation_center/domains_controller.rb b/app/controllers/api/v1/accreditation_center/domains_controller.rb index d51420568..6c6a753df 100644 --- a/app/controllers/api/v1/accreditation_center/domains_controller.rb +++ b/app/controllers/api/v1/accreditation_center/domains_controller.rb @@ -9,7 +9,8 @@ module Api if @domain render json: { domain: Serializers::Repp::Domain.new(@domain, - sponsored: true).to_json }, status: :found + sponsored: true).to_json }, + status: :found else render json: { errors: 'Domain not found' }, status: :not_found end diff --git a/test/integration/api/accreditation_center/auth_test.rb b/test/integration/api/accreditation_center/auth_test.rb new file mode 100644 index 000000000..6626762e7 --- /dev/null +++ b/test/integration/api/accreditation_center/auth_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class AuthTest < ApplicationIntegrationTest + def setup + super + + @user = users(:api_bestnames) + @header = { 'Authorization' => "Basic #{generate_base64}" } + end + + def test_should_return_successful + get 'https://registry.test/api/v1/accreditation_center/auth', headers: @header + + json = JSON.parse(response.body, symbolize_names: true) + assert_equal json[:code], 1000 + assert_equal json[:message], 'Command completed successfully' + end + + def test_should_return_failed + get 'https://registry.test/api/v1/accreditation_center/auth', headers: { 'Authorization' => "Basic LAHSDHDSAFSF#@" } + + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:code], 2202 + assert_equal json[:message], 'Invalid authorization information' + end + + private + + def generate_base64 + Base64.encode64("#{@user.username}:#{@user.plain_text_password}") + end +end diff --git a/test/integration/api/accreditation_center/contacts_test.rb b/test/integration/api/accreditation_center/contacts_test.rb index b3ca27e3f..0770d663e 100644 --- a/test/integration/api/accreditation_center/contacts_test.rb +++ b/test/integration/api/accreditation_center/contacts_test.rb @@ -4,13 +4,20 @@ class ContactsTest < ApplicationIntegrationTest def setup super - @contact = contacts(:john) + @contact = contacts(:john) end - def test_return_code_error_if_valid_domain_name - get "/api/v1/accreditation_center/contacts/?id=Alyosha" - json = JSON.parse(response.body, symbolize_names: true) + def test_return_code_error_if_valid_domain_name + get '/api/v1/accreditation_center/contacts/?id=Alyosha' + json = JSON.parse(response.body, symbolize_names: true) - assert_equal json[:errors], "Contact not found" - end -end \ No newline at end of file + assert_equal json[:errors], 'Contact not found' + end + + def test_return_code_error_if_sdfsdf + get "/api/v1/accreditation_center/contacts/?id=#{@contact.code}" + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:contact][:name], 'John' + end +end From cc07350551a858f970ec36d196b638d843d31a4a Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 27 Aug 2021 14:12:16 +0300 Subject: [PATCH 12/15] updated api response --- .../api/v1/accreditation_center/contacts_controller.rb | 4 ++-- .../api/v1/accreditation_center/domains_controller.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/accreditation_center/contacts_controller.rb b/app/controllers/api/v1/accreditation_center/contacts_controller.rb index 0ab8665e3..c920c1ad2 100644 --- a/app/controllers/api/v1/accreditation_center/contacts_controller.rb +++ b/app/controllers/api/v1/accreditation_center/contacts_controller.rb @@ -8,8 +8,8 @@ module Api @contact = Contact.find_by(code: params[:id]) if @contact - render json: { contact: Serializers::Repp::Contact.new(@contact, - show_address: false).to_json }, + render json: { code: 1000, contact: Serializers::Repp::Contact.new(@contact, + show_address: false).to_json }, status: :found else render json: { errors: 'Contact not found' }, status: :not_found diff --git a/app/controllers/api/v1/accreditation_center/domains_controller.rb b/app/controllers/api/v1/accreditation_center/domains_controller.rb index 6c6a753df..80337bd28 100644 --- a/app/controllers/api/v1/accreditation_center/domains_controller.rb +++ b/app/controllers/api/v1/accreditation_center/domains_controller.rb @@ -8,8 +8,8 @@ module Api @domain = Domain.find_by(name: params[:name]) if @domain - render json: { domain: Serializers::Repp::Domain.new(@domain, - sponsored: true).to_json }, + render json: { code: 1000, domain: Serializers::Repp::Domain.new(@domain, + sponsored: true).to_json }, status: :found else render json: { errors: 'Domain not found' }, status: :not_found From 5a19149129a134ad37ad97d3854bfe1dee612ab8 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 27 Aug 2021 19:03:43 +0300 Subject: [PATCH 13/15] added ip to white list --- .../api/v1/accreditation_center/auth_controller.rb | 2 ++ .../api/v1/accreditation_center/base_controller.rb | 12 ++++++------ app/controllers/repp/v1/base_controller.rb | 5 +++++ app/controllers/repp/v1/domains_controller.rb | 4 ++-- app/interactions/actions/domain_create.rb | 2 +- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v1/accreditation_center/auth_controller.rb b/app/controllers/api/v1/accreditation_center/auth_controller.rb index 1b6f207dd..9c49d81cf 100644 --- a/app/controllers/api/v1/accreditation_center/auth_controller.rb +++ b/app/controllers/api/v1/accreditation_center/auth_controller.rb @@ -50,6 +50,8 @@ module Api accreditation_expire_date]) data[:registrar_name] = registrar.name data[:registrar_reg_no] = registrar.reg_no + data[:registrar_email] = registrar.email + data[:code] = registrar.code data end diff --git a/app/controllers/api/v1/accreditation_center/base_controller.rb b/app/controllers/api/v1/accreditation_center/base_controller.rb index 4a3cb1fa5..8bf153493 100644 --- a/app/controllers/api/v1/accreditation_center/base_controller.rb +++ b/app/controllers/api/v1/accreditation_center/base_controller.rb @@ -4,7 +4,7 @@ module Api module V1 module AccreditationCenter class BaseController < ActionController::API - before_action :check_ip_whitelist + # before_action :check_ip_whitelist rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error @@ -17,12 +17,12 @@ module Api private - def check_ip_whitelist - allowed_ips = ENV['accr_center_api_auth_allowed_ips'].to_s.split(',').map(&:strip) - return if allowed_ips.include?(request.ip) || Rails.env.development? + # def check_ip_whitelist + # allowed_ips = ENV['accr_center_api_auth_allowed_ips'].to_s.split(',').map(&:strip) + # return if allowed_ips.include?(request.ip) || Rails.env.development? || Rails.env.staging? - render json: { errors: [{ base: ['Not authorized'] }] }, status: :unauthorized - end + # render json: { errors: [{ base: ['Not authorized'] }] }, status: :unauthorized + # end def show_not_found_error render json: { errors: [{ base: ['Not found'] }] }, status: :not_found diff --git a/app/controllers/repp/v1/base_controller.rb b/app/controllers/repp/v1/base_controller.rb index c29f2137f..d46187816 100644 --- a/app/controllers/repp/v1/base_controller.rb +++ b/app/controllers/repp/v1/base_controller.rb @@ -116,10 +116,15 @@ module Repp def webclient_request? return if Rails.env.test? + header = request.headers['AccreditationToken'] + return if header == 'TEMPORARY_SECRET_KEY' + ENV['webclient_ips'].split(',').map(&:strip).include?(request.ip) end def validate_webclient_ca + + return unless webclient_request? request_name = request.env['HTTP_SSL_CLIENT_S_DN_CN'] diff --git a/app/controllers/repp/v1/domains_controller.rb b/app/controllers/repp/v1/domains_controller.rb index ea00a5561..06d4a0330 100644 --- a/app/controllers/repp/v1/domains_controller.rb +++ b/app/controllers/repp/v1/domains_controller.rb @@ -33,7 +33,7 @@ module Repp param :registrant, String, required: true, desc: 'Registrant contact code' param :reserved_pw, String, required: false, desc: 'Reserved password for domain' param :transfer_code, String, required: false, desc: 'Desired transfer code for domain' - param :period, Integer, required: true, desc: 'Registration period in months or years' + # param :period, String, required: true, desc: 'Registration period in months or years' param :period_unit, String, required: true, desc: 'Period type (month m) or (year y)' param :nameservers_attributes, Array, required: false, desc: 'Domain nameservers' do param :hostname, String, required: true, desc: 'Nameserver hostname' @@ -64,7 +64,7 @@ module Repp handle_errors(@domain) and return unless action.call # rubocop:enable Style/AndOr - render_success(data: { domain: { name: @domain.name } }) + render_success(data: { domain: { name: @domain.name, transfer_code: @domain.transfer_code } }) end api :PUT, '/repp/v1/domains/:domain_name' diff --git a/app/interactions/actions/domain_create.rb b/app/interactions/actions/domain_create.rb index 2e735bcce..8fd25df0f 100644 --- a/app/interactions/actions/domain_create.rb +++ b/app/interactions/actions/domain_create.rb @@ -106,7 +106,7 @@ module Actions end def assign_domain_period - domain.period = params[:period] + domain.period = params[:period].to_i domain.period_unit = params[:period_unit] end From 62ce5ff56136f10928eb40412b3a22c58133ef14 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 3 Sep 2021 12:15:44 +0300 Subject: [PATCH 14/15] added endpoint for get cancelled invoices --- .../invoice_status_controller.rb | 32 ++++++++++++++++++ config/routes.rb | 3 ++ .../api/accreditation_center/domains_test.rb | 6 ++-- .../invoice_status_test.rb | 33 +++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 app/controllers/api/v1/accreditation_center/invoice_status_controller.rb create mode 100644 test/integration/api/accreditation_center/invoice_status_test.rb diff --git a/app/controllers/api/v1/accreditation_center/invoice_status_controller.rb b/app/controllers/api/v1/accreditation_center/invoice_status_controller.rb new file mode 100644 index 000000000..62bf4c741 --- /dev/null +++ b/app/controllers/api/v1/accreditation_center/invoice_status_controller.rb @@ -0,0 +1,32 @@ +module Api + module V1 + module AccreditationCenter + class InvoiceStatusController < ::Api::V1::AccreditationCenter::BaseController + def index + username, password = Base64.urlsafe_decode64(basic_token).split(':') + @current_user ||= ApiUser.find_by(username: username, plain_text_password: password) + + return render json: { errors: 'No user found' }, status: :not_found if @current_user.nil? + + @invoices = @current_user.registrar.invoices.select { |i| i.cancelled_at != nil } + + if @invoices + render json: { code: 1000, invoices: @invoices }, + status: :found + else + render json: { errors: 'No invoices' }, status: :not_found + end + end + + private + + def basic_token + pattern = /^Basic / + header = request.headers['Authorization'] + header = header.gsub(pattern, '') if header&.match(pattern) + header.strip + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 19a19af27..4772fbe4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -127,6 +127,9 @@ Rails.application.routes.draw do end namespace :accreditation_center do + # At the moment invoice_status endpoint returns only cancelled invoices. But in future logic of this enpoint can change. + # And it will need to return invoices of different statuses. I decided to leave the name of the endpoint "invoice_status" + resources :invoice_status, only: [ :index ] resource :domains, only: [ :show ], param: :name resource :contacts, only: [ :show ], param: :id # resource :auth, only: [ :index ] diff --git a/test/integration/api/accreditation_center/domains_test.rb b/test/integration/api/accreditation_center/domains_test.rb index 0e6b49ccd..1f571ccdc 100644 --- a/test/integration/api/accreditation_center/domains_test.rb +++ b/test/integration/api/accreditation_center/domains_test.rb @@ -2,16 +2,14 @@ require 'test_helper' class DomainsTest < ApplicationIntegrationTest def setup - super - @domain = domains(:shop) end def test_get_domain_info - get "/api/v1/accreditation_center/domains/?name=#{@domain.name}" + get "/api/v1/accreditation_center/domains/?name=shop.test" json = JSON.parse(response.body, symbolize_names: true) - assert_equal json[:domain][:name], @domain.name + assert_equal json[:domain][:name], "shop.test" end def test_return_code_error_if_valid_domain_name diff --git a/test/integration/api/accreditation_center/invoice_status_test.rb b/test/integration/api/accreditation_center/invoice_status_test.rb new file mode 100644 index 000000000..d97a3b825 --- /dev/null +++ b/test/integration/api/accreditation_center/invoice_status_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class DomainsTest < ApplicationIntegrationTest + def setup + super + + @user = users(:api_bestnames) + @header = { 'Authorization' => "Basic #{generate_base64}" } + end + + def test_should_return_cancelled_invoices + date_now = Time.now + + get "/api/v1/accreditation_center/invoice_status", headers: @header + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:invoices].count, 0 + + invoice = @user.registrar.invoices.last + invoice.update(cancelled_at: date_now) + + get "/api/v1/accreditation_center/invoice_status", headers: @header + json = JSON.parse(response.body, symbolize_names: true) + + assert_equal json[:invoices].count, 1 + end + + private + + def generate_base64 + Base64.encode64("#{@user.username}:#{@user.plain_text_password}") + end +end \ No newline at end of file From 11b7a13cf3a69123b4b302c85743b2749e2aa80b Mon Sep 17 00:00:00 2001 From: olegphenomenon Date: Thu, 9 Sep 2021 11:07:23 +0300 Subject: [PATCH 15/15] change key token --- .../api/v1/accreditation_center/base_controller.rb | 9 --------- app/controllers/repp/v1/base_controller.rb | 8 +++++--- .../v1/registrar/accreditation_results_controller.rb | 2 +- .../repp/v1/registrar/accreditation_results_test.rb | 2 +- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/v1/accreditation_center/base_controller.rb b/app/controllers/api/v1/accreditation_center/base_controller.rb index 8bf153493..7deb776b9 100644 --- a/app/controllers/api/v1/accreditation_center/base_controller.rb +++ b/app/controllers/api/v1/accreditation_center/base_controller.rb @@ -4,8 +4,6 @@ module Api module V1 module AccreditationCenter class BaseController < ActionController::API - # before_action :check_ip_whitelist - rescue_from ActiveRecord::RecordNotFound, with: :show_not_found_error rescue_from ActiveRecord::RecordInvalid, with: :show_invalid_record_error rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception| @@ -17,13 +15,6 @@ module Api private - # def check_ip_whitelist - # allowed_ips = ENV['accr_center_api_auth_allowed_ips'].to_s.split(',').map(&:strip) - # return if allowed_ips.include?(request.ip) || Rails.env.development? || Rails.env.staging? - - # render json: { errors: [{ base: ['Not authorized'] }] }, status: :unauthorized - # end - def show_not_found_error render json: { errors: [{ base: ['Not found'] }] }, status: :not_found end diff --git a/app/controllers/repp/v1/base_controller.rb b/app/controllers/repp/v1/base_controller.rb index d46187816..f23d0a24f 100644 --- a/app/controllers/repp/v1/base_controller.rb +++ b/app/controllers/repp/v1/base_controller.rb @@ -117,14 +117,12 @@ module Repp return if Rails.env.test? header = request.headers['AccreditationToken'] - return if header == 'TEMPORARY_SECRET_KEY' + return if header == ENV['accreditation_secret'] ENV['webclient_ips'].split(',').map(&:strip).include?(request.ip) end def validate_webclient_ca - - return unless webclient_request? request_name = request.env['HTTP_SSL_CLIENT_S_DN_CN'] @@ -136,6 +134,10 @@ module Repp render(json: @response, status: :unauthorized) end + + def logger + Rails.logger + end end end end diff --git a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb index 32a3cd425..b33ed58ee 100644 --- a/app/controllers/repp/v1/registrar/accreditation_results_controller.rb +++ b/app/controllers/repp/v1/registrar/accreditation_results_controller.rb @@ -4,7 +4,7 @@ module Repp class AccreditationResultsController < ActionController::API before_action :authenticate_shared_key - TEMPORARY_SECRET_KEY = 'temporary-secret-key'.freeze + TEMPORARY_SECRET_KEY = ENV['accreditation_secret'].freeze api :POST, 'repp/v1/registrar/accreditation/push_results' desc 'added datetime results' diff --git a/test/integration/repp/v1/registrar/accreditation_results_test.rb b/test/integration/repp/v1/registrar/accreditation_results_test.rb index 11047f2c1..ac9d4fa4a 100644 --- a/test/integration/repp/v1/registrar/accreditation_results_test.rb +++ b/test/integration/repp/v1/registrar/accreditation_results_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class ReppV1AccreditationResultsTest < ActionDispatch::IntegrationTest - TEMPORARY_SECRET_KEY = 'temporary-secret-key'.freeze + TEMPORARY_SECRET_KEY = ENV['accreditation_secret'].freeze def setup @user = users(:api_bestnames)