Skip to content

Commit 47c407e

Browse files
committed
add null checks for collections
1 parent 0ff5003 commit 47c407e

File tree

6 files changed

+71
-61
lines changed

6 files changed

+71
-61
lines changed

src/Amazon.Extensions.CognitoAuthentication/CognitoAuthenticationClasses.cs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public AuthFlowResponse(string sessionId, AuthenticationResultType authenticatio
3939
SessionID = sessionId;
4040
ChallengeName = challengeName;
4141
AuthenticationResult = authenticationResult;
42-
ChallengeParameters = challengeParameters;
43-
ClientMetadata = clientMetadata;
42+
ChallengeParameters = challengeParameters ?? new Dictionary<string, string>();
43+
ClientMetadata = clientMetadata ?? new Dictionary<string, string>();
4444
}
4545

4646
/// <summary>
@@ -69,9 +69,9 @@ public AuthFlowResponse(string sessionId, AuthenticationResultType authenticatio
6969
/// </summary>
7070
public IDictionary<string, string> ClientMetadata { get; }
7171

72-
/// <summary>
73-
/// The analytics metadata for collecting Amazon Pinpoint metrics.
74-
/// </summary>
72+
/// <summary>
73+
/// The analytics metadata for collecting Amazon Pinpoint metrics.
74+
/// </summary>
7575
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
7676
}
7777

@@ -83,30 +83,30 @@ public class InitiateSrpAuthRequest
8383
/// <summary>
8484
/// The password for the corresponding CognitoUser.
8585
/// </summary>
86-
public string Password { get; set; }
87-
/// <summary>
88-
/// The password for the device associated with the corresponding CognitoUser
89-
/// </summary>
90-
public string DevicePass { get; set; }
91-
/// <summary>
92-
/// The device password verifier for the device associated with the corresponding CognitoUser
93-
/// </summary>
94-
public string DeviceVerifier { get; set; }
95-
/// <summary>
96-
/// The Device Key Group for the device associated with the corresponding CognitoUser
97-
/// </summary>
98-
public string DeviceGroupKey { get; set; }
86+
public string Password { get; set; }
87+
/// <summary>
88+
/// The password for the device associated with the corresponding CognitoUser
89+
/// </summary>
90+
public string DevicePass { get; set; }
91+
/// <summary>
92+
/// The device password verifier for the device associated with the corresponding CognitoUser
93+
/// </summary>
94+
public string DeviceVerifier { get; set; }
95+
/// <summary>
96+
/// The Device Key Group for the device associated with the corresponding CognitoUser
97+
/// </summary>
98+
public string DeviceGroupKey { get; set; }
9999
/// <summary>
100100
/// The client metadata for the current authentication flow.
101101
/// </summary>
102-
public IDictionary<string, string> ClientMetadata { get; set; }
103-
/// <summary>
104-
/// The analytics metadata for collecting Amazon Pinpoint metrics.
105-
/// </summary>
102+
public IDictionary<string, string> ClientMetadata { get; set; }
103+
/// <summary>
104+
/// The analytics metadata for collecting Amazon Pinpoint metrics.
105+
/// </summary>
106106
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
107-
/// <summary>
108-
/// Enable custom auth flow
109-
/// https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html#Using-SRP-password-verification-in-custom-authentication-flow
107+
/// <summary>
108+
/// Enable custom auth flow
109+
/// https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html#Using-SRP-password-verification-in-custom-authentication-flow
110110
/// </summary>
111111
public bool IsCustomAuthFlow { get; set; }
112112
}
@@ -124,11 +124,11 @@ public class InitiateCustomAuthRequest
124124
/// <summary>
125125
/// The client metadata for the current authentication flow.
126126
/// </summary>
127-
public IDictionary<string, string> ClientMetadata { get; set; }
128-
129-
/// <summary>
130-
/// The analytics metadata for collecting Amazon Pinpoint metrics.
131-
/// </summary>
127+
public IDictionary<string, string> ClientMetadata { get; set; }
128+
129+
/// <summary>
130+
/// The analytics metadata for collecting Amazon Pinpoint metrics.
131+
/// </summary>
132132
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
133133
}
134134

@@ -208,9 +208,9 @@ public class RespondToCustomChallengeRequest
208208
/// </summary>
209209
public IDictionary<string, string> ClientMetadata { get; set; } = new Dictionary<string, string>();
210210

211-
/// <summary>
212-
/// The analytics metadata for collecting Amazon Pinpoint metrics.
213-
/// </summary>
211+
/// <summary>
212+
/// The analytics metadata for collecting Amazon Pinpoint metrics.
213+
/// </summary>
214214
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
215215

216216
/// <summary>
@@ -232,11 +232,11 @@ public class InitiateAdminNoSrpAuthRequest
232232
/// <summary>
233233
/// Optional client metadata to provide in the Initiate Admin Authentication API call
234234
/// </summary>
235-
public IDictionary<string, string> ClientMetadata { get; set; }
236-
237-
/// <summary>
238-
/// Optional analytics metadata for collecting Amazon Pinpoint metrics.
239-
/// </summary>
235+
public IDictionary<string, string> ClientMetadata { get; set; }
236+
237+
/// <summary>
238+
/// Optional analytics metadata for collecting Amazon Pinpoint metrics.
239+
/// </summary>
240240
public AnalyticsMetadataType AnalyticsMetadata { get; set; }
241241
}
242242
}

src/Amazon.Extensions.CognitoAuthentication/CognitoDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ private Dictionary<string,string> CreateDictionaryFromAttributeList(IList<Attrib
314314
{
315315
Dictionary<string, string> attributesDict = new Dictionary<string, string>();
316316

317-
foreach(AttributeType attribute in attributes)
317+
foreach(AttributeType attribute in attributes ?? new List<AttributeType>())
318318
{
319319
attributesDict.Add(attribute.Name, attribute.Value);
320320
}

src/Amazon.Extensions.CognitoAuthentication/CognitoUser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public virtual async Task UpdateAttributesAsync(IDictionary<string, string> attr
424424
await Provider.UpdateUserAttributesAsync(updateUserAttributesRequest, cancellationToken).ConfigureAwait(false);
425425

426426
//Update the local Attributes property
427-
foreach (KeyValuePair<string, string> entry in attributes)
427+
foreach (KeyValuePair<string, string> entry in attributes ?? new Dictionary<string, string>())
428428
{
429429
Attributes[entry.Key] = entry.Value;
430430
}
@@ -454,7 +454,7 @@ public virtual async Task DeleteAttributesAsync(IList<string> attributeNamesToDe
454454
await Provider.DeleteUserAttributesAsync(deleteUserAttributesRequest, cancellationToken).ConfigureAwait(false);
455455

456456
//Update the local Attributes property
457-
foreach (string attribute in attributeNamesToDelete)
457+
foreach (string attribute in attributeNamesToDelete ?? new List<string>())
458458
{
459459
if (Attributes.ContainsKey(attribute))
460460
{
@@ -486,7 +486,7 @@ public virtual async Task SetUserSettingsAsync(IDictionary<string, string> userS
486486
await Provider.SetUserSettingsAsync(setUserSettingsRequest, cancellationToken).ConfigureAwait(false);
487487

488488
//Update the local Settings property
489-
foreach (KeyValuePair<string, string> entry in userSettings)
489+
foreach (KeyValuePair<string, string> entry in userSettings ?? new Dictionary<string, string>())
490490
{
491491
Settings[entry.Key] = entry.Value;
492492
}
@@ -518,7 +518,7 @@ public virtual async Task<List<CognitoDevice>> ListDevicesAsync(int limit, strin
518518
ListDevicesResponse listDevicesReponse = await Provider.ListDevicesAsync(listDevicesRequest, cancellationToken).ConfigureAwait(false);
519519
List<CognitoDevice> devicesList = new List<CognitoDevice>();
520520

521-
foreach (DeviceType device in listDevicesReponse.Devices)
521+
foreach (DeviceType device in listDevicesReponse.Devices ?? new List<DeviceType>())
522522
{
523523
devicesList.Add(new CognitoDevice(device, this));
524524
}

src/Amazon.Extensions.CognitoAuthentication/CognitoUserAuthentication.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public virtual async Task<AuthFlowResponse> StartWithSrpAuthAsync(InitiateSrpAut
7373
if (srpRequest.IsCustomAuthFlow)
7474
{
7575
initiateRequest.AuthFlow = AuthFlowType.CUSTOM_AUTH;
76-
initiateRequest.AuthParameters.Add(CognitoConstants.ChlgParamChallengeName, CognitoConstants.ChlgParamSrpA);
76+
initiateRequest.AuthParameters?.Add(CognitoConstants.ChlgParamChallengeName, CognitoConstants.ChlgParamSrpA);
7777
}
7878
InitiateAuthResponse initiateResponse = await Provider.InitiateAuthAsync(initiateRequest, cancellationToken).ConfigureAwait(false);
7979
UpdateUsernameAndSecretHash(initiateResponse.ChallengeParameters);
@@ -172,6 +172,10 @@ private RespondToAuthChallengeRequest CreateDevicePasswordVerifierAuthRequest(Re
172172
string devicePassword,
173173
Tuple<BigInteger, BigInteger> tupleAa)
174174
{
175+
if (challenge == null)
176+
throw new ArgumentNullException(nameof(challenge), $"{nameof(challenge)} cannot be null");
177+
if (challenge.ChallengeParameters == null)
178+
throw new ArgumentNullException(nameof(challenge.ChallengeParameters), $"{nameof(challenge.ChallengeParameters)} cannot be null");
175179
string deviceKey = challenge.ChallengeParameters[CognitoConstants.ChlgParamDeviceKey];
176180
string username = challenge.ChallengeParameters[CognitoConstants.ChlgParamUsername];
177181
string secretBlock = challenge.ChallengeParameters[CognitoConstants.ChlgParamSecretBlock];
@@ -255,7 +259,7 @@ public virtual async Task<AuthFlowResponse> StartWithCustomAuthAsync(InitiateCus
255259
initiateResponse.AuthenticationResult,
256260
initiateResponse.ChallengeName,
257261
initiateResponse.ChallengeParameters,
258-
new Dictionary<string, string>(initiateResponse.ResponseMetadata.Metadata));
262+
new Dictionary<string, string>(initiateResponse.ResponseMetadata.Metadata ?? new Dictionary<string, string>()));
259263
}
260264

261265
/// <summary>
@@ -286,8 +290,8 @@ public virtual async Task<AuthFlowResponse> RespondToCustomAuthAsync(RespondToCu
286290
{
287291
ChallengeName = ChallengeNameType.CUSTOM_CHALLENGE,
288292
ClientId = ClientID,
289-
ChallengeResponses = new Dictionary<string, string>(customRequest.ChallengeParameters),
290-
ClientMetadata = new Dictionary<string, string>(customRequest.ClientMetadata),
293+
ChallengeResponses = new Dictionary<string, string>(customRequest.ChallengeParameters ?? new Dictionary<string, string>()),
294+
ClientMetadata = new Dictionary<string, string>(customRequest.ClientMetadata ?? new Dictionary<string, string>()),
291295
AnalyticsMetadata = customRequest.AnalyticsMetadata,
292296
Session = customRequest.SessionID
293297
};
@@ -300,8 +304,8 @@ public virtual async Task<AuthFlowResponse> RespondToCustomAuthAsync(RespondToCu
300304
return new AuthFlowResponse(authResponse.Session,
301305
authResponse.AuthenticationResult,
302306
authResponse.ChallengeName,
303-
authResponse.ChallengeParameters,
304-
new Dictionary<string, string>(authResponse.ResponseMetadata.Metadata));
307+
authResponse.ChallengeParameters ?? new Dictionary<string, string>(),
308+
new Dictionary<string, string>(authResponse.ResponseMetadata.Metadata ?? new Dictionary<string, string>()));
305309
}
306310

307311
/// <summary>
@@ -469,8 +473,8 @@ public async Task<AuthFlowResponse> RespondToMfaAuthAsync(RespondToMfaRequest mf
469473
return new AuthFlowResponse(challengeResponse.Session,
470474
challengeResponse.AuthenticationResult,
471475
challengeResponse.ChallengeName,
472-
challengeResponse.ChallengeParameters,
473-
new Dictionary<string, string>(challengeResponse.ResponseMetadata.Metadata));
476+
challengeResponse.ChallengeParameters ?? new Dictionary<string, string>(),
477+
new Dictionary<string, string>(challengeResponse.ResponseMetadata.Metadata ?? new Dictionary<string, string>()));
474478
}
475479

476480
/// <summary>
@@ -577,8 +581,8 @@ public virtual async Task<AuthFlowResponse> RespondToNewPasswordRequiredAsync(Re
577581
return new AuthFlowResponse(challengeResponse.Session,
578582
challengeResponse.AuthenticationResult,
579583
challengeResponse.ChallengeName,
580-
challengeResponse.ChallengeParameters,
581-
new Dictionary<string, string>(challengeResponse.ResponseMetadata.Metadata));
584+
challengeResponse.ChallengeParameters ?? new Dictionary<string, string>(),
585+
new Dictionary<string, string>(challengeResponse.ResponseMetadata.Metadata ?? new Dictionary<string, string>()));
582586
}
583587

584588
/// <summary>
@@ -617,8 +621,8 @@ public virtual async Task<AuthFlowResponse> StartWithRefreshTokenAuthAsync(Initi
617621
return new AuthFlowResponse(initiateResponse.Session,
618622
initiateResponse.AuthenticationResult,
619623
initiateResponse.ChallengeName,
620-
initiateResponse.ChallengeParameters,
621-
new Dictionary<string, string>(initiateResponse.ResponseMetadata.Metadata));
624+
initiateResponse.ChallengeParameters ?? new Dictionary<string, string>(),
625+
new Dictionary<string, string>(initiateResponse.ResponseMetadata.Metadata ?? new Dictionary<string, string>()));
622626
}
623627

624628
/// <summary>
@@ -653,8 +657,8 @@ public virtual async Task<AuthFlowResponse> StartWithAdminNoSrpAuthAsync(Initiat
653657
return new AuthFlowResponse(initiateResponse.Session,
654658
initiateResponse.AuthenticationResult,
655659
initiateResponse.ChallengeName,
656-
initiateResponse.ChallengeParameters,
657-
new Dictionary<string, string>(initiateResponse.ResponseMetadata.Metadata));
660+
initiateResponse.ChallengeParameters ?? new Dictionary<string, string>(),
661+
new Dictionary<string, string>(initiateResponse.ResponseMetadata.Metadata ?? new Dictionary<string, string>()));
658662
}
659663

660664
/// <summary>
@@ -715,7 +719,7 @@ private void UpdateUsernameAndSecretHash(IDictionary<string, string> challengePa
715719
return;
716720
}
717721

718-
if (challengeParameters.ContainsKey(CognitoConstants.ChlgParamUsername))
722+
if (challengeParamIsUsername)
719723
{
720724
Username = challengeParameters[CognitoConstants.ChlgParamUsername];
721725
}
@@ -802,6 +806,10 @@ private RespondToAuthChallengeRequest CreateSrpPasswordVerifierAuthRequest(Initi
802806
string password,
803807
Tuple<BigInteger, BigInteger> tupleAa)
804808
{
809+
if (challenge == null)
810+
throw new ArgumentNullException(nameof(challenge), $"{nameof(challenge)} cannot be null");
811+
if (challenge.ChallengeParameters == null)
812+
throw new ArgumentNullException(nameof(challenge.ChallengeParameters), $"{nameof(challenge.ChallengeParameters)} cannot be null");
805813
string username = challenge.ChallengeParameters[CognitoConstants.ChlgParamUsername];
806814
string poolName = PoolName;
807815
string secretBlock = challenge.ChallengeParameters[CognitoConstants.ChlgParamSecretBlock];

src/Amazon.Extensions.CognitoAuthentication/CognitoUserPool.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public virtual async Task<CognitoUser> FindByIdAsync(string userID, Cancellation
229229

230230
return new CognitoUser(response.Username, ClientID, this, Provider, ClientSecret,
231231
response.UserStatus.Value, response.Username,
232-
response.UserAttributes.ToDictionary(attribute => attribute.Name, attribute => attribute.Value));
232+
response.UserAttributes?.ToDictionary(attribute => attribute.Name, attribute => attribute.Value) ?? new Dictionary<string, string>());
233233

234234
}
235235
catch (UserNotFoundException)
@@ -288,7 +288,9 @@ public async Task<CognitoUserPoolClientConfiguration> GetUserPoolClientConfigura
288288
UserPoolId = this.PoolID
289289
}, cancellationToken).ConfigureAwait(false);
290290

291-
ClientConfiguration = new CognitoUserPoolClientConfiguration(response.UserPoolClient.ReadAttributes, response.UserPoolClient.WriteAttributes);
291+
ClientConfiguration = new CognitoUserPoolClientConfiguration(
292+
response.UserPoolClient.ReadAttributes ?? new List<string>(),
293+
response.UserPoolClient.WriteAttributes ?? new List<string>());
292294
}
293295

294296
return ClientConfiguration;

src/Amazon.Extensions.CognitoAuthentication/Util/CognitoAuthHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ internal static byte[] StringToByteArray(string hexString)
126126
internal static List<AttributeType> CreateAttributeList(IDictionary<string, string> attributeDict)
127127
{
128128
List<AttributeType> attributeList = new List<AttributeType>();
129-
foreach (KeyValuePair<string, string> data in attributeDict)
129+
foreach (KeyValuePair<string, string> data in attributeDict ?? new Dictionary<string, string>())
130130
{
131131
AttributeType attribute = new AttributeType()
132132
{

0 commit comments

Comments
 (0)