1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-24 10:14:55 +02:00
bitwarden-mobile/src/App/Utilities/PasswordFormatter.cs
Vincent Salucci 4c3df2e1e1
[Auto Logout] Final review of feature (#932)
* Initial commit of LockService name refactor (#831)

* [Auto-Logout] Update Service layer logic (#835)

* Initial commit of service logic update

* Added default value for action

* Updated ToggleTokensAsync conditional

* Removed unused variables, updated action conditional

* Initial commit: lockOption/lock refactor app layer (#840)

* [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844)

* Initial commit of app layer part 2

* Updated biometrics position

* Reverted resource name refactor

* LockOptions refactor revert

* Updated method casing :: Removed VaultTimeout prefix for timeouts

* Fixed dupe string resource (#854)

* Updated dependency to use VaultTimeoutService (#896)

* [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902)

* fix typo in PINRequireMasterPasswordRestart (#900)

* initial commit for xf usage in autofill

* Fixed databinding for hint button

* Updated Two Factor page launch - removed unused imports

* First pass at broadcast/messenger implentation for autofill

* setting theme in extension using theme manager

* extension app resources

* App resources from main app

* fix ref to twoFactorPage

* apply resources to page

* load empty app for sytling in extension

* move ios renderers to ios core

* static ref to resources and GetResourceColor helper

* fix method ref

* move application.current.resources refs to helper

* switch login page alerts to device action dialogs

* run on main thread

* showDialog with device action service

* abstract action sheet to device action service

* add support for yubikey

* add yubikey iimages to extension

* support close button action

* add support to action extension

* remove empty lines

Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com>
Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com>

* [Auto Logout] Update lock option to be default value (#929)

* Initial commit - make lock action default

* Removed extra whitespace

Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com>
Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com>
Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 12:26:36 -04:00

124 lines
4.2 KiB
C#

using System;
using System.Web;
using Xamarin.Forms;
namespace Bit.App.Utilities
{
/**
* Helper class to format a password with numeric encoding to separate
* normal text from numbers and special characters.
*/
class PasswordFormatter
{
/**
* This enum is used for the state machine when building the colorized
* password string.
*/
private enum CharType
{
None,
Normal,
Number,
Special
}
public static string FormatPassword(string password)
{
if (password == null)
{
return string.Empty;
}
// First two digits of returned hex code contains the alpha,
// which is not supported in HTML color, so we need to cut those out.
var normalColor = $"<span style=\"color:#{ThemeManager.GetResourceColor("TextColor").ToHex().Substring(3)}\">";
var numberColor = $"<span style=\"color:#{ThemeManager.GetResourceColor("PasswordNumberColor").ToHex().Substring(3)}\">";
var specialColor = $"<span style=\"color:#{ThemeManager.GetResourceColor("PasswordSpecialColor").ToHex().Substring(3)}\">";
var result = string.Empty;
// iOS won't hide the zero-width space char without these div attrs, but Android won't respect
// display:inline-block and adds a newline after the password. Hence, only iOS gets the div.
if (Device.RuntimePlatform == Device.iOS)
{
result += "<div style=\"display:inline-block; align-items:center; justify-content:center; text-align:center; word-break:break-all; white-space:pre-wrap; min-width:0\">";
}
// Start with an otherwise uncovered case so we will definitely enter the "something changed"
// state.
var currentType = CharType.None;
foreach (var c in password)
{
// First, identify what the current char is.
CharType charType;
if (char.IsLetter(c))
{
charType = CharType.Normal;
}
else if (char.IsDigit(c))
{
charType = CharType.Number;
}
else
{
charType = CharType.Special;
}
// If the char type changed, build a new span to append the text to.
if (charType != currentType)
{
// Close off previous span.
if (currentType != CharType.None)
{
result += "</span>";
}
currentType = charType;
// Switch the color if it is not a normal text. Otherwise leave the
// default value.
switch (currentType)
{
// Apply color style to span.
case CharType.Normal:
result += normalColor;
break;
case CharType.Number:
result += numberColor;
break;
case CharType.Special:
result += specialColor;
break;
}
}
if (currentType == CharType.Special)
{
result += HttpUtility.HtmlEncode(c);
}
else
{
result += c;
}
// Add zero-width space after every char so per-char wrapping works consistently
result += "&#8203;";
}
// Close off last span.
if (currentType != CharType.None)
{
result += "</span>";
}
// Close off iOS div
if (Device.RuntimePlatform == Device.iOS)
{
result += "</div>";
}
return result;
}
}
}