Class: SidekiqWebGoogleAuth::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_web_google_auth/extension.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.authorized_emailsObject

Returns the value of attribute authorized_emails.



7
8
9
# File 'lib/sidekiq_web_google_auth/extension.rb', line 7

def authorized_emails
  @authorized_emails
end

.authorized_emails_domainsObject

Returns the value of attribute authorized_emails_domains.



7
8
9
# File 'lib/sidekiq_web_google_auth/extension.rb', line 7

def authorized_emails_domains
  @authorized_emails_domains
end

Class Method Details

.registered(app) ⇒ Object

rubocop:disable Metrics/MethodLength



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sidekiq_web_google_auth/extension.rb', line 17

def registered(app) # rubocop:disable Metrics/MethodLength
  app.before do
    if !session[:authenticated] && !request.path_info.start_with?("/auth")
      redirect("#{root_path}auth/page")
    end
  end

  app.get "/auth/page" do
    "Please <a href='#{root_path}auth/oauth'>authenticate via Google</a>."
  end

  app.get "/auth/oauth/callback" do
    auth = request.env["omniauth.auth"]
    ext = SidekiqWebGoogleAuth::Extension

    if auth && ext.valid_email?(auth.info.email) && ext.valid_email_domain?(auth.info.email)
      session[:authenticated] = true
      redirect(root_path)
    else
      OmniAuth.logger.warn(
        "Someone unauthorized is trying to gain access to Sidekiq: #{auth.info}",
      )
      redirect("#{root_path}auth/page")
    end
  end

  app.get "/logout" do
    session.clear
    redirect(root_path)
  end

  app.tabs["Logout"] = "logout"
end

.valid_email?(email) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/sidekiq_web_google_auth/extension.rb', line 9

def valid_email?(email)
  authorized_emails.empty? || authorized_emails.include?(email)
end

.valid_email_domain?(email) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/sidekiq_web_google_auth/extension.rb', line 13

def valid_email_domain?(email)
  authorized_emails_domains.empty? || authorized_emails_domains.include?(email[/(?<=@).+/])
end