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