From 1c6b838b2bc5c6caa71068e63ef600a5d1836a12 Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Thu, 19 Jul 2018 10:31:31 +0300 Subject: [PATCH] Add auth-token class --- .../api/v1/registrant/auth_controller.rb | 18 +++++++++++-- app/models/registrant_user.rb | 10 +++++++ lib/auth_token.rb | 26 +++++++++++++++++++ .../registrant_api_authentication_test.rb | 15 ++++++++--- test/test_helper.rb | 2 +- 5 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 lib/auth_token.rb diff --git a/app/controllers/api/v1/registrant/auth_controller.rb b/app/controllers/api/v1/registrant/auth_controller.rb index c137a1286..36bf750a8 100644 --- a/app/controllers/api/v1/registrant/auth_controller.rb +++ b/app/controllers/api/v1/registrant/auth_controller.rb @@ -1,4 +1,5 @@ require 'rails5_api_controller_backport' +require 'auth_token' module Api module V1 @@ -7,11 +8,24 @@ module Api def eid login_params = set_eid_params - render json: login_params + user = RegistrantUser.find_or_create_by_api_data(login_params) + + unless user.valid? + render json: user.errors, status: :bad_request + else + token = create_token(user) + render json: token + end end def set_eid_params - params.permit(:ident, :first_name, :last_name, :country) + params.permit(:ident, :first_name, :last_name) + end + + def create_token(user) + token = AuthToken.new + hash = token.generate_token(user) + hash end end end diff --git a/app/models/registrant_user.rb b/app/models/registrant_user.rb index 12cae0d82..8f742a361 100644 --- a/app/models/registrant_user.rb +++ b/app/models/registrant_user.rb @@ -52,6 +52,16 @@ class RegistrantUser < User u end + def find_or_create_by_api_data(api_data = {}) + estonian_ident = "EE-#{api_data[:ident]}" + + user = find_or_create_by(registrant_ident: estonian_ident) + user.username = "#{api_data[:first_name]}, #{api_data[:last_name]}" + user.save + + user + end + def find_or_create_by_mid_data(response) u = where(registrant_ident: "#{response.user_country}-#{response.user_id_code}").first_or_create u.username = "#{response.user_givenname} #{response.user_surname}" diff --git a/lib/auth_token.rb b/lib/auth_token.rb new file mode 100644 index 000000000..5313d603d --- /dev/null +++ b/lib/auth_token.rb @@ -0,0 +1,26 @@ +class AuthToken + def initialize; end + + def generate_token(user, secret = Rails.application.config.secret_key_base) + cipher = OpenSSL::Cipher::AES.new(256, :CBC) + expires_at = (Time.now.utc + 2.hours).strftime("%F %T %Z") + + data = { + username: user.username, + expires_at: expires_at + } + + hashable = data.to_json + + cipher.encrypt + cipher.key = secret + encrypted = cipher.update(hashable) + cipher.final + base64_encoded = Base64.encode64(encrypted) + + { + access_token: base64_encoded, + expires_at: expires_at, + type: "Bearer" + } + end +end diff --git a/test/system/api/registrant/registrant_api_authentication_test.rb b/test/system/api/registrant/registrant_api_authentication_test.rb index 5ecd7e08a..6789b3d5d 100644 --- a/test/system/api/registrant/registrant_api_authentication_test.rb +++ b/test/system/api/registrant/registrant_api_authentication_test.rb @@ -4,6 +4,8 @@ class RegistrantApiAuthenticationTest < ApplicationSystemTestCase def setup super + @user_hash = {ident: "37010100049", first_name: 'Adam', last_name: 'Baker'} + @existing_user = RegistrantUser.find_or_create_by_api_data(@user_hash) end def teardown @@ -14,11 +16,18 @@ class RegistrantApiAuthenticationTest < ApplicationSystemTestCase def test_request_creates_user_when_one_does_not_exist params = { ident: "30110100103", - first_name: "Jan", - last_name: "Tamm", - country: "ee", + first_name: "John", + last_name: "Smith", } post '/api/v1/registrant/auth/eid', params + assert(User.find_by(registrant_ident: 'EE-30110100103')) + + json = JSON.parse(response.body, symbolize_names: true) + assert_equal([:access_token, :expires_at, :type], json.keys) + end + + def test_request_returns_existing_user + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 500861f75..56a4a7aeb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,7 +11,7 @@ require 'minitest/mock' require 'capybara/rails' require 'capybara/minitest' require 'webmock/minitest' -require 'support/rails5_assetions' # Remove once upgraded to Rails 5 +require 'support/rails5_assertions' # Remove once upgraded to Rails 5 require 'application_system_test_case'