Class: Uc3DmpCognito::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/uc3-dmp-cognito/client.rb

Overview

Helper functions for working with Dynamo JSON

Constant Summary collapse

MSG_MISSING_POOL =
'No Cognito Pool defined. Expecting `ENV[\'COGNITO_USER_POOL_ID\']'
MSG_COGNITO_ERROR =
'Cognito User Pool Error - %{msg} - %{trace}'

Class Method Summary collapse

Class Method Details

.get_client_name(client_id:, logger: nil) ⇒ Object

Fetch the name of the client from the client id provided. DMP Provenance names match the Cognito client names rubocop:disable Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/uc3-dmp-cognito/client.rb', line 15

def get_client_name(client_id:, logger: nil)
  user_pool_id = ENV.fetch('COGNITO_USER_POOL_ID', nil)
  raise ClientError, MSG_MISSING_POOL if user_pool_id.nil?

  client = Aws::CognitoIdentityProvider::Client.new(region: ENV.fetch('AWS_REGION', nil))
  resp = client.describe_user_pool_client({ user_pool_id:, client_id: })
  msg = "Searching for Client ID: #{client_id} in Cognito User Pool: #{user_pool_id} - found"
  logger.debug(message: "#{msg} '#{resp&.user_pool_client&.client_name&.downcase}'") if logger.respond_to?(:debug)
  resp&.user_pool_client&.client_name&.downcase
rescue Aws::Errors::ServiceError => e
  raise ClientError, format(MSG_COGNITO_ERROR, msg: e.message, trace: e.backtrace)
end