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

프로젝트 모든 메서드에 주석 추가하기 #262

Merged
merged 12 commits into from
Mar 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
2 changes: 1 addition & 1 deletion my-garden-be/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ include::dailyroutine/put.adoc[]
include::dailyroutine/delete.adoc[]

[[board-category]]
== Board Category [게시판 카테고리]
== Board Category [게시판 분류]

include::board/category/getList.adoc[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,55 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 인증 관련 API 컨트롤러
*/
@RestController
@RequiredArgsConstructor
@RequestMapping(AuthenticationController.AUTH_BASE_API_PATH)
public class AuthenticationController {
/**
* 인증 API의 기본 경로
*/
public static final String AUTH_BASE_API_PATH = "/api/auth";

/**
* 인증 서비스
*/
private final AuthenticationService authenticationService;

/**
* 회원가입
*
* @param request 회원가입 요청 객체
* @return 회원가입의 결과로 생성된 회원 ID를 반환
*/
@PostMapping("/signup")
public ApiResponse<Long> signUp(@RequestBody @Valid SignupRequest request) {
final Long memberId = authenticationService.signUp(request.email(), request.password());

return ApiResponse.ok(memberId);
}

/**
* 로그인
*
* @param request 로그인 요청 객체
* @return 로그인의 결과로 생성된 토큰들을 반환 (accessToken, refreshToken)
*/
@PostMapping("/login")
public ApiResponse<AuthenticationResponse> login(@RequestBody @Valid LoginRequest request) {
final AuthenticationResponse response = authenticationService.login(request.email(), request.password());

return ApiResponse.ok(response);
}

/**
* 토큰 재발급
*
* @param request 토큰 재발급 요청 객체
* @return 토큰 재발급의 결과로 생성된 토큰들을 반환 (accessToken, refreshToken)
*/
@PostMapping("/refresh")
public ApiResponse<AuthenticationResponse> refresh(@RequestBody @Valid RefreshRequest request) {
final AuthenticationResponse response = authenticationService.refresh(request.refreshToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import jakarta.validation.constraints.NotEmpty;
import lombok.Builder;

/**
* 로그인 요청시에 사용하는 Request DTO
*
* @param email 이메일
* @param password 비밀번호
*/
@Builder
public record LoginRequest(
@NotEmpty(message = "이메일은 null이 될 수 없습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import jakarta.validation.constraints.NotBlank;

/**
* 토큰 재발급 요청시에 사용하는 Request DTO [로그인시에 발급했던 리프레시 토큰을 사용하여 재발급]
*
* @param refreshToken 리프레시 토큰
*/
public record RefreshRequest(
@NotBlank(message = "리프레시 토큰은 비어있을 수 없습니다.")
String refreshToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import jakarta.validation.constraints.NotBlank;
import lombok.Builder;

/**
* 회원가입 요청시에 사용하는 Request DTO
*
* @param email 이메일
* @param password 비밀번호
*/
@Builder
public record SignupRequest(
@NotBlank(message = "이메일은 null이 될 수 없습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@
import lombok.Getter;
import org.springframework.util.Assert;

/**
* 토큰 Domain
*/
@Getter
public class Token {
/**
* 토큰 값
*/
private String tokenText;
/**
* 토큰 타입 - Bearer
*/
private TokenType tokenType;
/**
* 토큰 폐기 여부
*/
private boolean revoked;
/**
* 토큰 만료 여부
*/
private boolean expired;

@Builder(access = AccessLevel.PRIVATE)
Expand All @@ -20,6 +35,15 @@ private Token(final String tokenText, final TokenType tokenType, final boolean r
this.expired = expired;
}

/**
* 토큰 Doamin 생성
*
* @param tokenText 토큰 값
* @param tokenType 토큰 타입
* @param revoked 폐기 여부
* @param expired 만료 여부
* @return 토큰 Domain
*/
public static Token of(final String tokenText, final TokenType tokenType, final boolean revoked, final boolean expired) {
Assert.hasText(tokenText, "토큰은 null 혹은 빈 문자열이 될 수 없습니다.");
Assert.notNull(tokenType, "토큰 타입은 null이 될 수 없습니다.");
Expand All @@ -32,30 +56,58 @@ public static Token of(final String tokenText, final TokenType tokenType, final
.build();
}

/**
* Bearer 타입의 토큰 생성
*
* @param tokenText 토큰 값
* @return Bearer 토큰 Domain
*/
public static Token createBearerToken(final String tokenText) {
return of(tokenText, TokenType.BEARER, false, false);
}

/**
* 토큰이 유효한지 확인
*
* @return 유효 여부
*/
public boolean isValid() {
return !this.revoked && !this.expired;
}

/**
* 토큰을 폐기한다.
*/
public void revoke() {
this.revoked = true;
this.expired = true;
}

/**
* 토큰을 만료한다.
*/
public void expire() {
this.expired = true;
}

/**
* 토큰 값이 같은지 확인
*
* @param tokenText 비교할 토큰 값
* @return 받아온 토큰과 현재 토큰이 같은지 여부
*/
public boolean isSameTokenText(final String tokenText) {
return this.tokenText.equals(tokenText);
}

/**
* 토큰을 갱신한다.
*
* @param refreshTokenText 갱신할 토큰 값
*/
public void refresh(final String refreshTokenText) {
Assert.hasText(refreshTokenText, "토큰은 null 혹은 빈 문자열이 될 수 없습니다.");

this.tokenText = refreshTokenText;
this.revoked = false;
this.expired = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@

import lombok.Getter;

/**
* 토큰 타입
*/
@Getter
public enum TokenType {
/**
* Bearer 타입
*/
BEARER("Bearer ");

/**
* parsing시에 사용할 Text
*/
private final String parseText;

TokenType(final String parseText) {
this.parseText = parseText;
}

/**
* parseText 길이 반환
*
* @return parseText 길이
*/
public int getParseTextLength() {
return this.parseText.length();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,46 @@
import org.hyunggi.mygardenbe.common.entity.BaseEntity;
import org.springframework.util.Assert;

/**
* 토큰 Entity
*/
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class TokenEntity extends BaseEntity {
/**
* 토큰 ID
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

/**
* 토큰 값
*/
@Getter
@Column(nullable = false, unique = true)
private String tokenText;

/**
* 토큰 타입
*/
@Enumerated(EnumType.STRING)
@Column(nullable = false, length = 10)
private TokenType tokenType;

/**
* 폐기 여부
*/
private boolean revoked;

/**
* 만료 여부
*/
private boolean expired;

/**
* 회원 ID
*/
@Getter
private Long memberId;

Expand All @@ -36,6 +62,13 @@ private TokenEntity(final String tokenText, final TokenType tokenType, final boo
this.memberId = memberId;
}

/**
* 토큰 생성
*
* @param token 토큰 Entity로 변환할 토큰 Domain
* @param memberId 회원 ID
* @return 토큰 Entity
*/
public static TokenEntity of(final Token token, final Long memberId) {
Assert.notNull(token, "토큰은 null이 될 수 없습니다.");
Assert.isTrue(memberId != null && memberId > 0, "회원 ID는 null이 될 수 없고, 0보다 커야 합니다.");
Expand All @@ -49,10 +82,20 @@ public static TokenEntity of(final Token token, final Long memberId) {
.build();
}

/**
* 토큰 Entity를 토큰 Domain으로 변환
*
* @return 토큰 Domain
*/
public Token toDomain() {
return Token.of(this.tokenText, this.tokenType, this.revoked, this.expired);
}

/**
* 토큰 업데이트
*
* @param token 업데이트할 토큰
*/
public void update(final Token token) {
Assert.notNull(token, "토큰은 null이 될 수 없습니다.");

Expand Down
Loading
Loading