add rotate api

master
Yixian 3 years ago
parent 2f9635d4a0
commit fd58c9c34d

@ -67,10 +67,7 @@ import java.io.IOException;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.*;
/**
* 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)
public JSONObject rotateShopifySecret(@RequestBody JSONObject rotateRequest) {
shopifyManageService.rotateAccessToken(rotateRequest.getString("secret"), rotateRequest.getString("refresh_token"));
return new JSONObject();
List<JSONObject> failedStores = shopifyManageService.rotateAccessToken(rotateRequest.getString("secret"), rotateRequest.getString("refresh_token"));
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.entity.ShopifyAccessToken;
import au.com.royalpay.shopify.service.ShopifyAuthService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
public class ShopifyManageService {
@ -20,15 +26,26 @@ public class ShopifyManageService {
this.shopifyStoreMapper = shopifyStoreMapper;
}
public void rotateAccessToken(String newSecret, String refreshToken) {
public List<JSONObject> rotateAccessToken(String newSecret, String refreshToken) {
shopifyAuthProvider.addSecret(newSecret);
shopifyStoreMapper.listAvailableStores().parallelStream().filter(store -> !store.getTokenSecret().equalsIgnoreCase(newSecret))
.forEach(store -> rotateStoreAccessToken(store, refreshToken));
shopifyAuthProvider.activateSecret(newSecret);
List<JSONObject> failedStore = shopifyStoreMapper.listAvailableStores().parallelStream().filter(store -> !store.getTokenSecret().equalsIgnoreCase(newSecret))
.map(store -> rotateStoreAccessToken(store, refreshToken))
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (failedStore.isEmpty()) {
shopifyAuthProvider.activateSecret(newSecret);
}
return failedStore;
}
public void rotateStoreAccessToken(ShopifyStore store, String refreshToken) {
ShopifyAccessToken newToken = shopifyAuthService.rotateAccessToken(store.storeName(), refreshToken, store.getAccessToken());
shopifyStoreMapper.update(ShopifyStore.builder().id(store.getId()).accessToken(newToken.getAccess_token()).tokenSecret(newToken.getSecret()).build());
private JSONObject rotateStoreAccessToken(ShopifyStore store, String refreshToken) {
try {
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