-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require password complexity
- Loading branch information
Showing
12 changed files
with
142 additions
and
21 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
src/NuGetGallery/Infrastructure/PasswordValidationAttribute.cs
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,36 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Web.Mvc; | ||
using NuGetGallery.Configuration; | ||
|
||
namespace NuGetGallery.Infrastructure | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public sealed class PasswordValidationAttribute : ValidationAttribute | ||
{ | ||
private readonly RegularExpressionAttribute _regexAttribute; | ||
|
||
public PasswordValidationAttribute() | ||
{ | ||
var configuration = DependencyResolver.Current.GetService<IGalleryConfigurationService>().Current; | ||
|
||
_regexAttribute = new RegularExpressionAttribute(configuration.UserPasswordRegex) | ||
{ | ||
ErrorMessage = configuration.UserPasswordHint | ||
}; | ||
} | ||
|
||
public override bool IsValid(object value) | ||
{ | ||
return _regexAttribute.IsValid(value); | ||
} | ||
|
||
public override string FormatErrorMessage(string name) | ||
{ | ||
return _regexAttribute.FormatErrorMessage(name); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Text.RegularExpressions; | ||
using NuGetGallery.Configuration; | ||
using NuGetGallery.Framework; | ||
using Xunit; | ||
|
||
namespace NuGetGallery | ||
{ | ||
/// <summary> | ||
/// The regex checks that the password is at least 8 characters, one uppercase letter, one lowercase letter, and a digit. | ||
/// </summary> | ||
public class PasswordValidationRegexTests : TestContainer | ||
{ | ||
private readonly string _defaultPasswordRegex; | ||
|
||
public PasswordValidationRegexTests() | ||
{ | ||
var configuration = Get<ConfigurationService>(); | ||
_defaultPasswordRegex = configuration.Current.UserPasswordRegex; | ||
} | ||
|
||
[Theory] | ||
[InlineData("aA1aaaaa")] | ||
[InlineData("abcdefg$0B")] | ||
[InlineData("****1bB***")] | ||
public void Accepts(string password) | ||
{ | ||
|
||
var match = new Regex(_defaultPasswordRegex).IsMatch(password); | ||
Assert.True(match); | ||
} | ||
|
||
[Theory] | ||
[InlineData("v")] // Single letter | ||
[InlineData("V")] // Single upper case letter | ||
[InlineData("8")] // Single number | ||
[InlineData("89984214214")] // Just numbers | ||
[InlineData("%*`~&*()%#@$!@<>?\"")] // Special characters | ||
[InlineData("aaAAaaAAaaAA")] // No digit | ||
[InlineData("12345678a")] // No upperscase letter | ||
[InlineData("12345678A")] // No lowercase letter | ||
[InlineData("1aA")] // Too short | ||
public void DoesNotAccept(string password) | ||
{ | ||
var match = new Regex(_defaultPasswordRegex).IsMatch(password); | ||
Assert.False(match); | ||
} | ||
} | ||
} |