-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into docs/vote-crypto-proofs
- Loading branch information
Showing
13 changed files
with
285 additions
and
22 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
catalyst_voices/lib/widgets/indicators/voices_password_strength_indicator.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import 'package:catalyst_voices_brands/catalyst_voices_brands.dart'; | ||
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart'; | ||
import 'package:catalyst_voices_models/catalyst_voices_models.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
/// An indicator for a [PasswordStrength]. | ||
/// | ||
/// Fills in all the available horizontal space, | ||
/// use a [SizedBox] to limit it's width. | ||
final class VoicesPasswordStrengthIndicator extends StatelessWidget { | ||
final PasswordStrength passwordStrength; | ||
|
||
const VoicesPasswordStrengthIndicator({ | ||
super.key, | ||
required this.passwordStrength, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
mainAxisSize: MainAxisSize.min, | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
_Label(passwordStrength: passwordStrength), | ||
const SizedBox(height: 16), | ||
_Indicator(passwordStrength: passwordStrength), | ||
], | ||
); | ||
} | ||
} | ||
|
||
class _Label extends StatelessWidget { | ||
final PasswordStrength passwordStrength; | ||
|
||
const _Label({required this.passwordStrength}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Text( | ||
switch (passwordStrength) { | ||
PasswordStrength.weak => context.l10n.weakPasswordStrength, | ||
PasswordStrength.normal => context.l10n.normalPasswordStrength, | ||
PasswordStrength.strong => context.l10n.goodPasswordStrength, | ||
}, | ||
style: Theme.of(context).textTheme.bodySmall, | ||
); | ||
} | ||
} | ||
|
||
class _Indicator extends StatelessWidget { | ||
static const double _backgroundTrackHeight = 4; | ||
static const double _foregroundTrackHeight = 6; | ||
static const double _tracksGap = 8; | ||
|
||
final PasswordStrength passwordStrength; | ||
|
||
const _Indicator({required this.passwordStrength}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SizedBox( | ||
height: _foregroundTrackHeight, | ||
child: LayoutBuilder( | ||
builder: (context, constraints) { | ||
final totalWidthOfAllGaps = | ||
(PasswordStrength.values.length - 1) * _tracksGap; | ||
final availableWidth = constraints.maxWidth - totalWidthOfAllGaps; | ||
final trackWidth = availableWidth / PasswordStrength.values.length; | ||
|
||
return Stack( | ||
children: [ | ||
Positioned.fill( | ||
top: 1, | ||
child: Container( | ||
height: _backgroundTrackHeight, | ||
decoration: BoxDecoration( | ||
color: Theme.of(context).colors.onSurfaceSecondary08, | ||
borderRadius: BorderRadius.circular(_backgroundTrackHeight), | ||
), | ||
), | ||
), | ||
for (final strength in PasswordStrength.values) | ||
if (passwordStrength.index >= strength.index) | ||
Positioned( | ||
left: strength.index * (trackWidth + _tracksGap), | ||
width: trackWidth, | ||
child: _Track(passwordStrength: strength), | ||
), | ||
], | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class _Track extends StatelessWidget { | ||
final PasswordStrength passwordStrength; | ||
|
||
const _Track({required this.passwordStrength}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
height: _Indicator._foregroundTrackHeight, | ||
decoration: BoxDecoration( | ||
color: switch (passwordStrength) { | ||
PasswordStrength.weak => Theme.of(context).colorScheme.error, | ||
PasswordStrength.normal => Theme.of(context).colors.warning, | ||
PasswordStrength.strong => Theme.of(context).colors.success, | ||
}, | ||
borderRadius: BorderRadius.circular(_Indicator._foregroundTrackHeight), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
catalyst_voices/packages/catalyst_voices_models/lib/src/auth/password_strength.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:password_strength/password_strength.dart' as ps; | ||
|
||
/// Describes strength of a password. | ||
/// | ||
/// The enum values must be sorted from the weakest to the strongest. | ||
enum PasswordStrength { | ||
/// A weak password. Simple, already exposed, commonly used, etc. | ||
weak, | ||
|
||
/// A medium password, not complex. | ||
normal, | ||
|
||
/// A complex password with characters from different groups. | ||
strong; | ||
|
||
/// The minimum length of accepted password. | ||
static const int minimumPasswordLength = 8; | ||
|
||
factory PasswordStrength.calculate(String text) { | ||
if (text.length < minimumPasswordLength) return PasswordStrength.weak; | ||
|
||
final strength = ps.estimatePasswordStrength(text); | ||
if (strength <= 0.33) return PasswordStrength.weak; | ||
if (strength <= 0.66) return PasswordStrength.normal; | ||
return PasswordStrength.strong; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
catalyst_voices/packages/catalyst_voices_models/lib/src/catalyst_voices_models.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
catalyst_voices/packages/catalyst_voices_models/test/auth/password_strength_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'package:catalyst_voices_models/catalyst_voices_models.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group(PasswordStrength, () { | ||
test('weak password - too short', () { | ||
expect( | ||
PasswordStrength.calculate('123456'), | ||
equals(PasswordStrength.weak), | ||
); | ||
|
||
expect( | ||
PasswordStrength.calculate('Ab1!@_'), | ||
equals(PasswordStrength.weak), | ||
); | ||
}); | ||
|
||
test('weak password - too popular', () { | ||
expect( | ||
PasswordStrength.calculate('password'), | ||
equals(PasswordStrength.weak), | ||
); | ||
}); | ||
|
||
test('weak password - too simple', () { | ||
expect( | ||
/* cSpell:disable */ | ||
PasswordStrength.calculate('simplepw'), | ||
/* cSpell:enable */ | ||
equals(PasswordStrength.weak), | ||
); | ||
}); | ||
|
||
test('normal password', () { | ||
expect( | ||
PasswordStrength.calculate('Passwd12'), | ||
equals(PasswordStrength.normal), | ||
); | ||
}); | ||
|
||
test('strong password', () { | ||
expect( | ||
PasswordStrength.calculate('Passwd!@'), | ||
equals(PasswordStrength.strong), | ||
); | ||
}); | ||
|
||
test('strong password', () { | ||
expect( | ||
PasswordStrength.calculate('4Gf;Rd04WP,RxgBl)n5&RlG'), | ||
equals(PasswordStrength.strong), | ||
); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.