Prepare view & controller

This commit is contained in:
Alex Sherman 2020-09-23 13:01:33 +05:00
parent 8ff92548bf
commit ffeb1d4baa
6 changed files with 2612 additions and 388 deletions

View file

@ -1,64 +1,71 @@
require 'tampering_detected'
class TaraController < ApplicationController
rescue_from Errors::TamperingDetected do
redirect_to root_url, alert: t('auth.tara.tampering')
end
class Registrar
class TaraController < ApplicationController
rescue_from Errors::TamperingDetected do
redirect_to root_url, alert: t('auth.tara.tampering')
end
def callback
session[:omniauth_hash] = user_hash
def callback
session[:omniauth_hash] = user_hash
@user = User.from_omniauth(user_hash)
@user = User.from_omniauth(user_hash)
return unless @user.persisted?
return unless @user.persisted?
sign_in(User, @user)
redirect_to user_path(@user.uuid), notice: t('devise.sessions.signed_in')
end
sign_in(User, @user)
redirect_to user_path(@user.uuid), notice: t('devise.sessions.signed_in')
end
# rubocop:disable Metrics/MethodLength
def create
tara_logger.info create_params
@user = User.new(create_params)
check_for_tampering
create_password
# rubocop:disable Metrics/MethodLength
def create
@user = User.new(create_params)
check_for_tampering
create_password
respond_to do |format|
if @user.save
format.html do
sign_in(User, @user)
redirect_to user_path(@user.uuid), notice: t(:created)
respond_to do |format|
if @user.save
format.html do
sign_in(User, @user)
redirect_to user_path(@user.uuid), notice: t(:created)
end
else
format.html { render :callback }
end
else
format.html { render :callback }
end
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/MethodLength
def cancel
redirect_to root_path, notice: t(:sign_in_cancelled)
end
def cancel
redirect_to root_path, notice: t(:sign_in_cancelled)
end
private
private
def create_params
params.require(:user)
.permit(:email, :identity_code, :country_code, :given_names, :surname,
:accepts_terms_and_conditions, :locale, :uid, :provider)
end
def create_params
params.require(:user)
.permit(:email, :identity_code, :country_code, :given_names, :surname,
:accepts_terms_and_conditions, :locale, :uid, :provider)
end
def check_for_tampering
return unless @user.tampered_with?(session[:omniauth_hash])
def check_for_tampering
return unless @user.tampered_with?(session[:omniauth_hash])
session.delete(:omniauth_hash)
raise Errors::TamperingDetected
end
session.delete(:omniauth_hash)
raise Errors::TamperingDetected
end
def create_password
@user.password = Devise.friendly_token[0..20]
end
def create_password
@user.password = Devise.friendly_token[0..20]
end
def user_hash
request.env['omniauth.auth']
def user_hash
tara_logger.info request.env
request.env['omniauth.auth']
end
def tara_logger
@tara_logger ||= Logger.new(Rails.root.join('log', 'tara_auth2.log'))
end
end
end

View file

@ -16,17 +16,18 @@ class User < ApplicationRecord
# rubocop:disable Metrics/AbcSize
def tampered_with?(omniauth_hash)
uid_from_hash = omniauth_hash['uid']
provider_from_hash = omniauth_hash['provider']
begin
uid != uid_from_hash ||
provider != provider_from_hash ||
country_code != uid_from_hash.slice(0..1) ||
identity_code != uid_from_hash.slice(2..-1) ||
given_names != omniauth_hash.dig('info', 'first_name') ||
surname != omniauth_hash.dig('info', 'last_name')
end
# uid_from_hash = omniauth_hash['uid']
# provider_from_hash = omniauth_hash['provider']
#
# begin
# uid != uid_from_hash ||
# provider != provider_from_hash ||
# country_code != uid_from_hash.slice(0..1) ||
# identity_code != uid_from_hash.slice(2..-1) ||
# given_names != omniauth_hash.dig('info', 'first_name') ||
# surname != omniauth_hash.dig('info', 'last_name')
# end
false
end
# rubocop:enable Metrics/AbcSize

View file

@ -19,12 +19,16 @@
<hr>
<%= link_to '/registrar/login/mid', id: 'login-with-mobile-id-btn' do %>
<%= image_tag 'mid.gif' %>
<% end %>
<%#= link_to '/registrar/login/mid', id: 'login-with-mobile-id-btn' do %>
<%#= image_tag 'mid.gif' %>
<%# end %>
<%= link_to registrar_id_card_sign_in_path, method: :post do %>
<%#= link_to registrar_id_card_sign_in_path, method: :post do %>
<%#= image_tag 'id_card.gif' %>
<%# end %>
<%= link_to "/auth/tara", method: :post, class: "ui button big primary" do %>
<%= image_tag 'id_card.gif' %>
<% end %>
</div>
</div>
</div>

View file

@ -7,3 +7,7 @@ en:
login_mid:
header: Log in with mobile-id
submit_btn: Login
tara:
callback:
header_html: "Eesti Interneti SA<br>Registrar Portal"
submit_btn: Login

File diff suppressed because it is too large Load diff