Skip to content

Commit

Permalink
feat: 코드리뷰에 따른 기능 분리 및 테스트 코드 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
parksey committed Oct 31, 2023
1 parent dff5e2e commit 31407ab
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ gradle-app.setting
logs/
application-*.yml
src/main/resources/config
!application-test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package com.moabam.api.application;

import static com.moabam.global.common.util.OAuthParameterNames.*;

import java.io.IOException;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

import com.moabam.api.dto.AuthorizationCodeIssue;
import com.moabam.api.dto.AuthorizationCodeRequest;
import com.moabam.api.mapper.OAuthMapper;
import com.moabam.global.common.util.GlobalConstant;
import com.moabam.global.config.OAuthConfig;
import com.moabam.global.error.exception.BadRequestException;
import com.moabam.global.error.model.ErrorMessage;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -13,12 +24,34 @@ public class AuthenticationService {

private final OAuthConfig oAuthConfig;

public String getAuthorizaionCodeUri() {
return AuthorizationCodeIssue.builder()
.clientId(oAuthConfig.client().clientId())
.redirectUri(oAuthConfig.provider().redirectUri())
.scope(oAuthConfig.client().scope())
.build()
.generateQueryParamsWith(oAuthConfig.provider().authorizationUri());
private String getAuthorizaionCodeUri() {
AuthorizationCodeRequest authorizationCodeRequest = OAuthMapper.toAuthorizationCodeRequest(oAuthConfig);
return generateQueryParamsWith(authorizationCodeRequest);
}

private String generateQueryParamsWith(AuthorizationCodeRequest authorizationCodeRequest) {
UriComponentsBuilder authorizationCodeUri = UriComponentsBuilder
.fromUriString(oAuthConfig.provider().authorizationUri())
.queryParam(RESPONSE_TYPE, CODE)
.queryParam(CLIENT_ID, authorizationCodeRequest.clientId())
.queryParam(REDIRECT_URI, authorizationCodeRequest.redirectUri());

if (!authorizationCodeRequest.scope().isEmpty()) {
String scopes = String.join(GlobalConstant.COMMA, authorizationCodeRequest.scope());
authorizationCodeUri.queryParam(SCOPE, scopes);
}

return authorizationCodeUri.toUriString();
}

public void redirectToLoginPage(HttpServletResponse httpServletResponse) {
String authorizationCodeUri = getAuthorizaionCodeUri();

try {
httpServletResponse.setContentType(MediaType.APPLICATION_FORM_URLENCODED + GlobalConstant.CHARSET_UTF_8);
httpServletResponse.sendRedirect(authorizationCodeUri);
} catch (IOException e) {
throw new BadRequestException(ErrorMessage.REQUEST_FAILD);
}
}
}
16 changes: 11 additions & 5 deletions src/main/java/com/moabam/api/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.time.LocalDateTime;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import com.moabam.global.common.entity.BaseTimeEntity;
import com.moabam.global.common.util.BaseImageUrl;
Expand All @@ -24,13 +26,15 @@

@Getter
@Entity
@Table(name = "members")
@Table(name = "member")
@SQLDelete(sql = "UPDATE member SET deleted_at = CURRENT_TIMESTAMP where participant_id = ?")
@Where(clause = "deleted_at IS NOT NULL")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_id")
@Column(name = "id")
private Long id;

@Column(name = "social_id", nullable = false, unique = true)
Expand All @@ -39,7 +43,7 @@ public class Member extends BaseTimeEntity {
@Column(name = "nickname", nullable = false, unique = true)
private String nickname;

@Column(name = "intro")
@Column(name = "intro", length = 30)
private String intro;

@Column(name = "profile_image", nullable = false)
Expand Down Expand Up @@ -75,16 +79,18 @@ public class Member extends BaseTimeEntity {

@Enumerated(EnumType.STRING)
@Column(name = "role", nullable = false)
@ColumnDefault("USER")
private Role role;

@Column(name = "deleted_at")
private LocalDateTime deletedAt;

@Builder
private Member(String socialId, String nickname, String profileImage) {
private Member(Long id, String socialId, String nickname, String profileImage) {
this.id = id;
this.socialId = requireNonNull(socialId);
this.nickname = requireNonNull(nickname);
this.profileImage = requireNonNullElse(profileImage, BaseImageUrl.PROFILE_URL.getUrl());
this.profileImage = requireNonNullElse(profileImage, BaseImageUrl.PROFILE_URL);
this.role = Role.USER;
}
}
46 changes: 0 additions & 46 deletions src/main/java/com/moabam/api/dto/AuthorizationCodeIssue.java

This file was deleted.

26 changes: 26 additions & 0 deletions src/main/java/com/moabam/api/dto/AuthorizationCodeRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.moabam.api.dto;

import static java.util.Objects.*;

import java.util.List;

import lombok.Builder;

public record AuthorizationCodeRequest(
String clientId,
String redirectUri,
String responseType,
List<String> scope,
String state
) {

@Builder
public AuthorizationCodeRequest(String clientId, String redirectUri, String responseType, List<String> scope,
String state) {
this.clientId = requireNonNull(clientId);
this.redirectUri = requireNonNull(redirectUri);
this.responseType = responseType;
this.scope = scope;
this.state = state;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/moabam/api/mapper/OAuthMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.moabam.api.mapper;

import com.moabam.api.dto.AuthorizationCodeRequest;
import com.moabam.global.config.OAuthConfig;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OAuthMapper {

public static AuthorizationCodeRequest toAuthorizationCodeRequest(OAuthConfig oAuthConfig) {
return AuthorizationCodeRequest.builder()
.clientId(oAuthConfig.client().clientId())
.redirectUri(oAuthConfig.provider().redirectUri())
.scope(oAuthConfig.client().scope())
.build();
}
}
10 changes: 2 additions & 8 deletions src/main/java/com/moabam/api/presentation/MemberController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.moabam.api.presentation;

import java.io.IOException;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.moabam.api.application.AuthenticationService;
import com.moabam.global.common.util.GlobalConstant;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,9 +17,7 @@ public class MemberController {
private final AuthenticationService authenticationService;

@GetMapping
public void socialLogin(HttpServletResponse httpServletResponse) throws IOException {
String authorizationCodeUri = authenticationService.getAuthorizaionCodeUri();
httpServletResponse.setContentType(MediaType.APPLICATION_FORM_URLENCODED + GlobalConstant.CHARSET_UTF_8);
httpServletResponse.sendRedirect(authorizationCodeUri);
public void socialLogin(HttpServletResponse httpServletResponse) {
authenticationService.redirectToLoginPage(httpServletResponse);
}
}
15 changes: 5 additions & 10 deletions src/main/java/com/moabam/global/common/util/BaseImageUrl.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.moabam.global.common.util;

import lombok.Getter;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@Getter
public enum BaseImageUrl {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BaseImageUrl {

PROFILE_URL("/profile/baseUrl");

private String url;

BaseImageUrl(String url) {
this.url = url;
}
public static final String PROFILE_URL = "/profile/baseUrl";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
public enum ErrorMessage {

INVALID_REQUEST_FIELD("올바른 요청 정보가 아닙니다."),
LOGIN_FAILED("로그인에 실패했습니다.");
LOGIN_FAILED("로그인에 실패했습니다."),
REQUEST_FAILD("네트우크 접근 실패입니다.");

private final String message;
}
Loading

0 comments on commit 31407ab

Please sign in to comment.