Add Cloud Password Support

This commit is contained in:
Afshin Arani 2016-11-16 17:01:00 +03:30
parent 7a6191871d
commit 6f9c328349
4 changed files with 52 additions and 2 deletions

View file

@ -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");
}
else if (errorMessage == "SESSION_PASSWORD_NEEDED")
{
throw new CloudPasswordNeededException("This Account has Cloud Password !");
}
else
{
throw new InvalidOperationException(errorMessage);

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using TeleSharp.TL;
@ -159,6 +161,33 @@ namespace TLSharp.Core
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)
{
@ -320,4 +349,8 @@ namespace TLSharp.Core
{
internal InvalidPhoneCodeException(string msg) : base(msg) { }
}
public class CloudPasswordNeededException : Exception
{
internal CloudPasswordNeededException(string msg) : base(msg) { }
}
}

View file

@ -26,6 +26,8 @@ namespace TLSharp.Tests
private string CodeToAuthenticate { get; set; }
private string PasswordToAuthenticate { get; set; }
private string NotRegisteredNumberToSignUp { get; set; }
private string UserNameToSendMessage { get; set; }
@ -80,6 +82,10 @@ namespace TLSharp.Tests
if (string.IsNullOrEmpty(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)];
if (string.IsNullOrEmpty(NotRegisteredNumberToSignUp))
Debug.WriteLine(appConfigMsgWarning, nameof(NotRegisteredNumberToSignUp));
@ -117,12 +123,18 @@ namespace TLSharp.Tests
{
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)
{
throw new Exception("CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram",
ex);
}
Assert.IsNotNull(user);
Assert.IsTrue(client.IsUserAuthorized());
}

View file

@ -5,6 +5,7 @@
<add key="ApiId" value="" />
<add key="NumberToAuthenticate" value="" />
<add key="CodeToAuthenticate" value="" />
<add key="PasswordToAuthenticate" value=""/>
<add key="NotRegisteredNumberToSignUp" value=""/>
<add key="NumberToSendMessage" value=""/>
<add key="UserNameToSendMessage" value=""/>