Class: Okta::DirectoryService

Inherits:
Common::Client::Base show all
Defined in:
lib/okta/directory_service.rb

Constant Summary collapse

DEFAULT_OKTA_SCOPES =
%w[openid profile email address phone offline_access device_sso].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Common::Client::Base

configuration, #raise_backend_exception

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger

Constructor Details

#initializeDirectoryService

Returns a new instance of DirectoryService.



12
13
14
# File 'lib/okta/directory_service.rb', line 12

def initialize
  @okta_service = Okta::Service.new
end

Instance Attribute Details

#okta_serviceObject

Returns the value of attribute okta_service.



10
11
12
# File 'lib/okta/directory_service.rb', line 10

def okta_service
  @okta_service
end

Instance Method Details

#handle_health_serverObject



22
23
24
25
26
# File 'lib/okta/directory_service.rb', line 22

def handle_health_server
  server = okta_service.auth_server(Settings.directory.health_server_id)
  scopes = okta_service.get_server_scopes(server.body['id'])
  remove_scope_keys(scopes)
end

#handle_nonhealth_server(category) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/okta/directory_service.rb', line 28

def handle_nonhealth_server(category)
  servers = okta_service.auth_servers
  server = servers.body.select { |auth_server| auth_server['name'].include?(category.downcase) }
  return server if server.empty?

  scopes = @okta_service.get_server_scopes(server[0]['id'])
  remove_scope_keys(scopes)
end

#remove_base_okta_scopes(scopes) ⇒ Object



47
48
49
# File 'lib/okta/directory_service.rb', line 47

def remove_base_okta_scopes(scopes)
  scopes.delete_if { |scope| DEFAULT_OKTA_SCOPES.include? scope['name'] }
end

#remove_scope_keys(scopes) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/okta/directory_service.rb', line 37

def remove_scope_keys(scopes)
  # Removing unneccesary key/value pairs from the Okta Response.
  # Our response only requires the names and description
  parsed_scopes = scopes.body.each do |item|
    item.select! { |k, _v| %w[name displayName description].include?(k.to_s) }
  end
  # Removing the default scopes assigned to each Okta Authorization Server
  remove_base_okta_scopes(parsed_scopes)
end

#scopes(category) ⇒ Object



16
17
18
19
20
# File 'lib/okta/directory_service.rb', line 16

def scopes(category)
  # if the category is health we need to call a specific server instead of relying on querying by name,
  # since there is a 'health/systems' auth server that would affect results
  category == 'health' ? handle_health_server : handle_nonhealth_server(category)
end