Skip to content

Commit 16d8d05

Browse files
first version
1 parent 32a1d20 commit 16d8d05

18 files changed

Lines changed: 199 additions & 202 deletions

File tree

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2026 Your Name
3+
Copyright (c) 2026 Miguel Savignano
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,6 @@ as the given `AdminUser` — no username/password required.
6363
6464
---
6565

66-
## Route constraint (optional)
67-
68-
If you prefer to block at the routing layer instead of the controller layer:
69-
70-
```ruby
71-
# config/routes.rb
72-
constraints(ActiveAdminVpn::VpnConstraint) do
73-
ActiveAdmin.routes(self)
74-
end
75-
```
76-
77-
---
78-
7966
## Trusted proxies
8067

8168
If your app runs behind a load balancer or reverse proxy, make sure Rails trusts

active_admin_vpn.gemspec

Lines changed: 0 additions & 34 deletions
This file was deleted.

lib/active_admin/vpn_auto_login.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
# frozen_string_literal: true
22

3-
require_relative "vpn_auto_login/version"
3+
require "active_admin/vpn_auto_login/version"
4+
require "active_admin/vpn_auto_login/configuration"
5+
require "active_admin/vpn_auto_login/vpn_constraint"
6+
require "active_admin/vpn_auto_login/controller_helpers"
7+
require "active_admin/vpn_auto_login/railtie" if defined?(Rails)
48

59
module ActiveAdmin
610
module VpnAutoLogin
7-
# Your gem's code goes here.
11+
class << self
12+
def configuration
13+
@configuration ||= Configuration.new
14+
end
15+
16+
def configure
17+
yield(configuration)
18+
end
19+
20+
def reset!
21+
@configuration = Configuration.new
22+
end
23+
end
824
end
925
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveAdmin
4+
module VpnAutoLogin
5+
class Configuration
6+
attr_writer :ip_ranges
7+
attr_accessor :auto_login
8+
attr_accessor :auto_login_email
9+
attr_accessor :forbidden_status
10+
attr_accessor :forbidden_message
11+
12+
def initialize
13+
@auto_login = false
14+
@auto_login_email = nil
15+
@forbidden_status = :forbidden
16+
@forbidden_message = "Access denied. Connect to the VPN to access this area."
17+
end
18+
19+
def ip_ranges
20+
raw = @ip_ranges || ENV["VPN_IP_RANGE"] || ""
21+
raw.split(",").map(&:strip).reject(&:empty?)
22+
end
23+
24+
def parsed_ranges
25+
ip_ranges.map { |cidr| IPAddr.new(cidr) }
26+
rescue IPAddr::InvalidAddressError => e
27+
raise ArgumentError, "[ActiveAdmin::VpnAutoLogin] Invalid IP range in configuration: #{e.message}"
28+
end
29+
30+
def vpn_ip?(ip)
31+
return false if ip.blank?
32+
parsed_ranges.any? { |range| range.include?(IPAddr.new(ip)) }
33+
rescue IPAddr::InvalidAddressError
34+
false
35+
end
36+
end
37+
end
38+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveAdmin
4+
module VpnAutoLogin
5+
module ControllerHelpers
6+
extend ActiveSupport::Concern
7+
8+
included do
9+
before_action :enforce_vpn_access
10+
before_action :auto_login_from_vpn, if: -> { ActiveAdmin::VpnAutoLogin.configuration.auto_login }
11+
end
12+
13+
private
14+
15+
def on_vpn?
16+
ActiveAdmin::VpnAutoLogin.configuration.vpn_ip?(request.remote_ip)
17+
end
18+
19+
def enforce_vpn_access
20+
return if on_vpn?
21+
22+
config = ActiveAdmin::VpnAutoLogin.configuration
23+
render plain: config.forbidden_message, status: config.forbidden_status
24+
end
25+
26+
def auto_login_from_vpn
27+
return unless on_vpn?
28+
return if respond_to?(:current_admin_user) && current_admin_user
29+
30+
email = ActiveAdmin::VpnAutoLogin.configuration.auto_login_email
31+
if email.blank?
32+
Rails.logger.warn "[ActiveAdmin::VpnAutoLogin] auto_login is enabled but auto_login_email is not set."
33+
return
34+
end
35+
36+
user = AdminUser.find_by(email: email)
37+
if user
38+
sign_in(user)
39+
else
40+
Rails.logger.warn "[ActiveAdmin::VpnAutoLogin] auto_login_email '#{email}' not found in AdminUser."
41+
end
42+
end
43+
end
44+
end
45+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveAdmin
4+
module VpnAutoLogin
5+
class Railtie < Rails::Railtie
6+
initializer "active_admin_vpn_auto_login.setup" do
7+
ActiveSupport.on_load(:active_admin) do
8+
ActiveAdmin.application.on_load do
9+
ActiveAdmin::BaseController.include(ActiveAdmin::VpnAutoLogin::ControllerHelpers)
10+
end
11+
end
12+
end
13+
end
14+
end
15+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveAdmin
4+
module VpnAutoLogin
5+
class VpnConstraint
6+
def self.matches?(request)
7+
ActiveAdmin::VpnAutoLogin.configuration.vpn_ip?(request.remote_ip)
8+
end
9+
end
10+
end
11+
end

lib/active_admin_vpn.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/active_admin_vpn/configuration.rb

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)