mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
Colorized passwords (#424)
* Added utility to format passwords using spans * Use the password formatter to render the cipher password * Colorize the password in the password generator
This commit is contained in:
parent
8fc5ad099b
commit
421f7e8799
5 changed files with 98 additions and 7 deletions
|
@ -1,5 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using Bit.App.Utilities;
|
||||||
|
using Xamarin.Forms;
|
||||||
|
|
||||||
namespace Bit.App.Models.Page
|
namespace Bit.App.Models.Page
|
||||||
{
|
{
|
||||||
|
@ -19,9 +21,15 @@ namespace Bit.App.Models.Page
|
||||||
{
|
{
|
||||||
_password = value;
|
_password = value;
|
||||||
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Password)));
|
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Password)));
|
||||||
|
PropertyChanged(this, new PropertyChangedEventArgs(nameof(FormattedPassword)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FormattedString FormattedPassword
|
||||||
|
{
|
||||||
|
get { return PasswordFormatter.FormatPassword(_password); }
|
||||||
|
}
|
||||||
|
|
||||||
public string Length
|
public string Length
|
||||||
{
|
{
|
||||||
get { return _length; }
|
get { return _length; }
|
||||||
|
|
|
@ -4,6 +4,7 @@ using Xamarin.Forms;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Bit.App.Enums;
|
using Bit.App.Enums;
|
||||||
using Bit.App.Resources;
|
using Bit.App.Resources;
|
||||||
|
using Bit.App.Utilities;
|
||||||
|
|
||||||
namespace Bit.App.Models.Page
|
namespace Bit.App.Models.Page
|
||||||
{
|
{
|
||||||
|
@ -122,7 +123,7 @@ namespace Bit.App.Models.Page
|
||||||
{
|
{
|
||||||
_loginPassword = value;
|
_loginPassword = value;
|
||||||
PropertyChanged(this, new PropertyChangedEventArgs(nameof(LoginPassword)));
|
PropertyChanged(this, new PropertyChangedEventArgs(nameof(LoginPassword)));
|
||||||
PropertyChanged(this, new PropertyChangedEventArgs(nameof(MaskedLoginPassword)));
|
PropertyChanged(this, new PropertyChangedEventArgs(nameof(FormattedLoginPassword)));
|
||||||
PropertyChanged(this, new PropertyChangedEventArgs(nameof(ShowLoginPassword)));
|
PropertyChanged(this, new PropertyChangedEventArgs(nameof(ShowLoginPassword)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,12 +135,12 @@ namespace Bit.App.Models.Page
|
||||||
{
|
{
|
||||||
_loginRevealPassword = value;
|
_loginRevealPassword = value;
|
||||||
PropertyChanged(this, new PropertyChangedEventArgs(nameof(RevealLoginPassword)));
|
PropertyChanged(this, new PropertyChangedEventArgs(nameof(RevealLoginPassword)));
|
||||||
PropertyChanged(this, new PropertyChangedEventArgs(nameof(MaskedLoginPassword)));
|
PropertyChanged(this, new PropertyChangedEventArgs(nameof(FormattedLoginPassword)));
|
||||||
PropertyChanged(this, new PropertyChangedEventArgs(nameof(LoginShowHideImage)));
|
PropertyChanged(this, new PropertyChangedEventArgs(nameof(LoginShowHideImage)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public string MaskedLoginPassword => RevealLoginPassword ?
|
public FormattedString FormattedLoginPassword => RevealLoginPassword ?
|
||||||
LoginPassword : LoginPassword == null ? null : MaskedPasswordString;
|
PasswordFormatter.FormatPassword(LoginPassword) : LoginPassword == null ? null : MaskedPasswordString;
|
||||||
public ImageSource LoginShowHideImage => RevealLoginPassword ?
|
public ImageSource LoginShowHideImage => RevealLoginPassword ?
|
||||||
ImageSource.FromFile("eye_slash.png") : ImageSource.FromFile("eye.png");
|
ImageSource.FromFile("eye_slash.png") : ImageSource.FromFile("eye.png");
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ namespace Bit.App.Pages
|
||||||
|
|
||||||
Tgr = new TapGestureRecognizer();
|
Tgr = new TapGestureRecognizer();
|
||||||
Password.GestureRecognizers.Add(Tgr);
|
Password.GestureRecognizers.Add(Tgr);
|
||||||
Password.SetBinding(Label.TextProperty, nameof(PasswordGeneratorPageModel.Password));
|
Password.SetBinding(Label.FormattedTextProperty, nameof(PasswordGeneratorPageModel.FormattedPassword));
|
||||||
|
|
||||||
SliderCell = new SliderViewCell(this, _passwordGenerationService, _settings);
|
SliderCell = new SliderViewCell(this, _passwordGenerationService, _settings);
|
||||||
|
|
||||||
|
|
|
@ -129,8 +129,8 @@ namespace Bit.App.Pages
|
||||||
// Password
|
// Password
|
||||||
LoginPasswordCell = new LabeledValueCell(AppResources.Password, button1Image: string.Empty,
|
LoginPasswordCell = new LabeledValueCell(AppResources.Password, button1Image: string.Empty,
|
||||||
button2Image: "clipboard.png");
|
button2Image: "clipboard.png");
|
||||||
LoginPasswordCell.Value.SetBinding(Label.TextProperty,
|
LoginPasswordCell.Value.SetBinding(Label.FormattedTextProperty,
|
||||||
nameof(VaultViewCipherPageModel.MaskedLoginPassword));
|
nameof(VaultViewCipherPageModel.FormattedLoginPassword));
|
||||||
LoginPasswordCell.Button1.SetBinding(Button.ImageProperty,
|
LoginPasswordCell.Button1.SetBinding(Button.ImageProperty,
|
||||||
nameof(VaultViewCipherPageModel.LoginShowHideImage));
|
nameof(VaultViewCipherPageModel.LoginShowHideImage));
|
||||||
LoginPasswordCell.Button1.Command =
|
LoginPasswordCell.Button1.Command =
|
||||||
|
|
82
src/App/Utilities/PasswordFormatter.cs
Normal file
82
src/App/Utilities/PasswordFormatter.cs
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
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 statemachine 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 statemachine
|
||||||
|
// 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.DodgerBlue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case CharType.Special:
|
||||||
|
{
|
||||||
|
currentSpan.TextColor = Color.Firebrick;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
currentSpan.Text += c;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue