Validation of logged-in user can also be done by its user_id

This commit is contained in:
Wizou 2021-08-27 22:44:43 +02:00
parent ef93dda3ac
commit ce41af2f84
2 changed files with 20 additions and 5 deletions

View file

@ -103,7 +103,7 @@ await client.SendMessageAsync(target, "Hello, World");
An invalid API request can result in a RpcException being raised, reflecting the [error code and status text](https://core.telegram.org/api/errors) of the problem. An invalid API request can result in a RpcException being raised, reflecting the [error code and status text](https://core.telegram.org/api/errors) of the problem.
The other configuration items that you can override include: **session_pathname, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code** The other configuration items that you can override include: **session_pathname, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, user_id**
Optional API parameters have a default value of `null` when unset. Passing `null` for a required string/array is the same as *empty* (0-length). Required API parameters/fields can sometimes be set to 0 or `null` when unused (check API documentation). Optional API parameters have a default value of `null` when unset. Passing `null` for a required string/array is the same as *empty* (0-length). Required API parameters/fields can sometimes be set to 0 or `null` when unused (check API documentation).

View file

@ -604,13 +604,23 @@ namespace WTelegram
case MsgsAck msgsAck: case MsgsAck msgsAck:
break; // we don't do anything with these, for now break; // we don't do anything with these, for now
case BadMsgNotification badMsgNotification: case BadMsgNotification badMsgNotification:
{
Helpers.Log(4, $"BadMsgNotification {badMsgNotification.error_code} for msg #{(short)badMsgNotification.bad_msg_id.GetHashCode():X4}"); Helpers.Log(4, $"BadMsgNotification {badMsgNotification.error_code} for msg #{(short)badMsgNotification.bad_msg_id.GetHashCode():X4}");
goto default; var (type, tcs) = PullPendingRequest(badMsgNotification.bad_msg_id);
if (tcs != null)
{
if (_bareRequest == badMsgNotification.bad_msg_id) _bareRequest = 0;
_ = Task.Run(() => tcs.SetException(new ApplicationException($"BadMsgNotification {badMsgNotification.error_code}")));
}
else if (_updateHandler != null)
await _updateHandler?.Invoke(obj);
}
break;
default: default:
if (_bareRequest != 0) if (_bareRequest != 0)
{ {
var (type, tcs) = PullPendingRequest(_bareRequest); var (type, tcs) = PullPendingRequest(_bareRequest);
if (type.IsAssignableFrom(obj.GetType())) if (type?.IsAssignableFrom(obj.GetType()) == true)
{ {
_bareRequest = 0; _bareRequest = 0;
_ = Task.Run(() => tcs.SetResult(obj)); _ = Task.Run(() => tcs.SetResult(obj));
@ -670,12 +680,16 @@ namespace WTelegram
/// <returns>Detail about the logged user</returns> /// <returns>Detail about the logged user</returns>
public async Task<User> LoginUserIfNeeded(CodeSettings settings = null) public async Task<User> LoginUserIfNeeded(CodeSettings settings = null)
{ {
string phone_number = Config("phone_number"); string phone_number = null;
if (_session.User != null) if (_session.User != null)
{ {
try try
{ {
var prevUser = Schema.Deserialize<User>(_session.User); var prevUser = Schema.Deserialize<User>(_session.User);
var userId = _config("user_id"); // if config prefers to validate current user by its id, use it
if (userId != null && int.TryParse(userId, out int id) && prevUser.id == id)
return prevUser;
phone_number = Config("phone_number"); // otherwise, validation is done by the phone number
if (prevUser?.phone == string.Concat(phone_number.Where(char.IsDigit))) if (prevUser?.phone == string.Concat(phone_number.Where(char.IsDigit)))
return prevUser; return prevUser;
} }
@ -685,6 +699,7 @@ namespace WTelegram
} }
await Auth_LogOut(); await Auth_LogOut();
} }
phone_number ??= Config("phone_number");
var sentCode = await Auth_SendCode(phone_number, _apiId, _apiHash, settings ?? new()); var sentCode = await Auth_SendCode(phone_number, _apiId, _apiHash, settings ?? new());
Helpers.Log(3, $"A verification code has been sent via {sentCode.type.GetType().Name[17..]}"); Helpers.Log(3, $"A verification code has been sent via {sentCode.type.GetType().Name[17..]}");
var verification_code = Config("verification_code"); var verification_code = Config("verification_code");