Class: HolisticAuth::Providers::MsGraph
Constant Summary
collapse
- GRAPH_RESOURCE =
'https://graph.microsoft.com'.freeze
- DEFAULT_CONTENT_TYPE =
'application/json;odata.metadata=minimal;odata.streaming=true'.freeze
- API_VERSION =
'beta'.freeze
- SETTINGS =
{
site: 'https://login.microsoftonline.com',
token_url: 'oauth2/token',
user_info_url: URI("#{GRAPH_RESOURCE}/#{API_VERSION}/me"),
additional_parameters: {
resource: GRAPH_RESOURCE,
},
}.freeze
Instance Attribute Summary
#api_key, #client_id, #client_secret, #oauth2_client, #site, #tenant_id, #token_url, #user_info_url
Instance Method Summary
collapse
#add_secrets, #empty?, #exchange, #initialize, #present?, #secrets, #site_token_url, #to_hash
Instance Method Details
#full_site_url ⇒ Object
25
26
27
|
# File 'lib/holistic_auth/providers/ms_graph.rb', line 25
def full_site_url
tenant_id.present? ? (site + '/' + tenant_id + '/') : (site + '/common/')
end
|
#name ⇒ Object
21
22
23
|
# File 'lib/holistic_auth/providers/ms_graph.rb', line 21
def name
:ms_graph
end
|
#process_info(hash) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/holistic_auth/providers/ms_graph.rb', line 34
def process_info(hash)
sanity_check! hash
{
email_verified: hash['mail'].present?,
email: hash['mail'],
display_name: hash['displayName'],
name: {
givenName: hash['givenName'],
familyName: hash['familyName'],
},
picture_url: '',
uid: hash['id'],
language: hash['preferredLanguage'],
}.with_indifferent_access
end
|
#query!(method, access_token, uri, body = nil) ⇒ Object
Need error handling for when the token has expired.
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/holistic_auth/providers/ms_graph.rb', line 60
def query!(method, access_token, uri, body = nil)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
= {
'Authorization' => "Bearer #{access_token}",
'Content-Type' => DEFAULT_CONTENT_TYPE,
}
full_endpoint = uri.query.present? ? "#{uri.path}?#{uri.query}" : uri.path
response =
case method
when :get
http.get(full_endpoint, )
when :post
http.post(full_endpoint, body, )
else
raise "method #{method} not implemented"
end
response
end
|
#retrieve_user_info(access_token) ⇒ Object
29
30
31
32
|
# File 'lib/holistic_auth/providers/ms_graph.rb', line 29
def retrieve_user_info(access_token)
result = query! :get, access_token.token, settings[:user_info_url]
process_info JSON.parse(result.body)
end
|
#sanity_check!(hash) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/holistic_auth/providers/ms_graph.rb', line 84
def sanity_check!(hash)
raise "Can't process empty user info" unless hash.is_a? Hash
if hash.key?('error')
raise "Could not process user info: \n #{hash['error']['code']}: #{hash['error']['message']}"
end
end
|
#settings ⇒ Object
17
18
19
|
# File 'lib/holistic_auth/providers/ms_graph.rb', line 17
def settings
self.class::SETTINGS
end
|