Module: Descope::Mixins::Initializer

Included in:
Descope::Mixins
Defined in:
lib/descope/mixins/initializer.rb

Overview

Helper class for initializing the Descope API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mlockObject

Returns the value of attribute mlock.



9
10
11
# File 'lib/descope/mixins/initializer.rb', line 9

def mlock
  @mlock
end

#public_keysObject

Returns the value of attribute public_keys.



9
10
11
# File 'lib/descope/mixins/initializer.rb', line 9

def public_keys
  @public_keys
end

Class Method Details

.included(klass) ⇒ Object



51
52
53
# File 'lib/descope/mixins/initializer.rb', line 51

def self.included(klass)
  klass.send :prepend, Initializer
end

Instance Method Details

#authorization_header(pswd = nil) ⇒ Object



63
64
65
66
67
# File 'lib/descope/mixins/initializer.rb', line 63

def authorization_header(pswd = nil)
  pswd = @default_pswd if pswd.nil? || pswd.empty?
  bearer = "#{@project_id}:#{pswd}"
  add_headers('Authorization' => "Bearer #{bearer}")
end

#base_url(options) ⇒ Object

Raises:



55
56
57
58
59
60
61
# File 'lib/descope/mixins/initializer.rb', line 55

def base_url(options)
  url = options[:descope_base_uri] || ENV['DESCOPE_BASE_URI'] || Common::DEFAULT_BASE_URL
  return url if url.start_with? 'http'

  raise AuthException.new('base url must start with http or https', code: 400)

end

#initialize(config) ⇒ Object



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
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/descope/mixins/initializer.rb', line 11

def initialize(config)
  options = Hash[config.map { |(k, v)| [k.to_sym, v] }]
  @base_uri = base_url(options)
  @headers = client_headers
  @project_id = options[:project_id] || ENV['DESCOPE_PROJECT_ID'] || ''
  @headers['x-descope-project-id'] = @project_id
  @public_key = options[:public_key] || ENV['DESCOPE_PUBLIC_KEY']
  @mlock = Mutex.new
  @logger = options[:logger] || begin
    log_level = options[:log_level] || ENV['DESCOPE_LOG_LEVEL'] || 'info'
    Descope::Mixins::Logging.logger_for(self.class.name, log_level, @project_id)
  end

  @logger.debug("Initializing Descope API with project_id: #{@project_id} and base_uri: #{@base_uri}")

  if @public_key.nil?
    @public_keys = {}
  else
    kid, pub_key, alg = validate_and_load_public_key(@public_key)
    @public_keys = { kid => [pub_key, alg] }
  end

  @skip_verify = options[:skip_verify]
  @secure = !@skip_verify
  @management_key = options[:management_key] || ENV['DESCOPE_MANAGEMENT_KEY']
  @logger.debug("Management Key ID: #{@management_key}")
  @timeout_seconds = options[:timeout_seconds] || Common::DEFAULT_TIMEOUT_SECONDS
  @jwt_validation_leeway = options[:jwt_validation_leeway] || Common::DEFAULT_JWT_VALIDATION_LEEWAY

  if @project_id.to_s.empty?
    raise AuthException.new(
      'Unable to init Auth object because project_id cannot be empty. '\
              'Set environment variable DESCOPE_PROJECT_ID or pass your Project ID to the init function.',
      code: 400
    )
  else
    initialize_api(options)
  end
end

#initialize_api(options) ⇒ Object



69
70
71
72
73
# File 'lib/descope/mixins/initializer.rb', line 69

def initialize_api(options)
  initialize_v1(options)
  @default_pswd = options.fetch(:management_key, ENV['DESCOPE_MANAGEMENT_KEY'])
  authorization_header
end

#initialize_v1(_options) ⇒ Object



75
76
77
78
79
80
# File 'lib/descope/mixins/initializer.rb', line 75

def initialize_v1(_options)
  extend Descope::Api::V1
  extend Descope::Api::V1::Management
  extend Descope::Api::V1::Auth
  extend Descope::Api::V1::Session
end