Skip to content

Commit 3aa0e4c

Browse files
fix(register): [PM-27084] Account Register Uses New Data Types - Shuffled around validation a little. In a great place now.
1 parent c255e39 commit 3aa0e4c

File tree

1 file changed

+74
-56
lines changed

1 file changed

+74
-56
lines changed

src/Core/Auth/Models/Api/Request/Accounts/RegisterFinishRequestModel.cs

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,81 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
130130
}
131131
}
132132

133+
// 2. Validate kdf settings.
134+
if (MasterPasswordUnlock != null)
135+
{
136+
foreach (var validationResult in KdfSettingsValidator.Validate(MasterPasswordUnlock.ToData().Kdf))
137+
{
138+
yield return validationResult;
139+
}
140+
}
141+
142+
if (MasterPasswordAuthentication != null)
143+
{
144+
foreach (var validationResult in KdfSettingsValidator.Validate(MasterPasswordAuthentication.ToData().Kdf))
145+
{
146+
yield return validationResult;
147+
}
148+
}
149+
150+
// 3. Validate root kdf values if kdf values are not in the unlock and authentication.
151+
if (MasterPasswordUnlock == null && MasterPasswordAuthentication == null)
152+
{
153+
var hasMissingRequiredKdfInputs = false;
154+
if (Kdf == null)
155+
{
156+
yield return new ValidationResult($"{nameof(Kdf)} not found on RequestModel", [nameof(Kdf)]);
157+
hasMissingRequiredKdfInputs = true;
158+
}
159+
if (KdfIterations == null)
160+
{
161+
yield return new ValidationResult($"{nameof(KdfIterations)} not found on RequestModel", [nameof(KdfIterations)]);
162+
hasMissingRequiredKdfInputs = true;
163+
}
164+
165+
if (!hasMissingRequiredKdfInputs)
166+
{
167+
foreach (var validationResult in KdfSettingsValidator.Validate(
168+
Kdf!.Value,
169+
KdfIterations!.Value,
170+
KdfMemory,
171+
KdfParallelism))
172+
{
173+
yield return validationResult;
174+
}
175+
}
176+
}
177+
else if (MasterPasswordUnlock == null && MasterPasswordAuthentication != null)
178+
{
179+
// Authentication provided but Unlock missing
180+
yield return new ValidationResult($"{nameof(MasterPasswordUnlock)} not found on RequestModel", [nameof(MasterPasswordUnlock)]);
181+
}
182+
else if (MasterPasswordUnlock != null && MasterPasswordAuthentication == null)
183+
{
184+
// Unlock provided but Authentication missing
185+
yield return new ValidationResult($"{nameof(MasterPasswordAuthentication)} not found on RequestModel", [nameof(MasterPasswordAuthentication)]);
186+
}
187+
188+
// 3. Lastly, validate access token type and presence. Must be done last because of yield break.
189+
RegisterFinishTokenType tokenType;
190+
var tokenTypeResolved = true;
191+
try
192+
{
193+
tokenType = GetTokenType();
194+
}
195+
catch (InvalidOperationException)
196+
{
197+
tokenTypeResolved = false;
198+
tokenType = default;
199+
}
133200

134-
// 1. Access token presence verification check
135-
switch (GetTokenType())
201+
if (!tokenTypeResolved)
202+
{
203+
yield return new ValidationResult("No valid registration token provided");
204+
yield break;
205+
}
206+
207+
switch (tokenType)
136208
{
137209
case RegisterFinishTokenType.EmailVerification:
138210
if (string.IsNullOrEmpty(EmailVerificationToken))
@@ -190,59 +262,5 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
190262
yield return new ValidationResult("Invalid registration finish request");
191263
break;
192264
}
193-
194-
// 2. Validate kdf settings.
195-
if (MasterPasswordUnlock != null)
196-
{
197-
foreach (var validationResult in KdfSettingsValidator.Validate(MasterPasswordUnlock.ToData().Kdf))
198-
{
199-
yield return validationResult;
200-
}
201-
}
202-
203-
if (MasterPasswordAuthentication != null)
204-
{
205-
foreach (var validationResult in KdfSettingsValidator.Validate(MasterPasswordAuthentication.ToData().Kdf))
206-
{
207-
yield return validationResult;
208-
}
209-
}
210-
211-
if (MasterPasswordUnlock == null && MasterPasswordAuthentication == null)
212-
{
213-
var hasMissingRequiredKdfInputs = false;
214-
if (Kdf == null)
215-
{
216-
yield return new ValidationResult($"{nameof(Kdf)} not found on RequestModel", [nameof(Kdf)]);
217-
hasMissingRequiredKdfInputs = true;
218-
}
219-
if (KdfIterations == null)
220-
{
221-
yield return new ValidationResult($"{nameof(KdfIterations)} not found on RequestModel", [nameof(KdfIterations)]);
222-
hasMissingRequiredKdfInputs = true;
223-
}
224-
225-
if (!hasMissingRequiredKdfInputs)
226-
{
227-
foreach (var validationResult in KdfSettingsValidator.Validate(
228-
Kdf!.Value,
229-
KdfIterations!.Value,
230-
KdfMemory,
231-
KdfParallelism))
232-
{
233-
yield return validationResult;
234-
}
235-
}
236-
}
237-
else if (MasterPasswordUnlock == null && MasterPasswordAuthentication != null)
238-
{
239-
// Authentication provided but Unlock missing
240-
yield return new ValidationResult($"{nameof(MasterPasswordUnlock)} not found on RequestModel", [nameof(MasterPasswordUnlock)]);
241-
}
242-
else if (MasterPasswordUnlock != null && MasterPasswordAuthentication == null)
243-
{
244-
// Unlock provided but Authentication missing
245-
yield return new ValidationResult($"{nameof(MasterPasswordAuthentication)} not found on RequestModel", [nameof(MasterPasswordAuthentication)]);
246-
}
247265
}
248266
}

0 commit comments

Comments
 (0)