Class: MangoApps::Config
- Inherits:
-
Object
- Object
- MangoApps::Config
- Defined in:
- lib/mangoapps/config.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#internal_api_email_id ⇒ Object
Returns the value of attribute internal_api_email_id.
-
#internal_api_key ⇒ Object
Returns the value of attribute internal_api_key.
-
#internal_api_secret ⇒ Object
Returns the value of attribute internal_api_secret.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#token_store ⇒ Object
Returns the value of attribute token_store.
Instance Method Summary collapse
- #api_base ⇒ Object
- #authentication_method ⇒ Object
- #base_url ⇒ Object
- #has_internal_api_credentials? ⇒ Boolean
- #has_valid_token? ⇒ Boolean
-
#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
constructor
rubocop:disable Metrics/ParameterLists.
- #show_authentication_method ⇒ Object
- #token_expired? ⇒ Boolean
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_token ⇒ Object
Returns the value of attribute access_token.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def access_token @access_token end |
#client_id ⇒ Object
Returns the value of attribute client_id.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def client_secret @client_secret end |
#domain ⇒ Object
Returns the value of attribute domain.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def domain @domain end |
#internal_api_email_id ⇒ Object
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_key ⇒ Object
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_secret ⇒ Object
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 |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def logger @logger end |
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def open_timeout @open_timeout end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def refresh_token @refresh_token end |
#scope ⇒ Object
Returns the value of attribute scope.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def scope @scope end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/mangoapps/config.rb', line 7 def timeout @timeout end |
#token_store ⇒ Object
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_base ⇒ Object
39 |
# File 'lib/mangoapps/config.rb', line 39 def api_base = "#{base_url}/api/" |
#authentication_method ⇒ Object
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_url ⇒ Object
38 |
# File 'lib/mangoapps/config.rb', line 38 def base_url = "https://#{domain}" |
#has_internal_api_credentials? ⇒ 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
46 47 48 |
# File 'lib/mangoapps/config.rb', line 46 def has_valid_token? @access_token && !token_expired? end |
#show_authentication_method ⇒ Object
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
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 |