add rotate api

master
Yixian 2 years ago
parent 12de9f4856
commit 2f9635d4a0

@ -24,6 +24,7 @@ import au.com.royalpay.payment.manage.merchants.beans.NewSubMerchantIdApply;
import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.manage.merchants.core.ClientManager;
import au.com.royalpay.payment.manage.permission.manager.ManagerMapping; import au.com.royalpay.payment.manage.permission.manager.ManagerMapping;
import au.com.royalpay.payment.manage.pos.datasource.ReadOnlyConnection; import au.com.royalpay.payment.manage.pos.datasource.ReadOnlyConnection;
import au.com.royalpay.payment.manage.shopify.auth.domain.manage.ShopifyManageService;
import au.com.royalpay.payment.manage.system.core.TradeSecureService; import au.com.royalpay.payment.manage.system.core.TradeSecureService;
import au.com.royalpay.payment.manage.tradelog.beans.TradeLogQuery; import au.com.royalpay.payment.manage.tradelog.beans.TradeLogQuery;
import au.com.royalpay.payment.manage.tradelog.core.TradeLogService; import au.com.royalpay.payment.manage.tradelog.core.TradeLogService;
@ -131,6 +132,9 @@ public class TestController {
@Resource @Resource
private MerchantChannelApplicationManager merchantChannelApplicationManager; private MerchantChannelApplicationManager merchantChannelApplicationManager;
@Resource
private ShopifyManageService shopifyManageService;
@Resource @Resource
private AlipayRegisterService alipayRegisterService; private AlipayRegisterService alipayRegisterService;
@ -833,4 +837,10 @@ public class TestController {
alipayRegisterService.batchUpdateAlipayApply(); alipayRegisterService.batchUpdateAlipayApply();
} }
@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();
}
} }

@ -1,12 +1,15 @@
package au.com.royalpay.payment.manage.mappers.shopify; package au.com.royalpay.payment.manage.mappers.shopify;
import au.com.royalpay.payment.manage.shopify.store.domain.entity.ShopifyStore; import au.com.royalpay.payment.manage.shopify.store.domain.entity.ShopifyStore;
import com.yixsoft.support.mybatis.autosql.annotations.AdvanceSelect;
import com.yixsoft.support.mybatis.autosql.annotations.AutoMapper; import com.yixsoft.support.mybatis.autosql.annotations.AutoMapper;
import com.yixsoft.support.mybatis.autosql.annotations.AutoSql; import com.yixsoft.support.mybatis.autosql.annotations.AutoSql;
import com.yixsoft.support.mybatis.autosql.annotations.SqlType; import com.yixsoft.support.mybatis.autosql.annotations.SqlType;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator; import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import java.util.List;
@AutoMapper(tablename = "shopify_store", pkName = "id", keyGenerator = Jdbc3KeyGenerator.class) @AutoMapper(tablename = "shopify_store", pkName = "id", keyGenerator = Jdbc3KeyGenerator.class)
public interface ShopifyStoreMapper { public interface ShopifyStoreMapper {
@ -18,4 +21,7 @@ public interface ShopifyStoreMapper {
@AutoSql(SqlType.UPDATE) @AutoSql(SqlType.UPDATE)
void update(ShopifyStore shopifyStore); void update(ShopifyStore shopifyStore);
@AdvanceSelect(addonWhereClause = "status=1")
List<ShopifyStore> listAvailableStores();
} }

@ -0,0 +1,34 @@
package au.com.royalpay.payment.manage.shopify.auth.domain.manage;
import au.com.royalpay.payment.manage.mappers.shopify.ShopifyStoreMapper;
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 org.springframework.stereotype.Service;
@Service
public class ShopifyManageService {
private final ShopifyAuthProvider shopifyAuthProvider;
private final ShopifyAuthService shopifyAuthService;
private final ShopifyStoreMapper shopifyStoreMapper;
public ShopifyManageService(ShopifyAuthProvider shopifyAuthProvider, ShopifyAuthService shopifyAuthService, ShopifyStoreMapper shopifyStoreMapper) {
this.shopifyAuthProvider = shopifyAuthProvider;
this.shopifyAuthService = shopifyAuthService;
this.shopifyStoreMapper = shopifyStoreMapper;
}
public void 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);
}
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());
}
}

@ -9,6 +9,8 @@ import lombok.experimental.Accessors;
import java.util.Date; import java.util.Date;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Data @Data
@Builder @Builder
@ -35,6 +37,8 @@ public class ShopifyStore {
private String accessToken; private String accessToken;
private String tokenSecret;
private String scope; private String scope;
private int status; private int status;
@ -48,8 +52,18 @@ public class ShopifyStore {
.createTime(new Date()) .createTime(new Date())
.creator("shopify store") .creator("shopify store")
.accessToken(accessToken.getAccess_token()) .accessToken(accessToken.getAccess_token())
.tokenSecret(accessToken.getSecret())
.scope(accessToken.getScope()) .scope(accessToken.getScope())
.modifyTime(new Date()) .modifyTime(new Date())
.status(1).build(); .status(1).build();
} }
public String storeName(){
Pattern pattern = Pattern.compile("(^[a-zA-Z0-9][a-zA-Z0-9\\-]*)\\.myshopify\\.com");
Matcher matcher = pattern.matcher(shopifyShop);
if (matcher.matches()){
return matcher.group(1);
}
return shopifyShop;
}
} }

@ -0,0 +1,17 @@
package au.com.royalpay.payment.manage.shopify.store.domain.entity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ShopifyStoreTest {
@Test
void storeName() {
ShopifyStore store = ShopifyStore.builder()
.shopifyShop("test-store-nicks-4.myshopify.com")
.build();
Assertions.assertEquals(store.storeName(), "test-store-nicks-4", "storename match");
}
}
Loading…
Cancel
Save