mirror of
https://github.com/internetee/registry.git
synced 2025-06-08 05:34:46 +02:00
Add auth-token class
This commit is contained in:
parent
d67e777ea8
commit
1c6b838b2b
5 changed files with 65 additions and 6 deletions
|
@ -1,4 +1,5 @@
|
||||||
require 'rails5_api_controller_backport'
|
require 'rails5_api_controller_backport'
|
||||||
|
require 'auth_token'
|
||||||
|
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
|
@ -7,11 +8,24 @@ module Api
|
||||||
def eid
|
def eid
|
||||||
login_params = set_eid_params
|
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
|
end
|
||||||
|
|
||||||
def set_eid_params
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -52,6 +52,16 @@ class RegistrantUser < User
|
||||||
u
|
u
|
||||||
end
|
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)
|
def find_or_create_by_mid_data(response)
|
||||||
u = where(registrant_ident: "#{response.user_country}-#{response.user_id_code}").first_or_create
|
u = where(registrant_ident: "#{response.user_country}-#{response.user_id_code}").first_or_create
|
||||||
u.username = "#{response.user_givenname} #{response.user_surname}"
|
u.username = "#{response.user_givenname} #{response.user_surname}"
|
||||||
|
|
26
lib/auth_token.rb
Normal file
26
lib/auth_token.rb
Normal file
|
@ -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
|
|
@ -4,6 +4,8 @@ class RegistrantApiAuthenticationTest < ApplicationSystemTestCase
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
|
|
||||||
|
@user_hash = {ident: "37010100049", first_name: 'Adam', last_name: 'Baker'}
|
||||||
|
@existing_user = RegistrantUser.find_or_create_by_api_data(@user_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
|
@ -14,11 +16,18 @@ class RegistrantApiAuthenticationTest < ApplicationSystemTestCase
|
||||||
def test_request_creates_user_when_one_does_not_exist
|
def test_request_creates_user_when_one_does_not_exist
|
||||||
params = {
|
params = {
|
||||||
ident: "30110100103",
|
ident: "30110100103",
|
||||||
first_name: "Jan",
|
first_name: "John",
|
||||||
last_name: "Tamm",
|
last_name: "Smith",
|
||||||
country: "ee",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
post '/api/v1/registrant/auth/eid', params
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,7 +11,7 @@ require 'minitest/mock'
|
||||||
require 'capybara/rails'
|
require 'capybara/rails'
|
||||||
require 'capybara/minitest'
|
require 'capybara/minitest'
|
||||||
require 'webmock/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'
|
require 'application_system_test_case'
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue