-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
91 lines (76 loc) · 2.73 KB
/
Program.cs
File metadata and controls
91 lines (76 loc) · 2.73 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
using CommandLine;
using System;
using System.IO;
using System.Linq;
using rajapet.Apple;
namespace rajapet.isuserinapple
{
class Program
{
static int Main(string[] args)
{
return Parser.Default.ParseArguments<CommandLineOptions>(args)
.MapResult((CommandLineOptions opts) =>
{
try
{
return MatchEmail(opts.EmailAddress);
}
catch
{
Console.WriteLine("Error!");
return -3; // Unhandled error
}
},
errs => -1); // Invalid arguments
}
private static int MatchEmail(string EmailAddress)
{
var service = GetService();
var user = service.FindUser(EmailAddress);
if (user != null)
{
var roles = string.Join(", ", user.Roles.Select(s => s.ToString()).ToArray());
Console.WriteLine($"Found user: {EmailAddress}, {user.FirstName} {user.LastName} [{roles}]");
return 0;
}
else
{
Console.WriteLine($"User: {EmailAddress} not found");
return 1;
}
}
private static AppStoreApiService GetService()
{
var p = new Program();
var configFile = p.GetConfigSettings();
var privateKey = p.GetPrivateKey(configFile);
var appleJWT = new AppleJWT();
var token = appleJWT.GetToken(configFile.KeyID, configFile.IssuerID, privateKey);
return new AppStoreApiService(token);
}
/// <summary>
/// Reads a configuration file from the same folder as the executable
/// </summary>
/// <returns></returns>
private ConfigSettings GetConfigSettings()
{
var configFileName = Path.Combine(System.AppContext.BaseDirectory, "IsUserinApple.json");
return new ConfigSettings(configFileName);
}
/// <summary>
/// Read the PEMS files specfied by configSettings.PrivateKeyFile and return the private key
/// </summary>
/// <param name="configSettings"></param>
/// <returns>Private key in Base64 encoding</returns>
private string GetPrivateKey(ConfigSettings configSettings)
{
var certPEM = File.ReadAllText(configSettings.PrivateKeyFile);
return certPEM
.Replace("-----BEGIN PRIVATE KEY-----", "")
.Replace("\n", "")
.Replace("\r", "")
.Replace("-----END PRIVATE KEY-----", "");
}
}
}