fix problem in Register method

Signed-off-by: Aliwoto <aminnimaj@gmail.com>
This commit is contained in:
Aliwoto 2021-10-06 21:54:55 +00:00
parent b090408334
commit d7bb3c45b7
No known key found for this signature in database
GPG Key ID: 646B4FE4205EC48C
9 changed files with 171 additions and 49 deletions

View File

@ -217,7 +217,7 @@ namespace Socialvoid.Client
/// next jsonrpc request.
/// <code> since: v0.0.0 </code>
/// </summary>
protected string _otp_answer;
protected string _otp_challenge;
/// <summary>
/// the terms of service received from the socialvoid's server.
/// <code> since: v0.0.0 </code>
@ -395,7 +395,7 @@ namespace Socialvoid.Client
/// <exception cref="TwoFactorAuthenticationRequiredException">
/// Thrown if two-factor authentication is required.
/// </exception>
public virtual void AuthenticateUser(string username, string password,
public virtual bool AuthenticateUser(string username, string password,
string otp = null, SessionIdentification sessionID = null)
{
if (sessionID == null)
@ -431,31 +431,20 @@ namespace Socialvoid.Client
args.Add(OtpKey, otp);
}
if (_should_otp && IsOtpValid(_otp_answer))
CheckAnswer(sessionID);
var jresp = ParseContent<bool>(
GetMessage(AuthenticateUserMethod, args));
if (jresp == null)
{
// after adding otp answer to args, don't forget to set
// _should_otp to false (and _otp to null).
//args.Add(OtpKey, _challenge);
sessionID.ChallengeAnswer = _otp_answer;
_should_otp = false;
_otp_answer = null;
// this shouldn't happen in normal cases.
throw new InvalidOperationException(
"received invalid response from server.");
}
var request = GetRpcRequest(AuthenticateUserMethod, args);
var message = new HttpRequestMessage(HttpMethod.Post, _endpoint);
message.Content = SerializeContent(request);
message.Content.Headers.ContentType = _contentTypeValue;
var resp = HttpClient.Send(message);
var contentStr = ReadFromContent(resp.Content);
Console.WriteLine(contentStr);
return jresp.Result;
}
/// <summary>
/// CreateSession method (session.create), establishes a new session
@ -517,6 +506,11 @@ namespace Socialvoid.Client
SessionIdentification sessionID = null,
bool store = true)
{
if (password == null || password.Length < 8)
{
throw new ArgumentException("Password must be at least 8 characters long.");
}
if (sessionID == null)
{
if (_session == null)
@ -545,9 +539,11 @@ namespace Socialvoid.Client
tosID = _termsOfService.ID;
}
CheckAnswer(sessionID);
JArgs args = new(){
{SessionIDKey, sessionID},
{tosID, tosID},
{ToSKey, tosID},
{ToSAgreeKey, true},
{UsernameKey, username},
{PasswordKey, password},
@ -728,8 +724,7 @@ namespace Socialvoid.Client
protected internal JResponse<VType> ParseContent<VType>(
HttpRequestMessage message,
bool ex = true,
bool answerChallge = true)
where VType : class
bool answerChallege = true)
{
if (HttpClient == null)
{
@ -741,7 +736,7 @@ namespace Socialvoid.Client
throw new InvalidOperationException("HttpClient.Send returned null");
}
if (!answerChallge)
if (!answerChallege)
{
// if we don't need to answer challenge, we can just return the
// response.
@ -751,13 +746,38 @@ namespace Socialvoid.Client
if (jresp.Result is IChallenge result && result.HasSecret())
{
_should_otp = true;
_otp_answer = GetChallengeAnswer(result.GetChallengeSecret());
_otp_challenge = result.GetChallengeSecret();
//_otp_answer = GetChallengeAnswer(result.GetChallengeSecret());
// set challenege secret to null to avoid sending it again.
// this will avoid future conflicts in using old challenge secret.
result.DelSecret();
}
return jresp;
}
/// <summary>
/// Checks the <see cref="_should_otp"/> value of this client and
/// if it's <c>true</c>, it will set the
/// <see cref="SessionIdentification.ChallengeAnswer"/> to
/// the correct answer.
/// <code> since: v0.0.0 </code>
/// </summary>
protected virtual void CheckAnswer(SessionIdentification sessionID)
{
if (sessionID == null)
{
return;
}
if (_should_otp && IsOtpValid(_otp_challenge))
{
// after adding otp answer to args, don't forget to set
// _should_otp to false (and _otp to null).
//args.Add(OtpKey, _challenge);
sessionID.ChallengeAnswer = GetChallengeAnswer(_otp_challenge);
//_should_otp = false;
//_otp_challenge = null;
}
}
#endregion
//-------------------------------------------------
#region Set Method's Region
@ -885,7 +905,6 @@ namespace Socialvoid.Client
protected internal static JResponse<VType> ParseContent<VType>(
HttpContent content,
bool ex = true)
where VType : class
{
var jresp = JResponse<VType>.Deserialize(ReadFromContent(content));
if (ex && jresp.HasError())

View File

@ -10,7 +10,7 @@ namespace Socialvoid.JObjects
/// <code> since: v0.0.0 </code>
/// </summary>
[Serializable]
public sealed class JResponse<T> where T: class
public sealed class JResponse<T>
{
//-------------------------------------------------
#region Constant's Region

View File

@ -36,7 +36,7 @@ namespace Socialvoid.Security.Otp
{
//-------------------------------------------------
#region Constants Region
private const int MAX_EXTERNAL = 2;
private const int MAX_EXTERNAL = 3;
private const string EXTERNAL_CHARS = "\u0061\u006e\u0073\u0074";
private const string OTP_CHARS =
"\u006f" + MIDI_CHAR + "\u0070";

View File

@ -113,7 +113,7 @@ namespace Socialvoid.Security
/// otherwise a valid challenge secret.
/// </returns>
public string GetChallengeSecret() =>
HasSecret() ? null : ChallengeSecret;
!HasSecret() ? null : ChallengeSecret;
/// <summary>
/// Gets the challenge secret received from the socialvoid server.
/// <code> since: v0.0.0 </code>

View File

@ -34,7 +34,7 @@ namespace Socialvoid.SvObjects.Media
/// The width of the picture.
/// <code> since: v0.0.0 </code>
/// </summary>
[JsonPropertyName("height")]
[JsonPropertyName("width")]
public virtual int Width { get; set;}
/// <summary>
/// The height of the picture.

View File

@ -72,10 +72,10 @@ namespace Socialvoid.SvObjects.Media
/// <code> since: v0.0.0 </code>
/// </summary>
[JsonPropertyName("created_timestamp")]
public virtual long CreatedAt
public virtual string CreatedAt
{
get => _createdAt.Ticks;
set => _createdAt = new DateTime(value);
get => _createdAt.Ticks.ToString();
set => _createdAt = new DateTime(Convert.ToInt64(value));
}
#endregion
//-------------------------------------------------
@ -123,7 +123,7 @@ namespace Socialvoid.SvObjects.Media
/// </summary>
public System.DateTime GetTimeStamp() =>
_createdAt != DateTime.MinValue ? _createdAt:
new(CreatedAt);
new(Convert.ToInt64(CreatedAt));
#endregion
//-------------------------------------------------
}

View File

@ -63,7 +63,7 @@ namespace Socialvoid.SvObjects
/// The ID of the session obtained when establishing a session.
/// <code> since: v0.0.0 </code>
/// </summary>
[JsonPropertyName("name")]
[JsonPropertyName("username")]
public string Username { get; set; }
/// <summary>
/// The ID of the session obtained when establishing a session.

View File

@ -27,7 +27,6 @@ namespace Tests.Client
Log("got exception of type", ex.GetType(), "with error code of", ex.ErrorCode);
if (ex.Message != message || (int)ex.ErrorCode != errorCode)
{
throw;
}
}
@ -53,20 +52,42 @@ namespace Tests.Client
}
catch (Exception e)
{
NUnit.Framework.Assert.Fail("Exception thrown: " + e.Message);
Assert.Fail("Exception thrown: " + e.Message);
return;
}
Assert.IsNotNull(myClient.GetSession());
try
{
myClient.GetTermsOfService();
var peer = myClient.Register("aliwoto", "12345678", "エイリ・ヲト");
var peer = myClient.Register("aliwoto6", "ilovehentai69", "エイリ・ヲト");
}
catch (UsernameAlreadyExistsException)
{
// if the username already exists, we can ignore this exception
// and continue with the test
Console.WriteLine("Username already exists");
}
myClient.AuthenticateUser("aliwoto", "12345678");
catch (Exception ex)
{
Assert.Fail("Exception thrown: " + ex.Message);
return;
}
bool isAuthenticated = false;
try
{
isAuthenticated = myClient.AuthenticateUser("aliwoto", "ilovehentai69");
}
catch (Exception ex)
{
Log(ex);
Assert.Fail("Exception thrown: " + ex.Message);
return;
}
if (!isAuthenticated)
{
Assert.Fail("User could not be authenticated");
}
}

View File

@ -1,8 +1,90 @@
{
"endpoint": "http://127.0.0.1:5001/",
"public_hash": "4c7148caff498d24deee6c8325f1c15773d637ed76c3a4056e00b77b2beb3097",
"private_hash": "866d3218b239d39c174fa2b16f54e0fa58f9c69fce8c2d941c12a47a7bc75229",
"platform": "Linux",
"name": "Test Python RCP Client",
"version": "1.0.0.0"
"jsonrpc": "2.0",
"result": {
"id": "ecb6501631337dc4de338acc5e71388c05f59a92a36c51b59bfbe274bdbc29b0",
"type": "USER",
"name": "エイリ・ヲト",
"username": "aliwoto1",
"display_picture_sizes": [{
"width": 640,
"height": 640,
"document": {
"id": "35714fd511b6064908da4ed77c12f587-4cef1ae78044daa6924a80895c42d55b5e9c73202df59eff21d68cec6c1b8b3b-03e762e2",
"file_mime": "image/jpeg",
"file_name": "ecb6501631337dc4de338acc5e71388c05f59a92a36c51b59bfbe274bdbc29b0_default_640x640.jpg",
"file_size": 17583,
"file_type": "PHOTO",
"flags": [],
"created_timestamp": "1633548428"
}
},
{
"width": 512,
"height": 512,
"document": {
"id": "35714fd511b6064908da4ed77c12f587-4cef1ae78044daa6924a80895c42d55b5e9c73202df59eff21d68cec6c1b8b3b-b00caf2a",
"file_mime": "image/jpeg",
"file_name": "ecb6501631337dc4de338acc5e71388c05f59a92a36c51b59bfbe274bdbc29b0_default_512x512.jpg",
"file_size": 13460,
"file_type": "PHOTO",
"flags": [],
"created_timestamp": "1633548428"
}
},
{
"width": 360,
"height": 360,
"document": {
"id": "35714fd511b6064908da4ed77c12f587-4cef1ae78044daa6924a80895c42d55b5e9c73202df59eff21d68cec6c1b8b3b-c1aaaf42",
"file_mime": "image/jpeg",
"file_name": "ecb6501631337dc4de338acc5e71388c05f59a92a36c51b59bfbe274bdbc29b0_default_360x360.jpg",
"file_size": 63505,
"file_type": "PHOTO",
"flags": [],
"created_timestamp": "1633548428"
}
},
{
"width": 160,
"height": 160,
"document": {
"id": "35714fd511b6064908da4ed77c12f587-4cef1ae78044daa6924a80895c42d55b5e9c73202df59eff21d68cec6c1b8b3b-b5e56dd9",
"file_mime": "image/jpeg",
"file_name": "ecb6501631337dc4de338acc5e71388c05f59a92a36c51b59bfbe274bdbc29b0_default_160x160.jpg",
"file_size": 2999,
"file_type": "PHOTO",
"flags": [],
"created_timestamp": "1633548428"
}
},
{
"width": 64,
"height": 64,
"document": {
"id": "35714fd511b6064908da4ed77c12f587-4cef1ae78044daa6924a80895c42d55b5e9c73202df59eff21d68cec6c1b8b3b-6d540f46",
"file_mime": "image/jpeg",
"file_name": "ecb6501631337dc4de338acc5e71388c05f59a92a36c51b59bfbe274bdbc29b0_default_64x64.jpg",
"file_size": 1895,
"file_type": "PHOTO",
"flags": [],
"created_timestamp": "1633548428"
}
},
{
"width": 32,
"height": 32,
"document": {
"id": "35714fd511b6064908da4ed77c12f587-4cef1ae78044daa6924a80895c42d55b5e9c73202df59eff21d68cec6c1b8b3b-b73336ed",
"file_mime": "image/jpeg",
"file_name": "ecb6501631337dc4de338acc5e71388c05f59a92a36c51b59bfbe274bdbc29b0_default_32x32.jpg",
"file_size": 1188,
"file_type": "PHOTO",
"flags": [],
"created_timestamp": "1633548428"
}
}
],
"flags": []
},
"id": 637691452284371062
}