sidekiq_web_google_auth 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a95d3b7b58d3d19c6d13f84edea91a443ea09c944098d8fc274909dc5b0d6f5f
4
- data.tar.gz: e4a9320a379a21d000c7c51cb779aba75574016c65bb60e7e0edd8841c6dfac4
3
+ metadata.gz: 1a36fce164dda6b6f8809ca5157f7c0be81f45a418b645ebe4dfce7b8bfa7e1e
4
+ data.tar.gz: 4bc9c49d6ad42e542164d9ab2df5cfcebf93a7386d78a0a2282d78c34218265c
5
5
  SHA512:
6
- metadata.gz: e6c1ee2d889c7c825ae1215f52a883304a0814aa1a7a56ce88044140bc8440adf47702fdc694c9b0c0dd3cfe6e4ac69c79ca05d9f1f89ffbba59f3a42edd6c98
7
- data.tar.gz: 04b5baedba8a58f10159bdbdb4653586ef0a4298f63e3f76848cfe9fba23a930ea5325c83a57f1c7a3ac6de4dc35669ee88ac206bfd974aeb603d068d9db2cd9
6
+ metadata.gz: 81745342e845ca3438c70d08fa31d6a0d19eaa8d2a0fc2cd98ba0ad37a1d7fecffd934931030856a5b7a2d9e3f24686439f814d1e17e87c0ffcabf624f5105b2
7
+ data.tar.gz: 70d145273192809184660787169e4d40957f4f1089a53005c1d8f5bf2dcc246e56987c601e3a65da41d66b9b679fdec215de6a5c7973f4391b2b8db1dd451e4d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sidekiq_web_google_auth (0.1.1)
4
+ sidekiq_web_google_auth (0.1.2)
5
5
  omniauth
6
6
  omniauth-google-oauth2
7
7
 
data/README.md CHANGED
@@ -26,13 +26,15 @@ Or install it yourself as:
26
26
  Initialize builder:
27
27
 
28
28
  ```ruby
29
- Sidekiq::Web.use(SidekiqWebGoogleAuth::Builder) do
30
- provider(
31
- "example_client_id", # Google OAuth client ID
32
- "example_client_secret", # Google OAuth client secret
33
- authorized_emails: %w[test@mail.com], # List of authorized emails
34
- )
35
- end
29
+ Sidekiq::Web.use(SidekiqWebGoogleAuth::Builder) do
30
+ provider(
31
+ "example_client_id", # Google OAuth client ID
32
+ "example_client_secret", # Google OAuth client secret
33
+ # You must provide at least one of: authorized_emails, authorized_email_domains
34
+ authorized_emails: %w[test@mail.com], # List of authorized emails
35
+ authorized_emails_domains: %w[mail.com], # List of authorized emails domains
36
+ )
37
+ end
36
38
  ```
37
39
 
38
40
  ## Contributing
@@ -4,9 +4,23 @@ require_relative "extension"
4
4
 
5
5
  module SidekiqWebGoogleAuth
6
6
  class Builder < OmniAuth::Builder
7
- def provider(*args, authorized_emails:, **options, &block)
7
+ class ArgumentError < StandardError; end
8
+
9
+ ARGUMENT_ERROR = "You must provide authorized_emails or authorized_emails_domains (or both)"
10
+
11
+ def provider(*args, authorized_emails: [], authorized_emails_domains: [], **options, &block)
12
+ invalid_arguments! if authorized_emails.empty? && authorized_emails_domains.empty?
8
13
  super("google_oauth2", *args, options.merge(name: "oauth"), &block)
9
- Sidekiq::Web.register(SidekiqWebGoogleAuth::Extension.new(authorized_emails))
14
+
15
+ SidekiqWebGoogleAuth::Extension.authorized_emails = authorized_emails
16
+ SidekiqWebGoogleAuth::Extension.authorized_emails_domains = authorized_emails_domains
17
+ Sidekiq::Web.register(SidekiqWebGoogleAuth::Extension)
18
+ end
19
+
20
+ private
21
+
22
+ def invalid_arguments!
23
+ raise ArgumentError.new(ARGUMENT_ERROR)
10
24
  end
11
25
  end
12
26
  end
@@ -3,43 +3,50 @@
3
3
  # Idea taken from https://github.com/mperham/sidekiq/issues/2460#issuecomment-125694743
4
4
  module SidekiqWebGoogleAuth
5
5
  class Extension
6
- def initialize(authorized_emails)
7
- @authorized_emails = authorized_emails
8
- end
9
-
10
- def registered(app) # rubocop:disable Metrics/MethodLength
11
- authorized_emails = @authorized_emails
6
+ class << self
7
+ attr_accessor :authorized_emails, :authorized_emails_domains
12
8
 
13
- app.before do
14
- if !session[:authenticated] && !request.path_info.start_with?("/auth")
15
- redirect("#{root_path}auth/page")
16
- end
9
+ def valid_email?(email)
10
+ authorized_emails.empty? || authorized_emails.include?(email)
17
11
  end
18
12
 
19
- app.get "/auth/page" do
20
- "Please <a href='#{root_path}auth/oauth'>authenticate via Google</a>."
13
+ def valid_email_domain?(email)
14
+ authorized_emails_domains.empty? || authorized_emails_domains.include?(email[/(?<=@).+/])
21
15
  end
22
16
 
23
- app.get "/auth/oauth/callback" do
24
- auth = request.env["omniauth.auth"]
17
+ def registered(app) # rubocop:disable Metrics/MethodLength
18
+ app.before do
19
+ if !session[:authenticated] && !request.path_info.start_with?("/auth")
20
+ redirect("#{root_path}auth/page")
21
+ end
22
+ end
23
+
24
+ app.get "/auth/page" do
25
+ "Please <a href='#{root_path}auth/oauth'>authenticate via Google</a>."
26
+ end
27
+
28
+ app.get "/auth/oauth/callback" do
29
+ auth = request.env["omniauth.auth"]
30
+ ext = SidekiqWebGoogleAuth::Extension
31
+
32
+ if auth && ext.valid_email?(auth.info.email) && ext.valid_email_domain?(auth.info.email)
33
+ session[:authenticated] = true
34
+ redirect(root_path)
35
+ else
36
+ OmniAuth.logger.warn(
37
+ "Someone unauthorized is trying to gain access to Sidekiq: #{auth.info}",
38
+ )
39
+ redirect("#{root_path}auth/page")
40
+ end
41
+ end
25
42
 
26
- if auth && authorized_emails.include?(auth.info.email)
27
- session[:authenticated] = true
43
+ app.get "/logout" do
44
+ session.clear
28
45
  redirect(root_path)
29
- else
30
- OmniAuth.logger.warn(
31
- "Someone unauthorized is trying to gain access to Sidekiq: #{auth.info}",
32
- )
33
- redirect("#{root_path}auth/page")
34
46
  end
35
- end
36
47
 
37
- app.get "/logout" do
38
- session.clear
39
- redirect(root_path)
48
+ app.tabs["Logout"] = "logout"
40
49
  end
41
-
42
- app.tabs["Logout"] = "logout"
43
50
  end
44
51
  end
45
52
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "sidekiq_web_google_auth"
5
- spec.version = "0.1.1"
5
+ spec.version = "0.1.2"
6
6
  spec.authors = ["Igor Kir"]
7
7
  spec.email = ["igor.kir@cadolabs.io"]
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq_web_google_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Kir
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-22 00:00:00.000000000 Z
11
+ date: 2022-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: omniauth