-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from UPbrella/dev
release 0.4.5 release-dev
- Loading branch information
Showing
15 changed files
with
307 additions
and
16 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
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
13 changes: 13 additions & 0 deletions
13
src/main/java/upbrella/be/rent/dto/response/LockerPasswordResponse.java
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,13 @@ | ||
package upbrella.be.rent.dto.response; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class LockerPasswordResponse { | ||
|
||
private String password; | ||
|
||
public LockerPasswordResponse(String password) { | ||
this.password = password.substring(0, 4); | ||
} | ||
} |
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,34 @@ | ||
package upbrella.be.rent.entity; | ||
|
||
import lombok.*; | ||
import upbrella.be.store.entity.StoreMeta; | ||
|
||
import javax.persistence.*; | ||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class Locker { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private long id; | ||
|
||
@OneToOne | ||
@JoinColumn(name = "store_meta_id") | ||
private StoreMeta storeMeta; | ||
private long count; | ||
private String secretKey; | ||
private LocalDateTime lastAccess; | ||
|
||
public void updateCount() { | ||
this.count += 1; | ||
} | ||
|
||
public void updateLastAccess(LocalDateTime now) { | ||
this.lastAccess = now; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/upbrella/be/rent/exception/LockerCodeAlreadyIssuedException.java
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,9 @@ | ||
package upbrella.be.rent.exception; | ||
|
||
public class LockerCodeAlreadyIssuedException extends RuntimeException { | ||
|
||
public LockerCodeAlreadyIssuedException(String message) { | ||
|
||
super(message); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/upbrella/be/rent/exception/LockerSignatureErrorException.java
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,9 @@ | ||
package upbrella.be.rent.exception; | ||
|
||
public class LockerSignatureErrorException extends RuntimeException { | ||
|
||
public LockerSignatureErrorException(String message) { | ||
|
||
super(message); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/upbrella/be/rent/exception/NoSignatureException.java
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,9 @@ | ||
package upbrella.be.rent.exception; | ||
|
||
public class NoSignatureException extends RuntimeException{ | ||
|
||
public NoSignatureException(String message) { | ||
|
||
super(message); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/upbrella/be/rent/repository/LockerRepository.java
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,11 @@ | ||
package upbrella.be.rent.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import upbrella.be.rent.entity.Locker; | ||
|
||
import java.util.Optional; | ||
|
||
public interface LockerRepository extends JpaRepository<Locker, Long> { | ||
|
||
Optional<Locker> findByStoreMetaId(Long storeMetaId); | ||
} |
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,99 @@ | ||
package upbrella.be.rent.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import upbrella.be.rent.dto.request.RentUmbrellaByUserRequest; | ||
import upbrella.be.rent.dto.response.LockerPasswordResponse; | ||
import upbrella.be.rent.entity.Locker; | ||
import upbrella.be.rent.exception.LockerCodeAlreadyIssuedException; | ||
import upbrella.be.rent.exception.LockerSignatureErrorException; | ||
import upbrella.be.rent.exception.NoSignatureException; | ||
import upbrella.be.rent.repository.LockerRepository; | ||
import upbrella.be.util.HotpGenerator; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class LockerService { | ||
|
||
private final LockerRepository lockerRepository; | ||
|
||
@Transactional | ||
public LockerPasswordResponse findLockerPassword(RentUmbrellaByUserRequest rentUmbrellaByUserRequest) { | ||
|
||
Optional<Locker> lockerOptional = lockerRepository.findByStoreMetaId(rentUmbrellaByUserRequest.getStoreId()); | ||
|
||
return lockerOptional.map(this::getLockerPasswordResponse).orElse(null); | ||
} | ||
|
||
public void validateLockerSignature(Long storeId, String salt, String signature) { | ||
|
||
Optional<Locker> lockerOptional = lockerRepository.findByStoreMetaId(storeId); | ||
|
||
if (lockerOptional.isEmpty() && (salt != null || signature != null)) { | ||
throw new NoSignatureException("구형 보관함은 salt와 signature가 없습니다."); | ||
} | ||
|
||
if (lockerOptional.isPresent()) { | ||
Locker locker = lockerOptional.get(); | ||
String lockerSecretKey = locker.getSecretKey().toUpperCase(); | ||
salt = salt.toUpperCase(); | ||
|
||
validateSignature(signature, salt, lockerSecretKey); | ||
} | ||
} | ||
|
||
private LockerPasswordResponse getLockerPasswordResponse(Locker locker) { | ||
|
||
if (locker.getLastAccess() != null && locker.getLastAccess().isAfter(LocalDateTime.now().minusMinutes(1))) { | ||
throw new LockerCodeAlreadyIssuedException("UBU 우산 대여 실패: 1분 이내에 이미 대여된 우산"); | ||
} | ||
|
||
String password = HotpGenerator.generate((int) locker.getCount(), locker.getSecretKey()); | ||
locker.updateCount(); | ||
locker.updateLastAccess(LocalDateTime.now()); | ||
|
||
return new LockerPasswordResponse(password); | ||
} | ||
|
||
private void validateSignature(String signature, String salt, String lockerSecretKey) { | ||
|
||
String lockerSignature = encodeHash(lockerSecretKey, salt); | ||
|
||
if (!lockerSignature.equals(signature)) { | ||
throw new LockerSignatureErrorException("우산 반납 실패: 잘못된 signature"); | ||
} | ||
} | ||
|
||
private String encodeHash(String lockerSecretKey, String salt) { | ||
MessageDigest digest; | ||
try { | ||
digest = MessageDigest.getInstance("SHA-256"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
byte[] encodedhash = digest.digest((lockerSecretKey + "." + salt).getBytes(StandardCharsets.UTF_8)); | ||
|
||
// 바이트 배열을 16진수 문자열로 변환 | ||
StringBuilder hexString = new StringBuilder(2 * encodedhash.length); | ||
|
||
for (byte b : encodedhash) { | ||
String hex = Integer.toHexString(0xff & b); | ||
if (hex.length() == 1) { | ||
hexString.append('0'); | ||
} | ||
hexString.append(hex); | ||
} | ||
return hexString.toString(); | ||
} | ||
} | ||
|
||
|
||
|
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.