feat(password): simplify password validation rules

This commit is contained in:
aditya.chandel
2025-03-18 03:41:47 -06:00
committed by Aditya Chandel
parent 4e5b9b633b
commit e70a8e2497
3 changed files with 36 additions and 33 deletions
+29 -25
View File
@@ -2,9 +2,6 @@ name: Build, Tag, Push, and Release to GitHub Container Registry
on:
push:
branches:
- master
- develop
jobs:
build-and-push:
@@ -23,37 +20,43 @@ jobs:
- name: Log in to GitHub Container Registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Get Latest Tag
- name: Get Latest Master Version
id: get_version
run: |
branch="${GITHUB_REF#refs/heads/}"
if [[ "$branch" == "master" ]]; then
latest_tag=$(git tag --list "v*" --sort=-v:refname | head -n 1)
else
latest_tag=$(git tag --list "develop-v*" --sort=-v:refname | head -n 1)
fi
latest_tag=$(git tag --list "v*" --sort=-v:refname | head -n 1)
latest_tag=${latest_tag:-"v0.0.0"}
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
echo "Latest tag: $latest_tag"
echo "Latest master tag: $latest_tag"
- name: Auto-Increment Version
- name: Auto-Increment Version (Only for Master)
if: github.ref == 'refs/heads/master'
id: bump_version
run: |
old_version=${{ env.latest_tag }}
IFS='.' read -r -a parts <<< "${old_version//[!0-9.]/}"
new_version="${parts[0]}.${parts[1]}.$((parts[2] + 1))"
branch="${GITHUB_REF#refs/heads/}"
if [[ "$branch" == "master" ]]; then
new_tag="v${new_version}"
else
new_tag="develop-v${new_version}"
fi
new_tag="v${new_version}"
echo "new_tag=$new_tag" >> $GITHUB_ENV
echo "New version: $new_tag"
- name: Create Git Tag
- name: Generate Image Tag
id: set_image_tag
run: |
branch="${GITHUB_REF#refs/heads/}"
if [[ "$branch" == "master" ]]; then
image_tag="${{ env.new_tag }}"
elif [[ "$branch" == "develop" ]]; then
short_sha=$(git rev-parse --short HEAD)
image_tag="${{ env.latest_tag }}-develop-${short_sha}"
else
short_sha=$(git rev-parse --short HEAD)
image_tag="${short_sha}"
fi
echo "image_tag=$image_tag" >> $GITHUB_ENV
echo "Image tag: $image_tag"
- name: Create Git Tag (Only for Master)
if: github.ref == 'refs/heads/master'
run: |
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
@@ -62,19 +65,20 @@ jobs:
- name: Build Docker Image
run: |
docker build -t ghcr.io/${{ github.actor }}/booklore-app:${{ env.new_tag }} .
docker build -t ghcr.io/${{ github.actor }}/booklore-app:${{ env.image_tag }} .
- name: Push Docker Image to GHCR
run: |
docker push ghcr.io/${{ github.actor }}/booklore-app:${{ env.new_tag }}
docker push ghcr.io/${{ github.actor }}/booklore-app:${{ env.image_tag }}
- name: Push Latest Tag (only for master)
- name: Push Latest Tag (Only for Master)
if: github.ref == 'refs/heads/master'
run: |
docker tag ghcr.io/${{ github.actor }}/booklore-app:${{ env.new_tag }} ghcr.io/${{ github.actor }}/booklore-app:latest
docker push ghcr.io/${{ github.actor }}/booklore-app:latest
- name: Create GitHub Release
- name: Create GitHub Release (Only for Master)
if: github.ref == 'refs/heads/master'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
@@ -32,7 +32,7 @@ public enum ApiError {
CANNOT_DELETE_ADMIN(HttpStatus.FORBIDDEN, "Admin user cannot be deleted"),
UNAUTHORIZED(HttpStatus.FORBIDDEN, "%s"),
PASSWORD_INCORRECT(HttpStatus.BAD_REQUEST, "Incorrect current password"),
PASSWORD_WEAK(HttpStatus.BAD_REQUEST, "New password is not strong enough. It must be at least 8 characters long, contain an uppercase letter, a lowercase letter, a number, and a special character."),
PASSWORD_TOO_SHORT(HttpStatus.BAD_REQUEST, "Password must be at least 6 characters long"),
PASSWORD_SAME_AS_CURRENT(HttpStatus.BAD_REQUEST, "New password cannot be the same as the current password"),
INVALID_CREDENTIALS(HttpStatus.BAD_REQUEST, "Invalid credentials");
@@ -103,8 +103,8 @@ public class UserService {
throw ApiError.PASSWORD_SAME_AS_CURRENT.createException();
}
if (!isPasswordStrong(changePasswordRequest.getNewPassword())) {
throw ApiError.PASSWORD_WEAK.createException();
if (!isValidPassword(changePasswordRequest.getNewPassword())) {
throw ApiError.PASSWORD_TOO_SHORT.createException();
}
bookLoreUserEntity.setDefaultPassword(false);
@@ -114,15 +114,14 @@ public class UserService {
public void changeUserPassword(ChangeUserPasswordRequest request) {
BookLoreUserEntity userEntity = userRepository.findById(request.getUserId()).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(request.getUserId()));
if (!isPasswordStrong(request.getNewPassword())) {
throw ApiError.PASSWORD_WEAK.createException();
if (!isValidPassword(request.getNewPassword())) {
throw ApiError.PASSWORD_TOO_SHORT.createException();
}
userEntity.setPasswordHash(passwordEncoder.encode(request.getNewPassword()));
userRepository.save(userEntity);
}
private boolean isPasswordStrong(String password) {
String passwordPattern = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$";
return password.matches(passwordPattern);
private boolean isValidPassword(String password) {
return password != null && password.length() >= 6;
}
}