-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Authorization Server로 부터 토큰 발급 기능 추가 (#24)
* feat: 회원 엔티티 생성 및 테스트코드 추가 * feat: 카카오 OAuth 환경변수 추가 및 클래스 바인딩 * feat: authorization code를 받기 위한 queryString generator 추가 * feat: Authorization code의 parameter 만드는 로직 분리 및 테스트 코드 추가 * feat: 회원 가입/로그인 요청 api 및 소셜 로그인 페이지 반환 * refactor: member관련 클래스 네이밍과 폴더 위치 변경 * refactor: 로그인 페이지 요청 방식 Resttemplate -> response (redirect)하도록 변경 * style: 코드 포맷 재적용 및 사용하지 않는 클래스 삭제 * chore: config 파일 업데이트 * refactor: 테스트 코드 추가 및 코드 포맷 재적용 * refactor: 사용하지 않는 코드 제거 * refactor: CRLF -> LF로 변경 * fix: config 커밋, config 최근 커밋으로 변경 * feat: 테스트 코드 추가 및 패키지 구조 변경 * refactor: revert merge * fix: merge confilt해결 및 예외처리 추가 * test: oauth properties가 없을 때의 테스트코드 추가 * feat: 코드리뷰에 따른 기능 분리 및 테스트 코드 변경 * fix: 테스트코드 관련 code smell 제거 * feat: Authorization grant 받기 예외 코드 및 테스트 코드 추가 * feat: Authorization Token 요청 및 반환 코드, 에러 반환 테스트 코드 추가 * refactor: AuthenticationService에서 서버에 요청보내는 로직 OAuth2AuthorizationServerRequestService로 분리 * test: 로그인 요청 테스트 코드 추가 * feat: 토큰 발급 요청 기능 테스트 코드 추가 및 RestTemplate 필드변수로 변경 * test: restTemplate 및 서비스 테스트 추가 * refactor: 에러 메세지 이름 변경 * refacotr: 변수명 및 entity default 명 변경
- Loading branch information
Showing
17 changed files
with
562 additions
and
70 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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/moabam/api/application/OAuth2AuthorizationServerRequestService.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,55 @@ | ||
package com.moabam.api.application; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.moabam.api.dto.AuthorizationTokenResponse; | ||
import com.moabam.global.common.util.GlobalConstant; | ||
import com.moabam.global.error.exception.BadRequestException; | ||
import com.moabam.global.error.model.ErrorMessage; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@Service | ||
public class OAuth2AuthorizationServerRequestService { | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
public OAuth2AuthorizationServerRequestService() { | ||
restTemplate = new RestTemplate(); | ||
} | ||
|
||
public void loginRequest(HttpServletResponse httpServletResponse, String authorizationCodeUri) { | ||
try { | ||
httpServletResponse.setContentType(MediaType.APPLICATION_FORM_URLENCODED + GlobalConstant.CHARSET_UTF_8); | ||
httpServletResponse.sendRedirect(authorizationCodeUri); | ||
} catch (IOException e) { | ||
throw new BadRequestException(ErrorMessage.REQUEST_FAILED); | ||
} | ||
} | ||
|
||
public ResponseEntity<AuthorizationTokenResponse> requestAuthorizationServer(String tokenUri, | ||
MultiValueMap<String, String> uriParams) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add(HttpHeaders.CONTENT_TYPE, | ||
MediaType.APPLICATION_FORM_URLENCODED_VALUE + GlobalConstant.CHARSET_UTF_8); | ||
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(uriParams, headers); | ||
|
||
ResponseEntity<AuthorizationTokenResponse> authorizationTokenResponse = restTemplate.exchange(tokenUri, | ||
HttpMethod.POST, httpEntity, AuthorizationTokenResponse.class); | ||
|
||
if (authorizationTokenResponse.getStatusCode().isError()) { | ||
throw new BadRequestException(ErrorMessage.REQUEST_FAILED); | ||
} | ||
|
||
return authorizationTokenResponse; | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/moabam/api/dto/AuthorizationCodeResponse.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,10 @@ | ||
package com.moabam.api.dto; | ||
|
||
public record AuthorizationCodeResponse( | ||
String code, | ||
String error, | ||
String errorDescription, | ||
String state | ||
) { | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/moabam/api/dto/AuthorizationTokenRequest.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,24 @@ | ||
package com.moabam.api.dto; | ||
|
||
import static java.util.Objects.*; | ||
|
||
import lombok.Builder; | ||
|
||
public record AuthorizationTokenRequest( | ||
String grantType, | ||
String clientId, | ||
String redirectUri, | ||
String code, | ||
String clientSecret | ||
) { | ||
|
||
@Builder | ||
public AuthorizationTokenRequest(String grantType, String clientId, String redirectUri, String code, | ||
String clientSecret) { | ||
this.grantType = requireNonNull(grantType); | ||
this.clientId = requireNonNull(clientId); | ||
this.redirectUri = requireNonNull(redirectUri); | ||
this.code = requireNonNull(code); | ||
this.clientSecret = clientSecret; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/moabam/api/dto/AuthorizationTokenResponse.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,15 @@ | ||
package com.moabam.api.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record AuthorizationTokenResponse( | ||
@JsonProperty("token_type") String tokenType, | ||
@JsonProperty("access_token") String accessToken, | ||
@JsonProperty("id_token") String idToken, | ||
@JsonProperty("expires_in") String expiresIn, | ||
@JsonProperty("refresh_token") String refreshToken, | ||
@JsonProperty("refresh_token_expires_in") String refreshTokenExpiresIn, | ||
@JsonProperty("scope") String scope | ||
) { | ||
|
||
} |
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
Oops, something went wrong.