-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationConfig.cs
More file actions
117 lines (98 loc) · 4.34 KB
/
AuthenticationConfig.cs
File metadata and controls
117 lines (98 loc) · 4.34 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Extensions.Configuration;
using Microsoft.Identity.Web;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace ap_cli
{
/// <summary>
/// Description of the configuration of an AzureAD public client application (desktop/mobile application). This should
/// match the application registration done in the Azure portal
/// </summary>
internal class AuthenticationConfig
{
/// <summary>
/// instance of Azure AD, for example public Azure or a Sovereign cloud (Azure China, Germany, US government, etc ...)
/// </summary>
public string Instance { get; set; } = "https://login.microsoftonline.com/{0}";
/// <summary>
/// Graph API endpoint, could be public Azure (default) or a Sovereign cloud (US government, etc ...)
/// </summary>
public string ApiUrl { get; set; } = "https://graph.microsoft.com/";
/// <summary>
/// The Tenant is:
/// - either the tenant ID of the Azure AD tenant in which this application is registered (a guid)
/// or a domain name associated with the tenant
/// - or 'organizations' (for a multi-tenant application)
/// </summary>
public string Tenant { get; set; }
/// <summary>
/// Guid used by the application to uniquely identify itself to Azure AD
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// URL of the authority
/// </summary>
public string Authority
{
get
{
return String.Format(CultureInfo.InvariantCulture, Instance, Tenant);
}
}
/// <summary>
/// Client secret (application password)
/// </summary>
/// <remarks>Daemon applications can authenticate with AAD through two mechanisms: ClientSecret
/// (which is a kind of application password: this property)
/// or a certificate previously shared with AzureAD during the application registration
/// (and identified by the Certificate property belows)
/// <remarks>
public string ClientSecret { get; set; }
/// <summary>
/// The description of the certificate to be used to authenticate your application.
/// </summary>
/// <remarks>Daemon applications can authenticate with AAD through two mechanisms: ClientSecret
/// (which is a kind of application password: the property above)
/// or a certificate previously shared with AzureAD during the application registration
/// (and identified by this CertificateDescription)
/// <remarks>
public CertificateDescription Certificate { get; set; }
/// <summary>
/// Reads the configuration from a json file
/// </summary>
/// <param name="path">Path to the configuration json file</param>
/// <returns>AuthenticationConfig read from the json file</returns>
public static AuthenticationConfig ReadFromJsonFile(string path)
{
IConfigurationRoot Configuration;
var builder = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile(path);
Configuration = builder.Build();
return Configuration.Get<AuthenticationConfig>();
}
public static AuthenticationConfig ReadFromArgs(ap_cli.Program.Options o)
{
IConfigurationRoot Configuration;
var builder = new ConfigurationBuilder()
.AddInMemoryCollection(
new Dictionary<string, string>
{
["Instance"] = o.InstanceName,
["ApiUrl"] = o.ApiUrl,
["Tenant"] = o.TenantId,
["ClientId"] = o.ClientId,
["ClientSecret"] = o.ClientSecret,
["CertificateName"] = o.CetificateName,
["GroupId"] = o.GroupId,
["CatalogName"] = o.CatalogDisplayName,
["AccessPackageName"]= o.AccessPackageName
});
Configuration = builder.Build();
return Configuration.Get<AuthenticationConfig>();
}
}
}