Class: HammerCLIForeman::Api::Oauth::AuthenticationCodeGrant

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oidc_token_endpoint, oidc_authorization_endpoint, oidc_client_id, oidc_redirect_uri) ⇒ AuthenticationCodeGrant

Returns a new instance of AuthenticationCodeGrant.



11
12
13
14
15
16
17
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 11

def initialize(oidc_token_endpoint, oidc_authorization_endpoint, oidc_client_id, oidc_redirect_uri)
  @oidc_token_endpoint = oidc_token_endpoint
  @oidc_authorization_endpoint = oidc_authorization_endpoint
  @oidc_client_id = oidc_client_id
  @oidc_redirect_uri = oidc_redirect_uri
  super(nil)
end

Instance Attribute Details

#oidc_authorization_endpointObject

Returns the value of attribute oidc_authorization_endpoint.



8
9
10
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 8

def oidc_authorization_endpoint
  @oidc_authorization_endpoint
end

#oidc_client_idObject

Returns the value of attribute oidc_client_id.



8
9
10
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 8

def oidc_client_id
  @oidc_client_id
end

#oidc_redirect_uriObject

Returns the value of attribute oidc_redirect_uri.



8
9
10
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 8

def oidc_redirect_uri
  @oidc_redirect_uri
end

#oidc_token_endpointObject

Returns the value of attribute oidc_token_endpoint.



8
9
10
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 8

def oidc_token_endpoint
  @oidc_token_endpoint
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 8

def token
  @token
end

Instance Method Details

#authenticate(request, token) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 19

def authenticate(request, token)
  if HammerCLI.interactive?
    set_token_interactively
  else
    set_token(oidc_token_endpoint, oidc_authorization_endpoint, oidc_client_id, oidc_redirect_uri)
  end
  super
end

#error(ex) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 53

def error(ex)
  if ex.is_a?(RestClient::InternalServerError)
    @oidc_token_endpoint = @oidc_authorization_endpoint = @oidc_client_id = @oidc_client_id = nil
    original_message = _("Invalid oidc-client-id or oidc-token-endpoint or oidc-authorization-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_authorization_endpoint, input_oidc_client_id, input_oidc_redirect_uri) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 32

def set_token(input_oidc_token_endpoint, input_oidc_authorization_endpoint, input_oidc_client_id, input_oidc_redirect_uri)
  @oidc_token_endpoint = input_oidc_token_endpoint if input_oidc_token_endpoint
  @oidc_authorization_endpoint = input_oidc_authorization_endpoint if input_oidc_authorization_endpoint
  @oidc_client_id = input_oidc_client_id if input_oidc_client_id
  @oidc_redirect_uri = input_oidc_redirect_uri if input_oidc_redirect_uri

  if @oidc_client_id.to_s.empty? || @oidc_authorization_endpoint.to_s.empty? || @oidc_redirect_uri.to_s.empty? || @oidc_token_endpoint.to_s.empty?
    @token = nil
  else
    @code ||= get_code
    @token = HammerCLIForeman::OpenidConnect.new(
      @oidc_token_endpoint, @oidc_client_id).get_token_via_2fa(@code, @oidc_redirect_uri)
  end
end

#set_token_interactivelyObject



28
29
30
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 28

def set_token_interactively
  @token ||= set_token(get_oidc_token_endpoint, get_oidc_authorization_endpoint, get_oidc_client_id, get_oidc_redirect_uri)
end

#userObject



47
48
49
50
51
# File 'lib/hammer_cli_foreman/api/oauth/authentication_code_grant.rb', line 47

def user
  return nil unless @token
  payload = JWT.decode(@token, nil, false)
  payload.first["preferred_username"]
end