feat(password): simplify password validation rules
This commit is contained in:
committed by
Aditya Chandel
parent
4e5b9b633b
commit
e70a8e2497
@@ -2,9 +2,6 @@ name: Build, Tag, Push, and Release to GitHub Container Registry
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- develop
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-push:
|
build-and-push:
|
||||||
@@ -23,37 +20,43 @@ jobs:
|
|||||||
- name: Log in to GitHub Container Registry
|
- name: Log in to GitHub Container Registry
|
||||||
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
|
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
|
id: get_version
|
||||||
run: |
|
run: |
|
||||||
branch="${GITHUB_REF#refs/heads/}"
|
latest_tag=$(git tag --list "v*" --sort=-v:refname | head -n 1)
|
||||||
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=${latest_tag:-"v0.0.0"}
|
latest_tag=${latest_tag:-"v0.0.0"}
|
||||||
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
|
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
|
id: bump_version
|
||||||
run: |
|
run: |
|
||||||
old_version=${{ env.latest_tag }}
|
old_version=${{ env.latest_tag }}
|
||||||
IFS='.' read -r -a parts <<< "${old_version//[!0-9.]/}"
|
IFS='.' read -r -a parts <<< "${old_version//[!0-9.]/}"
|
||||||
new_version="${parts[0]}.${parts[1]}.$((parts[2] + 1))"
|
new_version="${parts[0]}.${parts[1]}.$((parts[2] + 1))"
|
||||||
|
new_tag="v${new_version}"
|
||||||
branch="${GITHUB_REF#refs/heads/}"
|
|
||||||
if [[ "$branch" == "master" ]]; then
|
|
||||||
new_tag="v${new_version}"
|
|
||||||
else
|
|
||||||
new_tag="develop-v${new_version}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "new_tag=$new_tag" >> $GITHUB_ENV
|
echo "new_tag=$new_tag" >> $GITHUB_ENV
|
||||||
echo "New version: $new_tag"
|
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: |
|
run: |
|
||||||
git config --global user.name "github-actions"
|
git config --global user.name "github-actions"
|
||||||
git config --global user.email "github-actions@github.com"
|
git config --global user.email "github-actions@github.com"
|
||||||
@@ -62,19 +65,20 @@ jobs:
|
|||||||
|
|
||||||
- name: Build Docker Image
|
- name: Build Docker Image
|
||||||
run: |
|
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
|
- name: Push Docker Image to GHCR
|
||||||
run: |
|
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'
|
if: github.ref == 'refs/heads/master'
|
||||||
run: |
|
run: |
|
||||||
docker tag ghcr.io/${{ github.actor }}/booklore-app:${{ env.new_tag }} ghcr.io/${{ github.actor }}/booklore-app:latest
|
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
|
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:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public enum ApiError {
|
|||||||
CANNOT_DELETE_ADMIN(HttpStatus.FORBIDDEN, "Admin user cannot be deleted"),
|
CANNOT_DELETE_ADMIN(HttpStatus.FORBIDDEN, "Admin user cannot be deleted"),
|
||||||
UNAUTHORIZED(HttpStatus.FORBIDDEN, "%s"),
|
UNAUTHORIZED(HttpStatus.FORBIDDEN, "%s"),
|
||||||
PASSWORD_INCORRECT(HttpStatus.BAD_REQUEST, "Incorrect current password"),
|
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"),
|
PASSWORD_SAME_AS_CURRENT(HttpStatus.BAD_REQUEST, "New password cannot be the same as the current password"),
|
||||||
INVALID_CREDENTIALS(HttpStatus.BAD_REQUEST, "Invalid credentials");
|
INVALID_CREDENTIALS(HttpStatus.BAD_REQUEST, "Invalid credentials");
|
||||||
|
|
||||||
|
|||||||
@@ -103,8 +103,8 @@ public class UserService {
|
|||||||
throw ApiError.PASSWORD_SAME_AS_CURRENT.createException();
|
throw ApiError.PASSWORD_SAME_AS_CURRENT.createException();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isPasswordStrong(changePasswordRequest.getNewPassword())) {
|
if (!isValidPassword(changePasswordRequest.getNewPassword())) {
|
||||||
throw ApiError.PASSWORD_WEAK.createException();
|
throw ApiError.PASSWORD_TOO_SHORT.createException();
|
||||||
}
|
}
|
||||||
|
|
||||||
bookLoreUserEntity.setDefaultPassword(false);
|
bookLoreUserEntity.setDefaultPassword(false);
|
||||||
@@ -114,15 +114,14 @@ public class UserService {
|
|||||||
|
|
||||||
public void changeUserPassword(ChangeUserPasswordRequest request) {
|
public void changeUserPassword(ChangeUserPasswordRequest request) {
|
||||||
BookLoreUserEntity userEntity = userRepository.findById(request.getUserId()).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(request.getUserId()));
|
BookLoreUserEntity userEntity = userRepository.findById(request.getUserId()).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(request.getUserId()));
|
||||||
if (!isPasswordStrong(request.getNewPassword())) {
|
if (!isValidPassword(request.getNewPassword())) {
|
||||||
throw ApiError.PASSWORD_WEAK.createException();
|
throw ApiError.PASSWORD_TOO_SHORT.createException();
|
||||||
}
|
}
|
||||||
userEntity.setPasswordHash(passwordEncoder.encode(request.getNewPassword()));
|
userEntity.setPasswordHash(passwordEncoder.encode(request.getNewPassword()));
|
||||||
userRepository.save(userEntity);
|
userRepository.save(userEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPasswordStrong(String password) {
|
private boolean isValidPassword(String password) {
|
||||||
String passwordPattern = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$";
|
return password != null && password.length() >= 6;
|
||||||
return password.matches(passwordPattern);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user