mirror of
https://github.com/bitwarden/server.git
synced 2025-04-14 17:48:12 -05:00

* Families for enterprise/split up organization sponsorship service (#1829) * Split OrganizationSponsorshipService into commands * Use tokenable for token validation * Use interfaces to set up for DI * Use commands over services * Move service tests to command tests * Value types can't be null * Run dotnet format * Update src/Core/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/CancelSponsorshipCommand.cs Co-authored-by: Justin Baur <admin@justinbaur.com> * Fix controller tests Co-authored-by: Justin Baur <admin@justinbaur.com> * Families for enterprise/split up organization sponsorship service (#1875) * Split OrganizationSponsorshipService into commands * Use tokenable for token validation * Use interfaces to set up for DI * Use commands over services * Move service tests to command tests * Value types can't be null * Run dotnet format * Update src/Core/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/CancelSponsorshipCommand.cs Co-authored-by: Justin Baur <admin@justinbaur.com> * Fix controller tests * Split create and send sponsorships * Split up create sponsorship * Add self hosted commands to dependency injection * Add field to store cloud billing sync key on self host instances * Fix typo * Fix data protector purpose of sponsorship offers * Split cloud and selfhosted sponsorship offer tokenable * Generate offer from self hosted with all necessary auth data * Add Required properties to constructor * Split up cancel sponsorship command * Split revoke sponsorship command between cloud and self hosted * Fix/f4e multiple sponsorships (#1838) * Use sponosorship from validate to redeem * Update tests * Format * Remove sponsorship service * Run dotnet format * Fix self hosted only controller attribute * Clean up file structure and fixes * Remove unneeded tokenables * Remove obsolete commands * Do not require file/class prefix if unnecessary * Update Organizaiton sprocs * Remove unnecessary models * Fix tests * Generalize LicenseService path calculation Use async file read and deserialization * Use interfaces for testability * Remove unused usings * Correct test direction * Test license reading * remove unused usings * Format Co-authored-by: Justin Baur <admin@justinbaur.com> * Improve DataProtectorTokenFactory test coverage (#1884) * Add encstring to server * Test factory Co-authored-by: Carlos Muentes <cmuentes@bitwarden.com> * Format * Remove SymmetricKeyProtectedString Not needed * Set ForcInvalid Co-authored-by: Carlos Muentes <cmuentes@bitwarden.com> * Feature/self f4e/api keys (#1896) * Add in ApiKey * Work on API Key table * Work on apikey table * Fix response model * Work on information for UI * Work on last sync date * Work on sync status * Work on auth * Work on tokenable * Work on merge * Add custom requirement * Add policy * Run formatting * Work on EF Migrations * Work on OrganizationConnection * Work on database * Work on additional database table * Run formatting * Small fixes * More cleanup * Cleanup * Add RevisionDate * Add GO * Finish Sql project * Add newlines * Fix stored proc file * Fix sqlproj * Add newlines * Fix table * Add navigation property * Delete Connections when organization is deleted * Add connection validation * Start adding ID column * Work on ID column * Work on SQL migration * Work on migrations * Run formatting * Fix test build * Fix sprocs * Work on migrations * Fix Create table * Fix sproc * Add prints to migration * Add default value * Update EF migrations * Formatting * Add to integration tests * Minor fixes * Formatting * Cleanup * Address PR feedback * Address more PR feedback * Fix formatting * Fix formatting * Fix * Address PR feedback * Remove accidential change * Fix SQL build * Run formatting * Address PR feedback * Add sync data to OrganizationUserOrgDetails * Add comments * Remove OrganizationConnectionService interface * Remove unused using * Address PR feedback * Formatting * Minor fix * Feature/self f4e/update db (#1930) * Fix migration * Fix TimesRenewed * Add comments * Make two properties non-nullable * Remove need for SponsoredOrg on SH (#1934) * Remove need for SponsoredOrg on SH * Add Family prefix * Add check for enterprise org on BillingSync key (#1936) * [PS-10] Feature/sponsorships removed at end of term (#1938) * Rename commands to min unique names * Inject revoke command based on self hosting * WIP: Remove/Revoke marks to delete * Complete WIP * Improve remove/revoke tests * PR review * Fail validation if sponsorship has failed to sync for 6 months * Feature/do not accept old self host sponsorships (#1939) * Do not accept >6mo old self-hosted sponsorships * Give disabled grace period of 3 months * Fix issues of Sql.proj differing from migration outcome (#1942) * Fix issues of Sql.proj differing from migration outcome * Yoink int tests * Add missing assert helpers * Feature/org sponsorship sync (#1922) * Self-hosted side sync first pass TODO: * flush out org sponsorship model * implement cloud side * process cloud-side response and update self-hosted records * sync scaffolding second pass * remove list of Org User ids from sync and begin work on SelfHostedRevokeSponsorship * allow authenticated http calls from server to return a result * update models * add logic for sync and change offer email template * add billing sync key and hide CreateSponsorship without user * fix tests * add job scheduling * add authorize attributes to endpoints * separate models into data/model and request/response * batch sync more, add EnableCloudCommunication for testing * send emails in bulk * make userId and sponsorshipType non nullable * batch more on self hosted side of sync * remove TODOs and formatting * changed logic of cloud sync * let BaseIdentityClientService handle all logging * call sync from scheduled job on self host * create bulk db operations for OrganizationSponsorships * remove SponsoredOrgId from sync, return default from server http call * validate BillingSyncKey during sync revert changes to CreateSponsorshipCommand * revert changes to ICreateSponsorshipCommand * add some tests * add DeleteExpiredSponsorshipsJob * add cloud sync test * remove extra method * formatting * prevent new sponsorships from disabled orgs * update packages * - pulled out send sponsorship command dependency from sync on cloud - don't throw error when sponsorships are empty - formatting * formatting models * more formatting * remove licensingService dependency from selfhosted sync * use installation urls and formatting * create constructor for RequestModel and formatting * add date parameter to OrganizationSponsorship_DeleteExpired * add new migration * formatting * rename OrganizationCreateSponsorshipRequestModel to OrganizationSponsorshipCreateRequestModel * prevent whole sync from failing if one sponsorship type is unsupported * deserialize config and billingsynckey from org connection * alter log message when sync disabled * Add grace period to disabled orgs * return early on self hosted if there are no sponsorships in database * rename BillingSyncConfig * send sponsorship offers from controller * allow config to be a null object * better exception handling in sync scheduler * add ef migrations * formatting * fix tests * fix validate test Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Fix OrganizationApiKey issues (#1941) Co-authored-by: Justin Baur <admin@justinbaur.com> * Feature/org sponsorship self hosted tests (#1947) * Self-hosted side sync first pass TODO: * flush out org sponsorship model * implement cloud side * process cloud-side response and update self-hosted records * sync scaffolding second pass * remove list of Org User ids from sync and begin work on SelfHostedRevokeSponsorship * allow authenticated http calls from server to return a result * update models * add logic for sync and change offer email template * add billing sync key and hide CreateSponsorship without user * fix tests * add job scheduling * add authorize attributes to endpoints * separate models into data/model and request/response * batch sync more, add EnableCloudCommunication for testing * send emails in bulk * make userId and sponsorshipType non nullable * batch more on self hosted side of sync * remove TODOs and formatting * changed logic of cloud sync * let BaseIdentityClientService handle all logging * call sync from scheduled job on self host * create bulk db operations for OrganizationSponsorships * remove SponsoredOrgId from sync, return default from server http call * validate BillingSyncKey during sync revert changes to CreateSponsorshipCommand * revert changes to ICreateSponsorshipCommand * add some tests * add DeleteExpiredSponsorshipsJob * add cloud sync test * remove extra method * formatting * prevent new sponsorships from disabled orgs * update packages * - pulled out send sponsorship command dependency from sync on cloud - don't throw error when sponsorships are empty - formatting * formatting models * more formatting * remove licensingService dependency from selfhosted sync * use installation urls and formatting * create constructor for RequestModel and formatting * add date parameter to OrganizationSponsorship_DeleteExpired * add new migration * formatting * rename OrganizationCreateSponsorshipRequestModel to OrganizationSponsorshipCreateRequestModel * prevent whole sync from failing if one sponsorship type is unsupported * deserialize config and billingsynckey from org connection * add mockHttp nuget package and use httpclientfactory * fix current tests * WIP of creating tests * WIP of new self hosted tests * WIP self hosted tests * finish self hosted tests * formatting * format of interface * remove extra config file * added newlines Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Fix Organization_DeleteById (#1950) * Fix Organization_Delete * Fix L * [PS-4] block enterprise user from sponsoring itself (#1943) * [PS-248] Feature/add connections enabled endpoint (#1953) * Move Organization models to sub namespaces * Add Organization Connection api endpoints * Get all connections rather than just enabled ones * Add missing services to DI * pluralize private api endpoints * Add type protection to org connection request/response * Fix route * Use nullable Id to signify no connection * Test Get Connections enabled * Fix data discoverer * Also drop this sproc for rerunning * Id is the OUTPUT of create sprocs * Fix connection config parsing * Linter fixes * update sqlproj file name * Use param xdocs on methods * Simplify controller path attribute * Use JsonDocument to avoid escaped json in our response/request strings * Fix JsonDoc tests * Linter fixes * Fix ApiKey Command and add tests (#1949) * Fix ApiKey command * Formatting * Fix test failures introduced in #1943 (#1957) * Remove "Did you know?" copy from emails. (#1962) * Remove "Did you know" * Remove jsonIf helper * Feature/fix send single sponsorship offer email (#1956) * Fix sponsorship offer email * Do not sanitize org name * PR feedback * Feature/f4e sync event [PS-75] (#1963) * Create sponsorship sync event type * Add InstallationId to Event model * Add combinatorics-based test case generators * Log sponsorships sync event on sync * Linter and test fixes * Fix failing test * Migrate sprocs and view * Remove unused `using`s * [PS-190] Add manual sync trigger in self hosted (#1955) * WIP add button to admin project for billing sync * add connection table to view page * minor fixes for self hosted side of sync * fixes number of bugs for cloud side of sync * deserialize before returning for some reason * add json attributes to return models * list of sponsorships parameter is immutable, add secondary list * change sproc name * add error handling * Fix tests * modify call to connection * Update src/Admin/Controllers/OrganizationsController.cs Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * undo change to sproc name * simplify logic * Update src/Core/OrganizationFeatures/OrganizationSponsorships/FamiliesForEnterprise/Cloud/CloudSyncSponsorshipsCommand.cs Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * register services despite if self hosted or cloud * remove json properties * revert merge conflict Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Update OrganizationSponsorship valid until when updating org expirati… (#1966) * Update OrganizationSponsorship valid until when updating org expiration date * Linter fixes * [PS-7] change revert email copy and add ValidUntil to sponsorship (#1965) * change revert email copy and add ValidUntil to sponsorship * add 15 days if no ValidUntil * Chore/merge/self hosted families for enterprise (#1972) * Log swallowed HttpRequestExceptions (#1866) Co-authored-by: Hinton <oscar@oscarhinton.com> * Allow for utilization of readonly db connection (#1937) * Bump the pin of the download-artifacts action to bypass the broken GitHub api (#1952) * Bumped version to 1.48.0 (#1958) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * [EC-160] Give Provider Users access to all org ciphers and collections (#1959) * Bumped version to 1.48.1 (#1961) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Avoid sending "user need confirmation" emails when there are no org admins (#1960) * Remove noncompliant users for new policies (#1951) * [PS-284] Allow installation clients to not need a user. (#1968) * Allow installation clients to not need a user. * Run formatting Co-authored-by: Andrei <30410186+Manolachi@users.noreply.github.com> Co-authored-by: Hinton <oscar@oscarhinton.com> Co-authored-by: sneakernuts <671942+sneakernuts@users.noreply.github.com> Co-authored-by: Joseph Flinn <58369717+joseph-flinn@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Co-authored-by: Justin Baur <136baur@gmail.com> * Fix/license file not found (#1974) * Handle null license * Throw hint message if license is not found by the admin project. * Use CloudOrganizationId from Connection config * Change test to support change * Fix test Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Feature/f4e selfhosted rename migration to .sql (#1971) * rename migration to .sql * format * Add unit tests to self host F4E (#1975) * Work on tests * Added more tests * Run linting * Address PR feedback * Fix AssertRecent * Linting * Fixed empty tests * Fix/misc self hosted f4e (#1973) * Allow setting of ApiUri * Return updates sponsorshipsData objects * Bind arguments by name * Greedy load sponsorships to email. When upsert was called, it creates Ids on _all_ records, which meant that the lazy-evaluation from this call always returned an empty list. * add scope for sync command DI in job. simplify error logic * update the sync job to get CloudOrgId from the BillingSyncKey Co-authored-by: Jacob Fink <jfink@bitwarden.com> * Chore/merge/self hosted families for enterprise (#1987) * Log swallowed HttpRequestExceptions (#1866) Co-authored-by: Hinton <oscar@oscarhinton.com> * Allow for utilization of readonly db connection (#1937) * Bump the pin of the download-artifacts action to bypass the broken GitHub api (#1952) * Bumped version to 1.48.0 (#1958) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * [EC-160] Give Provider Users access to all org ciphers and collections (#1959) * Bumped version to 1.48.1 (#1961) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Avoid sending "user need confirmation" emails when there are no org admins (#1960) * Remove noncompliant users for new policies (#1951) * [PS-284] Allow installation clients to not need a user. (#1968) * Allow installation clients to not need a user. * Run formatting * Use accept flow for sponsorship offers (#1964) * PS-82 check send 2FA email for new devices on TwoFactorController send-email-login (#1977) * [Bug] Skip WebAuthn 2fa event logs during login flow (#1978) * [Bug] Supress WebAuthn 2fa event logs during login process * Formatting * Simplified method call with new paramter input * Update RealIps Description (#1980) Describe the syntax of the real_ips configuration key with an example, to prevent type errors in the `setup` container when parsing `config.yml` * add proper URI validation to duo host (#1984) * captcha scores (#1967) * captcha scores * some api fixes * check bot on captcha attribute * Update src/Core/Services/Implementations/HCaptchaValidationService.cs Co-authored-by: e271828- <e271828-@users.noreply.github.com> Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> Co-authored-by: e271828- <e271828-@users.noreply.github.com> * ensure no path specific in duo host (#1985) Co-authored-by: Andrei <30410186+Manolachi@users.noreply.github.com> Co-authored-by: Hinton <oscar@oscarhinton.com> Co-authored-by: sneakernuts <671942+sneakernuts@users.noreply.github.com> Co-authored-by: Joseph Flinn <58369717+joseph-flinn@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Co-authored-by: Justin Baur <136baur@gmail.com> Co-authored-by: Federico Maccaroni <fedemkr@gmail.com> Co-authored-by: Vincent Salucci <26154748+vincentsalucci@users.noreply.github.com> Co-authored-by: Jordan Cooks <notnamed@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com> Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> Co-authored-by: e271828- <e271828-@users.noreply.github.com> * Address feedback (#1990) Co-authored-by: Justin Baur <admin@justinbaur.com> Co-authored-by: Carlos Muentes <cmuentes@bitwarden.com> Co-authored-by: Jake Fink <jfink@bitwarden.com> Co-authored-by: Justin Baur <136baur@gmail.com> Co-authored-by: Andrei <30410186+Manolachi@users.noreply.github.com> Co-authored-by: Hinton <oscar@oscarhinton.com> Co-authored-by: sneakernuts <671942+sneakernuts@users.noreply.github.com> Co-authored-by: Joseph Flinn <58369717+joseph-flinn@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Co-authored-by: Federico Maccaroni <fedemkr@gmail.com> Co-authored-by: Vincent Salucci <26154748+vincentsalucci@users.noreply.github.com> Co-authored-by: Jordan Cooks <notnamed@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com> Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> Co-authored-by: e271828- <e271828-@users.noreply.github.com>
885 lines
32 KiB
C#
885 lines
32 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using Azure;
|
|
using Azure.Storage.Blobs;
|
|
using Azure.Storage.Blobs.Models;
|
|
using Azure.Storage.Queues.Models;
|
|
using Bit.Core.Context;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Enums.Provider;
|
|
using Bit.Core.Settings;
|
|
using IdentityModel;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using MimeKit;
|
|
|
|
namespace Bit.Core.Utilities
|
|
{
|
|
public static class CoreHelpers
|
|
{
|
|
private static readonly long _baseDateTicks = new DateTime(1900, 1, 1).Ticks;
|
|
private static readonly DateTime _epoc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
private static readonly DateTime _max = new DateTime(9999, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
private static readonly Random _random = new Random();
|
|
private static string _version;
|
|
private static readonly string _qwertyDvorakMap = "-=qwertyuiop[]asdfghjkl;'zxcvbnm,./_+QWERTYUIO" +
|
|
"P{}ASDFGHJKL:\"ZXCVBNM<>?";
|
|
private static readonly string _dvorakMap = "[]',.pyfgcrl/=aoeuidhtns-;qjkxbmwvz{}\"<>PYFGC" +
|
|
"RL?+AOEUIDHTNS_:QJKXBMWVZ";
|
|
private static readonly string _qwertyColemakMap = "qwertyuiopasdfghjkl;zxcvbnmQWERTYUIOPASDFGHJKL:ZXCVBNM";
|
|
private static readonly string _colemakMap = "qwfpgjluy;arstdhneiozxcvbkmQWFPGJLUY:ARSTDHNEIOZXCVBKM";
|
|
private static readonly string CloudFlareConnectingIp = "CF-Connecting-IP";
|
|
private static readonly string RealIp = "X-Real-IP";
|
|
|
|
/// <summary>
|
|
/// Generate sequential Guid for Sql Server.
|
|
/// ref: https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Id/GuidCombGenerator.cs
|
|
/// </summary>
|
|
/// <returns>A comb Guid.</returns>
|
|
public static Guid GenerateComb()
|
|
=> GenerateComb(Guid.NewGuid(), DateTime.UtcNow);
|
|
|
|
/// <summary>
|
|
/// Implementation of <see cref="GenerateComb()" /> with input parameters to remove randomness.
|
|
/// This should NOT be used outside of testing.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// You probably don't want to use this method and instead want to use <see cref="GenerateComb()" /> with no parameters
|
|
/// </remarks>
|
|
internal static Guid GenerateComb(Guid startingGuid, DateTime time)
|
|
{
|
|
var guidArray = startingGuid.ToByteArray();
|
|
|
|
// Get the days and milliseconds which will be used to build the byte string
|
|
var days = new TimeSpan(time.Ticks - _baseDateTicks);
|
|
var msecs = time.TimeOfDay;
|
|
|
|
// Convert to a byte array
|
|
// Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333
|
|
var daysArray = BitConverter.GetBytes(days.Days);
|
|
var msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
|
|
|
|
// Reverse the bytes to match SQL Servers ordering
|
|
Array.Reverse(daysArray);
|
|
Array.Reverse(msecsArray);
|
|
|
|
// Copy the bytes into the guid
|
|
Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
|
|
Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
|
|
|
|
return new Guid(guidArray);
|
|
}
|
|
|
|
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> source, int size)
|
|
{
|
|
T[] bucket = null;
|
|
var count = 0;
|
|
foreach (var item in source)
|
|
{
|
|
if (bucket == null)
|
|
{
|
|
bucket = new T[size];
|
|
}
|
|
bucket[count++] = item;
|
|
if (count != size)
|
|
{
|
|
continue;
|
|
}
|
|
yield return bucket.Select(x => x);
|
|
bucket = null;
|
|
count = 0;
|
|
}
|
|
// Return the last bucket with all remaining elements
|
|
if (bucket != null && count > 0)
|
|
{
|
|
yield return bucket.Take(count);
|
|
}
|
|
}
|
|
|
|
public static string CleanCertificateThumbprint(string thumbprint)
|
|
{
|
|
// Clean possible garbage characters from thumbprint copy/paste
|
|
// ref http://stackoverflow.com/questions/8448147/problems-with-x509store-certificates-find-findbythumbprint
|
|
return Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();
|
|
}
|
|
|
|
public static X509Certificate2 GetCertificate(string thumbprint)
|
|
{
|
|
thumbprint = CleanCertificateThumbprint(thumbprint);
|
|
|
|
X509Certificate2 cert = null;
|
|
var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
|
|
certStore.Open(OpenFlags.ReadOnly);
|
|
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
|
|
if (certCollection.Count > 0)
|
|
{
|
|
cert = certCollection[0];
|
|
}
|
|
|
|
certStore.Close();
|
|
return cert;
|
|
}
|
|
|
|
public static X509Certificate2 GetCertificate(string file, string password)
|
|
{
|
|
return new X509Certificate2(file, password);
|
|
}
|
|
|
|
public async static Task<X509Certificate2> GetEmbeddedCertificateAsync(string file, string password)
|
|
{
|
|
var assembly = typeof(CoreHelpers).GetTypeInfo().Assembly;
|
|
using (var s = assembly.GetManifestResourceStream($"Bit.Core.{file}"))
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
await s.CopyToAsync(ms);
|
|
return new X509Certificate2(ms.ToArray(), password);
|
|
}
|
|
}
|
|
|
|
public static string GetEmbeddedResourceContentsAsync(string file)
|
|
{
|
|
var assembly = Assembly.GetCallingAssembly();
|
|
var resourceName = assembly.GetManifestResourceNames().Single(n => n.EndsWith(file));
|
|
using (var stream = assembly.GetManifestResourceStream(resourceName))
|
|
using (var reader = new StreamReader(stream))
|
|
{
|
|
return reader.ReadToEnd();
|
|
}
|
|
}
|
|
|
|
public async static Task<X509Certificate2> GetBlobCertificateAsync(string connectionString, string container, string file, string password)
|
|
{
|
|
try
|
|
{
|
|
var blobServiceClient = new BlobServiceClient(connectionString);
|
|
var containerRef2 = blobServiceClient.GetBlobContainerClient(container);
|
|
var blobRef = containerRef2.GetBlobClient(file);
|
|
|
|
using var memStream = new MemoryStream();
|
|
await blobRef.DownloadToAsync(memStream).ConfigureAwait(false);
|
|
return new X509Certificate2(memStream.ToArray(), password);
|
|
}
|
|
catch (RequestFailedException ex)
|
|
when (ex.ErrorCode == BlobErrorCode.ContainerNotFound || ex.ErrorCode == BlobErrorCode.BlobNotFound)
|
|
{
|
|
return null;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static long ToEpocMilliseconds(DateTime date)
|
|
{
|
|
return (long)Math.Round((date - _epoc).TotalMilliseconds, 0);
|
|
}
|
|
|
|
public static DateTime FromEpocMilliseconds(long milliseconds)
|
|
{
|
|
return _epoc.AddMilliseconds(milliseconds);
|
|
}
|
|
|
|
public static long ToEpocSeconds(DateTime date)
|
|
{
|
|
return (long)Math.Round((date - _epoc).TotalSeconds, 0);
|
|
}
|
|
|
|
public static DateTime FromEpocSeconds(long seconds)
|
|
{
|
|
return _epoc.AddSeconds(seconds);
|
|
}
|
|
|
|
public static string U2fAppIdUrl(GlobalSettings globalSettings)
|
|
{
|
|
return string.Concat(globalSettings.BaseServiceUri.Vault, "/app-id.json");
|
|
}
|
|
|
|
public static string RandomString(int length, bool alpha = true, bool upper = true, bool lower = true,
|
|
bool numeric = true, bool special = false)
|
|
{
|
|
return RandomString(length, RandomStringCharacters(alpha, upper, lower, numeric, special));
|
|
}
|
|
|
|
public static string RandomString(int length, string characters)
|
|
{
|
|
return new string(Enumerable.Repeat(characters, length).Select(s => s[_random.Next(s.Length)]).ToArray());
|
|
}
|
|
|
|
public static string SecureRandomString(int length, bool alpha = true, bool upper = true, bool lower = true,
|
|
bool numeric = true, bool special = false)
|
|
{
|
|
return SecureRandomString(length, RandomStringCharacters(alpha, upper, lower, numeric, special));
|
|
}
|
|
|
|
// ref https://stackoverflow.com/a/8996788/1090359 with modifications
|
|
public static string SecureRandomString(int length, string characters)
|
|
{
|
|
if (length < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(length), "length cannot be less than zero.");
|
|
}
|
|
|
|
if ((characters?.Length ?? 0) == 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(characters), "characters invalid.");
|
|
}
|
|
|
|
const int byteSize = 0x100;
|
|
if (byteSize < characters.Length)
|
|
{
|
|
throw new ArgumentException(
|
|
string.Format("{0} may contain no more than {1} characters.", nameof(characters), byteSize),
|
|
nameof(characters));
|
|
}
|
|
|
|
var outOfRangeStart = byteSize - (byteSize % characters.Length);
|
|
using (var rng = RandomNumberGenerator.Create())
|
|
{
|
|
var sb = new StringBuilder();
|
|
var buffer = new byte[128];
|
|
while (sb.Length < length)
|
|
{
|
|
rng.GetBytes(buffer);
|
|
for (var i = 0; i < buffer.Length && sb.Length < length; ++i)
|
|
{
|
|
// Divide the byte into charSet-sized groups. If the random value falls into the last group and the
|
|
// last group is too small to choose from the entire allowedCharSet, ignore the value in order to
|
|
// avoid biasing the result.
|
|
if (outOfRangeStart <= buffer[i])
|
|
{
|
|
continue;
|
|
}
|
|
|
|
sb.Append(characters[buffer[i] % characters.Length]);
|
|
}
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
|
|
private static string RandomStringCharacters(bool alpha, bool upper, bool lower, bool numeric, bool special)
|
|
{
|
|
var characters = string.Empty;
|
|
if (alpha)
|
|
{
|
|
if (upper)
|
|
{
|
|
characters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
}
|
|
|
|
if (lower)
|
|
{
|
|
characters += "abcdefghijklmnopqrstuvwxyz";
|
|
}
|
|
}
|
|
|
|
if (numeric)
|
|
{
|
|
characters += "0123456789";
|
|
}
|
|
|
|
if (special)
|
|
{
|
|
characters += "!@#$%^*&";
|
|
}
|
|
|
|
return characters;
|
|
}
|
|
|
|
// ref: https://stackoverflow.com/a/11124118/1090359
|
|
// Returns the human-readable file size for an arbitrary 64-bit file size .
|
|
// The format is "0.## XB", ex: "4.2 KB" or "1.43 GB"
|
|
public static string ReadableBytesSize(long size)
|
|
{
|
|
// Get absolute value
|
|
var absoluteSize = (size < 0 ? -size : size);
|
|
|
|
// Determine the suffix and readable value
|
|
string suffix;
|
|
double readable;
|
|
if (absoluteSize >= 0x40000000) // 1 Gigabyte
|
|
{
|
|
suffix = "GB";
|
|
readable = (size >> 20);
|
|
}
|
|
else if (absoluteSize >= 0x100000) // 1 Megabyte
|
|
{
|
|
suffix = "MB";
|
|
readable = (size >> 10);
|
|
}
|
|
else if (absoluteSize >= 0x400) // 1 Kilobyte
|
|
{
|
|
suffix = "KB";
|
|
readable = size;
|
|
}
|
|
else
|
|
{
|
|
return size.ToString("0 Bytes"); // Byte
|
|
}
|
|
|
|
// Divide by 1024 to get fractional value
|
|
readable = (readable / 1024);
|
|
|
|
// Return formatted number with suffix
|
|
return readable.ToString("0.## ") + suffix;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a clone of the given object through serializing to json and deserializing.
|
|
/// This method is subject to the limitations of System.Text.Json. For example, properties with
|
|
/// inaccessible setters will not be set.
|
|
/// </summary>
|
|
public static T CloneObject<T>(T obj)
|
|
{
|
|
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(obj));
|
|
}
|
|
|
|
public static bool SettingHasValue(string setting)
|
|
{
|
|
var normalizedSetting = setting?.ToLowerInvariant();
|
|
return !string.IsNullOrWhiteSpace(normalizedSetting) && !normalizedSetting.Equals("secret") &&
|
|
!normalizedSetting.Equals("replace");
|
|
}
|
|
|
|
public static string Base64EncodeString(string input)
|
|
{
|
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
|
|
}
|
|
|
|
public static string Base64DecodeString(string input)
|
|
{
|
|
return Encoding.UTF8.GetString(Convert.FromBase64String(input));
|
|
}
|
|
|
|
public static string Base64UrlEncodeString(string input)
|
|
{
|
|
return Base64UrlEncode(Encoding.UTF8.GetBytes(input));
|
|
}
|
|
|
|
public static string Base64UrlDecodeString(string input)
|
|
{
|
|
return Encoding.UTF8.GetString(Base64UrlDecode(input));
|
|
}
|
|
|
|
public static string Base64UrlEncode(byte[] input)
|
|
{
|
|
var output = Convert.ToBase64String(input)
|
|
.Replace('+', '-')
|
|
.Replace('/', '_')
|
|
.Replace("=", string.Empty);
|
|
return output;
|
|
}
|
|
|
|
public static byte[] Base64UrlDecode(string input)
|
|
{
|
|
var output = input;
|
|
// 62nd char of encoding
|
|
output = output.Replace('-', '+');
|
|
// 63rd char of encoding
|
|
output = output.Replace('_', '/');
|
|
// Pad with trailing '='s
|
|
switch (output.Length % 4)
|
|
{
|
|
case 0:
|
|
// No pad chars in this case
|
|
break;
|
|
case 2:
|
|
// Two pad chars
|
|
output += "=="; break;
|
|
case 3:
|
|
// One pad char
|
|
output += "="; break;
|
|
default:
|
|
throw new InvalidOperationException("Illegal base64url string!");
|
|
}
|
|
|
|
// Standard base64 decoder
|
|
return Convert.FromBase64String(output);
|
|
}
|
|
|
|
public static string PunyEncode(string text)
|
|
{
|
|
if (text == "")
|
|
{
|
|
return "";
|
|
}
|
|
|
|
if (text == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!text.Contains("@"))
|
|
{
|
|
// Assume domain name or non-email address
|
|
var idn = new IdnMapping();
|
|
return idn.GetAscii(text);
|
|
}
|
|
else
|
|
{
|
|
// Assume email address
|
|
return MailboxAddress.EncodeAddrspec(text);
|
|
}
|
|
}
|
|
|
|
public static string FormatLicenseSignatureValue(object val)
|
|
{
|
|
if (val == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
if (val.GetType() == typeof(DateTime))
|
|
{
|
|
return ToEpocSeconds((DateTime)val).ToString();
|
|
}
|
|
|
|
if (val.GetType() == typeof(bool))
|
|
{
|
|
return val.ToString().ToLowerInvariant();
|
|
}
|
|
|
|
if (val is PlanType planType)
|
|
{
|
|
return planType switch
|
|
{
|
|
PlanType.Free => "Free",
|
|
PlanType.FamiliesAnnually2019 => "FamiliesAnnually",
|
|
PlanType.TeamsMonthly2019 => "TeamsMonthly",
|
|
PlanType.TeamsAnnually2019 => "TeamsAnnually",
|
|
PlanType.EnterpriseMonthly2019 => "EnterpriseMonthly",
|
|
PlanType.EnterpriseAnnually2019 => "EnterpriseAnnually",
|
|
PlanType.Custom => "Custom",
|
|
_ => ((byte)planType).ToString(),
|
|
};
|
|
}
|
|
|
|
return val.ToString();
|
|
}
|
|
|
|
public static string GetVersion()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_version))
|
|
{
|
|
_version = Assembly.GetEntryAssembly()
|
|
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
|
|
.InformationalVersion;
|
|
}
|
|
|
|
return _version;
|
|
}
|
|
|
|
public static string Dvorak2Qwerty(string value)
|
|
{
|
|
return Other2Qwerty(value, _dvorakMap, _qwertyDvorakMap);
|
|
}
|
|
|
|
public static string Colemak2Qwerty(string value)
|
|
{
|
|
return Other2Qwerty(value, _colemakMap, _qwertyColemakMap);
|
|
}
|
|
|
|
private static string Other2Qwerty(string value, string otherMap, string qwertyMap)
|
|
{
|
|
var sb = new StringBuilder();
|
|
foreach (var c in value)
|
|
{
|
|
sb.Append(otherMap.IndexOf(c) > -1 ? qwertyMap[otherMap.IndexOf(c)] : c);
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static string SanitizeForEmail(string value, bool htmlEncode = true)
|
|
{
|
|
var cleanedValue = value.Replace("@", "[at]");
|
|
var regexOptions = RegexOptions.CultureInvariant |
|
|
RegexOptions.Singleline |
|
|
RegexOptions.IgnoreCase;
|
|
cleanedValue = Regex.Replace(cleanedValue, @"(\.\w)",
|
|
m => string.Concat("[dot]", m.ToString().Last()), regexOptions);
|
|
while (Regex.IsMatch(cleanedValue, @"((^|\b)(\w*)://)", regexOptions))
|
|
{
|
|
cleanedValue = Regex.Replace(cleanedValue, @"((^|\b)(\w*)://)",
|
|
string.Empty, regexOptions);
|
|
}
|
|
return htmlEncode ? HttpUtility.HtmlEncode(cleanedValue) : cleanedValue;
|
|
}
|
|
|
|
public static string DateTimeToTableStorageKey(DateTime? date = null)
|
|
{
|
|
if (date.HasValue)
|
|
{
|
|
date = date.Value.ToUniversalTime();
|
|
}
|
|
else
|
|
{
|
|
date = DateTime.UtcNow;
|
|
}
|
|
|
|
return _max.Subtract(date.Value).TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
|
|
}
|
|
|
|
// ref: https://stackoverflow.com/a/27545010/1090359
|
|
public static Uri ExtendQuery(Uri uri, IDictionary<string, string> values)
|
|
{
|
|
var baseUri = uri.ToString();
|
|
var queryString = string.Empty;
|
|
if (baseUri.Contains("?"))
|
|
{
|
|
var urlSplit = baseUri.Split('?');
|
|
baseUri = urlSplit[0];
|
|
queryString = urlSplit.Length > 1 ? urlSplit[1] : string.Empty;
|
|
}
|
|
|
|
var queryCollection = HttpUtility.ParseQueryString(queryString);
|
|
foreach (var kvp in values ?? new Dictionary<string, string>())
|
|
{
|
|
queryCollection[kvp.Key] = kvp.Value;
|
|
}
|
|
|
|
var uriKind = uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative;
|
|
if (queryCollection.Count == 0)
|
|
{
|
|
return new Uri(baseUri, uriKind);
|
|
}
|
|
return new Uri(string.Format("{0}?{1}", baseUri, queryCollection), uriKind);
|
|
}
|
|
|
|
public static string CustomProviderName(TwoFactorProviderType type)
|
|
{
|
|
return string.Concat("Custom_", type.ToString());
|
|
}
|
|
|
|
public static bool UserInviteTokenIsValid(IDataProtector protector, string token, string userEmail,
|
|
Guid orgUserId, IGlobalSettings globalSettings)
|
|
{
|
|
return TokenIsValid("OrganizationUserInvite", protector, token, userEmail, orgUserId,
|
|
globalSettings.OrganizationInviteExpirationHours);
|
|
}
|
|
|
|
public static bool TokenIsValid(string firstTokenPart, IDataProtector protector, string token, string userEmail,
|
|
Guid id, double expirationInHours)
|
|
{
|
|
var invalid = true;
|
|
try
|
|
{
|
|
var unprotectedData = protector.Unprotect(token);
|
|
var dataParts = unprotectedData.Split(' ');
|
|
if (dataParts.Length == 4 && dataParts[0] == firstTokenPart &&
|
|
new Guid(dataParts[1]) == id &&
|
|
dataParts[2].Equals(userEmail, StringComparison.InvariantCultureIgnoreCase))
|
|
{
|
|
var creationTime = FromEpocMilliseconds(Convert.ToInt64(dataParts[3]));
|
|
var expTime = creationTime.AddHours(expirationInHours);
|
|
invalid = expTime < DateTime.UtcNow;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
invalid = true;
|
|
}
|
|
|
|
return !invalid;
|
|
}
|
|
|
|
public static string GetApplicationCacheServiceBusSubcriptionName(GlobalSettings globalSettings)
|
|
{
|
|
var subName = globalSettings.ServiceBus.ApplicationCacheSubscriptionName;
|
|
if (string.IsNullOrWhiteSpace(subName))
|
|
{
|
|
var websiteInstanceId = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
|
|
if (string.IsNullOrWhiteSpace(websiteInstanceId))
|
|
{
|
|
throw new Exception("No service bus subscription name available.");
|
|
}
|
|
else
|
|
{
|
|
subName = $"{globalSettings.ProjectName.ToLower()}_{websiteInstanceId}";
|
|
if (subName.Length > 50)
|
|
{
|
|
subName = subName.Substring(0, 50);
|
|
}
|
|
}
|
|
}
|
|
return subName;
|
|
}
|
|
|
|
public static string GetIpAddress(this Microsoft.AspNetCore.Http.HttpContext httpContext,
|
|
GlobalSettings globalSettings)
|
|
{
|
|
if (httpContext == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!globalSettings.SelfHosted && httpContext.Request.Headers.ContainsKey(CloudFlareConnectingIp))
|
|
{
|
|
return httpContext.Request.Headers[CloudFlareConnectingIp].ToString();
|
|
}
|
|
if (globalSettings.SelfHosted && httpContext.Request.Headers.ContainsKey(RealIp))
|
|
{
|
|
return httpContext.Request.Headers[RealIp].ToString();
|
|
}
|
|
|
|
return httpContext.Connection?.RemoteIpAddress?.ToString();
|
|
}
|
|
|
|
public static bool IsCorsOriginAllowed(string origin, GlobalSettings globalSettings)
|
|
{
|
|
return
|
|
// Web vault
|
|
origin == globalSettings.BaseServiceUri.Vault ||
|
|
// Safari extension origin
|
|
origin == "file://" ||
|
|
// Product website
|
|
(!globalSettings.SelfHosted && origin == "https://bitwarden.com");
|
|
}
|
|
|
|
public static X509Certificate2 GetIdentityServerCertificate(GlobalSettings globalSettings)
|
|
{
|
|
if (globalSettings.SelfHosted &&
|
|
SettingHasValue(globalSettings.IdentityServer.CertificatePassword)
|
|
&& File.Exists("identity.pfx"))
|
|
{
|
|
return GetCertificate("identity.pfx",
|
|
globalSettings.IdentityServer.CertificatePassword);
|
|
}
|
|
else if (SettingHasValue(globalSettings.IdentityServer.CertificateThumbprint))
|
|
{
|
|
return GetCertificate(
|
|
globalSettings.IdentityServer.CertificateThumbprint);
|
|
}
|
|
else if (!globalSettings.SelfHosted &&
|
|
SettingHasValue(globalSettings.Storage?.ConnectionString) &&
|
|
SettingHasValue(globalSettings.IdentityServer.CertificatePassword))
|
|
{
|
|
return GetBlobCertificateAsync(globalSettings.Storage.ConnectionString, "certificates",
|
|
"identity.pfx", globalSettings.IdentityServer.CertificatePassword).GetAwaiter().GetResult();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static Dictionary<string, object> AdjustIdentityServerConfig(Dictionary<string, object> configDict,
|
|
string publicServiceUri, string internalServiceUri)
|
|
{
|
|
var dictReplace = new Dictionary<string, object>();
|
|
foreach (var item in configDict)
|
|
{
|
|
if (item.Key == "authorization_endpoint" && item.Value is string val)
|
|
{
|
|
var uri = new Uri(val);
|
|
dictReplace.Add(item.Key, string.Concat(publicServiceUri, uri.LocalPath));
|
|
}
|
|
else if ((item.Key == "jwks_uri" || item.Key.EndsWith("_endpoint")) && item.Value is string val2)
|
|
{
|
|
var uri = new Uri(val2);
|
|
dictReplace.Add(item.Key, string.Concat(internalServiceUri, uri.LocalPath));
|
|
}
|
|
}
|
|
foreach (var replace in dictReplace)
|
|
{
|
|
configDict[replace.Key] = replace.Value;
|
|
}
|
|
return configDict;
|
|
}
|
|
|
|
public static List<KeyValuePair<string, string>> BuildIdentityClaims(User user, ICollection<CurrentContentOrganization> orgs,
|
|
ICollection<CurrentContentProvider> providers, bool isPremium)
|
|
{
|
|
var claims = new List<KeyValuePair<string, string>>()
|
|
{
|
|
new KeyValuePair<string, string>("premium", isPremium ? "true" : "false"),
|
|
new KeyValuePair<string, string>(JwtClaimTypes.Email, user.Email),
|
|
new KeyValuePair<string, string>(JwtClaimTypes.EmailVerified, user.EmailVerified ? "true" : "false"),
|
|
new KeyValuePair<string, string>("sstamp", user.SecurityStamp)
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(user.Name))
|
|
{
|
|
claims.Add(new KeyValuePair<string, string>(JwtClaimTypes.Name, user.Name));
|
|
}
|
|
|
|
// Orgs that this user belongs to
|
|
if (orgs.Any())
|
|
{
|
|
foreach (var group in orgs.GroupBy(o => o.Type))
|
|
{
|
|
switch (group.Key)
|
|
{
|
|
case Enums.OrganizationUserType.Owner:
|
|
foreach (var org in group)
|
|
{
|
|
claims.Add(new KeyValuePair<string, string>("orgowner", org.Id.ToString()));
|
|
}
|
|
break;
|
|
case Enums.OrganizationUserType.Admin:
|
|
foreach (var org in group)
|
|
{
|
|
claims.Add(new KeyValuePair<string, string>("orgadmin", org.Id.ToString()));
|
|
}
|
|
break;
|
|
case Enums.OrganizationUserType.Manager:
|
|
foreach (var org in group)
|
|
{
|
|
claims.Add(new KeyValuePair<string, string>("orgmanager", org.Id.ToString()));
|
|
}
|
|
break;
|
|
case Enums.OrganizationUserType.User:
|
|
foreach (var org in group)
|
|
{
|
|
claims.Add(new KeyValuePair<string, string>("orguser", org.Id.ToString()));
|
|
}
|
|
break;
|
|
case Enums.OrganizationUserType.Custom:
|
|
foreach (var org in group)
|
|
{
|
|
claims.Add(new KeyValuePair<string, string>("orgcustom", org.Id.ToString()));
|
|
foreach (var (permission, claimName) in org.Permissions.ClaimsMap)
|
|
{
|
|
if (!permission)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
claims.Add(new KeyValuePair<string, string>(claimName, org.Id.ToString()));
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (providers.Any())
|
|
{
|
|
foreach (var group in providers.GroupBy(o => o.Type))
|
|
{
|
|
switch (group.Key)
|
|
{
|
|
case ProviderUserType.ProviderAdmin:
|
|
foreach (var provider in group)
|
|
{
|
|
claims.Add(new KeyValuePair<string, string>("providerprovideradmin", provider.Id.ToString()));
|
|
}
|
|
break;
|
|
case ProviderUserType.ServiceUser:
|
|
foreach (var provider in group)
|
|
{
|
|
claims.Add(new KeyValuePair<string, string>("providerserviceuser", provider.Id.ToString()));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return claims;
|
|
}
|
|
|
|
public static T LoadClassFromJsonData<T>(string jsonData) where T : new()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(jsonData))
|
|
{
|
|
return new T();
|
|
}
|
|
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
};
|
|
|
|
return System.Text.Json.JsonSerializer.Deserialize<T>(jsonData, options);
|
|
}
|
|
|
|
public static string ClassToJsonData<T>(T data)
|
|
{
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
};
|
|
|
|
return System.Text.Json.JsonSerializer.Serialize(data, options);
|
|
}
|
|
|
|
public static ICollection<T> AddIfNotExists<T>(this ICollection<T> list, T item)
|
|
{
|
|
if (list.Contains(item))
|
|
{
|
|
return list;
|
|
}
|
|
list.Add(item);
|
|
return list;
|
|
}
|
|
|
|
public static string DecodeMessageText(this QueueMessage message)
|
|
{
|
|
var text = message?.MessageText;
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
{
|
|
return text;
|
|
}
|
|
try
|
|
{
|
|
return Base64DecodeString(text);
|
|
}
|
|
catch
|
|
{
|
|
return text;
|
|
}
|
|
}
|
|
|
|
public static bool FixedTimeEquals(string input1, string input2)
|
|
{
|
|
return CryptographicOperations.FixedTimeEquals(
|
|
Encoding.UTF8.GetBytes(input1), Encoding.UTF8.GetBytes(input2));
|
|
}
|
|
|
|
public static string ObfuscateEmail(string email)
|
|
{
|
|
if (email == null)
|
|
{
|
|
return email;
|
|
}
|
|
|
|
var emailParts = email.Split('@', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (emailParts.Length != 2)
|
|
{
|
|
return email;
|
|
}
|
|
|
|
var username = emailParts[0];
|
|
|
|
if (username.Length < 2)
|
|
{
|
|
return email;
|
|
}
|
|
|
|
var sb = new StringBuilder();
|
|
sb.Append(emailParts[0][..2]);
|
|
for (var i = 2; i < emailParts[0].Length; i++)
|
|
{
|
|
sb.Append('*');
|
|
}
|
|
|
|
return sb.Append('@')
|
|
.Append(emailParts[1])
|
|
.ToString();
|
|
|
|
}
|
|
}
|
|
}
|