Class: Kadmin::AuthController

Inherits:
ApplicationController show all
Defined in:
app/controllers/kadmin/auth_controller.rb

Constant Summary

SESSION_KEY =
'kadmin.user'.freeze

Endpoints collapse

Helpers collapse

Methods inherited from ApplicationController

#handle_error, #handle_unexpected_error, #not_found, #params_missing

Methods included from Concerns::AuthorizedUser

#authorize, #authorized?, #authorized_user, #current_user, #logged_in?

Instance Method Details

#failureObject

GET /auth/failure



51
52
53
54
# File 'app/controllers/kadmin/auth_controller.rb', line 51

def failure
  flash.alert = params[:message]
  redirect_to (origin: request.env['omniauth.origin'])
end

#loginObject

GET /auth/login



10
11
12
13
14
15
16
# File 'app/controllers/kadmin/auth_controller.rb', line 10

def 
  if logged_in? && authorized?
    redirect_to dash_path
  else
    render 'kadmin/auth/login'
  end
end

#logoutObject

GET /auth/logout DELETE /auth/logout



20
21
22
23
# File 'app/controllers/kadmin/auth_controller.rb', line 20

def logout
  session.delete(SESSION_KEY)
  redirect_to 
end


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/kadmin/auth_controller.rb', line 79

def omniauth_provider_link
  auth_prefix = auth_path
  provider_link = "#{auth_prefix}/#{Kadmin::Auth.omniauth_provider}"
  origin = params[:origin]

  # if the referer is a auth route, then we risk ending in an endless loop
  if origin.blank?
    referer = request.referer
    if referer.blank?
      origin = Kadmin.config.mount_path
    else
      uri = URI(referer)
      origin = referer unless uri&.path&.start_with?(auth_prefix)
    end
  end

  provider_link = "#{provider_link}?origin=#{CGI.escape(origin)}" unless origin.blank?
  return provider_link
end

#saveObject

GET /auth/:provider/callback POST /auth/:provider/callback



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/kadmin/auth_controller.rb', line 27

def save
  auth_hash = request.env['omniauth.auth']

  if auth_hash.blank?
    Kadmin.logger.error('No authorization hash provided')
    flash.alert = I18n.t('kadmin.auth.error')
    redirect_to (origin: request.env['omniauth.origin'])
    return
  end

  email = auth_hash.dig('info', 'email')
  if Kadmin::Auth.users.exists?(email)
    session[SESSION_KEY] = email
    redirect_url = request.env['omniauth.origin']
    redirect_url = Kadmin.config.mount_path unless valid_redirect_url?(redirect_url)
  else
    flash.alert = I18n.t('kadmin.auth.unauthorized_message')
    redirect_url = (origin: request.env['omniauth.origin'])
  end

  redirect_to redirect_url
end

#unauthorizedObject



56
57
58
59
60
61
# File 'app/controllers/kadmin/auth_controller.rb', line 56

def unauthorized
  render 'kadmin/error', format: ['html'], locals: {
    title: I18n.t('kadmin.auth.unauthorized'),
    message: I18n.t('kadmin.auth.unauthorized_message')
  }
end