-
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.
YEL-146 [merge] 개발서버 배포
- Loading branch information
Showing
12 changed files
with
324 additions
and
72 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
45 changes: 2 additions & 43 deletions
45
src/main/java/com/yello/server/global/common/factory/TokenFactory.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,47 +1,6 @@ | ||
package com.yello.server.global.common.factory; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import java.security.KeyFactory; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.util.Date; | ||
import lombok.SneakyThrows; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
public interface TokenFactory { | ||
|
||
@Component | ||
public class TokenFactory { | ||
|
||
@Value("${kid}") | ||
private String kid; | ||
|
||
@Value("${iss}") | ||
private String iss; | ||
|
||
@Value("${aud}") | ||
private String aud; | ||
|
||
@Value("${bid}") | ||
private String bid; | ||
|
||
@Value("${sig}") | ||
private String sig; | ||
|
||
@SneakyThrows | ||
public String generateAppleToken() { | ||
return Jwts.builder() | ||
.setHeaderParam("kid", kid) | ||
.setIssuer(iss) | ||
.setIssuedAt(new Date(System.currentTimeMillis())) | ||
.setExpiration(new Date(System.currentTimeMillis() + (3 * 60 * 1000))) | ||
.setAudience(aud) | ||
.claim("bid", bid) | ||
.signWith( | ||
SignatureAlgorithm.ES256, | ||
KeyFactory.getInstance("EC") | ||
.generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(sig))) | ||
) | ||
.compact(); | ||
} | ||
String generateAppleToken(); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/yello/server/global/common/factory/TokenFactoryImpl.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,51 @@ | ||
package com.yello.server.global.common.factory; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import java.security.KeyFactory; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.util.Date; | ||
import lombok.SneakyThrows; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TokenFactoryImpl implements TokenFactory { | ||
|
||
@Value("${kid}") | ||
private String kid; | ||
|
||
@Value("${iss}") | ||
private String iss; | ||
|
||
@Value("${aud}") | ||
private String aud; | ||
|
||
@Value("${bid}") | ||
private String bid; | ||
|
||
@Value("${sig}") | ||
private String sig; | ||
|
||
|
||
@SneakyThrows | ||
@Override | ||
public String generateAppleToken() { | ||
return Jwts.builder() | ||
.setHeaderParam("kid", kid) | ||
.setIssuer(iss) | ||
.setIssuedAt(new Date(System.currentTimeMillis())) | ||
.setExpiration(new Date(System.currentTimeMillis() + (3 * 60 * 1000))) | ||
.setAudience(aud) | ||
.claim("bid", bid) | ||
.signWith( | ||
SignatureAlgorithm.ES256, | ||
KeyFactory.getInstance("EC") | ||
.generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(sig))) | ||
) | ||
.compact(); | ||
} | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/yello/server/infrastructure/client/ApiWebClient.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.yello.server.infrastructure.client; | ||
|
||
import com.yello.server.domain.purchase.dto.apple.AppleOrderResponse; | ||
import com.yello.server.domain.purchase.dto.apple.AppleTransaction; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
public interface ApiWebClient { | ||
|
||
ResponseEntity<AppleOrderResponse> appleGetTransaction( | ||
AppleTransaction appleTransaction); | ||
|
||
ResponseEntity<AppleOrderResponse> getTransactionByWebClient( | ||
AppleTransaction appleTransaction, | ||
String appleUrl); | ||
} |
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
61 changes: 61 additions & 0 deletions
61
src/test/java/com/yello/server/domain/purchase/FakeAppleApiWebClient.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,61 @@ | ||
package com.yello.server.domain.purchase; | ||
|
||
import static com.yello.server.global.common.ErrorCode.NOT_FOUND_TRANSACTION_EXCEPTION; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
|
||
import com.yello.server.domain.purchase.dto.apple.AppleOrderResponse; | ||
import com.yello.server.domain.purchase.dto.apple.AppleTransaction; | ||
import com.yello.server.domain.purchase.exception.PurchaseException; | ||
import com.yello.server.global.common.factory.TokenFactory; | ||
import com.yello.server.infrastructure.client.ApiWebClient; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
public class FakeAppleApiWebClient implements ApiWebClient { | ||
|
||
private final TokenFactory tokenFactory; | ||
|
||
private String APPLE_PRODUCTION_URL = "https://api.storekit.itunes.apple.com/inApps/v1/history"; | ||
|
||
private String APPLE_SANDBOX_URL = | ||
"https://api.storekit-sandbox.itunes.apple.com/inApps/v1/history"; | ||
|
||
public FakeAppleApiWebClient(TokenFactory tokenFactory) { | ||
this.tokenFactory = tokenFactory; | ||
} | ||
|
||
@Override | ||
public ResponseEntity<AppleOrderResponse> appleGetTransaction( | ||
AppleTransaction appleTransaction) { | ||
ResponseEntity<AppleOrderResponse> transactionResponse = | ||
getTransactionByWebClient(appleTransaction, APPLE_PRODUCTION_URL); | ||
|
||
HttpStatus statusCode = transactionResponse.getStatusCode(); | ||
if (transactionResponse==null) { | ||
throw new PurchaseException(NOT_FOUND_TRANSACTION_EXCEPTION); | ||
} | ||
if (statusCode.equals(HttpStatus.NOT_FOUND)) { | ||
return getTransactionByWebClient(appleTransaction, APPLE_SANDBOX_URL); | ||
} | ||
|
||
return transactionResponse; | ||
} | ||
|
||
@Override | ||
public ResponseEntity<AppleOrderResponse> getTransactionByWebClient( | ||
AppleTransaction appleTransaction, String appleUrl) { | ||
String appleToken = tokenFactory.generateAppleToken(); | ||
|
||
WebClient webClient = WebClient.builder() | ||
.defaultHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.defaultHeader(HttpHeaders.AUTHORIZATION, appleToken) | ||
.build(); | ||
|
||
return webClient.get() | ||
.uri(appleUrl + "/{transactionId}", appleTransaction.transactionId()) | ||
.exchangeToMono(clientResponse -> clientResponse.toEntity(AppleOrderResponse.class)) | ||
.block(); | ||
} | ||
} |
Oops, something went wrong.