Skip to content

Commit

Permalink
feat: #11 Controller flow ๊ตฌ์ƒ
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Dec 23, 2023
1 parent 1c1af45 commit 255bf66
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/kcy/fitapet/domain/member/api/AuthApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ public ResponseEntity<?> refresh(@CookieValue("refreshToken") @Valid String refr
* @return ResponseEntity<?>
*/
private ResponseEntity<?> getResponseEntity(Map<String, String> tokens) {
log.debug("access token: {}", tokens.get(ACCESS_TOKEN.getValue()));
log.debug("refresh token: {}", tokens.get(REFRESH_TOKEN.getValue()));
ResponseCookie cookie = cookieUtil.createCookie(REFRESH_TOKEN.getValue(), tokens.get(REFRESH_TOKEN.getValue()), 60 * 60 * 24 * 7);

return ResponseEntity.ok()
Expand Down
49 changes: 44 additions & 5 deletions src/main/java/com/kcy/fitapet/domain/oauth/api/OauthApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,84 @@
import com.kcy.fitapet.domain.oauth.dto.OauthSignUpReq;
import com.kcy.fitapet.domain.oauth.service.component.OauthService;
import com.kcy.fitapet.domain.oauth.type.ProviderType;
import com.kcy.fitapet.global.common.response.SuccessResponse;
import com.kcy.fitapet.global.common.security.jwt.dto.Jwt;
import com.kcy.fitapet.global.common.util.cookie.CookieUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.core5.http.HttpStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.kcy.fitapet.global.common.security.jwt.AuthConstants.*;
import static com.kcy.fitapet.global.common.security.jwt.AuthConstants.ACCESS_TOKEN;

@Tag(name = "OAuth API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/auth/oauth")
@Slf4j
public class OauthApi {
private final OauthService oAuthService;
private final CookieUtil cookieUtil;

@Operation(summary = "OAuth ๋กœ๊ทธ์ธ")
@Parameters({
@Parameter(name = "provider", description = "OAuth ์ œ๊ณต์ž"),
@Parameter(name = "req", description = "OAuth ๋กœ๊ทธ์ธ ์š”์ฒญ ์ •๋ณด")
})
@PostMapping("")
@PreAuthorize("isAnonymous()")
public void signIn(
public ResponseEntity<?> signIn(
@RequestParam("provider") ProviderType provider,
@RequestBody @Valid OauthSignInReq req
) {
Jwt jwt = null;
if (ProviderType.NAVER.equals(provider)) {

return null;
} else {

jwt = oAuthService.signInByOIDC(req.id(), req.id_token(), provider, req.nonce());
}

return (jwt == null)
? ResponseEntity.ok(SuccessResponse.from(Map.of("id", req.id())))
: getResponseEntity(jwt);
}

@PostMapping("/{id}")
@PreAuthorize("isAnonymous()")
public void signUp(
public ResponseEntity<?> signUp(
@PathVariable("id") Long id,
@RequestParam("provider") ProviderType provider,
@RequestBody @Valid OauthSignUpReq req
) {
Jwt jwt = null;
if (ProviderType.NAVER.equals(provider)) {

return null;
} else {

}

ResponseCookie cookie = cookieUtil.createCookie(REFRESH_TOKEN.getValue(), jwt.refreshToken(), 60 * 60 * 24 * 7);
return getResponseEntity(jwt);
}

private ResponseEntity<?> getResponseEntity(Jwt jwt) {
ResponseCookie cookie = cookieUtil.createCookie(REFRESH_TOKEN.getValue(), jwt.refreshToken(), 60 * 60 * 24 * 7);

return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.header(ACCESS_TOKEN.getValue(), jwt.accessToken())
.body(SuccessResponse.noContent());
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.kcy.fitapet.domain.oauth.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;

@Schema(description = "Oauth Sign In Request")
public record OauthSignInReq(
@Schema(description = "Member Oauth Id")
@NotEmpty
Long id,
@Schema(description = "Member Oauth Id Token")
@NotEmpty
String id_token,
@Schema(description = "Member Oauth Nonce")
@NotEmpty
String nonce
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class OauthService {
private final OauthClientHelper oauthClientHelper;

private final JwtUtil jwtUtil;

@Transactional
public void signUpByOIDC() {

Expand All @@ -53,6 +54,8 @@ public Jwt signInByOIDC(Long id, String idToken, ProviderType provider, String n
Member member = oauthSearchService.findMemberByOauthIdAndProvider(id, provider);
return generateToken(JwtUserInfo.from(member));
} else {
// 1. redis์— {id_token, id} ์ €์žฅ
// 2. null ๋ฐ˜ํ™˜ (ํšŒ์›๊ฐ€์ž… ์ง„ํ–‰)
return null;
}
}
Expand Down

0 comments on commit 255bf66

Please sign in to comment.