-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathConfidentialClientApplicationBuilderExtension.cs
More file actions
169 lines (150 loc) · 7.12 KB
/
ConfidentialClientApplicationBuilderExtension.cs
File metadata and controls
169 lines (150 loc) · 7.12 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Identity.Abstractions;
using Microsoft.Identity.Client;
namespace Microsoft.Identity.Web
{
internal static partial class ConfidentialClientApplicationBuilderExtension
{
[Obsolete(IDWebErrorMessage.WithClientCredentialsIsObsolete, false)]
public static ConfidentialClientApplicationBuilder WithClientCredentials(
this ConfidentialClientApplicationBuilder builder,
IEnumerable<CredentialDescription> clientCredentials,
ILogger logger,
ICredentialsLoader credentialsLoader,
CredentialSourceLoaderParameters credentialSourceLoaderParameters)
{
return WithClientCredentialsAsync(builder, clientCredentials, logger, credentialsLoader,
credentialSourceLoaderParameters).GetAwaiter().GetResult();
}
public static async Task<ConfidentialClientApplicationBuilder> WithClientCredentialsAsync(
this ConfidentialClientApplicationBuilder builder,
IEnumerable<CredentialDescription> clientCredentials,
ILogger logger,
ICredentialsLoader credentialsLoader,
CredentialSourceLoaderParameters? credentialSourceLoaderParameters,
bool isTokenBinding = false)
{
var credential = await LoadCredentialForMsalOrFailAsync(
clientCredentials,
logger,
credentialsLoader,
credentialSourceLoaderParameters)
.ConfigureAwait(false);
if (credential == null)
{
return builder;
}
// regardless of credential type being set, bound token requires a certificate
if (isTokenBinding)
{
if (credential.Certificate == null)
{
logger.LogError("Loaded credentials for token binding doesn't contain a certificate");
}
return builder.WithCertificate(credential.Certificate);
}
switch (credential.CredentialType)
{
case CredentialType.SignedAssertion:
return builder.WithClientAssertion((credential.CachedValue as ClientAssertionProviderBase)!.GetSignedAssertionAsync);
case CredentialType.Certificate:
return builder.WithCertificate(credential.Certificate);
case CredentialType.Secret:
return builder.WithClientSecret(credential.ClientSecret);
default:
throw new NotImplementedException();
}
}
internal /* for test */ async static Task<CredentialDescription?> LoadCredentialForMsalOrFailAsync(
IEnumerable<CredentialDescription> clientCredentials,
ILogger logger,
ICredentialsLoader credentialsLoader,
CredentialSourceLoaderParameters? credentialSourceLoaderParameters)
{
string errorMessage = "\n";
foreach (CredentialDescription credential in clientCredentials)
{
Logger.AttemptToLoadCredentials(logger, credential);
if (!credential.Skip)
{
// Load the credentials and record error messages in case we need to fail at the end
try
{
await credentialsLoader.LoadCredentialsIfNeededAsync(credential, credentialSourceLoaderParameters);
}
catch (Exception ex)
{
Logger.AttemptToLoadCredentialsFailed(logger, credential, ex);
errorMessage += $"Credential {credential.Id} failed because: {ex} \n";
}
if (credential.CredentialType == CredentialType.SignedAssertion)
{
if (credential.SourceType == CredentialSource.SignedAssertionFromManagedIdentity)
{
if (credential.Skip)
{
Logger.NotUsingManagedIdentity(logger, errorMessage);
}
else
{
Logger.UsingManagedIdentity(logger);
return credential;
}
}
if (credential.SourceType == CredentialSource.SignedAssertionFilePath)
{
if (!credential.Skip)
{
Logger.UsingPodIdentityFile(logger, credential.SignedAssertionFileDiskPath ?? "not found");
return credential;
}
}
if (credential.SourceType == CredentialSource.SignedAssertionFromVault)
{
if (!credential.Skip)
{
Logger.UsingSignedAssertionFromVault(logger, credential.KeyVaultUrl ?? "undefined");
return credential;
}
}
if (credential.SourceType == CredentialSource.CustomSignedAssertion)
{
if (!credential.Skip)
{
Logger.UsingSignedAssertionFromCustomProvider(logger, credential.CustomSignedAssertionProviderName ?? "undefined");
return credential;
}
}
}
if (credential.CredentialType == CredentialType.Certificate)
{
if (credential.Certificate != null)
{
Logger.UsingCertThumbprint(logger, credential.Certificate.Thumbprint);
return credential;
}
}
if (credential.CredentialType == CredentialType.Secret)
{
return credential;
}
}
}
if (clientCredentials.Any(c => c.CredentialType == CredentialType.Certificate || c.CredentialType == CredentialType.SignedAssertion))
{
throw new ArgumentException(
IDWebErrorMessage.ClientCertificatesHaveExpiredOrCannotBeLoaded + errorMessage,
nameof(clientCredentials));
}
logger.LogInformation($"No client credential could be used. Secret may have been defined elsewhere. " +
$"Count {(clientCredentials != null ? clientCredentials.Count() : 0)} ");
return null;
}
}
}