-
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.
- Loading branch information
Showing
33 changed files
with
823 additions
and
49 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
35 changes: 35 additions & 0 deletions
35
src/main/java/org/kakaoshare/backend/common/util/EnumValidator.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,35 @@ | ||
package org.kakaoshare.backend.common.util; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
import java.util.Arrays; | ||
|
||
public class EnumValidator implements ConstraintValidator<EnumValue, String> { | ||
private EnumValue enumValue; | ||
|
||
@Override | ||
public void initialize(final EnumValue constraintAnnotation) { | ||
this.enumValue = constraintAnnotation; | ||
} | ||
|
||
@Override | ||
public boolean isValid(final String value, final ConstraintValidatorContext context) { | ||
final Enum<?>[] enumConstants = enumValue.enumClass().getEnumConstants(); | ||
if (enumConstants == null) { | ||
return false; | ||
} | ||
|
||
return Arrays.stream(enumConstants) | ||
.anyMatch(enumConstant -> convertible(value, enumConstant)); | ||
} | ||
|
||
private boolean convertible(final String value, final Enum<?> enumConstant) { | ||
final String trimValue = value.trim(); | ||
if (enumValue.ignoreCase()) { | ||
return trimValue.equalsIgnoreCase(enumConstant.name()); | ||
} | ||
|
||
return trimValue.equals(enumConstant.name()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/kakaoshare/backend/common/util/EnumValue.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,20 @@ | ||
package org.kakaoshare.backend.common.util; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Constraint(validatedBy = EnumValidator.class) | ||
@Target({ElementType.FIELD, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface EnumValue { | ||
Class<? extends Enum<?>> enumClass(); | ||
String message() default ""; | ||
Class<?> [] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
boolean ignoreCase() default false; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/kakaoshare/backend/common/util/MultiValueMapConverter.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,28 @@ | ||
package org.kakaoshare.backend.common.util; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
import java.util.Map; | ||
|
||
public final class MultiValueMapConverter { | ||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
private MultiValueMapConverter() { | ||
|
||
} | ||
|
||
public static MultiValueMap<String, String> convert(final Object object) { | ||
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); | ||
try { | ||
final Map<String, String> convertedValue = MAPPER.convertValue(object, new TypeReference<Map<String, String>>() { | ||
}); | ||
params.setAll(convertedValue); | ||
return params; | ||
} catch (Exception e) { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/kakaoshare/backend/domain/member/controller/OAuthController.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,23 @@ | ||
package org.kakaoshare.backend.domain.member.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.kakaoshare.backend.domain.member.dto.oauth.authenticate.OAuthLoginRequest; | ||
import org.kakaoshare.backend.domain.member.dto.oauth.authenticate.OAuthLoginResponse; | ||
import org.kakaoshare.backend.domain.member.service.oauth.OAuthService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/oauth") | ||
@RestController | ||
public class OAuthController { | ||
private final OAuthService oAuthService; | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<OAuthLoginResponse> login(@ModelAttribute final OAuthLoginRequest oAuthAuthenticateRequest) { | ||
return ResponseEntity.ok(oAuthService.login(oAuthAuthenticateRequest)); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...n/java/org/kakaoshare/backend/domain/member/dto/oauth/authenticate/OAuthLoginRequest.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,4 @@ | ||
package org.kakaoshare.backend.domain.member.dto.oauth.authenticate; | ||
|
||
public record OAuthLoginRequest(String provider, String code) { | ||
} |
14 changes: 14 additions & 0 deletions
14
.../java/org/kakaoshare/backend/domain/member/dto/oauth/authenticate/OAuthLoginResponse.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,14 @@ | ||
package org.kakaoshare.backend.domain.member.dto.oauth.authenticate; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record OAuthLoginResponse(String grantType, String accessToken) { | ||
public static OAuthLoginResponse of(final String grantType, | ||
final String accessToken) { | ||
return OAuthLoginResponse.builder() | ||
.grantType(grantType) | ||
.accessToken(accessToken) | ||
.build(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/kakaoshare/backend/domain/member/dto/oauth/profile/OAuthProfile.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,31 @@ | ||
package org.kakaoshare.backend.domain.member.dto.oauth.profile; | ||
|
||
import org.kakaoshare.backend.common.util.EnumValue; | ||
import org.kakaoshare.backend.domain.member.entity.Gender; | ||
import org.kakaoshare.backend.domain.member.entity.Member; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class OAuthProfile { | ||
protected final Map<String, Object> attributes; | ||
|
||
protected OAuthProfile(final Map<String, Object> attributes) { | ||
this.attributes = attributes; | ||
} | ||
|
||
@EnumValue(enumClass = Gender.class, message = "성별이 잘못되었습니다.", ignoreCase = true) | ||
public abstract String getGender(); | ||
public abstract String getName(); | ||
public abstract String getPhoneNumber(); | ||
public abstract String getProvider(); | ||
public abstract String getProviderId(); | ||
|
||
public Member toEntity() { | ||
return Member.builder() | ||
.name(getName()) | ||
.gender(Gender.from(getGender())) | ||
.phoneNumber(getPhoneNumber()) | ||
.providerId(getProviderId()) | ||
.build(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...main/java/org/kakaoshare/backend/domain/member/dto/oauth/profile/OAuthProfileFactory.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,29 @@ | ||
package org.kakaoshare.backend.domain.member.dto.oauth.profile; | ||
|
||
import org.kakaoshare.backend.domain.member.dto.oauth.profile.detail.KakaoProfile; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public enum OAuthProfileFactory { | ||
KAKAO("kakao", KakaoProfile::new); | ||
|
||
private final String registrationId; | ||
private final Function<Map<String, Object>, OAuthProfile> mapper; | ||
|
||
OAuthProfileFactory(final String provider, | ||
final Function<Map<String, Object>, OAuthProfile> mapper) { | ||
this.registrationId = provider; | ||
this.mapper = mapper; | ||
} | ||
|
||
public static OAuthProfile of(final Map<String, Object> attributes, | ||
final String registrationId) { | ||
return Arrays.stream(values()) | ||
.filter(value -> value.registrationId.equals(registrationId)) | ||
.findAny() | ||
.map(value -> value.mapper.apply(attributes)) | ||
.orElseThrow(() -> new IllegalArgumentException()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...main/java/org/kakaoshare/backend/domain/member/dto/oauth/profile/detail/KakaoProfile.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,44 @@ | ||
package org.kakaoshare.backend.domain.member.dto.oauth.profile.detail; | ||
|
||
import org.kakaoshare.backend.domain.member.dto.oauth.profile.OAuthProfile; | ||
|
||
import java.util.Map; | ||
|
||
public class KakaoProfile extends OAuthProfile { | ||
public KakaoProfile(final Map<String, Object> attributes) { | ||
super(attributes); | ||
} | ||
|
||
@Override | ||
public String getGender() { | ||
return String.valueOf(getAccount().get("gender")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return String.valueOf(getAccount().get("name")); | ||
} | ||
|
||
@Override | ||
public String getPhoneNumber() { | ||
return String.valueOf(getAccount().get("phone_number")); | ||
} | ||
|
||
@Override | ||
public String getProvider() { | ||
return "KAKAO"; | ||
} | ||
|
||
@Override | ||
public String getProviderId() { | ||
return String.valueOf(attributes.get("id")); | ||
} | ||
|
||
private Map<String, Object> getAccount() { | ||
return (Map<String, Object>) attributes.get("kakao_account"); | ||
} | ||
|
||
private Map<String, Object> getProfile() { | ||
return (Map<String, Object>) getAccount().get("profile"); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/kakaoshare/backend/domain/member/dto/oauth/token/OAuthTokenRequest.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,18 @@ | ||
package org.kakaoshare.backend.domain.member.dto.oauth.token; | ||
|
||
import lombok.Builder; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
|
||
@Builder | ||
public record OAuthTokenRequest(String code, String client_id, String client_secret, String grant_type, String redirect_id) { | ||
public static OAuthTokenRequest of(final ClientRegistration registration, | ||
final String code) { | ||
return OAuthTokenRequest.builder() | ||
.code(code) | ||
.client_id(registration.getClientId()) | ||
.client_secret(registration.getClientSecret()) | ||
.grant_type(registration.getAuthorizationGrantType().getValue()) | ||
.redirect_id(registration.getRedirectUri()) | ||
.build(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/kakaoshare/backend/domain/member/dto/oauth/token/OAuthTokenResponse.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,12 @@ | ||
package org.kakaoshare.backend.domain.member.dto.oauth.token; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record OAuthTokenResponse(String access_token, String refresh_token, Long expires_in) { | ||
public static OAuthTokenResponse of(final String accessToken) { | ||
return OAuthTokenResponse.builder() | ||
.access_token(accessToken) | ||
.build(); | ||
} | ||
} |
11 changes: 10 additions & 1 deletion
11
src/main/java/org/kakaoshare/backend/domain/member/entity/Gender.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 |
---|---|---|
@@ -1,5 +1,14 @@ | ||
package org.kakaoshare.backend.domain.member.entity; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum Gender { | ||
MALE, FEMALE | ||
MALE, FEMALE; | ||
|
||
public static Gender from(final String gender) { | ||
return Arrays.stream(values()) | ||
.filter(value -> value.name().equals(gender.toUpperCase())) | ||
.findAny() | ||
.orElseThrow(() -> new IllegalArgumentException()); | ||
} | ||
} |
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.