-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_ad_authenticate.rb
76 lines (56 loc) · 2.17 KB
/
azure_ad_authenticate.rb
1
2
3
4
5
6
7
8
9
10
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module AzureADAuthenticate
# The Client ID is used by the application to uniquely identify itself to Azure AD.
# The Tenant is the name of the Azure AD tenant in which this application is registered.
# The AAD Instance is the instance of Azure, for example public Azure or Azure China.
# The Redirect URI is the URI where Azure AD will return OAuth responses.
# The Authority is the sign-in URL of the tenant.
def self.load_settings
require 'yaml'
YAML.load_file(File.join(Rails.root, "lib/settings.yml"))
# @azure_ad_aad_instance = 'xxx';
# @azure_ad_tenant = Settings['client_id'];
# @azure_ad_client_id = Settings['client_id'];
# @azure_ad_redirect_url = 'xxx';
# @azure_ad_application_resource_id = 'xxx';
# @azure_ad_application_base_address = 'xxx';
end
def self.get_login_page
require 'net/http'
settings = load_settings
uri = URI(settings['aad_instance'] + settings['tenant'] + '/oauth2/authorize')
params = {
'resource' => settings['application_resource_id'],
'client_id' => settings['client_id'],
'response_type' => 'code',
'redirect_uri' => settings['redirect_url'],
# 'client-request-id' => ,
'prompt' => 'login'
}
uri.query = URI.encode_www_form(params)
return uri
end
def self.get_access_token(code)
require 'net/http'
settings = load_settings
uri = URI(settings['aad_instance'] + settings['tenant'] + '/oauth2/token')
params = {
'resource' => settings['application_resource_id'],
'client_id' => settings['client_id'],
'grant_type' => 'authorization_code',
'code' => code,
'redirect_uri' => settings['redirect_url']
}
response = Net::HTTP.post_form(uri, params)
return response
end
def self.refresh_access_token
params = {
'grant_type' => 'refresh_token',
'resource' => settings['application_resource_id'],
'refresh_token' => settings['refresh_token'],
'client_id' => settings['client_id']
}
response = Net::HTTP.post_form(uri, params)
return response
end
end