-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchUserIds.cs
168 lines (144 loc) · 6.46 KB
/
fetchUserIds.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class FetchUserIds
{
private readonly ILogger<FetchUserIds> _logger;
public FetchUserIds(ILogger<FetchUserIds> logger) {
_logger = logger;
}
[Function("FetchUserIds")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "FetchUserIds/{product}")] HttpRequest req,
string product)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
if (string.IsNullOrEmpty(product))
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
product = data?.product;
}
if (!string.IsNullOrEmpty(product))
{
var azureToken = await GetAzureAccessTokenAsync();
if (string.IsNullOrEmpty(azureToken))
{
return new BadRequestObjectResult("Failed to acquire Azure access token.");
}
var subscriptionId = Environment.GetEnvironmentVariable("SUB_ID");
var resourceGroup = Environment.GetEnvironmentVariable("RG");
var apimName = Environment.GetEnvironmentVariable("APIM_NAME");
var ownerId = await GetProductSubscriptionOwnerIdAsync(azureToken, subscriptionId, resourceGroup, apimName, product, _logger);
if (string.IsNullOrEmpty(ownerId))
{
return new NotFoundObjectResult($"Product '{product}' not found ");
}
return new OkObjectResult(ownerId);
}
else
{
return null;
}
}
private static async Task<string> GetAzureAccessTokenAsync()
{
string clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
string clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
string tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
var httpClient = new HttpClient();
var tokenEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/token";
var tokenRequestBody = $"grant_type=client_credentials&client_id={clientId}&client_secret={clientSecret}&resource=https://management.azure.com/";
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenEndpoint)
{
Content = new StringContent(tokenRequestBody, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded")
};
var tokenResponse = await httpClient.SendAsync(tokenRequest);
if (!tokenResponse.IsSuccessStatusCode)
{
return null;
}
var responseBody = await tokenResponse.Content.ReadAsStringAsync();
dynamic tokenData = Newtonsoft.Json.JsonConvert.DeserializeObject(responseBody);
string accessToken = tokenData?.access_token;
return accessToken;
}
private static List<string> handleUserIds(string responseBody, string product, List<String> users )
{
var subscriptions = JsonConvert.DeserializeObject<SubscriptionListResult>(responseBody);
var subProducts = subscriptions?.Value?.FindAll(s => s.properties.scope.Contains(product));
if (subProducts != null)
{
foreach (Subscription sub in subProducts)
{
int i = sub.properties.OwnerId.LastIndexOf('/');
if(i >= 0 && i < sub.properties.OwnerId.Length) {
string id = sub.properties.OwnerId.Substring(i + 1);
users.Add(id);
}
}
}
return users;
}
private static async Task<List<string>> handleResponse(string response, string product, List<String> users, HttpClient client, ILogger log)
{
handleUserIds(response, product, users);
JObject jsonObj = JObject.Parse(response);
string nextLink = (string)jsonObj["nextLink"];
if(!string.IsNullOrEmpty(nextLink)) {
var newResponse = await client.GetAsync(nextLink);
if( newResponse.IsSuccessStatusCode) {
var responseBody = await newResponse.Content.ReadAsStringAsync();
return await handleResponse(responseBody, product, users, client, log);
}
}
return users;
}
private static async Task<string> GetProductSubscriptionOwnerIdAsync(string azureToken, string subscriptionId, string resourceGroup, string apimName, string product, ILogger log)
{
List<String> returnStr = new List<string>();
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", azureToken);
var apiUrl = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.ApiManagement/service/{apimName}/subscriptions?api-version=2020-06-01-preview";
var response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
returnStr = await handleResponse(responseBody, product, returnStr, client, log);
return JsonConvert.SerializeObject(returnStr);
}
return null;
}
}
}
public class Subscription
{
public string type {get; set;}
public string name {get; set;}
public string id {get; set;}
public Properties properties { get; set; }
}
public class Properties
{
public string OwnerId {get; set;}
public string scope {get; set;}
public string displayName {get; set;}
public string state {get; set;}
public string createdDate {get; set;}
public string startDate {get; set;}
public string expirationDate {get; set;}
public string endDate {get; set;}
public string notificationDate {get; set;}
public string stateComment {get; set;}
public string allowTracing {get; set;}
public string allowTracingTill {get; set;}
}
public class SubscriptionListResult
{
public List<Subscription> Value { get; set; }
}