mark Auth_* methods as obsolete. call Auth_CancelCode on exception

This commit is contained in:
Wizou 2022-04-22 23:07:43 +02:00
parent 9d18eefe32
commit 61dd83a162
3 changed files with 44 additions and 29 deletions

4
FAQ.md
View file

@ -201,8 +201,8 @@ See the [full method list](https://core.telegram.org/methods) (just replace the
A session file is created or resumed automatically on startup, and maintained up-to-date automatically throughout the session. A session file is created or resumed automatically on startup, and maintained up-to-date automatically throughout the session.
That session file is incompatible with TLSharp so you cannot reuse a TLSharp .dat file. You'll need to create a new session. That session file is incompatible with TLSharp so you cannot reuse a TLSharp .dat file. You'll need to create a new session.
You don't have to call methods Auth_SignIn/SignUp/.. manually anymore because all the login phase is handled automatically by calling `await client.LoginUserIfNeeded()` after creating the client. **DON'T** call methods Auth_SendCode/SignIn/SignUp/... because all the login phase is handled automatically by calling `await client.LoginUserIfNeeded()` after creating the client.
Your Config callback just need to provide the various login answers if they are needed. Your Config callback just need to provide the various login answers if they are needed (see [ReadMe](README.md)).
In particular, it will detect and handle automatically the various login cases/particularity like: In particular, it will detect and handle automatically the various login cases/particularity like:
* Login not necessary (when a session is resumed with an already logged-in user) * Login not necessary (when a session is resumed with an already logged-in user)
* 2FA password required (your Config needs to provide "password") * 2FA password required (your Config needs to provide "password")

View file

@ -912,6 +912,7 @@ namespace WTelegram
} }
phone_number ??= Config("phone_number"); phone_number ??= Config("phone_number");
Auth_SentCode sentCode; Auth_SentCode sentCode;
#pragma warning disable CS0618 // Auth_* methods are marked as obsolete
try try
{ {
sentCode = await this.Auth_SendCode(phone_number, _session.ApiId, _apiHash ??= Config("api_hash"), settings ??= new()); sentCode = await this.Auth_SendCode(phone_number, _session.ApiId, _apiHash ??= Config("api_hash"), settings ??= new());
@ -920,11 +921,13 @@ namespace WTelegram
{ {
sentCode = await this.Auth_SendCode(phone_number, _session.ApiId, _apiHash, settings); sentCode = await this.Auth_SendCode(phone_number, _session.ApiId, _apiHash, settings);
} }
Auth_AuthorizationBase authorization = null;
try
{
resent: resent:
var timeout = DateTime.UtcNow + TimeSpan.FromSeconds(sentCode.timeout); var timeout = DateTime.UtcNow + TimeSpan.FromSeconds(sentCode.timeout);
OnUpdate(sentCode); OnUpdate(sentCode);
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..]}");
Auth_AuthorizationBase authorization = null;
for (int retry = 1; authorization == null; retry++) for (int retry = 1; authorization == null; retry++)
try try
{ {
@ -952,6 +955,12 @@ namespace WTelegram
catch (RpcException e) when (e.Code == 400 && e.Message == "PHONE_CODE_INVALID" && retry != 3) catch (RpcException e) when (e.Code == 400 && e.Message == "PHONE_CODE_INVALID" && retry != 3)
{ {
} }
}
catch
{
await this.Auth_CancelCode(phone_number, sentCode.phone_code_hash);
throw;
}
if (authorization is Auth_AuthorizationSignUpRequired signUpRequired) if (authorization is Auth_AuthorizationSignUpRequired signUpRequired)
{ {
var waitUntil = DateTime.UtcNow.AddSeconds(3); var waitUntil = DateTime.UtcNow.AddSeconds(3);
@ -962,6 +971,7 @@ namespace WTelegram
if (wait > TimeSpan.Zero) await Task.Delay(wait); // we get a FLOOD_WAIT_3 if we SignUp too fast if (wait > TimeSpan.Zero) await Task.Delay(wait); // we get a FLOOD_WAIT_3 if we SignUp too fast
authorization = await this.Auth_SignUp(phone_number, sentCode.phone_code_hash, first_name, last_name); authorization = await this.Auth_SignUp(phone_number, sentCode.phone_code_hash, first_name, last_name);
} }
#pragma warning restore CS0618
return LoginAlreadyDone(authorization); return LoginAlreadyDone(authorization);
} }

View file

