Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,15 @@ MOTOR_SYNC_REMOTE_URL=https://remote-app-url/ MOTOR_SYNC_API_KEY=secure-random-s

## Authentication

Admin panel can be secured with 'Basic authentication' by specifying `MOTOR_AUTH_USERNAME` and `MOTOR_AUTH_PASSWORD` environment variables.
Admin panel can be secured with 'Basic authentication' by specifying `MOTOR_AUTH_USERNAME` and `MOTOR_AUTH_PASSWORD` environment variables. Alternatively, the username and password can be added to the credentials file:

Alternatively, it can be secured with [devise](https://github.com/heartcombo/devise/wiki/How-To:-Define-resource-actions-that-require-authentication-using-routes.rb) or any other authentication library used by the application:
```yaml
motor:
username: <username>
password: <password>
```

The admin panel can also be secured using [devise](https://github.com/heartcombo/devise/wiki/How-To:-Define-resource-actions-that-require-authentication-using-routes.rb) or any other authentication library used by the application:

```ruby
authenticate :admin_user do
Expand Down
8 changes: 5 additions & 3 deletions lib/motor/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ class Admin < ::Rails::Engine
end

initializer 'motor.basic_auth' do
next if ENV['MOTOR_AUTH_PASSWORD'].blank?
motor_username = ENV['MOTOR_AUTH_USERNAME'].presence || Rails.application.credentials.dig(:motor, :username)
motor_password = ENV['MOTOR_AUTH_PASSWORD'].presence || Rails.application.credentials.dig(:motor, :password)
next if motor_username.blank? || motor_password.blank?

config.middleware.use Rack::Auth::Basic do |username, password|
ActiveSupport::SecurityUtils.secure_compare(
::Digest::SHA256.hexdigest(username),
::Digest::SHA256.hexdigest(ENV['MOTOR_AUTH_USERNAME'].to_s)
::Digest::SHA256.hexdigest(motor_username.to_s)
) &
ActiveSupport::SecurityUtils.secure_compare(
::Digest::SHA256.hexdigest(password),
::Digest::SHA256.hexdigest(ENV['MOTOR_AUTH_PASSWORD'].to_s)
::Digest::SHA256.hexdigest(motor_password.to_s)
)
end
end
Expand Down