Skip to content

Commit 431fc9c

Browse files
added eeid auth
1 parent dd027b5 commit 431fc9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+473
-283
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ gem 'net-smtp', require: false
3939
gem 'net-imap', require: false
4040
gem 'net-pop', require: false
4141
gem "apipie-rails", "~> 1.2.0"
42+
gem 'omniauth', '>=2.0.0'
43+
gem 'omniauth-rails_csrf_protection'
44+
gem 'omniauth-tara', github: 'internetee/omniauth-tara'
4245

4346
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
4447
# gem 'rack-cors'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# rubocop:disable Metrics
2+
3+
module Auth
4+
class TaraController < ParentController
5+
allow_unauthenticated
6+
7+
def callback
8+
expires_now
9+
10+
unless in_white_list?
11+
flash[:alert] = I18n.t('.access_denied')
12+
redirect_to sign_in_path, status: :see_other and return
13+
end
14+
15+
session[:omniauth_hash] = user_hash.delete_if { |key, _| key == 'credentials' }
16+
@user = User.from_omniauth(user_hash)
17+
@user.save! && @user.reload
18+
19+
@app_session = create_app_session
20+
21+
if @app_session
22+
log_in @app_session
23+
set_current_session
24+
25+
redirect_to root_path, status: :see_other
26+
else
27+
flash[:alert] = I18n.t('.incorrect_details')
28+
render 'dashboard/index', status: :unprocessable_entity
29+
end
30+
end
31+
32+
private
33+
34+
def in_white_list?
35+
WhiteCode.find_by(code: user_hash['uid'].slice(2..-1)).present?
36+
end
37+
38+
def set_current_session
39+
Current.user = @user
40+
flash[:notice] = I18n.t('.success')
41+
end
42+
43+
def user_hash
44+
request.env['omniauth.auth']
45+
end
46+
47+
def create_app_session
48+
@user.app_sessions.create
49+
end
50+
end
51+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module Authenticate
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
before_action :authenticate
6+
before_action :need_to_login, unless: :logged_in?
7+
8+
helper_method :logged_in?
9+
helper_method :current_user
10+
end
11+
12+
class_methods do
13+
def skip_authentication(**options)
14+
skip_before_action :authenticate, options
15+
skip_before_action :need_to_login, options
16+
end
17+
18+
def allow_unauthenticated(**options)
19+
skip_before_action :need_to_login, options
20+
end
21+
end
22+
23+
protected
24+
25+
def log_in(app_session, remember_me: false)
26+
if remember_me
27+
cookies.encrypted.permanent[:app_session] = {
28+
value: app_session.to_h
29+
}
30+
else
31+
cookies.signed[:app_session] = {
32+
value: app_session.to_h,
33+
expires: 1.day
34+
}
35+
end
36+
end
37+
38+
def logout
39+
Current&.app_session&.destroy
40+
end
41+
42+
def logged_in?
43+
Current.user.present?
44+
end
45+
46+
def current_user
47+
Current.user
48+
end
49+
50+
private
51+
52+
def need_to_login
53+
flash[:notice] = t('login_required')
54+
render 'sessions/new', status: :unauthorized
55+
end
56+
57+
def authenticate
58+
cookie = cookies.encrypted[:app_session]&.with_indifferent_access
59+
cookie = cookies.signed[:app_session]&.with_indifferent_access if cookie.nil?
60+
61+
return nil if cookie.nil?
62+
63+
user = User.find(cookie[:user_id])
64+
app_session = user&.authenticate_session_token(cookie[:app_session], cookie[:token])
65+
66+
Current.user = app_session&.user
67+
Current.app_session = app_session
68+
rescue NoMatchingPatternError, ActiveRecord::RecordNotFound
69+
Current.user = nil
70+
Current.app_session = nil
71+
end
72+
end

app/controllers/dashboard_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class DashboardController < ParentController
2-
before_action :require_user_logged_in!
3-
42
def index
53
@pagy, @invoices = pagy(Invoice.search(params),
64
items: params[:per_page] ||= 25,

app/controllers/dashboards/invoice_status_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class Dashboards::InvoiceStatusController < ParentController
2-
before_action :require_user_logged_in!
3-
42
def update
53
@invoice = Invoice.find(params[:id])
64
temporary_unavailable and return unless @invoice.registry?

app/controllers/everypay_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class EverypayController < ParentController
2-
before_action :require_user_logged_in!
3-
42
def index; end
53

64
def everypay_data

app/controllers/invoice_details/descriptions_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class InvoiceDetails::DescriptionsController < ParentController
2-
before_action :require_user_logged_in!
3-
42
def show
53
@description = Invoice.find(params[:id])&.description
64
end

app/controllers/invoice_details/directo_controller.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
class InvoiceDetails::DirectoController < ParentController
22
require 'rexml/document'
3-
4-
before_action :require_user_logged_in!
5-
63
def show
74
directo = Invoice.find(params[:id])
85

app/controllers/invoice_details/everypay_response_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class InvoiceDetails::EverypayResponseController < ParentController
2-
before_action :require_user_logged_in!
3-
42
def show
53
everypay = Invoice.find(params[:id])
64
@everypay = everypay.everypay_response

app/controllers/invoice_details/payment_references_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class InvoiceDetails::PaymentReferencesController < ParentController
2-
before_action :require_user_logged_in!
3-
42
def show
53
@payment_reference = Invoice.find(params[:id])&.payment_reference
64
end

0 commit comments

Comments
 (0)