Class: HammerCLIForeman::Api::Oauth::PasswordGrant

Inherits:
ApipieBindings::Authenticators::TokenAuth
  • Object
show all
Defined in:
lib/hammer_cli_foreman/api/oauth/password_grant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oidc_token_endpoint, oidc_client_id, user, password) ⇒ PasswordGrant

Returns a new instance of PasswordGrant.



9
10
11
12
13
14
15
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 9

def initialize(oidc_token_endpoint, oidc_client_id, user, password)
  @oidc_token_endpoint = oidc_token_endpoint
  @oidc_client_id = oidc_client_id
  @user = user
  @password = password
  super set_token(oidc_token_endpoint, oidc_client_id, user, password)
end

Instance Attribute Details

#oidc_client_idObject

Returns the value of attribute oidc_client_id.



7
8
9
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 7

def oidc_client_id
  @oidc_client_id
end

#oidc_token_endpointObject

Returns the value of attribute oidc_token_endpoint.



7
8
9
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 7

def oidc_token_endpoint
  @oidc_token_endpoint
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 7

def password
  @password
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 7

def token
  @token
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 7

def user
  @user
end

Instance Method Details

#authenticate(request, token) ⇒ Object



17
18
19
20
21
22
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 17

def authenticate(request, token)
  if HammerCLI.interactive?
    set_token_interactively
  end
  super
end

#error(ex) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 41

def error(ex)
  if ex.is_a?(RestClient::InternalServerError)
    @user = @password = @oidc_token_endpoint = @oidc_client_id = nil
    original_message = _("Invalid credentials or oidc-client-id or oidc-token-endpoint.\n")
    begin
      message = JSON.parse(ex.response.body)['error']['message']
    rescue
    end
    UnauthorizedError.new(original_message << message)
  end
end

#set_token(input_oidc_token_endpoint, input_oidc_client_id, input_user, input_password) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 28

def set_token(input_oidc_token_endpoint, input_oidc_client_id, input_user, input_password)
  @oidc_token_endpoint = input_oidc_token_endpoint if input_oidc_token_endpoint
  @user = input_user
  @password = input_password
  @oidc_client_id = input_oidc_client_id if input_oidc_client_id
  if @user.to_s.empty? || @password.to_s.empty? || @oidc_token_endpoint.to_s.empty? || @oidc_client_id.to_s.empty?
    @token = nil
  else
    @token = HammerCLIForeman::OpenidConnect.new(
      @oidc_token_endpoint, @oidc_client_id).get_token(@user, @password)
  end
end

#set_token_interactivelyObject



24
25
26
# File 'lib/hammer_cli_foreman/api/oauth/password_grant.rb', line 24

def set_token_interactively
  @token ||= set_token(get_oidc_token_endpoint, get_oidc_client_id, get_user, get_password)
end