From 8f234a5852ffb29dd702a1f5fda70a9b48f9d96f Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Mon, 23 Jul 2018 16:23:56 +0300 Subject: [PATCH] Create base controller class --- .../api/v1/registrant/base_controller.rb | 31 +++++++++++++++++++ .../api/v1/registrant/domains_controller.rb | 23 +------------- lib/auth_token/auth_token_decryptor.rb | 2 +- .../auth_token/auth_token_decryptor_test.rb | 5 +++ .../registrant/registrant_api_domains_test.rb | 29 +++++++++++++++++ 5 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 app/controllers/api/v1/registrant/base_controller.rb create mode 100644 test/system/api/registrant/registrant_api_domains_test.rb diff --git a/app/controllers/api/v1/registrant/base_controller.rb b/app/controllers/api/v1/registrant/base_controller.rb new file mode 100644 index 000000000..5b01f94b5 --- /dev/null +++ b/app/controllers/api/v1/registrant/base_controller.rb @@ -0,0 +1,31 @@ +require 'rails5_api_controller_backport' +require 'auth_token/auth_token_decryptor' + +module Api + module V1 + module Registrant + class BaseController < ActionController::API + before_action :authenticate + + private + + def bearer_token + pattern = /^Bearer / + header = request.headers['Authorization'] + header.gsub(pattern, '') if header && header.match(pattern) + end + + def authenticate + decryptor = AuthTokenDecryptor.create_with_defaults(bearer_token) + decryptor.decrypt_token + + if decryptor.valid? + sign_in decryptor.user + else + render json: { error: 'Not authorized' }, status: 403 + end + end + end + end + end +end diff --git a/app/controllers/api/v1/registrant/domains_controller.rb b/app/controllers/api/v1/registrant/domains_controller.rb index 744692d80..cc53e6772 100644 --- a/app/controllers/api/v1/registrant/domains_controller.rb +++ b/app/controllers/api/v1/registrant/domains_controller.rb @@ -4,9 +4,7 @@ require 'auth_token/auth_token_decryptor' module Api module V1 module Registrant - class DomainsController < ActionController::API - before_filter :authenticate - + class DomainsController < BaseController def index registrant = ::Registrant.find_by(ident: current_user.registrant_ident) unless registrant @@ -16,25 +14,6 @@ module Api render json: domains end end - - private - - def bearer_token - pattern = /^Bearer / - header = request.headers['Authorization'] - header.gsub(pattern, '') if header && header.match(pattern) - end - - def authenticate - decryptor = AuthTokenDecryptor.create_with_defaults(bearer_token) - decryptor.decrypt_token - - if decryptor.valid? - sign_in decryptor.user - else - render json: { error: "Not authorized" }, status: 403 - end - end end end end diff --git a/lib/auth_token/auth_token_decryptor.rb b/lib/auth_token/auth_token_decryptor.rb index 2af4be0a9..61146aa4d 100644 --- a/lib/auth_token/auth_token_decryptor.rb +++ b/lib/auth_token/auth_token_decryptor.rb @@ -18,7 +18,7 @@ class AuthTokenDecryptor decipher.decrypt decipher.key = key - base64_decoded = Base64.urlsafe_decode64(token) + base64_decoded = Base64.urlsafe_decode64(token.to_s) plain = decipher.update(base64_decoded) + decipher.final @decrypted_data = JSON.parse(plain, symbolize_names: true) diff --git a/test/lib/auth_token/auth_token_decryptor_test.rb b/test/lib/auth_token/auth_token_decryptor_test.rb index fbb18d6d3..49ca2b820 100644 --- a/test/lib/auth_token/auth_token_decryptor_test.rb +++ b/test/lib/auth_token/auth_token_decryptor_test.rb @@ -35,6 +35,11 @@ class AuthTokenDecryptorTest < ActiveSupport::TestCase refute(faulty_decryptor.decrypt_token) end + def test_decrypt_token_return_false_when_token_is_nil + faulty_decryptor = AuthTokenDecryptor.new(nil, @key) + refute(faulty_decryptor.decrypt_token) + end + def test_valid_returns_true_for_valid_token decryptor = AuthTokenDecryptor.new(@access_token, @key) decryptor.decrypt_token diff --git a/test/system/api/registrant/registrant_api_domains_test.rb b/test/system/api/registrant/registrant_api_domains_test.rb new file mode 100644 index 000000000..e7abe2cae --- /dev/null +++ b/test/system/api/registrant/registrant_api_domains_test.rb @@ -0,0 +1,29 @@ +require 'test_helper' +require 'auth_token/auth_token_creator' + +class RegistrantApiDomainsTest < ApplicationSystemTestCase + def setup + super + + @user = users(:registrant) + @auth_headers = { 'HTTP_AUTHORIZATION' => auth_token } + end + + def test_root_returns_domain_list + get '/api/v1/registrant/domains', {}, @auth_headers + assert_equal(200, response.status) + end + + def test_root_returns_403_without_authorization + get '/api/v1/registrant/domains', {}, {} + assert_equal(403, response.status) + end + + private + + def auth_token + token_creator = AuthTokenCreator.create_with_defaults(@user) + hash = token_creator.token_in_hash + "Bearer #{hash[:access_token]}" + end +end