-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
204 lines (179 loc) · 10.5 KB
/
Program.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
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
using System;
using System.IO;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.EnvironmentVariables;
using System.Reflection;
using Newtonsoft.Json.Linq;
namespace QuickTeams
{
class Program
{
// all of your per-tenant and per-environment settings are (now) in appsettings.json
public static IConfigurationRoot Configuration { get; set; }
// Don't change this constant
// It is a constant that corresponds to fixed values in AAD that corresponds to Microsoft Graph
// Required Permissions - Microsoft Graph -> API
// Read all users' full profiles
// Read and write all groups
const string aadResourceAppId = "00000003-0000-0000-c000-000000000000";
static AuthenticationContext authenticationContext = null;
static AuthenticationResult authenticationResult = null;
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
// retreive settings from appsettings.json instead of hard coding them here
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
string commandString = string.Empty;
Console.WriteLine("");
Console.WriteLine("****************************************************************************************************");
Console.WriteLine("Welcome to Quick Teams!");
Console.WriteLine("****************************************************************************************************");
Console.WriteLine("");
while (Configuration["AzureAd:TenantId"] == "" || Configuration["AzureAd:ClientId"] == "")
{
if (args.Length == 2)
{
Configuration["AzureAd:TenantId"] = args[0];
Configuration["AzureAd:ClientId"] = args[1];
}
else
{
Console.WriteLine("");
Console.WriteLine("****************************************************************************************************");
Console.WriteLine("You need to provide your Azure Active Directory Tenant Name and the Application ID you created for");
Console.WriteLine("use with application to continue. You can do this by altering Program.cs and re-compiling this app.");
Console.WriteLine("Or, you can provide it right now.");
Console.Write("Azure Active Directory Tenant Name (i.e your-domain.onmicrosoft.com): ");
Configuration["AzureAd:TenantId"] = Console.ReadLine();
Console.Write("Azure Active Directory Application ID: ");
Configuration["AzureAd:ClientId"] = Console.ReadLine();
Console.WriteLine("****************************************************************************************************");
}
}
Console.WriteLine("**************************************************");
Console.WriteLine("Tenant is " + (Configuration["AzureAd:TenantId"]));
Console.WriteLine("Application ID is " + (Configuration["AzureAd:ClientId"]));
Console.WriteLine("Redirect URI is " + (Configuration["AzureAd:AadRedirectUri"]));
Console.WriteLine("**************************************************");
Console.WriteLine("");
Console.WriteLine("****************************************************************************************************");
Console.WriteLine("Your tenant admin consent URL is https://login.microsoftonline.com/common/oauth2/authorize?response_type=id_token" +
"&client_id=" + Configuration["AzureAd:ClientId"] + "&redirect_uri=" + Configuration["AzureAd:AadRedirectUri"] + "&prompt=admin_consent" + "&nonce=" + Guid.NewGuid().ToString());
Console.WriteLine("****************************************************************************************************");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("****************************************************************************************************");
Console.WriteLine("Let's get started! Sign in to Microsoft with your Teams credentials:");
authenticationResult = UserLogin();
var aadAccessToken = authenticationResult.AccessToken;
if (String.IsNullOrEmpty(authenticationResult.AccessToken))
{
Console.WriteLine("Something went wrong. Please try again!");
Environment.Exit(1);
}
else
{
Console.WriteLine("You've successfully signed in. Welcome " + authenticationResult.UserInfo.DisplayableId);
}
var sourceTeamId = Utils.Teams.SelectJoinedTeam(aadAccessToken);
dynamic sourceGroupObject = JObject.Parse(Utils.Groups.GetGroupDetails("",sourceTeamId, aadAccessToken));
string sourceTeamName = sourceGroupObject.displayName;
while (!commandString.Equals("Exit", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("Selected Team: {0} ", sourceTeamName);
Console.Write("Enter command ( apps | clone | archive | unarchive | download | delete | channel | switch | exit ) > ");
commandString = Console.ReadLine();
switch (commandString.ToUpper())
{
case "APPS":
AppsCommands(sourceTeamId, sourceTeamName, aadAccessToken);
break;
case "CLONE":
Utils.Teams.CloneTeam(sourceTeamId, aadAccessToken);
break;
case "ARCHIVE":
Utils.Teams.ArchiveTeam(sourceTeamId, aadAccessToken);
break;
case "UNARCHIVE":
Utils.Teams.UnArchiveTeam(sourceTeamId, aadAccessToken);
break;
case "DOWNLOAD":
Utils.Teams.DownloadTeam(sourceTeamId, aadAccessToken);
break;
case "CHANNEL":
Utils.Channels.SelectChannel(sourceTeamId, aadAccessToken);
break;
case "DELETE":
Utils.Groups.DeleteGroup(sourceTeamId, aadAccessToken);
Console.WriteLine("Since you deleted the Team {0}, you need to select a new Team.", sourceTeamName);
dynamic deleteGroupsObject = JObject.Parse(Utils.Groups.GetGroupDetails("",sourceTeamId, aadAccessToken));
sourceTeamName = deleteGroupsObject.displayName;
break;
case "SWITCH":
sourceTeamId = Utils.Teams.SelectJoinedTeam(aadAccessToken);
dynamic switchGroupsObject = JObject.Parse(Utils.Groups.GetGroupDetails("",sourceTeamId, aadAccessToken));
sourceTeamName = switchGroupsObject.displayName;
break;
case "EXIT":
Console.WriteLine("Bye!");
break;
default:
Console.WriteLine("Invalid command.");
break;
}
}
}
static void AppsCommands(string sourceTeamId, string sourceTeamName, string aadAccessToken)
{
string appsCommandString = string.Empty;
while (!appsCommandString.Equals("Back", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("Selected Team: {0}", sourceTeamName);
Console.Write("Enter apps command ( list | add | delete | back ) > ");
appsCommandString = Console.ReadLine();
switch (appsCommandString.ToUpper())
{
case "LIST":
Utils.Apps.ListApps(sourceTeamId, aadAccessToken);
break;
case "ADD":
Console.Write("Enter the ID of the app you want to add: ");
var appIdToAdd = Console.ReadLine();
Utils.Apps.AddApp(sourceTeamId, appIdToAdd, aadAccessToken);
break;
case "DELETE":
Console.WriteLine("Delete has certain \"limitations\"");
Console.WriteLine("installedAndPermanent apps can't be deleted");
Console.WriteLine("installed apps that are teamsOwned will become installedAndHidden");
Console.Write("Enter the ID of the app you want to delete: ");
var appIdToDelete = Console.ReadLine();
Utils.Apps.DeleteApp(sourceTeamId, appIdToDelete, aadAccessToken);
break;
case "BACK":
Console.WriteLine("Going back!");
break;
default:
Console.WriteLine("Invalid command.");
break;
}
}
return;
}
static AuthenticationResult UserLogin()
{
authenticationContext = new AuthenticationContext
(String.Format(CultureInfo.InvariantCulture, Configuration["AzureAd:AadInstance"], Configuration["AzureAd:TenantId"]));
authenticationContext.TokenCache.Clear();
DeviceCodeResult deviceCodeResult = authenticationContext.AcquireDeviceCodeAsync(aadResourceAppId, (Configuration["AzureAd:ClientId"])).Result;
Console.WriteLine(deviceCodeResult.Message);
return authenticationContext.AcquireTokenByDeviceCodeAsync(deviceCodeResult).Result;
}
}
}