Class: MangoApps::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mangoapps/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain: nil, client_id: nil, client_secret: nil, redirect_uri: nil, scope: nil, token_store: nil, timeout: 30, open_timeout: 10, logger: nil, internal_api_key: nil, internal_api_secret: nil, internal_api_email_id: nil) ⇒ Config

rubocop:disable Metrics/ParameterLists



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mangoapps/config.rb', line 11

def initialize(domain: nil, client_id: nil, client_secret: nil, redirect_uri: nil, scope: nil, # rubocop:disable Metrics/ParameterLists
               token_store: nil, timeout: 30, open_timeout: 10, logger: nil,
               internal_api_key: nil, internal_api_secret: nil, internal_api_email_id: nil)
  # Load environment variables from .env file
  Dotenv.load if File.exist?(".env")

  @domain        = domain || ENV.fetch("MANGOAPPS_DOMAIN", nil)
  @client_id     = client_id || ENV.fetch("MANGOAPPS_CLIENT_ID", nil)
  @client_secret = client_secret || ENV.fetch("MANGOAPPS_CLIENT_SECRET", nil)
  @redirect_uri  = redirect_uri || ENV["MANGOAPPS_REDIRECT_URI"] || "https://localhost:3000/oauth/callback"
  @scope         = scope || ENV["MANGOAPPS_SCOPE"] || "openid profile email"
  @access_token  = ENV["MANGOAPPS_ACCESS_TOKEN"]
  @refresh_token = ENV["MANGOAPPS_REFRESH_TOKEN"]
  @internal_api_key = internal_api_key || ENV["MANGOAPPS_INTERNAL_API_KEY"]
  @internal_api_secret = internal_api_secret || ENV["MANGOAPPS_INTERNAL_API_SECRET"]
  @internal_api_email_id = internal_api_email_id || ENV["MANGOAPPS_INTERNAL_API_EMAIL_ID"]
  @token_store   = token_store
  @timeout       = timeout
  @open_timeout  = open_timeout
  @logger        = logger

  validate_required_fields!
  
  # Show authentication method being used
  show_authentication_method
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def access_token
  @access_token
end

#client_idObject

Returns the value of attribute client_id.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def client_secret
  @client_secret
end

#domainObject

Returns the value of attribute domain.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def domain
  @domain
end

#internal_api_email_idObject

Returns the value of attribute internal_api_email_id.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def internal_api_email_id
  @internal_api_email_id
end

#internal_api_keyObject

Returns the value of attribute internal_api_key.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def internal_api_key
  @internal_api_key
end

#internal_api_secretObject

Returns the value of attribute internal_api_secret.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def internal_api_secret
  @internal_api_secret
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def open_timeout
  @open_timeout
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def refresh_token
  @refresh_token
end

#scopeObject

Returns the value of attribute scope.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def scope
  @scope
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def timeout
  @timeout
end

#token_storeObject

Returns the value of attribute token_store.



7
8
9
# File 'lib/mangoapps/config.rb', line 7

def token_store
  @token_store
end

Instance Method Details

#api_baseObject



39
# File 'lib/mangoapps/config.rb', line 39

def api_base  = "#{base_url}/api/"

#authentication_methodObject



54
55
56
57
58
59
60
61
62
# File 'lib/mangoapps/config.rb', line 54

def authentication_method
  if has_internal_api_credentials?
    :internal_api
  elsif has_valid_token?
    :oauth
  else
    :none
  end
end

#base_urlObject



38
# File 'lib/mangoapps/config.rb', line 38

def base_url  = "https://#{domain}"

#has_internal_api_credentials?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/mangoapps/config.rb', line 50

def has_internal_api_credentials?
  @internal_api_key && @internal_api_secret && !@internal_api_key.empty? && !@internal_api_secret.empty?
end

#has_valid_token?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/mangoapps/config.rb', line 46

def has_valid_token?
  @access_token && !token_expired?
end

#show_authentication_methodObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mangoapps/config.rb', line 64

def show_authentication_method
  case authentication_method
  when :internal_api
    puts "🔑 Using Internal API authentication"
    puts "   Domain: #{@domain}"
    puts "   API Key: #{@internal_api_key[0..7]}..."
  when :oauth
    puts "🔐 Using OAuth2 authentication"
    puts "   Domain: #{@domain}"
    puts "   Client ID: #{@client_id[0..7]}..."
    puts "   Redirect URI: #{@redirect_uri}"
  end
end

#token_expired?Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/mangoapps/config.rb', line 41

def token_expired?
  return true unless ENV["MANGOAPPS_TOKEN_EXPIRES_AT"]
  Time.now.to_i >= ENV["MANGOAPPS_TOKEN_EXPIRES_AT"].to_i
end