-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from Modagbul/fix/auth
Fix/auth
- Loading branch information
Showing
24 changed files
with
292 additions
and
42 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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/moing/backend/domain/auth/application/service/WithdrawProvider.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,7 @@ | ||
package com.moing.backend.domain.auth.application.service; | ||
|
||
import java.io.IOException; | ||
|
||
public interface WithdrawProvider { | ||
void withdraw(String token) throws IOException; | ||
} |
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
95 changes: 95 additions & 0 deletions
95
...n/java/com/moing/backend/domain/auth/application/service/apple/AppleWithdrawUserCase.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,95 @@ | ||
package com.moing.backend.domain.auth.application.service.apple; | ||
|
||
import com.moing.backend.domain.auth.application.service.WithdrawProvider; | ||
import com.moing.backend.domain.auth.application.service.apple.utils.AppleClient; | ||
import com.moing.backend.domain.auth.application.service.apple.utils.AppleToken; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import lombok.RequiredArgsConstructor; | ||
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; | ||
import org.bouncycastle.openssl.PEMParser; | ||
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.FileCopyUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.security.Key; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Service("appleWithdraw") | ||
@RequiredArgsConstructor | ||
public class AppleWithdrawUserCase implements WithdrawProvider { | ||
|
||
@Value("${oauth2.apple.keyId}") | ||
private String keyId; | ||
|
||
@Value("${oauth2.apple.teamId}") | ||
private String teamId; | ||
|
||
@Value("${oauth2.apple.clientId}") | ||
private String clientId; | ||
|
||
@Value("${oauth2.apple.keyPath}") | ||
private String keyPath; | ||
|
||
private final AppleClient appleClient; | ||
|
||
public void withdraw(String token) throws IOException { | ||
AppleToken.Response response = generateAuthToken(token); | ||
|
||
if (response.getAccess_token() != null) { | ||
appleClient.revoke(AppleToken.RevokeRequest.of( | ||
clientId, | ||
createClientSecret(), | ||
response.getAccess_token() | ||
) | ||
); | ||
} | ||
} | ||
|
||
public AppleToken.Response generateAuthToken(String authorizationCode) throws IOException { | ||
|
||
return appleClient.getToken(AppleToken.Request.of( | ||
authorizationCode, | ||
clientId, | ||
createClientSecret(), | ||
"authorization_code" | ||
)); | ||
} | ||
|
||
public String createClientSecret() throws IOException { | ||
Date expirationDate = Date.from(LocalDateTime.now().plusDays(30).atZone(ZoneId.systemDefault()).toInstant()); | ||
Map<String, Object> jwtHeader = new HashMap<>(); | ||
jwtHeader.put("kid", keyId); | ||
jwtHeader.put("alg", "ES256"); | ||
|
||
return Jwts.builder() | ||
.setHeaderParams(jwtHeader) | ||
.setIssuer(teamId) | ||
.setIssuedAt(new Date(System.currentTimeMillis())) | ||
.setExpiration(expirationDate) | ||
.setAudience("https://appleid.apple.com") | ||
.setSubject(clientId) | ||
.signWith(getPrivateKey(), SignatureAlgorithm.ES256) | ||
.compact(); | ||
} | ||
|
||
private Key getPrivateKey() throws IOException { | ||
ClassPathResource resource = new ClassPathResource(keyPath); | ||
String privateKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream())); | ||
|
||
Reader pemReader = new StringReader(privateKey); | ||
PEMParser pemParser = new PEMParser(pemReader); | ||
JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); | ||
PrivateKeyInfo object = (PrivateKeyInfo) pemParser.readObject(); | ||
return converter.getPrivateKey(object); | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
.../java/com/moing/backend/domain/auth/application/service/apple/feign/AppleFeignClient.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
src/main/java/com/moing/backend/domain/auth/application/service/apple/utils/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,17 @@ | ||
package com.moing.backend.domain.auth.application.service.apple.utils; | ||
|
||
import com.moing.backend.global.util.FeignClientConfig; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
@FeignClient(name = "appleClient", url = "https://appleid.apple.com/auth", configuration = FeignClientConfig.class) | ||
public interface AppleClient { | ||
@GetMapping(value = "/keys") | ||
Keys getKeys(); | ||
@PostMapping(value = "/token", consumes = "application/x-www-form-urlencoded") | ||
AppleToken.Response getToken(AppleToken.Request request); | ||
|
||
@PostMapping(value = "/revoke", consumes = "application/x-www-form-urlencoded") | ||
void revoke(AppleToken.RevokeRequest request); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/moing/backend/domain/auth/application/service/apple/utils/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,47 @@ | ||
package com.moing.backend.domain.auth.application.service.apple.utils; | ||
|
||
import lombok.Getter; | ||
|
||
public class AppleToken { | ||
|
||
public static class Request { | ||
private String code; | ||
private String client_id; | ||
private String client_secret; | ||
private String grant_type; | ||
|
||
public static Request of(String code, String clientId, String clientSecret, String grantType) { | ||
Request request = new Request(); | ||
request.code = code; | ||
request.client_id = clientId; | ||
request.client_secret = clientSecret; | ||
request.grant_type = grantType; | ||
return request; | ||
} | ||
} | ||
|
||
@Getter | ||
public static class Response { | ||
private String access_token; | ||
private String expires_in; | ||
private String id_token; | ||
private String refresh_token; | ||
private String token_type; | ||
private String error; | ||
} | ||
|
||
@Getter | ||
public static class RevokeRequest { | ||
private String client_id; | ||
private String client_secret; | ||
private String token; | ||
|
||
public static RevokeRequest of(String clientId, String clientSecret, String token) { | ||
RevokeRequest request = new RevokeRequest(); | ||
request.client_id = clientId; | ||
request.client_secret = clientSecret; | ||
request.token = token; | ||
return request; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...on/service/apple/feign/response/Keys.java → ...application/service/apple/utils/Keys.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
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
20 changes: 20 additions & 0 deletions
20
...java/com/moing/backend/domain/auth/application/service/google/GoogleWithdrawUserCase.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.moing.backend.domain.auth.application.service.google; | ||
|
||
import com.moing.backend.domain.auth.application.service.WithdrawProvider; | ||
import com.moing.backend.domain.auth.application.service.google.utils.GoogleClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
|
||
@Service("googleWithdraw") | ||
@RequiredArgsConstructor | ||
public class GoogleWithdrawUserCase implements WithdrawProvider { | ||
|
||
private final GoogleClient googleClient; | ||
|
||
public void withdraw(String token) throws IOException { | ||
googleClient.revoke(token); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...ain/java/com/moing/backend/domain/auth/application/service/google/utils/GoogleClient.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 com.moing.backend.domain.auth.application.service.google.utils; | ||
|
||
import com.moing.backend.global.util.FeignClientConfig; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
@FeignClient(name = "googleClient", url = "https://oauth2.googleapis.com", configuration = FeignClientConfig.class) | ||
public interface GoogleClient { | ||
@PostMapping(value = "/revoke", consumes = "application/x-www-form-urlencoded") | ||
void revoke(@RequestParam("token") String token); | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...n/java/com/moing/backend/domain/auth/application/service/kakao/KakaoWithdrawUserCase.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.moing.backend.domain.auth.application.service.kakao; | ||
|
||
import com.moing.backend.domain.auth.application.service.WithdrawProvider; | ||
import com.moing.backend.domain.auth.application.service.kakao.utils.KakaoClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
|
||
@Service("kakaoWithdraw") | ||
@RequiredArgsConstructor | ||
public class KakaoWithdrawUserCase implements WithdrawProvider { | ||
|
||
private final KakaoClient kakaoClient; | ||
|
||
public void withdraw(String token) throws IOException { | ||
|
||
kakaoClient.unlinkUser("Bearer " + token); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/moing/backend/domain/auth/application/service/kakao/utils/KakaoClient.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 com.moing.backend.domain.auth.application.service.kakao.utils; | ||
|
||
import com.moing.backend.global.util.FeignClientConfig; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
|
||
@FeignClient(name = "kakaoClient", url = "https://kapi.kakao.com", configuration = FeignClientConfig.class) | ||
public interface KakaoClient { | ||
@PostMapping("/v1/user/unlink") | ||
KakaoUnlinkResponse unlinkUser(@RequestHeader("Authorization") String accessToken); | ||
} |
8 changes: 8 additions & 0 deletions
8
...va/com/moing/backend/domain/auth/application/service/kakao/utils/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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.moing.backend.domain.auth.application.service.kakao.utils; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class KakaoUnlinkResponse { | ||
private String id; | ||
} |
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.