-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generator to set up HTTP Basic authentication easily
This asks for the username, generates and password and stores both in Rails credentials.
- Loading branch information
Showing
8 changed files
with
95 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
lib/generators/mission_control/jobs/authentication_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class MissionControl::Jobs::AuthenticationGenerator < Rails::Generators::Base | ||
def init | ||
if credentials_accessible? | ||
if authentication_configured? | ||
say "HTTP Basic Authentication is already configured for `#{Rails.env}`. You can edit it using `credentials:edit`" | ||
else | ||
say "Setting up credentials for HTTP Basic Authentication for `#{Rails.env}` environment." | ||
say "" | ||
|
||
username = ask "Enter username: " | ||
password = SecureRandom.base58(64) | ||
|
||
store_credentials(username, password) | ||
say "Username and password stored in Rails encrypted credentials." | ||
say "" | ||
say "You can now access Mission Control – Jobs with: " | ||
say "" | ||
say " - Username: #{username}" | ||
say " - password: #{password}" | ||
say "" | ||
say "You can also edit these in the future via `credentials:edit`" | ||
end | ||
else | ||
say "Rails credentials haven't been configured or aren't accessible. Configure them following the instructions in `credentials:help`" | ||
end | ||
end | ||
|
||
private | ||
def credentials_accessible? | ||
credentials.read.present? | ||
end | ||
|
||
def authentication_configured? | ||
%i[ http_basic_auth_user http_basic_auth_password ].any? do |key| | ||
credentials.dig(:mission_control, key).present? | ||
end | ||
end | ||
|
||
def store_credentials(username, password) | ||
content = credentials.read + "\n" + http_authentication_entry(username, password) + "\n" | ||
credentials.write(content) | ||
end | ||
|
||
def credentials | ||
@credentials ||= Rails.application.encrypted(config.content_path, key_path: config.key_path) | ||
end | ||
|
||
def config | ||
Rails.application.config.credentials | ||
end | ||
|
||
def http_authentication_entry(username, password) | ||
<<~ENTRY | ||
mission_control: | ||
http_basic_auth_user: #{username} | ||
http_basic_auth_password: #{password} | ||
ENTRY | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace :mission_control do | ||
namespace :jobs do | ||
desc "Configure HTTP Basic Authentication" | ||
task "authentication:init" do | ||
Rails::Command.invoke :generate, [ "mission_control:jobs:authentication" ] | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
67f819f011ec672273c91cf789afb5d7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3wr+OnlAdcQJl0WURd7JXv+pleXbJVWozLH4JfPU6dGc9A0VlQ/kQosdPqDF7Yf/WrLtodre258ALf0ZHE2bQYgH3Eq0cJQ7xN8WwfGjBjXiL6uWaOHcfgcPVNg4E3Ag+YN3EOH8aquSttX7Uqyfv3tPlYQBQ7fs8lXjx3APfl3P8Vk2Yz6bhQcBgXhtFqH+--f7tDKb8EHxaT9l+Z--WIHpj/e3mEcqupnMrf5fvw== |