@ -97,6 +97,7 @@ namespace TL
/// <param name="api_id">Application identifier (see <a href="https://corefork.telegram.org/myapp">App configuration</a>)</param> /// <param name="api_id">Application identifier (see <a href="https://corefork.telegram.org/myapp">App configuration</a>)</param>
/// <param name="api_hash">Application secret hash (see <a href="https://corefork.telegram.org/myapp">App configuration</a>)</param> /// <param name="api_hash">Application secret hash (see <a href="https://corefork.telegram.org/myapp">App configuration</a>)</param>
/// <param name="settings">Settings for the code type to send</param> /// <param name="settings">Settings for the code type to send</param>
[Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")]
public static Task<Auth_SentCode> Auth_SendCode(this Client client, string phone_number, int api_id, string api_hash, CodeSettings settings) public static Task<Auth_SentCode> Auth_SendCode(this Client client, string phone_number, int api_id, string api_hash, CodeSettings settings)
=> client.Invoke(new Auth_SendCode => client.Invoke(new Auth_SendCode
{ {
@ -111,6 +112,7 @@ namespace TL
/// <param name="phone_code_hash">SMS-message ID</param> /// <param name="phone_code_hash">SMS-message ID</param>
/// <param name="first_name">New user first name</param> /// <param name="first_name">New user first name</param>
/// <param name="last_name">New user last name</param> /// <param name="last_name">New user last name</param>
[Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")]
public static Task<Auth_AuthorizationBase> Auth_SignUp(this Client client, string phone_number, string phone_code_hash, string first_name, string last_name) public static Task<Auth_AuthorizationBase> Auth_SignUp(this Client client, string phone_number, string phone_code_hash, string first_name, string last_name)
=> client.Invoke(new Auth_SignUp => client.Invoke(new Auth_SignUp
{ {
@ -124,6 +126,7 @@ namespace TL
/// <param name="phone_number">Phone number in the international format</param> /// <param name="phone_number">Phone number in the international format</param>
/// <param name="phone_code_hash">SMS-message ID, obtained from <a href="https://corefork.telegram.org/method/auth.sendCode">auth.sendCode</a></param> /// <param name="phone_code_hash">SMS-message ID, obtained from <a href="https://corefork.telegram.org/method/auth.sendCode">auth.sendCode</a></param>
/// <param name="phone_code">Valid numerical code from the SMS-message</param> /// <param name="phone_code">Valid numerical code from the SMS-message</param>
[Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")]
public static Task<Auth_AuthorizationBase> Auth_SignIn(this Client client, string phone_number, string phone_code_hash, string phone_code) public static Task<Auth_AuthorizationBase> Auth_SignIn(this Client client, string phone_number, string phone_code_hash, string phone_code)
=> client.Invoke(new Auth_SignIn => client.Invoke(new Auth_SignIn
{ {
@ -217,6 +220,7 @@ namespace TL
/// <summary>Resend the login code via another medium, the phone code type is determined by the return value of the previous auth.sendCode/auth.resendCode: see <a href="https://corefork.telegram.org/api/auth">login</a> for more info. <para>See <a href="https://corefork.telegram.org/method/auth.resendCode"/></para> <para>Possible <see cref="RpcException"/> codes: 400,406 (<a href="https://corefork.telegram.org/method/auth.resendCode#possible-errors">details</a>)</para></summary> /// <summary>Resend the login code via another medium, the phone code type is determined by the return value of the previous auth.sendCode/auth.resendCode: see <a href="https://corefork.telegram.org/api/auth">login</a> for more info. <para>See <a href="https://corefork.telegram.org/method/auth.resendCode"/></para> <para>Possible <see cref="RpcException"/> codes: 400,406 (<a href="https://corefork.telegram.org/method/auth.resendCode#possible-errors">details</a>)</para></summary>
/// <param name="phone_number">The phone number</param> /// <param name="phone_number">The phone number</param>
/// <param name="phone_code_hash">The phone code hash obtained from <a href="https://corefork.telegram.org/method/auth.sendCode">auth.sendCode</a></param> /// <param name="phone_code_hash">The phone code hash obtained from <a href="https://corefork.telegram.org/method/auth.sendCode">auth.sendCode</a></param>
[Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")]
public static Task<Auth_SentCode> Auth_ResendCode(this Client client, string phone_number, string phone_code_hash) public static Task<Auth_SentCode> Auth_ResendCode(this Client client, string phone_number, string phone_code_hash)
=> client.Invoke(new Auth_ResendCode => client.Invoke(new Auth_ResendCode
{ {
@ -227,6 +231,7 @@ namespace TL
/// <summary>Cancel the login verification code <para>See <a href="https://corefork.telegram.org/method/auth.cancelCode"/></para> <para>Possible <see cref="RpcException"/> codes: 400,406 (<a href="https://corefork.telegram.org/method/auth.cancelCode#possible-errors">details</a>)</para></summary> /// <summary>Cancel the login verification code <para>See <a href="https://corefork.telegram.org/method/auth.cancelCode"/></para> <para>Possible <see cref="RpcException"/> codes: 400,406 (<a href="https://corefork.telegram.org/method/auth.cancelCode#possible-errors">details</a>)</para></summary>
/// <param name="phone_number">Phone number</param> /// <param name="phone_number">Phone number</param>
/// <param name="phone_code_hash">Phone code hash from <a href="https://corefork.telegram.org/method/auth.sendCode">auth.sendCode</a></param> /// <param name="phone_code_hash">Phone code hash from <a href="https://corefork.telegram.org/method/auth.sendCode">auth.sendCode</a></param>
[Obsolete("Use LoginUserIfNeeded instead of this method. See https://github.com/wiz0u/WTelegramClient/blob/master/FAQ.md#tlsharp")]
public static Task<bool> Auth_CancelCode(this Client client, string phone_number, string phone_code_hash) public static Task<bool> Auth_CancelCode(this Client client, string phone_number, string phone_code_hash)
=> client.Invoke(new Auth_CancelCode => client.Invoke(new Auth_CancelCode
{ {