Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

491: Restrict passwords #506

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions FU.API/FU.API.Tests/AccountServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Dispose()
public async void CreateUser_WithValidCredentials_ReturnsUser()
{
// Arange
Credentials credentials = new() { Username = "Test", Password = "Test" };
Credentials credentials = new() { Username = "Test", Password = "Test12345" };

// Act
ApplicationUser user = await _accountsService.Register(credentials);
Expand All @@ -62,7 +62,7 @@ public async void CreateUser_WithValidCredentials_ReturnsUser()
public async void ChangeUsername_WithValidUsername_ChangesUsername()
{
// Arange
Credentials credentials = new() { Username = "Username1", Password = "Test" };
Credentials credentials = new() { Username = "Username1", Password = "Test12345" };
string newUsername = "Username2";
ApplicationUser user = await _accountsService.Register(credentials);

Expand Down
6 changes: 3 additions & 3 deletions FU.API/FU.API.Tests/PostServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public async void GetPostUsers_WithMultipleUsers_ReturnsCorrectUserCount()
// Arrange
// Note: CreateTestPostAsync creates one user as part of creating a post
Post post = await TestsHelper.CreateTestPostAsync(_dbContext);
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" });
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "Test12345", Email = "[email protected]" });
await _postService.JoinPost(post.Id, user2);

// Act
Expand Down Expand Up @@ -166,7 +166,7 @@ public async void JoinPost_WhenAlreadyMember_ThrowsConflictException()
{
// Arrange
Post post = await TestsHelper.CreateTestPostAsync(_dbContext);
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" });
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "Test12345", Email = "[email protected]" });
await _postService.JoinPost(post.Id, user2);

// Act & Assert
Expand All @@ -180,7 +180,7 @@ public async void LeavePost_WhenMember_LeavesPost()
{
// Arrange
Post post = await TestsHelper.CreateTestPostAsync(_dbContext);
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "pass2", Email = "[email protected]" });
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials() { Username = "user2", Password = "Test12345", Email = "[email protected]" });
await _postService.JoinPost(post.Id, user2);

// Act
Expand Down
6 changes: 3 additions & 3 deletions FU.API/FU.API.Tests/SearchServiceSearchUsersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ public async Task Search_WithKeyword_SearchesTitleAndBio()
{
Username = "User1",
Email = "[email protected]",
Password = "Pass1"
Password = "Test12345"
});
var user2 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials()
{
Username = "User2",
Email = "[email protected]",
Password = "Pass2"
Password = "Test12345"
});
var user3 = await TestsHelper.CreateUserAsync(_dbContext, new Credentials()
{
Username = "User3",
Email = "[email protected]",
Password = "Pass3"
Password = "Test12345"
});
await _userService.UpdateUserProfile(new UserProfile() { Bio = "Bio3", Id = user3.UserId });

Expand Down
2 changes: 1 addition & 1 deletion FU.API/FU.API.Tests/TestsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static async Task<Game> CreateTestGameAsync(AppDbContext dbContext)

public static async Task<ApplicationUser> CreateUserAsync(AppDbContext context)
{
Credentials credentials = new() { Username = "Test", Password = "Test", Email = "[email protected]" };
Credentials credentials = new() { Username = "Test", Password = "Test12345", Email = "[email protected]" };
return await CreateUserAsync(context, credentials);
}

Expand Down
7 changes: 7 additions & 0 deletions FU.API/FU.API/Services/AccountsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public async Task<ApplicationUser> Register(Credentials credentials)
throw new ConflictException("User with email already exists");
}

// Make sure the password is valid
// Must be 5 characters or longer, and contain at least one special character or number
if (credentials.Password.Length < 8 || !credentials.Password.Any(c => char.IsDigit(c) || char.IsPunctuation(c)))
{
throw new BadRequestException("Password must be at least 8 characters long and contain at least one special character or number");
}

_dbContext.Users.Add(new ApplicationUser()
{
Username = credentials.Username,
Expand Down
40 changes: 8 additions & 32 deletions FU.SPA/src/components/pages/SignUp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ export default function SignUp() {
const [username, setUsername] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [confirmedReadTerms, setConfirmedReadTerms] = useState(false);
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);

// Showing passwords when user wants
const handleClickShowPassword = () => {
setShowPassword(!showPassword);
setShowConfirmPassword(!showConfirmPassword);
};

// Update state for username
Expand All @@ -59,18 +56,11 @@ export default function SignUp() {
setPasswordError('');
};

// Update state for confirmed password
const handleConfirmPasswordChange = (event) => {
setConfirmPassword(event.target.value);
setPasswordError('');
};

// Check if all fields are filled
const isEnabled =
username.length > 0 &&
email.length > 0 &&
password.length > 0 &&
confirmPassword.length > 0 &&
password.length >= 8 &&
confirmedReadTerms;

// Function called when button is pressed
Expand All @@ -84,12 +74,6 @@ export default function SignUp() {
password: data.get('password'),
};

// Checking if passwords are identical
if (creds.password !== data.get('confirmPassword')) {
setPasswordError('Passwords do not match');
return;
}

// This try/catch block will attempt to sign the user up, check for any
// errors in signup, and redirect to signin/last page if there are no errors
try {
Expand Down Expand Up @@ -128,6 +112,9 @@ export default function SignUp() {
} else if (errorResponse?.status === 409) {
// Duplicate email
setEmailError(errorResponse.detail);
} else if (errorResponse?.status === 400) {
// bad password
setPasswordError(errorResponse.detail);
} else if (errorResponse?.errors?.Email) {
setEmailError(errorResponse.errors.Email[0]);
} else {
Expand Down Expand Up @@ -189,7 +176,10 @@ export default function SignUp() {
<Grid item xs={12}>
<TextField
error={!!passwordError}
helperText={passwordError}
helperText={
passwordError ||
'Password must be 8 characters long and contain either 1 special character or number'
}
onChange={handlePasswordChange}
required
fullWidth
Expand All @@ -213,20 +203,6 @@ export default function SignUp() {
}}
/>
</Grid>
<Grid item xs={12}>
<TextField
error={!!passwordError}
helperText={passwordError}
onChange={handleConfirmPasswordChange}
required
fullWidth
name="confirmPassword"
label="Confirm Password"
type={showConfirmPassword ? 'text' : 'password'}
id="confirmPassword"
autoComplete="new-password"
/>
</Grid>
</Grid>
<Grid item sx={{ mt: 1 }}>
<ConfirmedCheckbox
Expand Down
Loading