From dc4f255e0fab5f6b39b7add197830591b89103c0 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 25 Apr 2021 21:37:52 +0800 Subject: ... --- .../Services/User/PasswordBadFormatException.cs | 26 --------- BackEnd/Timeline/Services/User/PasswordService.cs | 24 ++++----- .../Timeline/Services/User/Resource.Designer.cs | 63 ++++++++++++++++++++++ BackEnd/Timeline/Services/User/Resource.resx | 21 ++++++++ 4 files changed, 94 insertions(+), 40 deletions(-) delete mode 100644 BackEnd/Timeline/Services/User/PasswordBadFormatException.cs (limited to 'BackEnd/Timeline/Services/User') diff --git a/BackEnd/Timeline/Services/User/PasswordBadFormatException.cs b/BackEnd/Timeline/Services/User/PasswordBadFormatException.cs deleted file mode 100644 index b9d76017..00000000 --- a/BackEnd/Timeline/Services/User/PasswordBadFormatException.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; - -namespace Timeline.Services.User -{ - [Serializable] - public class PasswordBadFormatException : Exception - { - public PasswordBadFormatException() : base(Resources.Services.Exception.PasswordBadFormatException) { } - public PasswordBadFormatException(string message) : base(message) { } - public PasswordBadFormatException(string message, Exception inner) : base(message, inner) { } - - public PasswordBadFormatException(string password, string validationMessage) : this() - { - Password = password; - ValidationMessage = validationMessage; - } - - protected PasswordBadFormatException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - - public string Password { get; set; } = ""; - - public string ValidationMessage { get; set; } = ""; - } -} diff --git a/BackEnd/Timeline/Services/User/PasswordService.cs b/BackEnd/Timeline/Services/User/PasswordService.cs index 580471e1..1c14875f 100644 --- a/BackEnd/Timeline/Services/User/PasswordService.cs +++ b/BackEnd/Timeline/Services/User/PasswordService.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Cryptography.KeyDerivation; using System; +using System.Globalization; using System.Runtime.CompilerServices; using System.Security.Cryptography; @@ -12,18 +13,13 @@ namespace Timeline.Services.User [Serializable] public class HashedPasswordBadFromatException : Exception { - private static string MakeMessage(string reason) - { - return Resources.Services.Exception.HashedPasswordBadFromatException + " Reason: " + reason; - } - - public HashedPasswordBadFromatException() : base(Resources.Services.Exception.HashedPasswordBadFromatException) { } + public HashedPasswordBadFromatException() : base(Resource.ExceptionHashedPasswordBadFormat) { } public HashedPasswordBadFromatException(string message) : base(message) { } public HashedPasswordBadFromatException(string message, Exception inner) : base(message, inner) { } - public HashedPasswordBadFromatException(string hashedPassword, string reason) : base(MakeMessage(reason)) { HashedPassword = hashedPassword; } - public HashedPasswordBadFromatException(string hashedPassword, string reason, Exception inner) : base(MakeMessage(reason), inner) { HashedPassword = hashedPassword; } + public HashedPasswordBadFromatException(string hashedPassword, string reason, Exception? inner = null) + : base(string.Format(CultureInfo.InvariantCulture, Resource.ExceptionHashedPasswordBadFormat, reason), inner) { HashedPassword = hashedPassword; } protected HashedPasswordBadFromatException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } @@ -150,19 +146,19 @@ namespace Timeline.Services.User } catch (FormatException e) { - throw new HashedPasswordBadFromatException(hashedPassword, Resources.Services.Exception.HashedPasswordBadFromatExceptionNotBase64, e); + throw new HashedPasswordBadFromatException(hashedPassword, Resource.ExceptionHashedPasswordBadFormatReasonNotBase64, e); } // read the format marker from the hashed password if (decodedHashedPassword.Length == 0) { - throw new HashedPasswordBadFromatException(hashedPassword, Resources.Services.Exception.HashedPasswordBadFromatExceptionNotLength0); + throw new HashedPasswordBadFromatException(hashedPassword, Resource.ExceptionHashedPasswordBadFormatReasonLength0); } return (decodedHashedPassword[0]) switch { 0x01 => VerifyHashedPasswordV3(decodedHashedPassword, providedPassword, hashedPassword), - _ => throw new HashedPasswordBadFromatException(hashedPassword, Resources.Services.Exception.HashedPasswordBadFromatExceptionNotUnknownMarker), + _ => throw new HashedPasswordBadFromatException(hashedPassword, Resource.ExceptionHashedPasswordBadFormatReasonUnknownMarker), }; } @@ -178,7 +174,7 @@ namespace Timeline.Services.User // Read the salt: must be >= 128 bits if (saltLength < 128 / 8) { - throw new HashedPasswordBadFromatException(hashedPasswordString, Resources.Services.Exception.HashedPasswordBadFromatExceptionNotSaltTooShort); + throw new HashedPasswordBadFromatException(hashedPasswordString, Resource.ExceptionHashedPasswordBadFormatReasonSaltTooShort); } byte[] salt = new byte[saltLength]; Buffer.BlockCopy(hashedPassword, 13, salt, 0, salt.Length); @@ -187,7 +183,7 @@ namespace Timeline.Services.User int subkeyLength = hashedPassword.Length - 13 - salt.Length; if (subkeyLength < 128 / 8) { - throw new HashedPasswordBadFromatException(hashedPasswordString, Resources.Services.Exception.HashedPasswordBadFromatExceptionNotSubkeyTooShort); + throw new HashedPasswordBadFromatException(hashedPasswordString, Resource.ExceptionHashedPasswordBadFormatReasonSubkeyTooShort); } byte[] expectedSubkey = new byte[subkeyLength]; Buffer.BlockCopy(hashedPassword, 13 + salt.Length, expectedSubkey, 0, expectedSubkey.Length); @@ -201,7 +197,7 @@ namespace Timeline.Services.User // This should never occur except in the case of a malformed payload, where // we might go off the end of the array. Regardless, a malformed payload // implies verification failed. - throw new HashedPasswordBadFromatException(hashedPasswordString, Resources.Services.Exception.HashedPasswordBadFromatExceptionNotOthers, e); + throw new HashedPasswordBadFromatException(hashedPasswordString, Resource.ExceptionHashedPasswordBadFormatReasonOthers, e); } } diff --git a/BackEnd/Timeline/Services/User/Resource.Designer.cs b/BackEnd/Timeline/Services/User/Resource.Designer.cs index d64a7aab..4f75b055 100644 --- a/BackEnd/Timeline/Services/User/Resource.Designer.cs +++ b/BackEnd/Timeline/Services/User/Resource.Designer.cs @@ -69,6 +69,69 @@ namespace Timeline.Services.User { } } + /// + /// Looks up a localized string similar to The hashes password is of bad format. It might not be created by server. Reason: {0}. + /// + internal static string ExceptionHashedPasswordBadFormat { + get { + return ResourceManager.GetString("ExceptionHashedPasswordBadFormat", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Decoded hashed password is of length 0.. + /// + internal static string ExceptionHashedPasswordBadFormatReasonLength0 { + get { + return ResourceManager.GetString("ExceptionHashedPasswordBadFormatReasonLength0", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Not of valid base64 format. See inner exception.. + /// + internal static string ExceptionHashedPasswordBadFormatReasonNotBase64 { + get { + return ResourceManager.GetString("ExceptionHashedPasswordBadFormatReasonNotBase64", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to See inner exception.. + /// + internal static string ExceptionHashedPasswordBadFormatReasonOthers { + get { + return ResourceManager.GetString("ExceptionHashedPasswordBadFormatReasonOthers", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Salt length < 128 bits.. + /// + internal static string ExceptionHashedPasswordBadFormatReasonSaltTooShort { + get { + return ResourceManager.GetString("ExceptionHashedPasswordBadFormatReasonSaltTooShort", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Subkey length < 128 bits.. + /// + internal static string ExceptionHashedPasswordBadFormatReasonSubkeyTooShort { + get { + return ResourceManager.GetString("ExceptionHashedPasswordBadFormatReasonSubkeyTooShort", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Unknown format marker.. + /// + internal static string ExceptionHashedPasswordBadFormatReasonUnknownMarker { + get { + return ResourceManager.GetString("ExceptionHashedPasswordBadFormatReasonUnknownMarker", resourceCulture); + } + } + /// /// Looks up a localized string similar to Nickname is of bad format. {0}. /// diff --git a/BackEnd/Timeline/Services/User/Resource.resx b/BackEnd/Timeline/Services/User/Resource.resx index 732cfefd..28e75b19 100644 --- a/BackEnd/Timeline/Services/User/Resource.resx +++ b/BackEnd/Timeline/Services/User/Resource.resx @@ -120,6 +120,27 @@ Password is wrong. + + The hashes password is of bad format. It might not be created by server. Reason: {0} + + + Decoded hashed password is of length 0. + + + Not of valid base64 format. See inner exception. + + + See inner exception. + + + Salt length < 128 bits. + + + Subkey length < 128 bits. + + + Unknown format marker. + Nickname is of bad format. {0} -- cgit v1.2.3