-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #11317 - Support basic auth for external sources
(cherry picked from commit 3173a4f)
- Loading branch information
Showing
9 changed files
with
144 additions
and
67 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
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,71 @@ | ||
module HammerCLIForeman | ||
module Api | ||
module BasicAuth | ||
def authenticate(request, args) | ||
if HammerCLI.interactive? | ||
get_user | ||
get_password | ||
end | ||
super | ||
end | ||
|
||
def error(ex) | ||
return unless ex.is_a?(RestClient::Unauthorized) | ||
|
||
clear | ||
default_message = _('Invalid username or password.') | ||
message = begin | ||
response_msg = JSON.parse(ex.response.body)['error'] | ||
response_msg.is_a?(Hash) ? response_msg['message'] : response_msg | ||
rescue | ||
end | ||
return UnauthorizedError.new(default_message) unless message | ||
|
||
UnauthorizedError.new("#{message}\n#{default_message}") | ||
end | ||
|
||
def status | ||
unless @user.nil? || @password.nil? | ||
_("Using configured credentials for user '%s'.") % @user | ||
else | ||
_('Credentials are not configured.') | ||
end | ||
end | ||
|
||
def user(ask = nil) | ||
@user ||= ask && get_user | ||
end | ||
|
||
def password(ask = nil) | ||
@password ||= ask && get_password | ||
end | ||
|
||
def set_credentials(user, password) | ||
@user = user | ||
@password = password | ||
end | ||
|
||
def clear | ||
set_credentials(nil, nil) | ||
end | ||
|
||
private | ||
|
||
def get_user | ||
@user ||= ask_user(_('[Foreman] Username:%s') % ' ') | ||
end | ||
|
||
def get_password | ||
@password ||= ask_user(_("[Foreman] Password for %{user}:%{wsp}") % { user: @user, wsp: ' ' }, true) | ||
end | ||
|
||
def ask_user(prompt, silent = false) | ||
if silent | ||
HammerCLI.interactive_output.ask(prompt) { |q| q.echo = false } | ||
else | ||
HammerCLI.interactive_output.ask(prompt) | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,9 @@ | ||
require 'hammer_cli_foreman/api/basic_auth' | ||
|
||
module HammerCLIForeman | ||
module Api | ||
class InteractiveBasicAuth < ApipieBindings::Authenticators::BasicAuth | ||
def authenticate(request, args) | ||
if HammerCLI.interactive? | ||
get_user | ||
get_password | ||
end | ||
super | ||
end | ||
|
||
def error(ex) | ||
if ex.is_a?(RestClient::Unauthorized) | ||
self.clear | ||
message = _('Invalid username or password.') | ||
begin | ||
message = JSON.parse(ex.response.body)['error']['message'] | ||
rescue | ||
end | ||
UnauthorizedError.new(message) | ||
end | ||
end | ||
|
||
def status | ||
unless @user.nil? || @password.nil? | ||
_("Using configured credentials for user '%s'.") % @user | ||
else | ||
_("Credentials are not configured.") | ||
end | ||
end | ||
|
||
def user(ask=nil) | ||
@user ||= ask && get_user | ||
end | ||
|
||
def password(ask=nil) | ||
@password ||= ask && get_password | ||
end | ||
|
||
def set_credentials(user, password) | ||
@user = user | ||
@password = password | ||
end | ||
|
||
def clear | ||
set_credentials(nil, nil) | ||
end | ||
|
||
private | ||
|
||
def get_user | ||
@user ||= ask_user(_("[Foreman] Username:%s") % " ") | ||
end | ||
|
||
def get_password | ||
@password ||= ask_user(_("[Foreman] Password for %{user}:%{wsp}") % {:user => @user, :wsp => " "}, true) | ||
end | ||
|
||
def ask_user(prompt, silent=false) | ||
if silent | ||
HammerCLI.interactive_output.ask(prompt) { |q| q.echo = false } | ||
else | ||
HammerCLI.interactive_output.ask(prompt) | ||
end | ||
end | ||
include HammerCLIForeman::Api::BasicAuth | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
lib/hammer_cli_foreman/api/interactive_basic_auth_external.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,17 @@ | ||
require 'hammer_cli_foreman/api/basic_auth' | ||
|
||
module HammerCLIForeman | ||
module Api | ||
class InteractiveBasicAuthExternal < ApipieBindings::Authenticators::BasicAuthExternal | ||
include HammerCLIForeman::Api::BasicAuth | ||
|
||
def initialize(user, password, foreman_url) | ||
super(user, password, "#{foreman_url}/api/users/extlogin", HammerCLI::SSLOptions.new.get_options(foreman_url)) | ||
end | ||
|
||
def session_id | ||
auth_cookie&.delete_prefix('_session_id=') | ||
end | ||
end | ||
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