-
Notifications
You must be signed in to change notification settings - Fork 1
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
16 changed files
with
214 additions
and
13 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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/umc5th/muffler/domain/member/dto/AppleIdToken.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,8 @@ | ||
package com.umc5th.muffler.domain.member.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class AppleIdToken { | ||
private String sub; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/umc5th/muffler/domain/member/dto/AppleToken.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 com.umc5th.muffler.domain.member.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class AppleToken { | ||
@JsonProperty("access_token") | ||
private String accessToken; | ||
@JsonProperty("token_type") | ||
private String tokenType; | ||
@JsonProperty("expires_in") | ||
private String expiresIn; | ||
@JsonProperty("refresh_token") | ||
private String refreshToken; | ||
@JsonProperty("id_token") | ||
private String idToken; | ||
} |
4 changes: 0 additions & 4 deletions
4
src/main/java/com/umc5th/muffler/domain/member/dto/KakaoUnlinkResponse.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,12 +1,8 @@ | ||
package com.umc5th.muffler.domain.member.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class KakaoUnlinkResponse { | ||
private Long id; | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/umc5th/muffler/domain/member/service/AppleProperties.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,46 @@ | ||
package com.umc5th.muffler.domain.member.service; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import javax.annotation.PostConstruct; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.FileCopyUtils; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "social-login.apple") | ||
@Getter | ||
@Setter | ||
@RequiredArgsConstructor | ||
public class AppleProperties { | ||
private String grantType; | ||
private String clientId; | ||
private String keyId; | ||
private String teamId; | ||
private String audience; | ||
private String privateKeyPath; | ||
private String privateKey; | ||
|
||
private final ResourceLoader resourceLoader; | ||
|
||
@PostConstruct | ||
public void init() throws IOException { | ||
Resource resource = resourceLoader.getResource(privateKeyPath); | ||
try (InputStreamReader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { | ||
this.privateKey = FileCopyUtils.copyToString(reader); | ||
} | ||
this.privateKey = processKey(this.privateKey); | ||
} | ||
|
||
private String processKey(String pemKey) { | ||
return pemKey.replaceAll("-----BEGIN PRIVATE KEY-----", "") | ||
.replaceAll("-----END PRIVATE KEY-----", "") | ||
.replaceAll("\\s", ""); | ||
} | ||
} |
56 changes: 55 additions & 1 deletion
56
src/main/java/com/umc5th/muffler/domain/member/service/AppleService.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,13 +1,67 @@ | ||
package com.umc5th.muffler.domain.member.service; | ||
|
||
import static com.umc5th.muffler.global.response.code.ErrorCode.INTERNAL_SERVER_ERROR; | ||
|
||
import com.umc5th.muffler.domain.member.dto.AppleIdToken; | ||
import com.umc5th.muffler.domain.member.dto.LoginRequest; | ||
import com.umc5th.muffler.global.feign.AppleClient; | ||
import com.umc5th.muffler.global.response.exception.MemberException; | ||
import com.umc5th.muffler.global.security.jwt.JwtDecoder; | ||
import com.umc5th.muffler.global.util.DateTimeProvider; | ||
import io.jsonwebtoken.JwsHeader; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import java.security.PrivateKey; | ||
import java.security.Security; | ||
import java.util.Base64; | ||
import lombok.RequiredArgsConstructor; | ||
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AppleService { | ||
private final AppleClient appleClient; | ||
private final AppleProperties appleProperties; | ||
private final DateTimeProvider dateTimeProvider; | ||
|
||
public String login(LoginRequest request) { | ||
return request.getIdToken(); | ||
String authentication = request.getIdToken(); | ||
String idToken = appleClient.getIdToken( | ||
appleProperties.getClientId(), | ||
generateClientSecret(), | ||
appleProperties.getGrantType(), | ||
authentication | ||
).getIdToken(); | ||
|
||
return JwtDecoder.decodePayload(idToken, AppleIdToken.class).getSub(); | ||
} | ||
|
||
private String generateClientSecret() { | ||
return Jwts.builder() | ||
.setHeaderParam(JwsHeader.KEY_ID, appleProperties.getKeyId()) | ||
.setIssuer(appleProperties.getTeamId()) | ||
.setAudience(appleProperties.getAudience()) | ||
.setSubject(appleProperties.getClientId()) | ||
.setExpiration(dateTimeProvider.getDateAfterMinutes(5)) | ||
.setIssuedAt(dateTimeProvider.getIssuedDate()) | ||
.signWith(getPrivateKey(), SignatureAlgorithm.ES256) | ||
.compact(); | ||
} | ||
|
||
private PrivateKey getPrivateKey() { | ||
Security.addProvider(new BouncyCastleProvider()); | ||
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC"); | ||
|
||
try { | ||
byte[] privateKeyBytes = Base64.getDecoder().decode(appleProperties.getPrivateKey()); | ||
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(privateKeyBytes); | ||
return converter.getPrivateKey(privateKeyInfo); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new MemberException(INTERNAL_SERVER_ERROR, "String 타입 ApplePrivateKey convert 중 에러 발생"); | ||
} | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/umc5th/muffler/global/feign/AppleClient.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 com.umc5th.muffler.global.feign; | ||
|
||
import com.umc5th.muffler.domain.member.dto.AppleToken; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@FeignClient( | ||
name = "apple-client", | ||
url = "https://appleid.apple.com" | ||
) | ||
public interface AppleClient { | ||
@PostMapping("/auth/token") | ||
AppleToken getIdToken( | ||
@RequestParam("client_id") String clientId, | ||
@RequestParam("client_secret") String clientSecret, | ||
@RequestParam("grant_type") String grantType, | ||
@RequestParam("code") String code | ||
); | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/umc5th/muffler/global/security/jwt/JwtDecoder.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,27 @@ | ||
package com.umc5th.muffler.global.security.jwt; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.umc5th.muffler.global.response.code.ErrorCode; | ||
import com.umc5th.muffler.global.response.exception.CommonException; | ||
import java.util.Base64; | ||
import java.util.Base64.Decoder; | ||
|
||
public class JwtDecoder { | ||
private JwtDecoder() {} | ||
|
||
public static <T> T decodePayload(String token, Class<T> targetClass) { | ||
String[] tokenParts = token.split("\\."); | ||
String payloadJWT = tokenParts[1]; | ||
Decoder decoder = Base64.getUrlDecoder(); | ||
String payload = new String(decoder.decode(payloadJWT)); | ||
ObjectMapper objectMapper = new ObjectMapper() | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
|
||
try { | ||
return objectMapper.readValue(payload, targetClass); | ||
} catch (Exception e) { | ||
throw new CommonException(ErrorCode.TOKEN_ERROR, "JWT 토큰 페이로드 decode 중 에러 발생"); | ||
} | ||
} | ||
} |
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