add rotate api

master
Yixian 2 years ago
parent 2f9635d4a0
commit fd58c9c34d

@ -67,10 +67,7 @@ import java.io.IOException;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Optional;
/** /**
* Created by yixian on 2016-07-06. * Created by yixian on 2016-07-06.
@ -840,7 +837,7 @@ public class TestController {
@ManagerMapping(value = "/shopify/rotate_secret", method = RequestMethod.POST, role = ManagerRole.DEVELOPER) @ManagerMapping(value = "/shopify/rotate_secret", method = RequestMethod.POST, role = ManagerRole.DEVELOPER)
public JSONObject rotateShopifySecret(@RequestBody JSONObject rotateRequest) { public JSONObject rotateShopifySecret(@RequestBody JSONObject rotateRequest) {
shopifyManageService.rotateAccessToken(rotateRequest.getString("secret"), rotateRequest.getString("refresh_token")); List<JSONObject> failedStores = shopifyManageService.rotateAccessToken(rotateRequest.getString("secret"), rotateRequest.getString("refresh_token"));
return new JSONObject(); return new JSONObject(Map.of("failed", failedStores));
} }
} }

@ -5,8 +5,14 @@ import au.com.royalpay.payment.manage.shopify.store.domain.entity.ShopifyStore;
import au.com.royalpay.shopify.config.ShopifyAuthProvider; import au.com.royalpay.shopify.config.ShopifyAuthProvider;
import au.com.royalpay.shopify.entity.ShopifyAccessToken; import au.com.royalpay.shopify.entity.ShopifyAccessToken;
import au.com.royalpay.shopify.service.ShopifyAuthService; import au.com.royalpay.shopify.service.ShopifyAuthService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Service @Service
public class ShopifyManageService { public class ShopifyManageService {
@ -20,15 +26,26 @@ public class ShopifyManageService {
this.shopifyStoreMapper = shopifyStoreMapper; this.shopifyStoreMapper = shopifyStoreMapper;
} }
public void rotateAccessToken(String newSecret, String refreshToken) { public List<JSONObject> rotateAccessToken(String newSecret, String refreshToken) {
shopifyAuthProvider.addSecret(newSecret); shopifyAuthProvider.addSecret(newSecret);
shopifyStoreMapper.listAvailableStores().parallelStream().filter(store -> !store.getTokenSecret().equalsIgnoreCase(newSecret)) List<JSONObject> failedStore = shopifyStoreMapper.listAvailableStores().parallelStream().filter(store -> !store.getTokenSecret().equalsIgnoreCase(newSecret))
.forEach(store -> rotateStoreAccessToken(store, refreshToken)); .map(store -> rotateStoreAccessToken(store, refreshToken))
shopifyAuthProvider.activateSecret(newSecret); .filter(Objects::nonNull)
.collect(Collectors.toList());
if (failedStore.isEmpty()) {
shopifyAuthProvider.activateSecret(newSecret);
}
return failedStore;
} }
public void rotateStoreAccessToken(ShopifyStore store, String refreshToken) { private JSONObject rotateStoreAccessToken(ShopifyStore store, String refreshToken) {
ShopifyAccessToken newToken = shopifyAuthService.rotateAccessToken(store.storeName(), refreshToken, store.getAccessToken()); try {
shopifyStoreMapper.update(ShopifyStore.builder().id(store.getId()).accessToken(newToken.getAccess_token()).tokenSecret(newToken.getSecret()).build()); ShopifyAccessToken newToken = shopifyAuthService.rotateAccessToken(store.storeName(), refreshToken, store.getAccessToken());
shopifyStoreMapper.update(ShopifyStore.builder().id(store.getId()).accessToken(newToken.getAccess_token()).tokenSecret(newToken.getSecret()).build());
return null;
} catch (Exception e) {
return new JSONObject(Map.of("shop", store.getShopifyShop(), "client_moniker", store.getClientMoniker(), "message", e.getMessage()));
}
} }
} }

Loading…
Cancel
Save