Class: OpenC3::OpenC3Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/utilities/authentication.rb

Overview

OpenC3 COSMOS Core authentication code

Direct Known Subclasses

OpenC3KeycloakAuthentication

Instance Method Summary collapse

Constructor Details

#initializeOpenC3Authentication

Returns a new instance of OpenC3Authentication.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openc3/utilities/authentication.rb', line 30

def initialize()
  password = ENV['OPENC3_API_PASSWORD']
  if password.nil?
    raise OpenC3AuthenticationError, "Authentication requires environment variable OPENC3_API_PASSWORD"
  end
  @service = password == ENV['OPENC3_SERVICE_PASSWORD']
  retry_faraday_request do
    response = _make_auth_request(password)
    @token = response.body
  end
  if @token.nil? or @token.empty?
    raise OpenC3AuthenticationError, "Authentication failed. Please check the password in the environment variable OPENC3_API_PASSWORD"
  end
end

Instance Method Details

#_generate_auth_url(endpoint = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openc3/utilities/authentication.rb', line 75

def _generate_auth_url(endpoint = nil)
  schema = ENV['OPENC3_API_SCHEMA'] || 'http'
  hostname = ENV['OPENC3_API_HOSTNAME'] || (ENV['OPENC3_DEVEL'] ? '127.0.0.1' : 'openc3-cosmos-cmd-tlm-api')
  port = ENV['OPENC3_API_PORT'] || '2901'
  port = port.to_i
  unless endpoint
    endpoint = if @service
      "auth/verify_service"
    else
      "auth/verify"
    end
  end
  return "#{schema}://#{hostname}:#{port}/openc3-api/#{endpoint}"
end

#_make_auth_request(password) ⇒ Object



61
62
63
# File 'lib/openc3/utilities/authentication.rb', line 61

def _make_auth_request(password)
  Faraday.new.post(_generate_auth_url, '{"password": "' + password + '"}', {'Content-Type' => 'application/json'})
end

#_make_otp_request(scope: 'DEFAULT') ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/openc3/utilities/authentication.rb', line 65

def _make_otp_request(scope: 'DEFAULT')
  params = {
    'scope' => scope
  }
  headers = {
    'Authorization' => token,
  }
  Faraday.new.get(_generate_auth_url('/auth/otp'), params, headers)
end

#get_otp(scope: 'DEFAULT') ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/openc3/utilities/authentication.rb', line 50

def get_otp(scope: 'DEFAULT')
  session_token = token()
  if session_token.nil? or session_token.empty?
    raise OpenC3AuthenticationError, "Uninitialized authentication: unable to get OTP"
  end
  retry_faraday_request do
    response = _make_otp_request(scope: scope)
    return response.body
  end
end

#retry_faraday_request(max_retries: 3) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/openc3/utilities/authentication.rb', line 90

def retry_faraday_request(max_retries: 3)
  retries = 0
  begin
    yield
  rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
    retries += 1
    if retries <= max_retries
      STDOUT.puts "Authentication request failed (attempt #{retries}/3): #{e.message}. Retrying in #{retries}s..."
      sleep(retries)
      retry
    end
    raise
  end
end

#token(include_bearer: true) ⇒ Object

Load the token from the environment



46
47
48
# File 'lib/openc3/utilities/authentication.rb', line 46

def token(include_bearer: true)
  @token
end