set otp and challenge answer in jsonrpc request

Signed-off-by: Aliwoto <aminnimaj@gmail.com>
This commit is contained in:
Aliwoto 2021-09-30 19:39:59 +00:00
parent ca932a74db
commit fa13a7e88d
No known key found for this signature in database
GPG Key ID: 646B4FE4205EC48C
4 changed files with 38 additions and 11 deletions

View File

@ -17,9 +17,11 @@
*/ */
using System; using System;
using System.Text;
using System.Net.Http; using System.Net.Http;
using System.IO; using System.IO;
using Socialvoid.Security; using Socialvoid.Security;
using Socialvoid.Security.Otp;
using Socialvoid.JObjects; using Socialvoid.JObjects;
using Socialvoid.Errors.ServerErrors; using Socialvoid.Errors.ServerErrors;
using Socialvoid.Errors.AuthenticationErrors; using Socialvoid.Errors.AuthenticationErrors;
@ -341,9 +343,18 @@ namespace Socialvoid.Client
/// <exception cref="TwoFactorAuthenticationRequiredException"> /// <exception cref="TwoFactorAuthenticationRequiredException">
/// Thrown if two-factor authentication is required. /// Thrown if two-factor authentication is required.
/// </exception> /// </exception>
public virtual void AuthenticateUser(SessionIdentification sessionID, public virtual void AuthenticateUser(string username, string password,
string username, string password, string otp = null) string otp = null, SessionIdentification sessionID = null)
{ {
if (sessionID == null && _session != null)
{
sessionID = new()
{
SessionID = _session.SessionID,
ClientPublicHash = PublicHash
};
}
JArgs args = new(){ JArgs args = new(){
{UsernameKey, username}, {UsernameKey, username},
{PasswordKey, password}, {PasswordKey, password},
@ -357,12 +368,14 @@ namespace Socialvoid.Client
if (IsOtpValid(otp)) if (IsOtpValid(otp))
{ {
args.Add(OtpKey, otp); args.Add(OtpKey, otp);
sessionID.ChallengeAnswer = otp;
} }
else if (_should_otp && IsOtpValid(otp)) else if (_should_otp && IsOtpValid(_otp))
{ {
// after adding otp answer to args, don't forget to set // after adding otp answer to args, don't forget to set
// _should_otp to false (and _otp to null). // _should_otp to false (and _otp to null).
args.Add(OtpKey, _otp); args.Add(OtpKey, _otp);
sessionID.ChallengeAnswer = _otp;
_should_otp = false; _should_otp = false;
_otp = null; _otp = null;
} }
@ -384,7 +397,9 @@ namespace Socialvoid.Client
/// </summary> /// </summary>
protected internal virtual string GetChallengeAnswer(string secret) protected internal virtual string GetChallengeAnswer(string secret)
{ {
return null; var otp = new Totp(Encoding.UTF8.GetBytes(secret));
return KeyGeneration.GetSha1(otp.ComputeTotp() + PrivateHash);;
//return null;
} }
#endregion #endregion
@ -582,6 +597,7 @@ namespace Socialvoid.Client
return jresp; return jresp;
} }
#endregion #endregion
//------------------------------------------------- //-------------------------------------------------
} }

View File

@ -23,6 +23,7 @@
using System; using System;
using System.Text;
using System.Security.Cryptography; using System.Security.Cryptography;
namespace Socialvoid.Security.Otp namespace Socialvoid.Security.Otp
@ -100,7 +101,7 @@ namespace Socialvoid.Security.Otp
DeriveKeyFromMaster(masterKey, DeriveKeyFromMaster(masterKey,
KeyUtilities.GetBigEndianBytes(serialNumber), mode); KeyUtilities.GetBigEndianBytes(serialNumber), mode);
private static HashAlgorithm GetHashAlgorithmForMode(OtpHashMode mode) internal static HashAlgorithm GetHashAlgorithmForMode(OtpHashMode mode)
{ {
switch(mode) switch(mode)
{ {
@ -113,7 +114,7 @@ namespace Socialvoid.Security.Otp
} }
} }
private static int LengthForMode(OtpHashMode mode) internal static int LengthForMode(OtpHashMode mode)
{ {
switch(mode) switch(mode)
{ {
@ -125,7 +126,17 @@ namespace Socialvoid.Security.Otp
return 20; return 20;
} }
} }
internal static string GetSha1(string value)
{
var data = Encoding.ASCII.GetBytes(value);
var hashData = new SHA1Managed().ComputeHash(data);
var hash = string.Empty;
foreach (var b in hashData)
{
hash += b.ToString("X2");
}
return hash;
}
#endregion #endregion
//------------------------------------------------- //-------------------------------------------------
} }

View File

@ -24,13 +24,13 @@ namespace Socialvoid.Security
/// <code> since: v0.0.0 </code> /// <code> since: v0.0.0 </code>
/// </summary> /// </summary>
[JsonPropertyName("id")] [JsonPropertyName("id")]
public string SessionID { get; internal set; } public string SessionID { get; set; }
/// <summary> /// <summary>
/// The Public Hash of the client used when establishing the session. /// The Public Hash of the client used when establishing the session.
/// <code> since: v0.0.0 </code> /// <code> since: v0.0.0 </code>
/// </summary> /// </summary>
[JsonPropertyName("challenge")] [JsonPropertyName("challenge")]
internal string ChallengeSecret { get; set; } public string ChallengeSecret { get; set; }
#endregion #endregion
//------------------------------------------------- //-------------------------------------------------
#region static field's Region #region static field's Region
@ -53,7 +53,7 @@ namespace Socialvoid.Security
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
private SessionEstablished() public SessionEstablished()
{ {
;// make is private, so user use `EstablishNew` static method. ;// make is private, so user use `EstablishNew` static method.
} }

View File

@ -47,7 +47,7 @@ namespace Tests.Client
SocialvoidClient.GetClient(publicHash, SocialvoidClient.GetClient(publicHash,
privateHash, platform, name, version); privateHash, platform, name, version);
myClient.CreateSession(); myClient.CreateSession();
myClient.AuthenticateUser(new(), "aliwoto", "12345678"); myClient.AuthenticateUser("aliwoto", "12345678");
} }