-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
260 lines (208 loc) · 9.14 KB
/
Program.cs
File metadata and controls
260 lines (208 loc) · 9.14 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using CommandLine;
namespace AzureCLI_Extractor
{
[Verb("adduser", HelpText = "Adds a Global Admin User")]
class AddUserOptions
{
[Option('d', "displayname", Required = true, HelpText = "User display name.")]
public string DisplayName { get; set; }
[Option('u', "username", Required = true, HelpText = "Account username.")]
public string Username { get; set; }
[Option('a', "accountprincipal", Required = true, HelpText = "The account principal name. It should be something like user@company.onmicrosoft.com / user@company.com .")]
public string UserPrincipal { get; set; }
[Option('p', "password", Required = true, HelpText = "Account password.")]
public string Password { get; set; }
}
[Verb("gettoken", HelpText = "Gets the user access token.")]
class TokenOptions
{
[Option('v', "verbose", Required = false, HelpText = "Print entire response.")]
public bool Verbose { get; set; }
}
class Program
{
static string AccessToken;
static string RefreshToken;
static string ClientId;
static string TenantId;
static string Authority;
static string Resource;
static string CallApiMethod(string method, string endpoint, string accessToken, string body, string contentType)
{
string graphUrl = string.Format("https://graph.microsoft.com/v1.0/{0}", endpoint);
WebRequest request = WebRequest.Create(graphUrl);
request.Method = method;
request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken));
if (contentType != null)
{
request.ContentType = contentType;
}
if (body != null)
{
byte[] data = Encoding.ASCII.GetBytes(body);
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
if (response.StatusCode.ToString() == "OK" || response.StatusCode.ToString() == "Created" || response.StatusCode.ToString() == "NoContent")
{
return responseFromServer;
}
else
{
Console.WriteLine(response.StatusCode);
Console.WriteLine(responseFromServer);
return null;
}
}
static string GetUpdatedAccessToken()
{
Console.WriteLine("Requesting token");
WebRequest request = WebRequest.Create(Authority + "/oauth2/token");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("client-request-id", "01d1sa19-1337-42d8-1337-82c0d8d3c43e");
request.Headers.Add("Accept-Charset", "UTF-8");
String postdata = String.Format("grant_type=refresh_token&client_id={0}&resource=https%3A%2F%2Fgraph.microsoft.com%2F&refresh_token={1}", ClientId, RefreshToken);
byte[] data = Encoding.ASCII.GetBytes(postdata);
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
// Retrieve the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode.ToString() != "OK")
{
Console.WriteLine(response.StatusDescription);
Console.WriteLine("Failed to retrieve the Access Token, exiting.");
return null;
}
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
dynamic JsonData = JsonConvert.DeserializeObject(responseFromServer);
// Console.WriteLine(JsonData.access_token);
Console.WriteLine("Token Retrieved Successfully");
return JsonData.access_token;
}
static bool ReadAzureFile(string path)
{
String data;
if (path == null)
{
Console.Write("No path specified, using the default one: ");
string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1];
path = String.Format("C:\\Users\\{0}\\.azure\\accessTokens.json", username);
Console.WriteLine(path);
data = File.ReadAllText(path);
}
else
{
Console.Write(String.Format("Using Path: {0}", path));
data = File.ReadAllText(path);
}
dynamic jsonData = JsonConvert.DeserializeObject(data);
foreach (dynamic temp in jsonData)
{
string tempResource = temp.resource;
if (tempResource.Equals("https://graph.microsoft.com/"))
{
AccessToken = temp.accessToken;
RefreshToken = temp.refreshToken;
Resource = temp.resource;
ClientId = temp._clientId;
Authority = temp._authority;
TenantId = Authority.Split('/')[3];
return true;
}
}
return false;
}
static bool CreateGlobalAdminUser(string displayName, string userName, string userPrincipal, string userPassword) {
string UserId;
dynamic jsonData;
string responseData;
string RoleId = null;
string postdata = @"{{
""accountEnabled"": true,
""displayName"": ""{0}"",
""mailNickname"": ""{1}"",
""userPrincipalName"": ""{2}"",
""passwordProfile"" : {{
""forceChangePasswordNextSignIn"": false,
""password"": ""{3}""
}}
}}";
postdata = String.Format(postdata, displayName, userName, userPrincipal, userPassword);
// Create User
responseData = CallApiMethod("POST", "users", AccessToken, postdata, "application/json");
jsonData = JsonConvert.DeserializeObject(responseData);
UserId = jsonData.id;
Console.WriteLine(UserId);
responseData = CallApiMethod("GET", "directoryRoles", AccessToken, null, null);
jsonData = JsonConvert.DeserializeObject(responseData);
foreach (dynamic temp in jsonData.value)
{
string roleName = temp.displayName;
if (roleName.Equals("Company Administrator"))
{
RoleId = temp.id;
Console.WriteLine(RoleId);
}
}
if (RoleId == null)
{
Console.WriteLine("Failed to find the Global Administrators Role ID");
return false;
}
string addMemberEndpoint = String.Format("/directoryRoles/{0}/members/$ref", RoleId);
postdata = @"{{
""@odata.id"": ""https://graph.microsoft.com/v1.0/directoryObjects/{0}""
}}
";
postdata = String.Format(postdata, UserId);
CallApiMethod("POST", addMemberEndpoint, AccessToken, postdata, "application/json");
Console.WriteLine("User with id {0} was added successfully to the Global Administrator Group", UserId);
return true;
}
static int Main(string[] args)
{
return CommandLine.Parser.Default.ParseArguments<AddUserOptions,TokenOptions>(args)
.MapResult(
(AddUserOptions opts) => AddUser(opts),
(TokenOptions opts) => GetToken(opts),
errs => 1);
}
static int GetToken(TokenOptions tokenOptions)
{
ReadAzureFile(null);
AccessToken = GetUpdatedAccessToken();
Console.WriteLine("Access token : {0}",AccessToken);
return 0;
}
static int AddUser(AddUserOptions opts)
{
ReadAzureFile(null);
AccessToken = GetUpdatedAccessToken();
Console.WriteLine("Creating Global Administrator with provided information.");
CreateGlobalAdminUser(opts.DisplayName, opts.Username, opts.UserPrincipal, opts.Password);
return 0;
}
}
}