mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Add Cloud Password Support
This commit is contained in:
parent
7a6191871d
commit
6f9c328349
|
|
@ -309,6 +309,10 @@ namespace TLSharp.Core.Network
|
||||||
{
|
{
|
||||||
throw new InvalidPhoneCodeException("The numeric code used to authenticate does not match the numeric code sent by SMS/Telegram");
|
throw new InvalidPhoneCodeException("The numeric code used to authenticate does not match the numeric code sent by SMS/Telegram");
|
||||||
}
|
}
|
||||||
|
else if (errorMessage == "SESSION_PASSWORD_NEEDED")
|
||||||
|
{
|
||||||
|
throw new CloudPasswordNeededException("This Account has Cloud Password !");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException(errorMessage);
|
throw new InvalidOperationException(errorMessage);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using TeleSharp.TL;
|
using TeleSharp.TL;
|
||||||
|
|
@ -159,6 +161,33 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
return ((TLUser)request.Response.user);
|
return ((TLUser)request.Response.user);
|
||||||
}
|
}
|
||||||
|
public async Task<TLPassword> GetPasswordSetting()
|
||||||
|
{
|
||||||
|
var request = new TLRequestGetPassword();
|
||||||
|
|
||||||
|
await _sender.Send(request);
|
||||||
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
return ((TLPassword)request.Response);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str)
|
||||||
|
{
|
||||||
|
|
||||||
|
byte[] password_bytes = Encoding.UTF8.GetBytes(password_str);
|
||||||
|
IEnumerable<byte> rv = password.current_salt.Concat(password_bytes).Concat(password.current_salt);
|
||||||
|
|
||||||
|
SHA256Managed hashstring = new SHA256Managed();
|
||||||
|
var password_hash = hashstring.ComputeHash(rv.ToArray());
|
||||||
|
|
||||||
|
var request = new TLRequestCheckPassword() { password_hash = password_hash };
|
||||||
|
await _sender.Send(request);
|
||||||
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
OnUserAuthenticated(((TLUser)request.Response.user));
|
||||||
|
|
||||||
|
return ((TLUser)request.Response.user);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<TLUser> SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName)
|
public async Task<TLUser> SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName)
|
||||||
{
|
{
|
||||||
|
|
@ -283,7 +312,7 @@ namespace TLSharp.Core
|
||||||
_session.AuthKey = authKey;
|
_session.AuthKey = authKey;
|
||||||
_session.TimeOffset = timeOffset;
|
_session.TimeOffset = timeOffset;
|
||||||
_transport = new TcpTransport(serverAddress, serverPort);
|
_transport = new TcpTransport(serverAddress, serverPort);
|
||||||
_session.ServerAddress =serverAddress;
|
_session.ServerAddress = serverAddress;
|
||||||
_session.Port = serverPort;
|
_session.Port = serverPort;
|
||||||
await ConnectAsync();
|
await ConnectAsync();
|
||||||
|
|
||||||
|
|
@ -320,4 +349,8 @@ namespace TLSharp.Core
|
||||||
{
|
{
|
||||||
internal InvalidPhoneCodeException(string msg) : base(msg) { }
|
internal InvalidPhoneCodeException(string msg) : base(msg) { }
|
||||||
}
|
}
|
||||||
|
public class CloudPasswordNeededException : Exception
|
||||||
|
{
|
||||||
|
internal CloudPasswordNeededException(string msg) : base(msg) { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,8 @@ namespace TLSharp.Tests
|
||||||
|
|
||||||
private string CodeToAuthenticate { get; set; }
|
private string CodeToAuthenticate { get; set; }
|
||||||
|
|
||||||
|
private string PasswordToAuthenticate { get; set; }
|
||||||
|
|
||||||
private string NotRegisteredNumberToSignUp { get; set; }
|
private string NotRegisteredNumberToSignUp { get; set; }
|
||||||
|
|
||||||
private string UserNameToSendMessage { get; set; }
|
private string UserNameToSendMessage { get; set; }
|
||||||
|
|
@ -80,6 +82,10 @@ namespace TLSharp.Tests
|
||||||
if (string.IsNullOrEmpty(CodeToAuthenticate))
|
if (string.IsNullOrEmpty(CodeToAuthenticate))
|
||||||
Debug.WriteLine(appConfigMsgWarning, nameof(CodeToAuthenticate));
|
Debug.WriteLine(appConfigMsgWarning, nameof(CodeToAuthenticate));
|
||||||
|
|
||||||
|
PasswordToAuthenticate = ConfigurationManager.AppSettings[nameof(PasswordToAuthenticate)];
|
||||||
|
if (string.IsNullOrEmpty(PasswordToAuthenticate))
|
||||||
|
Debug.WriteLine(appConfigMsgWarning, nameof(PasswordToAuthenticate));
|
||||||
|
|
||||||
NotRegisteredNumberToSignUp = ConfigurationManager.AppSettings[nameof(NotRegisteredNumberToSignUp)];
|
NotRegisteredNumberToSignUp = ConfigurationManager.AppSettings[nameof(NotRegisteredNumberToSignUp)];
|
||||||
if (string.IsNullOrEmpty(NotRegisteredNumberToSignUp))
|
if (string.IsNullOrEmpty(NotRegisteredNumberToSignUp))
|
||||||
Debug.WriteLine(appConfigMsgWarning, nameof(NotRegisteredNumberToSignUp));
|
Debug.WriteLine(appConfigMsgWarning, nameof(NotRegisteredNumberToSignUp));
|
||||||
|
|
@ -117,12 +123,18 @@ namespace TLSharp.Tests
|
||||||
{
|
{
|
||||||
user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);
|
user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);
|
||||||
}
|
}
|
||||||
|
catch (CloudPasswordNeededException ex)
|
||||||
|
{
|
||||||
|
var password = await client.GetPasswordSetting();
|
||||||
|
var password_str = PasswordToAuthenticate;
|
||||||
|
|
||||||
|
user = await client.MakeAuthWithPasswordAsync(password,password_str);
|
||||||
|
}
|
||||||
catch (InvalidPhoneCodeException ex)
|
catch (InvalidPhoneCodeException ex)
|
||||||
{
|
{
|
||||||
throw new Exception("CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram",
|
throw new Exception("CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram",
|
||||||
ex);
|
ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.IsNotNull(user);
|
Assert.IsNotNull(user);
|
||||||
Assert.IsTrue(client.IsUserAuthorized());
|
Assert.IsTrue(client.IsUserAuthorized());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
<add key="ApiId" value="" />
|
<add key="ApiId" value="" />
|
||||||
<add key="NumberToAuthenticate" value="" />
|
<add key="NumberToAuthenticate" value="" />
|
||||||
<add key="CodeToAuthenticate" value="" />
|
<add key="CodeToAuthenticate" value="" />
|
||||||
|
<add key="PasswordToAuthenticate" value=""/>
|
||||||
<add key="NotRegisteredNumberToSignUp" value=""/>
|
<add key="NotRegisteredNumberToSignUp" value=""/>
|
||||||
<add key="NumberToSendMessage" value=""/>
|
<add key="NumberToSendMessage" value=""/>
|
||||||
<add key="UserNameToSendMessage" value=""/>
|
<add key="UserNameToSendMessage" value=""/>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue