Class: HammerCLIForeman::Api::SessionAuthenticatorWrapper
- Inherits:
-
ApipieBindings::Authenticators::Base
- Object
- ApipieBindings::Authenticators::Base
- HammerCLIForeman::Api::SessionAuthenticatorWrapper
- Defined in:
- lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb
Constant Summary collapse
- SESSION_STORAGE =
'~/.hammer/sessions/'
Instance Attribute Summary collapse
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
Instance Method Summary collapse
- #authenticate(request, args) ⇒ Object
- #clear ⇒ Object
- #error(ex) ⇒ Object
- #force_user_change ⇒ Object
-
#initialize(authenticator, url, storage_dir = nil) ⇒ SessionAuthenticatorWrapper
constructor
A new instance of SessionAuthenticatorWrapper.
- #password(ask = nil) ⇒ Object
- #response(r) ⇒ Object
- #set_credentials(*args) ⇒ Object
- #status ⇒ Object
- #user(ask = nil) ⇒ Object
- #user_changed? ⇒ Boolean
Constructor Details
#initialize(authenticator, url, storage_dir = nil) ⇒ SessionAuthenticatorWrapper
Returns a new instance of SessionAuthenticatorWrapper.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 10 def initialize(authenticator, url, storage_dir = nil) @authenticator = authenticator @url = url @session_file = "#{uri.scheme}_#{uri.host}" @storage_dir = storage_dir || File.(SESSION_STORAGE) @permissions_ok = warn _("Can't use session auth due to invalid permissions on session files.") unless @permissions_ok end |
Instance Attribute Details
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
8 9 10 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 8 def session_id @session_id end |
Instance Method Details
#authenticate(request, args) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 42 def authenticate(request, args) load_session user = @authenticator.user @user_changed ||= (!user.nil? && user != @user) if !@user_changed && @permissions_ok && @session_id jar = HTTP::CookieJar.new jar.add(HTTP::Cookie.new('_session_id', @session_id, domain: uri.hostname.downcase, path: '/', for_domain: true)) request['Cookie'] = HTTP::Cookie.(jar.) request else @authenticator.authenticate(request, args) end end |
#clear ⇒ Object
21 22 23 24 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 21 def clear destroy_session @authenticator.clear if @authenticator.respond_to?(:clear) end |
#error(ex) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 59 def error(ex) load_session if ex.is_a?(RestClient::Unauthorized) && !@session_id.nil? if @user_changed return UnauthorizedError.new(_("Invalid username or password, continuing with session for '%s'.") % @user) else destroy_session return SessionExpired.new(_("Session has expired.")) end else return @authenticator.error(ex) end end |
#force_user_change ⇒ Object
34 35 36 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 34 def force_user_change @user_changed = true end |
#password(ask = nil) ⇒ Object
85 86 87 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 85 def password(ask=nil) @authenticator.password(ask) if @authenticator.respond_to?(:password) end |
#response(r) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 73 def response(r) if (r.['_session_id'] && r.code != 401) @session_id = r.['_session_id'] save_session(@session_id, @authenticator.user) end @authenticator.response(r) end |
#set_credentials(*args) ⇒ Object
89 90 91 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 89 def set_credentials(*args) @authenticator.set_credentials(*args) if @authenticator.respond_to?(:set_credentials) end |
#status ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 26 def status if load_session _("Session exists, currently logged in as '%s'.") % @user else _("Using sessions, you are currently not logged in.") end end |
#user(ask = nil) ⇒ Object
81 82 83 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 81 def user(ask=nil) @authenticator.user(ask) if @authenticator.respond_to?(:user) end |
#user_changed? ⇒ Boolean
38 39 40 |
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 38 def user_changed? !!@user_changed end |