17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
19
using System . Security ;
20
+ using System . Threading ;
21
+ using System . Threading . Tasks ;
20
22
using MongoDB . Bson ;
21
23
using MongoDB . Bson . Serialization ;
22
24
using MongoDB . Driver . Core . Authentication . External ;
@@ -51,26 +53,28 @@ private static MongoAWSMechanism CreateMechanism(
51
53
UsernamePasswordCredential credential ,
52
54
IEnumerable < KeyValuePair < string , string > > properties ,
53
55
IRandomByteGenerator randomByteGenerator ,
56
+ IExternalAuthenticationCredentialsProvider < AwsCredentials > externalAuthenticationCredentialsProvider ,
54
57
IClock clock )
55
58
{
56
59
if ( credential . Source != "$external" )
57
60
{
58
61
throw new ArgumentException ( "MONGODB-AWS authentication may only use the $external source." , nameof ( credential ) ) ;
59
62
}
60
63
61
- return CreateMechanism ( credential . Username , credential . Password , properties , randomByteGenerator , clock ) ;
64
+ return CreateMechanism ( credential . Username , credential . Password , properties , randomByteGenerator , externalAuthenticationCredentialsProvider , clock ) ;
62
65
}
63
66
64
67
private static MongoAWSMechanism CreateMechanism (
65
68
string username ,
66
69
SecureString password ,
67
70
IEnumerable < KeyValuePair < string , string > > properties ,
68
71
IRandomByteGenerator randomByteGenerator ,
72
+ IExternalAuthenticationCredentialsProvider < AwsCredentials > externalAuthenticationCredentialsProvider ,
69
73
IClock clock )
70
74
{
71
75
var awsCredentials =
72
76
CreateAwsCredentialsFromMongoCredentials ( username , password , properties ) ??
73
- ExternalCredentialsAuthenticators . Instance . Aws . CreateCredentialsFromExternalSource ( ) ;
77
+ externalAuthenticationCredentialsProvider . CreateCredentialsFromExternalSource ( ) ;
74
78
75
79
return new MongoAWSMechanism ( awsCredentials , randomByteGenerator , clock ) ;
76
80
}
@@ -97,7 +101,7 @@ private static AwsCredentials CreateAwsCredentialsFromMongoCredentials(string us
97
101
throw new InvalidOperationException ( "When using MONGODB-AWS authentication if a session token is provided via settings then a username and password must be provided also." ) ;
98
102
}
99
103
100
- return new AwsCredentials ( accessKeyId : username , secretAccessKey : password , sessionToken ) ;
104
+ return new AwsCredentials ( accessKeyId : username , secretAccessKey : password , sessionToken , expiration : null ) ;
101
105
}
102
106
103
107
private static string ExtractSessionTokenFromMechanismProperties ( IEnumerable < KeyValuePair < string , string > > properties )
@@ -131,18 +135,9 @@ private static void ValidateMechanismProperties(IEnumerable<KeyValuePair<string,
131
135
}
132
136
#endregion
133
137
134
- // constructors
135
- /// <summary>
136
- /// Initializes a new instance of the <see cref="MongoAWSAuthenticator"/> class.
137
- /// </summary>
138
- /// <param name="credential">The credentials.</param>
139
- /// <param name="properties">The properties.</param>
140
- [ Obsolete ( "Use the newest overload instead." ) ]
141
- public MongoAWSAuthenticator ( UsernamePasswordCredential credential , IEnumerable < KeyValuePair < string , string > > properties )
142
- : this ( credential , properties , serverApi : null )
143
- {
144
- }
138
+ private readonly ICredentialsCache < AwsCredentials > _credentialsCache ;
145
139
140
+ // constructors
146
141
/// <summary>
147
142
/// Initializes a new instance of the <see cref="MongoAWSAuthenticator"/> class.
148
143
/// </summary>
@@ -153,18 +148,13 @@ public MongoAWSAuthenticator(
153
148
UsernamePasswordCredential credential ,
154
149
IEnumerable < KeyValuePair < string , string > > properties ,
155
150
ServerApi serverApi )
156
- : this ( credential , properties , new DefaultRandomByteGenerator ( ) , SystemClock . Instance , serverApi )
157
- {
158
- }
159
-
160
- /// <summary>
161
- /// Initializes a new instance of the <see cref="MongoAWSAuthenticator"/> class.
162
- /// </summary>
163
- /// <param name="username">The username.</param>
164
- /// <param name="properties">The properties.</param>
165
- [ Obsolete ( "Use the newest overload instead." ) ]
166
- public MongoAWSAuthenticator ( string username , IEnumerable < KeyValuePair < string , string > > properties )
167
- : this ( username , properties , serverApi : null )
151
+ : this (
152
+ credential ,
153
+ properties ,
154
+ new DefaultRandomByteGenerator ( ) ,
155
+ ExternalCredentialsAuthenticators . Instance . Aws ,
156
+ SystemClock . Instance ,
157
+ serverApi )
168
158
{
169
159
}
170
160
@@ -178,28 +168,38 @@ public MongoAWSAuthenticator(
178
168
string username ,
179
169
IEnumerable < KeyValuePair < string , string > > properties ,
180
170
ServerApi serverApi )
181
- : this ( username , properties , new DefaultRandomByteGenerator ( ) , SystemClock . Instance , serverApi )
171
+ : this (
172
+ username ,
173
+ properties ,
174
+ new DefaultRandomByteGenerator ( ) ,
175
+ ExternalCredentialsAuthenticators . Instance . Aws ,
176
+ SystemClock . Instance ,
177
+ serverApi )
182
178
{
183
179
}
184
180
185
181
internal MongoAWSAuthenticator (
186
182
UsernamePasswordCredential credential ,
187
183
IEnumerable < KeyValuePair < string , string > > properties ,
188
184
IRandomByteGenerator randomByteGenerator ,
185
+ IExternalAuthenticationCredentialsProvider < AwsCredentials > externalAuthenticationCredentialsProvider ,
189
186
IClock clock ,
190
187
ServerApi serverApi )
191
- : base ( CreateMechanism ( credential , properties , randomByteGenerator , clock ) , serverApi )
188
+ : base ( CreateMechanism ( credential , properties , randomByteGenerator , externalAuthenticationCredentialsProvider , clock ) , serverApi )
192
189
{
190
+ _credentialsCache = externalAuthenticationCredentialsProvider as ICredentialsCache < AwsCredentials > ; // can be null
193
191
}
194
192
195
193
internal MongoAWSAuthenticator (
196
194
string username ,
197
195
IEnumerable < KeyValuePair < string , string > > properties ,
198
196
IRandomByteGenerator randomByteGenerator ,
197
+ IExternalAuthenticationCredentialsProvider < AwsCredentials > externalAuthenticationCredentialsProvider ,
199
198
IClock clock ,
200
199
ServerApi serverApi )
201
- : base ( CreateMechanism ( username , null , properties , randomByteGenerator , clock ) , serverApi )
200
+ : base ( CreateMechanism ( username , null , properties , randomByteGenerator , externalAuthenticationCredentialsProvider , clock ) , serverApi )
202
201
{
202
+ _credentialsCache = externalAuthenticationCredentialsProvider as ICredentialsCache < AwsCredentials > ; // can be null
203
203
}
204
204
205
205
/// <inheritdoc/>
@@ -208,6 +208,34 @@ public override string DatabaseName
208
208
get { return "$external" ; }
209
209
}
210
210
211
+ /// <inheritdoc/>
212
+ public override void Authenticate ( IConnection connection , ConnectionDescription description , CancellationToken cancellationToken )
213
+ {
214
+ try
215
+ {
216
+ base . Authenticate ( connection , description , cancellationToken ) ;
217
+ }
218
+ catch
219
+ {
220
+ _credentialsCache ? . Clear ( ) ;
221
+ throw ;
222
+ }
223
+ }
224
+
225
+ /// <inheritdoc/>
226
+ public override async Task AuthenticateAsync ( IConnection connection , ConnectionDescription description , CancellationToken cancellationToken )
227
+ {
228
+ try
229
+ {
230
+ await base . AuthenticateAsync ( connection , description , cancellationToken ) . ConfigureAwait ( false ) ;
231
+ }
232
+ catch
233
+ {
234
+ _credentialsCache ? . Clear ( ) ;
235
+ throw ;
236
+ }
237
+ }
238
+
211
239
// nested classes
212
240
private class MongoAWSMechanism : ISaslMechanism
213
241
{
0 commit comments