Sha256: 772a89cac9f9efd7c70a195510f5a5812abc7afc22cbac72c7854d57c0be07eb

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

# Idea taken from https://github.com/mperham/sidekiq/issues/2460#issuecomment-125694743
module SidekiqWebGoogleAuth
  class Extension
    def initialize(authorized_emails)
      @authorized_emails = authorized_emails
    end

    def registered(app) # rubocop:disable Metrics/MethodLength
      authorized_emails = @authorized_emails

      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"]

        if auth && authorized_emails.include?(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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq_web_google_auth-0.1.1 lib/sidekiq_web_google_auth/extension.rb
sidekiq_web_google_auth-0.1.0 lib/sidekiq_web_google_auth/extension.rb