Skip to content

Commit

Permalink
Remove non-generic data decryption method
Browse files Browse the repository at this point in the history
  • Loading branch information
poulad committed Sep 2, 2018
1 parent ab1f460 commit 9cddd92
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 156 deletions.
36 changes: 14 additions & 22 deletions src/Telegram.Bot.Extensions.Passport/Decryption/Decrypter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,38 @@ EncryptedCredentials encryptedCredentials
}

/// <inheritdoc />
public string DecryptData(
public TValue DecryptData<TValue>(
string encryptedData,
DataCredentials dataCredentials
)
where TValue : IDecryptedValue
{
if (encryptedData is null)
throw new ArgumentNullException(nameof(encryptedData));
if (dataCredentials is null)
throw new ArgumentNullException(nameof(dataCredentials));
if (dataCredentials.Secret is null)
throw new ArgumentNullException(nameof(dataCredentials.Secret));
if (dataCredentials.DataHash is null)
throw new ArgumentNullException(nameof(dataCredentials.DataHash));

byte[] data = Convert.FromBase64String(encryptedData);
if (data.Length == 0)
throw new ArgumentException("Data array is empty.", nameof(encryptedData));
if (data.Length % 16 != 0)
throw new PassportDataDecryptionException($"Invalid data length: {data.Length}");
throw new PassportDataDecryptionException
($"Data length is not divisible by 16: {data.Length}.");

byte[] dataSecret = Convert.FromBase64String(dataCredentials.Secret);

byte[] dataHash = Convert.FromBase64String(dataCredentials.DataHash);
if (dataHash.Length != 32)
throw new PassportDataDecryptionException($"Invalid hash length: {dataHash.Length}");

byte[] dataSecret = Convert.FromBase64String(dataCredentials.Secret);
throw new PassportDataDecryptionException($"Hash length is not 32: {dataHash.Length}.");

byte[] decryptedData = DecryptDataBytes(data, dataSecret, dataHash);
string content = Encoding.UTF8.GetString(decryptedData);

return content;
}

/// <inheritdoc />
public TValue DecryptData<TValue>(
string encryptedData,
DataCredentials dataCredentials
)
where TValue : IDecryptedValue
{
if (encryptedData is null)
throw new ArgumentNullException(nameof(encryptedData));
if (dataCredentials is null)
throw new ArgumentNullException(nameof(dataCredentials));

string json = DecryptData(encryptedData, dataCredentials);
return JsonConvert.DeserializeObject<TValue>(json);
return JsonConvert.DeserializeObject<TValue>(content);
}

/// <inheritdoc />
Expand Down
5 changes: 0 additions & 5 deletions src/Telegram.Bot.Extensions.Passport/Decryption/IDecrypter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ Credentials DecryptCredentials(
EncryptedCredentials encryptedCredentials
);

string DecryptData(
string encryptedData,
DataCredentials dataCredentials
);

TValue DecryptData<TValue>(
string encryptedData,
DataCredentials dataCredentials
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable PossibleNullReferenceException
// ReSharper disable CheckNamespace
// ReSharper disable StringLiteralTypo

using System;
using System.Net.Http;
Expand Down Expand Up @@ -41,7 +42,7 @@ public IdentityCardAndUtilityBillTests(
}

[OrderedFact("Should generate passport authorization request link")]
public async Task Should_generate_auth_link()
public async Task Should_Generate_Auth_Link()
{
const string publicKey = "-----BEGIN PUBLIC KEY-----\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0VElWoQA2SK1csG2/sY/\n" +
Expand Down Expand Up @@ -75,14 +76,14 @@ public async Task Should_generate_auth_link()
AuthorizationRequest authReq = new AuthorizationRequest(
botId: _fixture.BotUser.Id,
publicKey: publicKey,
nonce: "TEST",
nonce: "Test nonce for id card & utility bill",
scope: scope
);

await BotClient.SendTextMessageAsync(
_fixture.SupergroupChat,
"Share your *identity card with a selfie* and " +
"a *utiltiy bill with its translation* with bot using Passport.\n\n" +
"a *utility bill with its translation* with bot using Passport.\n\n" +
"1. Click inline button\n" +
"2. Open link in browser to redirect you back to Telegram passport\n" +
"3. Authorize bot to access the info",
Expand All @@ -98,7 +99,7 @@ await BotClient.SendTextMessageAsync(
}

[OrderedFact("Should validate values in the Passport massage")]
public void Should_validate_passport_message()
public void Should_Validate_Passport_Message()
{
Update update = _classFixture.Entity;
PassportData passportData = update.Message.PassportData;
Expand Down Expand Up @@ -154,7 +155,7 @@ public void Should_validate_passport_message()
}

[OrderedFact("Should decrypt and validate credentials")]
public void Should_decrypt_credentials()
public void Should_Decrypt_Credentials()
{
Update update = _classFixture.Entity;
PassportData passportData = update.Message.PassportData;
Expand All @@ -170,9 +171,9 @@ public void Should_decrypt_credentials()

Assert.NotNull(credentials);
Assert.NotNull(credentials.SecureData);
Assert.Equal("TEST", credentials.Nonce);
Assert.Equal("Test nonce for id card & utility bill", credentials.Nonce);

// decryption of docuemnt data in 'identity_card' element requires accompanying DataCredentials
// decryption of document data in 'identity_card' element requires accompanying DataCredentials
Assert.NotNull(credentials.SecureData.IdentityCard);
Assert.NotNull(credentials.SecureData.IdentityCard.Data);
Assert.NotEmpty(credentials.SecureData.IdentityCard.Data.Secret);
Expand All @@ -198,19 +199,20 @@ public void Should_decrypt_credentials()

// decryption of file scan in 'utility_bill' element requires accompanying FileCredentials
Assert.NotNull(credentials.SecureData.UtilityBill.Files);
FileCredentials billFileCreds = Assert.Single(credentials.SecureData.UtilityBill.Files);
Assert.NotEmpty(billFileCreds.Secret);
Assert.NotEmpty(billFileCreds.FileHash);
FileCredentials billCredentials = Assert.Single(credentials.SecureData.UtilityBill.Files);
Assert.NotEmpty(billCredentials.Secret);
Assert.NotEmpty(billCredentials.FileHash);

// decryption of translation file scan in 'utility_bill' element requires accompanying FileCredentials
Assert.NotNull(credentials.SecureData.UtilityBill.Files);
FileCredentials billTranslationFileCreds = Assert.Single(credentials.SecureData.UtilityBill.Translation);
Assert.NotEmpty(billTranslationFileCreds.Secret);
Assert.NotEmpty(billTranslationFileCreds.FileHash);
FileCredentials billTranslationFileCredentials =
Assert.Single(credentials.SecureData.UtilityBill.Translation);
Assert.NotEmpty(billTranslationFileCredentials.Secret);
Assert.NotEmpty(billTranslationFileCredentials.FileHash);
}

[OrderedFact("Should decrypt docuemnt data in 'identity_card' element")]
public void Should_decrypt_identity_card_element_document()
[OrderedFact("Should decrypt document data in 'identity_card' element")]
public void Should_Decrypt_Identity_Card_Element_Document()
{
Update update = _classFixture.Entity;
PassportData passportData = update.Message.PassportData;
Expand All @@ -221,12 +223,6 @@ public void Should_decrypt_identity_card_element_document()
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);
EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card");

string documentDataJson = decrypter.DecryptData(
idCardEl.Data,
credentials.SecureData.IdentityCard.Data
);
Assert.StartsWith("{", documentDataJson);

IdDocumentData documentData = decrypter.DecryptData<IdDocumentData>(
idCardEl.Data,
credentials.SecureData.IdentityCard.Data
Expand All @@ -244,7 +240,7 @@ public void Should_decrypt_identity_card_element_document()
}

[OrderedFact("Should decrypt front side photo in 'identity_card' element")]
public async Task Should_decrypt_identity_card_element_frontside()
public async Task Should_Decrypt_Identity_Card_Element_Front_Side()
{
Update update = _classFixture.Entity;
PassportData passportData = update.Message.PassportData;
Expand Down Expand Up @@ -274,7 +270,7 @@ await BotClient.GetInfoAndDownloadFileAsync(

[OrderedFact("Should decrypt reverse side photo in 'identity_card' element from HTTP response " +
"and write it to a file on disk")]
public async Task Should_decreypt_identity_card_element_reverseside()
public async Task Should_Decrypt_Identity_Card_Element_Reverse_Side()
{
Update update = _classFixture.Entity;
PassportData passportData = update.Message.PassportData;
Expand Down Expand Up @@ -309,7 +305,7 @@ await decrypter.DecryptFileAsync(
}

[OrderedFact("Should decrypt selfie photo in 'identity_card' element")]
public async Task Should_decrypt_identity_card_element_selfie()
public async Task Should_Decrypt_Identity_Card_Element_Selfie()
{
Update update = _classFixture.Entity;
PassportData passportData = update.Message.PassportData;
Expand Down Expand Up @@ -338,7 +334,7 @@ await BotClient.GetInfoAndDownloadFileAsync(
}

[OrderedFact("Should decrypt the single file in 'utility_bill' element")]
public async Task Should_decrypt_utility_bill_element_file()
public async Task Should_Decrypt_Utility_Bill_Element_File()
{
Update update = _classFixture.Entity;
PassportData passportData = update.Message.PassportData;
Expand All @@ -350,14 +346,14 @@ public async Task Should_decrypt_utility_bill_element_file()
IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);

FileCredentials billFileCreds = Assert.Single(credentials.SecureData.UtilityBill.Files);
FileCredentials fileCredentials = Assert.Single(credentials.SecureData.UtilityBill.Files);

File encryptedFileInfo;
using (System.IO.Stream decryptedFile = new System.IO.MemoryStream())
{
encryptedFileInfo = await BotClient.DownloadAndDecryptPassportFileAsync(
billScanFile,
billFileCreds,
fileCredentials,
decryptedFile
);
Assert.InRange(decryptedFile.Length, billScanFile.FileSize - 256, billScanFile.FileSize + 256);
Expand All @@ -381,14 +377,14 @@ public async Task Should_decrypt_utility_bill_element_translation()
IDecrypter decrypter = new Decrypter();
Credentials credentials = decrypter.DecryptCredentials(key, passportData.Credentials);

FileCredentials billTranslationFileCreds = Assert.Single(credentials.SecureData.UtilityBill.Translation);
FileCredentials fileCredentials = Assert.Single(credentials.SecureData.UtilityBill.Translation);

File encryptedFileInfo;
using (System.IO.Stream decryptedFile = new System.IO.MemoryStream())
{
encryptedFileInfo = await BotClient.DownloadAndDecryptPassportFileAsync(
translationFile,
billTranslationFileCreds,
fileCredentials,
decryptedFile
);
Assert.InRange(decryptedFile.Length, translationFile.FileSize - 256, translationFile.FileSize + 256);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable PossibleNullReferenceException
// ReSharper disable CheckNamespace
// ReSharper disable StringLiteralTypo

using System.Security.Cryptography;
using System.Threading.Tasks;
Expand Down
Loading

0 comments on commit 9cddd92

Please sign in to comment.