-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathDefaultCredentialsLoader.cs
More file actions
159 lines (142 loc) · 6.36 KB
/
DefaultCredentialsLoader.cs
File metadata and controls
159 lines (142 loc) · 6.36 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Identity.Abstractions;
using Microsoft.IdentityModel.Tokens;
namespace Microsoft.Identity.Web
{
/// <summary>
/// Default credentials loader.
/// </summary>
public partial class DefaultCredentialsLoader : ICredentialsLoader, ISigningCredentialsLoader
{
private readonly ILogger<DefaultCredentialsLoader> _logger;
private readonly ConcurrentDictionary<string, SemaphoreSlim> _loadingSemaphores = new ConcurrentDictionary<string, SemaphoreSlim>();
/// <summary>
/// Constructor with a logger
/// </summary>
/// <param name="logger"></param>
public DefaultCredentialsLoader(ILogger<DefaultCredentialsLoader>? logger)
{
_logger = logger ?? new NullLogger<DefaultCredentialsLoader>();
CredentialSourceLoaders = new Dictionary<CredentialSource, ICredentialSourceLoader>
{
{ CredentialSource.KeyVault, new KeyVaultCertificateLoader() },
{ CredentialSource.Path, new FromPathCertificateLoader() },
{ CredentialSource.StoreWithThumbprint, new StoreWithThumbprintCertificateLoader() },
{ CredentialSource.StoreWithDistinguishedName, new StoreWithDistinguishedNameCertificateLoader() },
{ CredentialSource.Base64Encoded, new Base64EncodedCertificateLoader() },
{ CredentialSource.SignedAssertionFromManagedIdentity, new SignedAssertionFromManagedIdentityCredentialLoader(_logger) },
{ CredentialSource.SignedAssertionFilePath, new SignedAssertionFilePathCredentialsLoader(_logger) }
};
}
/// <summary>
/// Default constructor (for backward compatibility)
/// </summary>
public DefaultCredentialsLoader() : this(null)
{
}
/// <summary>
/// Dictionary of credential loaders per credential source. The application can add more to
/// process additional credential sources(like dSMS).
/// </summary>
public IDictionary<CredentialSource, ICredentialSourceLoader> CredentialSourceLoaders { get; }
/// <inheritdoc/>
/// Load the credentials from the description, if needed.
/// Important: Ignores SKIP flag, propagates exceptions.
public async Task LoadCredentialsIfNeededAsync(CredentialDescription credentialDescription, CredentialSourceLoaderParameters? parameters = null)
{
_ = Throws.IfNull(credentialDescription);
if (credentialDescription.CachedValue == null)
{
// Get or create a semaphore for this credentialDescription
var semaphore = _loadingSemaphores.GetOrAdd(credentialDescription.Id, (v) => new SemaphoreSlim(1));
// Wait to acquire the semaphore
await semaphore.WaitAsync();
try
{
if (credentialDescription.CachedValue == null)
{
if (credentialDescription.SourceType == CredentialSource.CustomSignedAssertion)
{
await ProcessCustomSignedAssertionAsync(credentialDescription, parameters);
}
else if (CredentialSourceLoaders.TryGetValue(credentialDescription.SourceType, out ICredentialSourceLoader? loader))
{
try
{
await loader.LoadIfNeededAsync(credentialDescription, parameters);
}
catch (Exception ex)
{
Logger.CredentialLoadingFailure(_logger, credentialDescription, ex);
throw;
}
}
}
}
finally
{
// Release the semaphore
semaphore.Release();
}
}
}
/// <inheritdoc/>
/// Loads first valid credential which is not marked as Skipped.
public async Task<CredentialDescription?> LoadFirstValidCredentialsAsync(
IEnumerable<CredentialDescription> credentialDescriptions,
CredentialSourceLoaderParameters? parameters = null)
{
foreach (var credentialDescription in credentialDescriptions)
{
await LoadCredentialsIfNeededAsync(credentialDescription, parameters);
if (!credentialDescription.Skip)
{
return credentialDescription;
}
}
return null;
}
/// <inheritdoc/>
public async Task<SigningCredentials?> LoadSigningCredentialsAsync(
CredentialDescription credentialDescription,
CredentialSourceLoaderParameters? parameters = null)
{
_ = Throws.IfNull(credentialDescription);
try
{
await LoadCredentialsIfNeededAsync(credentialDescription, parameters);
if (credentialDescription.Certificate != null)
{
return new X509SigningCredentials(credentialDescription.Certificate, credentialDescription.Algorithm);
}
return null;
}
catch (Exception ex)
{
Logger.CredentialLoadingFailure(_logger, credentialDescription, ex);
throw;
}
}
/// <inheritdoc/>
public void ResetCredentials(IEnumerable<CredentialDescription> credentialDescriptions)
{
foreach (var credentialDescription in credentialDescriptions)
{
credentialDescription.CachedValue = null;
credentialDescription.Skip = false;
if (credentialDescription.SourceType != CredentialSource.Certificate)
{
credentialDescription.Certificate = null;
}
}
}
}
}