From a029c33e3469bf3d78f4a91ac5b5039c6ea48a90 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Sun, 30 Oct 2016 22:31:00 +0800 Subject: [PATCH 1/2] TelegramClient ctor: move null checks earlier This way we avoid creating a FileSessionStore object or initializing the TLContext if API credentials are not supplied, to be on the safe side. --- TLSharp.Core/TelegramClient.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 03083f4..2fa8c3a 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -31,16 +31,17 @@ namespace TLSharp.Core public TelegramClient(int apiId, string apiHash, ISessionStore store = null, string sessionUserId = "session") { + if (apiId == default(int)) + throw new MissingApiConfigurationException("API_ID"); + if (string.IsNullOrEmpty(apiHash)) + throw new MissingApiConfigurationException("API_HASH"); + if (store == null) store = new FileSessionStore(); TLContext.Init(); _apiHash = apiHash; _apiId = apiId; - if (_apiId == default(int)) - throw new MissingApiConfigurationException("API_ID"); - if (string.IsNullOrEmpty(_apiHash)) - throw new MissingApiConfigurationException("API_HASH"); _session = Session.TryLoadOrCreateNew(store, sessionUserId); _transport = new TcpTransport(_session.ServerAddress, _session.Port); From 34e89f3f3708ad692b2ddf101c98346ea785ca91 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Sun, 30 Oct 2016 23:34:12 +0800 Subject: [PATCH 2/2] Require some parameters as non empty & not null before proceeding --- TLSharp.Core/TelegramClient.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 2fa8c3a..85561ad 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -99,6 +99,9 @@ namespace TLSharp.Core public async Task IsPhoneRegisteredAsync(string phoneNumber) { + if (String.IsNullOrWhiteSpace(phoneNumber)) + throw new ArgumentNullException(nameof(phoneNumber)); + if (_sender == null) throw new InvalidOperationException("Not connected!"); @@ -111,6 +114,9 @@ namespace TLSharp.Core public async Task SendCodeRequestAsync(string phoneNumber) { + if (String.IsNullOrWhiteSpace(phoneNumber)) + throw new ArgumentNullException(nameof(phoneNumber)); + var completed = false; TLRequestSendCode request = null; @@ -136,6 +142,15 @@ namespace TLSharp.Core public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code) { + if (String.IsNullOrWhiteSpace(phoneNumber)) + throw new ArgumentNullException(nameof(phoneNumber)); + + if (String.IsNullOrWhiteSpace(phoneCodeHash)) + throw new ArgumentNullException(nameof(phoneCodeHash)); + + if (String.IsNullOrWhiteSpace(code)) + throw new ArgumentNullException(nameof(code)); + var request = new TLRequestSignIn() { phone_number = phoneNumber, phone_code_hash = phoneCodeHash, phone_code = code }; await _sender.Send(request); await _sender.Receive(request);