colored password

This commit is contained in:
Kyle Spearrin 2019-04-26 23:58:15 -04:00
parent 223e8a5293
commit 5e114e8074
5 changed files with 83 additions and 1 deletions

View file

@ -96,7 +96,7 @@
Grid.Column="0"
IsVisible="{Binding ShowPassword, Converter={StaticResource inverseBool}}" />
<controls:MonoLabel
Text="{Binding Cipher.Login.Password, Mode=OneWay}"
FormattedText="{Binding ColoredPassword, Mode=OneWay}"
StyleClass="box-value"
Grid.Row="1"
Grid.Column="0"

View file

@ -1,6 +1,7 @@
using Bit.App.Abstractions;
using Bit.App.Models;
using Bit.App.Resources;
using Bit.App.Utilities;
using Bit.Core.Abstractions;
using Bit.Core.Models.View;
using Bit.Core.Utilities;
@ -67,6 +68,7 @@ namespace Bit.App.Pages
nameof(ShowNotes),
nameof(ShowTotp),
nameof(ShowUsername),
nameof(ColoredPassword),
nameof(ShowPasswordBox)
});
}
@ -103,6 +105,7 @@ namespace Bit.App.Pages
public bool IsCard => _cipher?.Type == Core.Enums.CipherType.Card;
public bool IsSecureNote => _cipher?.Type == Core.Enums.CipherType.SecureNote;
public bool ShowUsername => IsLogin && !string.IsNullOrWhiteSpace(_cipher.Login.Username);
public FormattedString ColoredPassword => PasswordFormatter.FormatPassword(_cipher.Login.Password);
public bool ShowPasswordBox => IsLogin && !string.IsNullOrWhiteSpace(_cipher.Login.Password);
public bool ShowUris => IsLogin && _cipher.Login.HasUris;
public bool ShowNotes => !string.IsNullOrWhiteSpace(_cipher.Notes);

View file

@ -9,6 +9,8 @@
<Color x:Key="InfoColor">#555555</Color>
<Color x:Key="WarningColor">#bf7e16</Color>
<Color x:Key="MutedColor">#777777</Color>
<Color x:Key="PasswordNumberColor">#007fde</Color>
<Color x:Key="PasswordSpecialColor">#c40800</Color>
<Color x:Key="BorderColor">#dddddd</Color>

View file

@ -9,6 +9,8 @@
<Color x:Key="InfoColor">#555555</Color>
<Color x:Key="WarningColor">#bf7e16</Color>
<Color x:Key="MutedColor">#777777</Color>
<Color x:Key="PasswordNumberColor">#007fde</Color>
<Color x:Key="PasswordSpecialColor">#c40800</Color>
<Color x:Key="BorderColor">#dddddd</Color>

View file

@ -0,0 +1,75 @@
using System;
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 FormattedString FormatPassword(String password)
{
var result = new FormattedString();
// Start off with an empty span to prevent possible NPEs. Due to the way the state machine
// works, this will actually always be replaced by a new span anyway.
var currentSpan = new Span();
// 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)
{
currentSpan = new Span();
result.Spans.Add(currentSpan);
currentType = charType;
// Switch the color if it is not a normal text. Otherwise leave the
// default value.
switch(currentType)
{
case CharType.Number:
currentSpan.TextColor = (Color)Application.Current.Resources["PasswordNumberColor"];
break;
case CharType.Special:
currentSpan.TextColor = (Color)Application.Current.Resources["PasswordSpecialColor"];
break;
}
}
currentSpan.Text += c;
}
return result;
}
}
}