-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from internetee/encrypt-aith-info
Encrypted auth info cache
- Loading branch information
Showing
7 changed files
with
173 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# app/controllers/concerns/authentication.rb | ||
module Authentication | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
helper_method :current_user, :logged_in? | ||
end | ||
|
||
def current_user | ||
@current_user ||= OpenStruct.new(auth_info) if auth_info | ||
end | ||
|
||
def logged_in? | ||
current_user != nil | ||
end | ||
|
||
def sign_out | ||
session[:uuid] = nil | ||
clear_cache | ||
end | ||
|
||
def sign_in(uuid) | ||
session[:uuid] = uuid | ||
cookies.delete(:request_ip) | ||
end | ||
|
||
def store_auth_info(token:, request_ip:, data:) | ||
uuid = SecureRandom.uuid | ||
data = construct_auth_info(token, request_ip, data) | ||
encrypted_data = Encryptor.encrypt(data.to_json) | ||
Rails.cache.write(uuid, encrypted_data, expires_in: 18.hours) | ||
|
||
uuid | ||
end | ||
|
||
private | ||
|
||
def auth_info | ||
cached_data = Rails.cache.fetch(session[:uuid]) || '' | ||
decrypted_data = Encryptor.decrypt(cached_data) | ||
return unless decrypted_data | ||
|
||
JSON.parse(decrypted_data).symbolize_keys | ||
rescue JSON::ParserError => e | ||
logger.info(e) | ||
nil | ||
end | ||
|
||
def construct_auth_info(token, request_ip, data) | ||
{ | ||
username: data[:username], | ||
registrar_name: data[:registrar_name], | ||
role: data[:roles].first, | ||
legaldoc_mandatory: data[:legaldoc_mandatory], | ||
address_processing: data[:address_processing], | ||
token: token, | ||
request_ip: request_ip, | ||
abilities: data[:abilities] | ||
} | ||
end | ||
|
||
def clear_cache | ||
Rails.cache.instance_variable_get(:@data)&.each_key do |key| | ||
Rails.cache.delete(key) unless key.match?(/distribution_data|growth_rate_data/) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# app/controllers/concerns/authorization.rb | ||
module Authorization | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
helper_method :can? | ||
end | ||
|
||
def can?(action, subject) | ||
abilities = current_user.abilities.with_indifferent_access | ||
return false if abilities[:can].blank? | ||
return false if abilities[:can][action].blank? | ||
|
||
abilities[:can][action].keys.include? subject | ||
end | ||
|
||
def authorize!(action, subject) | ||
return if can? action, subject | ||
|
||
respond_to do |format| | ||
format.html { redirect_to dashboard_url, alert: 'Authorization error' } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# app/controllers/concerns/localization.rb | ||
module Localization | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :switch_locale | ||
end | ||
|
||
private | ||
|
||
def switch_locale | ||
I18n.locale = extract_locale || I18n.default_locale | ||
@pagy_locale = I18n.locale.to_s | ||
end | ||
|
||
def extract_locale | ||
set_locale_cookie_if_present | ||
locale = cookies[:locale] | ||
|
||
return locale.to_sym if valid_locale?(locale) | ||
|
||
log_invalid_locale(locale) | ||
nil | ||
end | ||
|
||
def set_locale_cookie_if_present | ||
cookies.permanent[:locale] = params[:locale] if params[:locale].present? | ||
end | ||
|
||
def valid_locale?(locale) | ||
I18n.available_locales.map(&:to_s).include?(locale) | ||
end | ||
|
||
def log_invalid_locale(locale) | ||
notice = "#{locale} #{t(:no_translation)}" | ||
# flash.now[:notice] = notice | ||
logger.error notice | ||
end | ||
|
||
def default_url_options | ||
{ locale: I18n.locale } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# lib/encryptor.rb | ||
class Encryptor | ||
class << self | ||
def encrypt(value) | ||
encryptor.encrypt_and_sign(value) | ||
end | ||
|
||
def decrypt(value) | ||
encryptor.decrypt_and_verify(value) | ||
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveSupport::MessageEncryptor::InvalidMessage | ||
nil | ||
end | ||
|
||
private | ||
|
||
def encryptor | ||
@encryptor ||= begin | ||
secret_key_base = Rails.application.secret_key_base | ||
key = ActiveSupport::KeyGenerator.new(secret_key_base).generate_key('', 32) | ||
ActiveSupport::MessageEncryptor.new(key) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters