Module: Legion::Extensions::Identity::Entra::Helpers::GraphClient

Extended by:
GraphClient
Includes:
JSON::Helper, Logging::Helper, Settings::Helper
Included in:
GraphClient
Defined in:
lib/legion/extensions/identity/entra/helpers/graph_client.rb

Constant Summary collapse

ME_SELECT =
'id,displayName,mail,employeeId,onPremisesSamAccountName,' \
'onPremisesDomainName,mailNickname,department,jobTitle,companyName'

Instance Method Summary collapse

Instance Method Details

#fetch_me(access_token) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/identity/entra/helpers/graph_client.rb', line 19

def fetch_me(access_token)
  log.debug('GraphClient.fetch_me: requesting /me profile from Microsoft Graph')
  response = graph_connection(access_token).get("me?$select=#{ME_SELECT}")

  unless response.success?
    log.warn("GraphClient.fetch_me: Graph API returned #{response.status}")
    return nil
  end

  log.debug('GraphClient.fetch_me: profile fetched successfully')
  parse_profile(json_load(response.body))
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'graph_client.fetch_me')
  nil
end

#graph_connection(access_token) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/identity/entra/helpers/graph_client.rb', line 50

def graph_connection(access_token)
  Faraday.new(url: Legion::Extensions::Identity::Entra::Client::GRAPH_BASE) do |f|
    f.headers['Authorization'] = "Bearer #{access_token}"
    f.headers['Accept'] = 'application/json'
    f.options.open_timeout = 5
    f.options.timeout = 10
  end
end

#parse_profile(data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/identity/entra/helpers/graph_client.rb', line 35

def parse_profile(data)
  {
    id:                           data[:id],
    display_name:                 data[:displayName] || data[:display_name],
    mail:                         data[:mail],
    employee_id:                  data[:employeeId] || data[:employee_id],
    on_premises_sam_account_name: data[:onPremisesSamAccountName] || data[:on_premises_sam_account_name],
    on_premises_domain_name:      data[:onPremisesDomainName] || data[:on_premises_domain_name],
    mail_nickname:                data[:mailNickname] || data[:mail_nickname],
    department:                   data[:department],
    job_title:                    data[:jobTitle] || data[:job_title],
    company_name:                 data[:companyName] || data[:company_name]
  }
end