-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuth.cs
144 lines (121 loc) · 6.12 KB
/
Auth.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Azure.Core;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.Azure.KeyVault;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using PnP.Framework;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace appsvc_fnc_dev_scw_sensitivity_dotnet001
{
//internal class Auth
//{
// internal static X509Certificate2 GetKeyVaultCertificateAsync(string keyVaultUrl, string name, ILogger log)
// {
// log.LogInformation("GetKeyVaultCertificateAsync received a request.");
// var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
// var secret = client.GetSecret(name).Value;
// X509Certificate2 certificate = new X509Certificate2(Convert.FromBase64String(secret.Value), string.Empty, X509KeyStorageFlags.MachineKeySet);
// log.LogInformation("GetKeyVaultCertificateAsync processed a request.");
// return certificate;
// }
// internal static ClientContext GetContextByCertificate(string siteUrl, string keyVaultUrl,string certificateName, string clientId, string tenantId, ILogger log)
// {
// X509Certificate2 mycert = Auth.GetKeyVaultCertificateAsync(keyVaultUrl, certificateName, log);
// var ctx = new AuthenticationManager(clientId, mycert, tenantId).GetContext(siteUrl);
// log.LogInformation($"Created client connection for {siteUrl}");
// return ctx;
// }
//}
public class ROPCConfidentialTokenCredential : Azure.Core.TokenCredential
{
// Implementation of the Azure.Core.TokenCredential class
string _clientId;
string _clientSecret;
string _password;
string _tenantId;
string _tokenEndpoint;
string _username;
ILogger _log;
public ROPCConfidentialTokenCredential(ILogger log)
{
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables().Build();
string keyVaultUrl = config["keyVaultUrl"];
string secretName = config["secretName"];
string secretNamePassword = config["secretNamePassword"];
_clientId = config["clientId"];
_tenantId = config["tenantId"];
_username = config["user_name"];
_log = log;
_tokenEndpoint = "https://login.microsoftonline.com/" + _tenantId + "/oauth2/v2.0/token";
log.LogInformation($"_username = {_username}");
SecretClientOptions options = new SecretClientOptions()
{
Retry =
{
Delay= TimeSpan.FromSeconds(2),
MaxDelay = TimeSpan.FromSeconds(16),
MaxRetries = 5,
Mode = RetryMode.Exponential
}
};
var client = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential(), options);
KeyVaultSecret secret = client.GetSecret(secretName);
_clientSecret = secret.Value;
KeyVaultSecret password = client.GetSecret(secretNamePassword);
_password = password.Value;
}
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
{
HttpClient httpClient = new HttpClient();
// Create the request body
var Parameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("client_id", _clientId),
new KeyValuePair<string, string>("client_secret", _clientSecret),
new KeyValuePair<string, string>("scope", string.Join(" ", requestContext.Scopes)),
new KeyValuePair<string, string>("username", _username),
new KeyValuePair<string, string>("password", _password),
new KeyValuePair<string, string>("grant_type", "password")
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, _tokenEndpoint)
{
Content = new FormUrlEncodedContent(Parameters)
};
var response = httpClient.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
dynamic responseJson = JsonConvert.DeserializeObject(response);
var expirationDate = DateTimeOffset.UtcNow.AddMinutes(60.0);
return new AccessToken(responseJson.access_token.ToString(), expirationDate);
}
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
{
HttpClient httpClient = new HttpClient();
// Create the request body
var Parameters = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("client_id", _clientId),
new KeyValuePair<string, string>("client_secret", _clientSecret),
new KeyValuePair<string, string>("scope", string.Join(" ", requestContext.Scopes)),
new KeyValuePair<string, string>("username", _username),
new KeyValuePair<string, string>("password", _password),
new KeyValuePair<string, string>("grant_type", "password")
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, _tokenEndpoint)
{
Content = new FormUrlEncodedContent(Parameters)
};
var response = httpClient.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
dynamic responseJson = JsonConvert.DeserializeObject(response);
var expirationDate = DateTimeOffset.UtcNow.AddMinutes(60.0);
return new ValueTask<AccessToken>(new AccessToken(responseJson.access_token.ToString(), expirationDate));
}
}
}