This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRunner.cs
More file actions
1030 lines (929 loc) · 43.9 KB
/
Runner.cs
File metadata and controls
1030 lines (929 loc) · 43.9 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Copyright 2019 Centrify Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Net;
namespace CentrifyCLI
{
/// <summary>The Centrify-specific logic and knowledge.</summary>
class Runner
{
// Constants
private const string FAILED = "FAILED: ";
public static readonly TimeSpan SWAGGER_REFRESH = TimeSpan.FromDays(15);
/// <summary>Result codes for REST calls</summary>
public enum ResultCode { Success, Redirected, Unauthorized, NotFound, Exception, Failed, Timeout};
/// <summary>Centrify service URL.</summary>
private string m_url ="";
/// <summary>List of APIs for the drop-down/easy reference.<para/>
/// These each have a structure of Group/Api/Path/Reference/Sample.</summary>
private JObject m_apiCalls = new JObject();
/// <summary>Use the one restUpdater for the entire short session</summary>
private RestUpdater m_restUpdater = new RestUpdater();
/// <summary>Record that can be stored as a server profile.</summary>
[Serializable]
public class ServerProfile
{
/// <summary>What to call this connection.</summary>
public string NickName;
/// <summary>Tenant URL</summary>
public string URL;
/// <summary>OAuth application id</summary>
public string OAuthAppId;
/// <summary>User name for authentication</summary>
public string UserName;
/// <summary>User password for authentication</summary>
public string Password;
/// <summary>OAuth token</summary>
[JsonIgnore]
public string OAuthToken;
/// <summary>Machine Scope to use when using DMC</summary>
public string MachineScope;
public override string ToString()
{
string name = (String.IsNullOrEmpty(NickName) ? "-default-" : NickName);
return $"Profile {name}: URL: '{URL}' AppId: '{OAuthAppId}' User: '{UserName}' MachineScope: '{MachineScope}'";
}
}
/// <summary>Available server profiles, by name</summary>
public Dictionary<string, ServerProfile> ServerList { get; private set; } = new Dictionary<string, ServerProfile>();
/// <summary>Parse contents from a REST response into JObject, handling errors.</summary>
private JObject ParseContentsToJObject(string contents)
{
if (string.IsNullOrWhiteSpace(contents))
{
return null;
}
JObject result = null;
try
{
result = JObject.Parse(contents);
}
catch (Exception e)
{
result = new JObject()
{
{ "success", false },
{ "contents", contents },
{ "exception", e.Message },
};
}
return result;
}
// Roughly: http://stackoverflow.com/questions/3404421/password-masking-console-application
public static string ReadMaskedPassword(bool prompt)
{
ConsoleKeyInfo key;
string password = null;
if (prompt) Console.Write("Password: ");
do
{
// Read a character without echoing it
key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
password += key.KeyChar;
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && password != null && password.Length > 0)
{
password = password.Substring(0, password.Length - 1);
Console.Write("\b \b");
}
}
} while (key.Key != ConsoleKey.Enter);
Console.WriteLine();
return password;
}
/// <summary>Authenticate the user via username/password/etc.<para/>
/// OAuth token is the preferred method of authenication; this exists for ease of use</summary>
/// <param name="config">Contains config info, including profile to use</param>
/// <param name="timeout">Timeout is milliserconds for REST calls</param>
/// <returns>Tuple of success or failure reason and message or results.</returns>
public Tuple<ResultCode, string> Authenticate(CentrifyCliConfig config, int timeout)
{
ServerProfile profile = config.Profile;
try
{
if (string.IsNullOrWhiteSpace(profile.UserName))
{
return new Tuple<ResultCode, string>(ResultCode.NotFound, $"No user in config to authenticate.");
}
InitializeClient(profile.URL);
// Do StartAuthentication
string endpoint = "/Security/StartAuthentication";
Dictionary<string, string> args = new Dictionary<string, string>()
{
{ "User", profile.UserName },
{ "Version", "1.0" }
};
var authTask = PlaceCall(endpoint, JsonConvert.SerializeObject(args));
if (!authTask.Wait(timeout))
{
return new Tuple<ResultCode, string>(ResultCode.Timeout, $"Request timed out ({endpoint}).");
}
Tuple<Runner.ResultCode, string> callResult = authTask.Result;
if (callResult.Item1 != ResultCode.Success)
{
return new Tuple<ResultCode, string>(ResultCode.Failed, MakeFailResult(callResult.Item2, $"Authentication request failed ({endpoint}): {callResult.Item1}"));
}
// Remember session and tenant
JObject resultSet = ParseContentsToJObject(callResult.Item2);
if (!TryGetJObjectValue(resultSet, "Result", out JObject results))
{
throw new ArgumentException($"Authentication results have no 'result' property ({endpoint}):\n{ResultToString(resultSet)}", "Result");
}
if (TryGetJObjectValue(results, "Redirect", out string redirect))
{
Ccli.ConditionalWrite($"Authentication is redirected, use the prefered URL: {redirect}", config.Silent);
return new Tuple<ResultCode, string>(ResultCode.Redirected, redirect);
}
if (!TryGetJObjectValue(results, "SessionId", out string sessionId))
{
throw new ArgumentException($"Authentication results are missing 'SessionId' ({endpoint}): {ResultToString(results)}", "SessionId");
}
if (!TryGetJObjectValue(results, "TenantId", out string tenantId))
{
throw new ArgumentException($"Authentication results are missing 'TenantId' ({endpoint}): {ResultToString(results)}", "TenantId");
}
if (!TryGetJObjectValue(results, "Challenges", out JToken challenges))
{
throw new ArgumentException($"Authentication results are missing 'Challenges' ({endpoint}): {ResultToString(results)}", "Challenges");
}
// If pw was supplied, and is one of the first mechs, use what was supplied automagically
int passwordMechIdx = -1;
if (profile.Password != null)
{
// Present the option(s) to the user
for (int mechIdx = 0; mechIdx < challenges[0]["Mechanisms"].Children().Count() && passwordMechIdx == -1; mechIdx++)
{
if (challenges[0]["Mechanisms"][mechIdx]["Name"].Value<string>() == "UP")
{
passwordMechIdx = mechIdx;
}
}
}
// Need to satisfy at least 1 challenge in each collection:
for (int x = 0; x < challenges.Children().Count(); x++)
{
int optionSelect = -1;
// If passwordMechIdx is set, we should auto-fill password first, do it
if (passwordMechIdx == -1)
{
// Present the option(s) to the user
for (int mechIdx = 0; mechIdx < challenges[x]["Mechanisms"].Children().Count(); mechIdx++)
{
Console.WriteLine("Option {0}: {1}", mechIdx, MechToDescription(challenges[x]["Mechanisms"][mechIdx]));
}
if (challenges[x]["Mechanisms"].Children().Count() == 1)
{
optionSelect = 0;
}
else
{
while (optionSelect < 0 || optionSelect > challenges[x]["Mechanisms"].Children().Count())
{
Console.Write("Select option and press enter: ");
string optEntered = Console.ReadLine();
int.TryParse(optEntered, out optionSelect);
}
}
}
else
{
optionSelect = passwordMechIdx;
passwordMechIdx = -1;
}
try
{
var newChallenges = AdvanceForMech(timeout, tenantId, sessionId, challenges[x]["Mechanisms"][optionSelect], config);
if(newChallenges != null)
{
challenges = newChallenges;
x = -1;
}
}
catch (Exception ex)
{
return new Tuple<ResultCode, string>(ResultCode.Failed, ex.Message);
}
}
}
catch (Exception e)
{
Exception i = e;
string allMessages = "";
// De-dup; sometimes they double up.
while (i != null)
{
if (!allMessages.Contains(i.Message)) allMessages += i.Message + " " + Environment.NewLine;
i = i.InnerException;
}
return new Tuple<ResultCode, string>(ResultCode.Exception, allMessages);
}
m_url = profile.URL;
return new Tuple<ResultCode, string>(ResultCode.Success, "");
}
private dynamic AdvanceForMech(int timeout, string tenantId, string sessionId, dynamic mech, CentrifyCliConfig config)
{
ServerProfile profile = config.Profile;
Dictionary<string, dynamic> advanceArgs = new Dictionary<string, dynamic>();
advanceArgs["TenantId"] = tenantId;
advanceArgs["SessionId"] = sessionId;
advanceArgs["MechanismId"] = mech["MechanismId"];
advanceArgs["PersistentLogin"] = false;
bool autoFillPassword = (mech["Name"] == "UP" && profile.Password != null);
// Write prompt (unless auto-filling)
if (!autoFillPassword)
{
Console.Write(MechToPrompt(mech));
}
// Read or poll for response. For StartTextOob we simplify and require user to enter the response, rather
// than simultaenously prompting and polling, though this can be done as well.
dynamic result = null;
string answerType = mech["AnswerType"];
switch (answerType)
{
case "Text":
case "StartTextOob":
{
if (answerType == "StartTextOob")
{
// First we start oob, to get the mech activated
advanceArgs["Action"] = "StartOOB";
if(!PlaceCall("/security/advanceauthentication", JsonConvert.SerializeObject(advanceArgs)).Wait(timeout))
{
throw new ApplicationException("Request timed out (/security/advanceauthentication)");
}
}
// Now prompt for the value to submit and do so
string promptResponse = null;
if (autoFillPassword)
{
promptResponse = profile.Password;
}
else
{
promptResponse = ReadMaskedPassword(false);
}
advanceArgs["Answer"] = promptResponse;
advanceArgs["Action"] = "Answer";
result = SimpleCall(timeout, "/security/advanceauthentication", advanceArgs);
if(result["Result"]["Summary"] == "NewPackage")//-V3080
{
return result["Result"]["Challenges"];
}
if (!(result["Result"]["Summary"] == "StartNextChallenge" ||
result["Result"]["Summary"] == "LoginSuccess"))
{
throw new ApplicationException(result["Message"]);
}
}
break;
case "StartOob":
// Pure out of band mech, simply poll until complete or fail
advanceArgs["Action"] = "StartOOB";
SimpleCall(timeout, "/security/advanceauthentication", advanceArgs);
// Poll
advanceArgs["Action"] = "Poll";
do
{
Console.Write(".");
result = SimpleCall(timeout, "/security/advanceauthentication", advanceArgs);
System.Threading.Thread.Sleep(1000);
} while (result["Result"]["Summary"] == "OobPending");//-V3080
// We are done polling, did it work ?
if (result["Result"]["Summary"] == "NewPackage")
{
return result["Result"]["Challenges"];
}
if (!(result["Result"]["Summary"] == "StartNextChallenge" ||
result["Result"]["Summary"] == "LoginSuccess"))
{
throw new ApplicationException(result["Message"]);
}
break;
}
// On success need to save the access token if present for future API calls
if ((result != null) && (result["Result"]["Summary"] == "LoginSuccess") && (result["Result"]["OAuthTokens"] != null))
{
string token = result["Result"]["OAuthTokens"]["access_token"];
if (!string.IsNullOrWhiteSpace(token))
{
m_restUpdater.AuthValue = token;
Ccli.ConditionalWrite($"Authentication success, acquired access token from response.", config.Silent);
}
}
return null;
}
public const string OneTimePassCode = "OTP";
public const string OathPassCode = "OATH";
public const string PhoneFactor = "PF";
public const string Sms = "SMS";
public const string Email = "EMAIL";
public const string PasswordReset = "RESET";
public const string SecurityQuestion = "SQ";
private static string MechToDescription(dynamic mech)
{
string mechName = mech["Name"];
try
{
return mech["PromptSelectMech"];
}
catch { /* Doesn't support this property */ }
switch (mechName)
{
case "UP":
return "Password";
case "SMS":
return string.Format("SMS to number ending in {0}", mech["PartialDeviceAddress"]);
case "EMAIL":
return string.Format("Email to address ending with {0}", mech["PartialAddress"]);
case "PF":
return string.Format("Phone call to number ending with {0}", mech["PartialPhoneNumber"]);
case "OATH":
return string.Format("OATH compatible client");
case "SQ":
return string.Format("Security Question");
default:
return mechName;
}
}
private static string MechToPrompt(dynamic mech)
{
string mechName = mech["Name"];
try
{
string servicePrompt = mech["PromptMechChosen"];
if(!string.IsNullOrEmpty(servicePrompt))
{
if (!servicePrompt.EndsWith(":"))
{
return servicePrompt + ": ";
}
else
{
return servicePrompt + " ";
}
}
}
catch { /* Doesn't support this property */ }
switch (mechName)
{
case "UP":
return "Password: ";
case "SMS":
return string.Format("Enter the code sent via SMS to number ending in {0}: ", mech["PartialDeviceAddress"]);
case "EMAIL":
return string.Format("Please click or open the link sent to the email to address ending with {0}.", mech["PartialAddress"]);
case "PF":
return string.Format("Calling number ending with {0}, please follow the spoken prompt.", mech["PartialPhoneNumber"]);
case "OATH":
return string.Format("Enter your current OATH code: ");
case "SQ":
return string.Format("Enter the response to your secret question: ");
default:
return mechName;
}
}
/// <summary>Call the rest endpoint with the JSON.<para/>
/// Authentication has already set the URL.</summary>
/// <param name="endpoint">Path to REST call, starts with a slash.</param>
/// <param name="json">Json payload</param>
/// <returns>Task with the result code and payload of the REST call</returns>
public async Task<Tuple<ResultCode, string>> PlaceCall(string endpoint, string json)
{
RestUpdater.RESTCall restCall = new RestUpdater.RESTCall()
{
Endpoint = endpoint,
JsonTemplate = json
};
HttpResponseMessage response = null;
try
{
response = await m_restUpdater.NewRequestAsync(m_url, restCall);
}
catch (System.Threading.Tasks.TaskCanceledException)
{
return new Tuple<ResultCode, string>(ResultCode.Timeout, $"Request timed out or canceled ({endpoint}).");
}
HttpStatusCode statusCode = response.StatusCode;
string contents = await response.Content.ReadAsStringAsync();
switch (statusCode)
{
case HttpStatusCode.OK:
return new Tuple<ResultCode, string>(ResultCode.Success, contents);
case HttpStatusCode.Unauthorized:
return new Tuple<ResultCode, string>(ResultCode.Unauthorized, $"Access to {endpoint} not allowed.");
case HttpStatusCode.NotFound:
return new Tuple<ResultCode, string>(ResultCode.NotFound, $"Endpoint {endpoint} not found.");
default:
return new Tuple<ResultCode, string>(ResultCode.Failed, $"Unexpected response status from {endpoint}: {statusCode} - {contents}");
}
}
// Wraps PlaceCall to simplify calling pattern:
// Throws exception on API fail or timeout (see ex.Message)
// Returns Result object as simple Dictionary
public dynamic SimpleCall(int timeout, string endpoint, object args)
{
var call = PlaceCall(endpoint, JsonConvert.SerializeObject(args));
if (!call.Wait(timeout))
{
throw new ApplicationException($"Request timed out ({endpoint})");
}
var callResult = call.Result;
if (callResult.Item1 != ResultCode.Success)
{
throw new ApplicationException(MakeFailResult(callResult.Item2, $"API call failed ({endpoint}): {callResult.Item1}"));
}
return ParseContentsToJObject(callResult.Item2);
}
/// <summary>Returns the path to a user file.</summary>
/// <param name="fileName">filename to generate path for</param>
/// <returns>Platform appropriate path string from the environmental settings</returns>
public static string GetUserFilePath(string filename)
{
string userDir = null;
if ((Environment.OSVersion.Platform == PlatformID.Unix) || (Environment.OSVersion.Platform == PlatformID.MacOSX))
{
userDir = Environment.GetEnvironmentVariable("HOME") + "/";
}
else
{
userDir = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\";
}
return userDir + filename;
}
/// <summary>Load server profiles.</summary>
/// <param name="fileName">File to load server profiles from</param>
/// <param name="silent">Process logging disabled</param>
/// <returns>Number of profiles loaded</returns>
public int LoadServerList(string fileName, bool silent)
{
if (File.Exists(fileName))
{
try
{
string importJson = System.IO.File.ReadAllText(fileName);
ServerList = JsonConvert.DeserializeObject<Dictionary<string, ServerProfile>>(importJson);
}
catch (Exception e)
{
Ccli.ConditionalWrite($"Error parsing Server Profiles from {fileName}: {e.Message}", silent);
return -1;
}
}
return ServerList.Count;
}
/// <summary>Save configured server profiles.</summary>
/// <param name="fileName">File to save server profiles to</param>
/// <returns></returns>
public void SaveServerList(string fileName)
{
using (StreamWriter sw = new StreamWriter(fileName))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(sw, ServerList);
}
}
/// <summary>Get latest swagger definitions from cloud.</summary>
/// <param name="config">Contains config info, including profile to use</param>
/// <returns>Success or failure</returns>
public bool GetSwagger(CentrifyCliConfig config)
{
try
{
Ccli.ConditionalWrite($"Fetching latest swagger definitions from cloud.", config.Silent);
// Doesn't require auth
InitializeClient(config.Profile.URL);
var runTask = PlaceCall("/vfslow/lib/api/swagger.json", "");
runTask.Wait();
Tuple<Runner.ResultCode, string> callResult = runTask.Result;
if (callResult.Item1 == Runner.ResultCode.Success)
{
// Write item2 to swagger.json file. There's no JSON to process.
using (StreamWriter file = File.CreateText(config.SwaggerDirectory))
{
file.Write(callResult.Item2);
}
return true;
}
else
{
Ccli.WriteErrorText($"Error fetching swagger definitions from cloud: {callResult}");
}
}
catch (Exception e)
{
Ccli.WriteErrorText($"Exception fetching swagger definitions from cloud: {e.Message}");
}
return false;
}
/// <summary>Loads the swagger.json file from, typically, depot2\Cloud\Lib\Api\swagger.json
/// Builds API Resource from it.</summary>
/// <param name="config">Contains config info, including profile to use</param>
/// <returns>Success or failure</returns>
public bool LoadSwagger(CentrifyCliConfig config)
{
string swaggerPath = config.SwaggerDirectory;
if (String.IsNullOrEmpty(swaggerPath))
{
Ccli.WriteErrorText("No swagger path defined in config.");
return false;
}
Ccli.ConditionalWrite($"Loading swagger definitions from {swaggerPath}", config.Silent);
bool exists = File.Exists(swaggerPath);
if ((!exists) || (File.GetCreationTimeUtc(swaggerPath) < (DateTime.UtcNow - SWAGGER_REFRESH)))
{
// Fetch from cloud if no swagger or swagger is 'old'
if (!GetSwagger(config))
{
if (exists)
{
Ccli.ConditionalWrite($"Using existing swagger defintiions.", config.Silent);
}
else
{
Ccli.WriteErrorText($"No swagger definitions available from cloud.");
return false;
}
}
}
JObject swagSet = null;
try
{
using (StreamReader file = File.OpenText(swaggerPath))
using (JsonTextReader reader = new JsonTextReader(file))
{
swagSet = (JObject)JToken.ReadFrom(reader);
}
}
catch (Exception e)
{
Ccli.WriteErrorText($"Error loading swagger definitions from {swaggerPath}: {e.Message}");
return false;
}
JArray calls = new JArray();
foreach (JProperty path in swagSet["paths"])
{
JProperty restPath = new JProperty("path", (string)path.Name);
JProperty group = new JProperty("group", (string)path.First["post"]["tags"].First);
string[] pathBits = ((string)path.Name).Split(new char[] { '/' });
JProperty desc = new JProperty("api", pathBits[pathBits.Length - 1]);
JProperty reference = new JProperty("reference", (string)path.First["post"]["summary"]);
string parameters = "{";
int paramCount = 0;
JToken pathParams = path.First["post"]["parameters"].First;
if (pathParams != null)
{
try
{
foreach (JProperty prop in pathParams["schema"]["properties"])
{
if (paramCount++ > 0)
{
parameters += ",\n";
}
parameters += " \"" + (string)prop.Name + "\": \"\"";
}
}
catch
{
try
{
foreach (JToken tok in pathParams.First)
{
if (tok is JProperty prop)
{
if (paramCount++ > 0)
{
parameters += ",\n";
}
parameters += " \"" + (string)prop + "\": \"\"";
}
}
}
catch (Exception e)
{
Ccli.WriteErrorText($"Error parsing swagger properties from {swaggerPath}: {e.Message}");
return false;
};
}
}
if (paramCount > 0)
{
parameters += "\n";
}
parameters += "}";
JProperty sample = new JProperty("sample", parameters);
JObject thisCall = new JObject
{
restPath, // path == REST endpoint
sample, // parameters
group, // Grouping of calls
reference, // Reference (not really API, misnamed)
desc // Name of call
};
calls.Add(thisCall);
}
m_apiCalls = new JObject();
JProperty callWrapper = new JProperty("apis", calls);
m_apiCalls.Add(callWrapper);
return true;
}
/// <summary>Locate the specified API. Could put in dict instead, but this is fine for current numbers.</summary>
/// <param name="groupAndName"></param>
/// <returns>API group object</returns>
public JObject FindAPI(string groupAndName)
{
foreach (JObject jo in m_apiCalls["apis"])
{
if ((jo["group"] + ":" + jo["api"]).CompareTo(groupAndName) == 0)
{
return jo;
}
}
return null;
}
/// <summary>JObject to string</summary>
/// <param name="jo">JObject</param>
/// <returns>String version of JObject</returns>
private String ApiJObjectToString(JObject jo)
{
StringBuilder sb = new StringBuilder();
return sb.Append(jo["path"] + ": " + jo["reference"] + "\n")
.Append(jo["sample"].ToString().Replace("{", "{\n").Replace("\\n", "\n")).Append("\n\n").ToString();
}
/// <summary>List all APIs.</summary>
/// <returns>API list</returns>
public string ListAPIs()
{
StringBuilder sb = new StringBuilder();
SortedSet<string> report = new SortedSet<string>();
foreach (JObject jo in m_apiCalls["apis"])
{
report.Add(ApiJObjectToString(jo));
}
foreach (string s in report)
{
sb.Append(s);
}
return sb.ToString();
}
/// <summary>Returns APIs with the specified substring in their path or summary or tag</summary>
/// <param name="subset"></param>
/// <returns>API list</returns>
public string FindAPIMatch(string subset)
{
SortedSet<string> report = new SortedSet<string>();
foreach (JObject jo in m_apiCalls["apis"])
{
if ((jo["group"].ToString().Contains(subset, StringComparison.InvariantCultureIgnoreCase))
|| (jo["path"].ToString().Contains(subset, StringComparison.InvariantCultureIgnoreCase))
|| (jo["reference"].ToString().Contains(subset, StringComparison.InvariantCultureIgnoreCase))
|| (jo["api"].ToString().Contains(subset, StringComparison.InvariantCultureIgnoreCase))
)
{
report.Add(ApiJObjectToString(jo));
}
}
StringBuilder sb = new StringBuilder();
foreach (string s in report)
{
sb.Append(s);
}
return sb.ToString();
}
/// <summary>REST result (JObject) to string</summary>
/// <param name="result">JObject result</param>
/// <returns>String version of result</returns>
private static string ResultToString(JObject result)
{
if (result == null)
{
return string.Empty;
}
List<string> propNames = result.Properties().Select(prop => prop.Name).ToList();
foreach (string propName in propNames)
{
// Never remove "Result", always remove "IsSoftError" or null values
switch (propName)
{
case "Result":
break;
case "IsSoftError":
result.Remove(propName);
break;
default:
JToken val = result[propName];
if ((val == null) ||
(val.Type == JTokenType.Array && !val.HasValues) ||
(val.Type == JTokenType.Object && !val.HasValues) ||
(val.Type == JTokenType.String && String.IsNullOrEmpty(val.ToString())) ||
(val.Type == JTokenType.Null))
{
result.Remove(propName);
}
break;
}
}
return JsonConvert.SerializeObject(result, Formatting.Indented);
}
/// <summary>Finds nodes matching the string. </summary>
static List<string> FindNodes(JToken node, string target)
{
List<string> returns = new List<string>();
if (node.Type == JTokenType.Object)
{
foreach (JProperty child in node.Children<JProperty>())
{
if (child.Name == target)
returns.Add(child.Value.ToString());
returns.AddRange(FindNodes(child.Value, target));
}
}
else if (node.Type == JTokenType.Array)
{
foreach (JToken child in node.Children())
{
returns.AddRange(FindNodes(child, target));
}
}
return returns;
}
/// <summary>Fetch value of key from JObject, if key exists</summary>
/// <param name="obj">JObject</param>
/// <param name="key">Key name</param>
/// <param name="value">(out) Key value</param>
/// <returns>Value of key, or default value if key is not present</returns>
private bool TryGetJObjectValue<T>(JObject obj, string key, out T value)
{
if ((obj != null) && obj.TryGetValue(key, out JToken token))
{
value = token.ToObject<T>();
return true;
}
else
{
value = default(T);
return false;
}
}
/// <summary>Parses the output and returns success or failure and the string to write to the console.</summary>
/// <param name="restResults"></param>
/// <param name="extract">Variable to extract. If not specified, print the whole thing.</param>
/// <returns></returns>
public Tuple<bool, string> ParseResults(string restResults, string extract)
{
JObject resultSet;
try
{
resultSet = ParseContentsToJObject(restResults);
if (!TryGetJObjectValue(resultSet, "success", out bool success))
{
throw new ArgumentException($"Results are missing 'success': {ResultToString(resultSet)}", "success");
}
if (String.IsNullOrEmpty(extract))
return new Tuple<bool, string>(success, ResultToString(resultSet));
// else Just return the one value
List<string> returns = FindNodes(resultSet, extract);//-V3080
if (returns.Count == 0)
return new Tuple<bool, string>(success, "");
return new Tuple<bool, string>(success, string.Join("\n", returns));
}
catch (Exception e)
{
return new Tuple<bool, string>(false, MakeFailResult(restResults, "REST results could not be parsed.", e.Message));
}
}
/// <summary>Generate a failure result fpr a REST call.</summary>
/// <param name="result">JToken result</param>
/// <param name="message">Optional error message string</param>
/// <param name="exception">Optional exception string</param>
/// <returns>rResult string</returns>
public static string MakeFailResult(JToken result, string message = null, string exception = null)
{
JObject failResult = new JObject
{
["success"] = false,
["Message"] = (message != null ? FAILED + message : null),
["Exception"] = exception
};
return ResultToString(failResult);
}
/// <summary>generate a stromng of random characters.</summary>
/// <param name="chars">Number of characters</param>
/// <returns>Random string</returns>
public static string RandomString(int chars)
{
Random r = new Random();
string random = "";
for (int i = 0; i < chars; i++)
{
int x = r.Next(62);
random += (char)(x < 26?(char)(x+'a'):x<52?(char)(x-26+'A'):(char)x-52+'0');
}
return random;
}
/// <summary>Centrify-specific OAuth2 Token request</summary>
/// <param name="config">Contains config info, including profile to use</param>
/// <param name="timeout">Timeout is milliserconds for REST calls</param>
/// <returns>Tuple of success and either token or error message.</returns>
public async Task<Tuple<ResultCode, string>> OAuth2_GenerateTokenRequest(CentrifyCliConfig config, int timeout)
{
ServerProfile profile = config.Profile;
string TokenEndpoint = "/oauth2/token/" + profile.OAuthAppId;
if(profile.Password == null && !config.Silent)
{
Console.Write($"Enter password for {profile.UserName}: ");
profile.Password = ReadMaskedPassword(false);
}
string queryParams = $"grant_type=client_credentials&response_type=code&state={RandomString(15)}&scope=ccli&client_id={profile.UserName}&client_secret={profile.Password}";
try
{
Ccli.ConditionalWrite($"Requesting token for {profile.UserName}.", config.Silent);
InitializeClient(profile.URL);
Task<HttpResponseMessage> response = m_restUpdater.NewRequestAsync(profile.URL+ TokenEndpoint, queryParams);
if (!response.Wait(timeout))
{
return new Tuple<ResultCode, string>(ResultCode.Timeout, "Request for token timed out.");
}
HttpStatusCode statusCode = response.Result.StatusCode;
string contents = await response.Result.Content.ReadAsStringAsync();
JObject resultSet = ParseContentsToJObject(contents);
if (response.Result.StatusCode != HttpStatusCode.OK)
{
return new Tuple<ResultCode, string>(ResultCode.Failed, $"Token request failed/denied: {statusCode}, {ResultToString(resultSet)}");
}
if (!TryGetJObjectValue(resultSet, "access_token", out string accessToken))
{
throw new ArgumentException($"Token response is missing 'access_token': {ResultToString(resultSet)}", "access_token");
}
return new Tuple<ResultCode, string>(ResultCode.Success, accessToken);
}
catch (Exception ex)
{
return new Tuple<ResultCode, string>(ResultCode.Exception, ex.Message);
}
}
/// <summary>Accepts either the token or the entire response string, parses out the token.</summary>
/// <param name="response"></param>
/// <returns>Parsed token</returns>
public static string ParseToken(string response)
{
string tokenFlag = "access_token";
if (!response.Contains(tokenFlag))
{
return response;
}
string[] keys = response.Split(new char[]{ '&' });
// Length includes "="
return keys.First(k => k.StartsWith(tokenFlag)).Substring(tokenFlag.Length + 1);
}
/// <summary>Initialize a new rest client.</summary>