|
|
@ -2,15 +2,21 @@ package au.com.royalpay.payment.manage.shopify.auth.web;
|
|
|
|
|
|
|
|
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.ShopifyRequestVerifyException;
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.ShopifyRequestVerifyException;
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.application.ShopifyMerchantAuthApplication;
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.application.ShopifyMerchantAuthApplication;
|
|
|
|
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyCommonParameter;
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyPermissionURL;
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyPermissionURL;
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.service.ShopifyRequestValidator;
|
|
|
|
import au.com.royalpay.payment.manage.shopify.auth.domain.service.ShopifyRequestValidator;
|
|
|
|
|
|
|
|
import au.com.royalpay.payment.tools.env.PlatformEnvironment;
|
|
|
|
|
|
|
|
import au.com.royalpay.payment.tools.exceptions.BadRequestException;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.servlet.view.RedirectView;
|
|
|
|
import org.springframework.web.servlet.view.RedirectView;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
@Controller
|
|
|
|
@RequestMapping(value = "/shopify")
|
|
|
|
@RequestMapping(value = "/shopify")
|
|
|
|
public class ShopifyAuthTemplateController {
|
|
|
|
public class ShopifyAuthTemplateController {
|
|
|
@ -21,23 +27,73 @@ public class ShopifyAuthTemplateController {
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private ShopifyRequestValidator shopifyRequestValidator;
|
|
|
|
private ShopifyRequestValidator shopifyRequestValidator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* shopify店铺安装入口
|
|
|
|
* shopify店铺安装入口
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param shopifyStoreHost
|
|
|
|
* @param shop
|
|
|
|
* @param hmac
|
|
|
|
* @param hmac
|
|
|
|
* @param timestamp
|
|
|
|
* @param timestamp
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@GetMapping("/auth")
|
|
|
|
@GetMapping("/auth")
|
|
|
|
public RedirectView shopifyStorePermission(@RequestParam("shop") String shopifyStoreHost,
|
|
|
|
public RedirectView shopifyStorePermission(@RequestParam("shop") String shop,
|
|
|
|
@RequestParam("hmac") String hmac,
|
|
|
|
@RequestParam("hmac") String hmac,
|
|
|
|
@RequestParam("timestamp") String timestamp) {
|
|
|
|
@RequestParam("timestamp") String timestamp) {
|
|
|
|
if (!shopifyRequestValidator.verifyPermission(shopifyStoreHost, hmac, timestamp)) {
|
|
|
|
if (!Pattern.matches("^[a-zA-Z0-9][a-zA-Z0-9\\-]*\\.myshopify\\.com", shop)) {
|
|
|
|
|
|
|
|
throw new BadRequestException("Parameter shop is invalid.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!shopifyRequestValidator.verifyPermission(shop, hmac, timestamp)) {
|
|
|
|
throw new ShopifyRequestVerifyException("This request parameters is invalid");
|
|
|
|
throw new ShopifyRequestVerifyException("This request parameters is invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ShopifyPermissionURL shopifyPermissionURL = shopifyMerchantAuthApplication.getShopifyPermissionUrl(shopifyStoreHost);
|
|
|
|
ShopifyPermissionURL shopifyPermissionURL = shopifyMerchantAuthApplication.getShopifyPermissionUrl(shop);
|
|
|
|
return new RedirectView(shopifyPermissionURL.getUrl());
|
|
|
|
return new RedirectView(shopifyPermissionURL.getUrl());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* shopify店铺授权后回调URL
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param code
|
|
|
|
|
|
|
|
* @param hmac
|
|
|
|
|
|
|
|
* @param host
|
|
|
|
|
|
|
|
* @param state
|
|
|
|
|
|
|
|
* @param shop
|
|
|
|
|
|
|
|
* @param timestamp
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@GetMapping("/auth/back")
|
|
|
|
|
|
|
|
public RedirectView shopifyStoreAuthRedirect(@RequestParam("code") String code,
|
|
|
|
|
|
|
|
@RequestParam("hmac") String hmac,
|
|
|
|
|
|
|
|
@RequestParam("host") String host,
|
|
|
|
|
|
|
|
@RequestParam("state") String state,
|
|
|
|
|
|
|
|
@RequestParam("shop") String shop,
|
|
|
|
|
|
|
|
@RequestParam("timestamp") String timestamp) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Pattern.matches("^[a-zA-Z0-9][a-zA-Z0-9\\-]*\\.myshopify\\.com", shop)) {
|
|
|
|
|
|
|
|
throw new ShopifyRequestVerifyException("Parameter shop is invalid.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String preState = stringRedisTemplate.boundValueOps("shopifyAuthState:" + shop).get();
|
|
|
|
|
|
|
|
if (!state.equals(preState)) {
|
|
|
|
|
|
|
|
throw new ShopifyRequestVerifyException("This request parameters is invalid");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
stringRedisTemplate.delete("shopifyAuthState:" + shop);
|
|
|
|
|
|
|
|
ShopifyCommonParameter shopifyCommonParameter = ShopifyCommonParameter.builder()
|
|
|
|
|
|
|
|
.code(code)
|
|
|
|
|
|
|
|
.hmac(hmac)
|
|
|
|
|
|
|
|
.host(host)
|
|
|
|
|
|
|
|
.state(state)
|
|
|
|
|
|
|
|
.shop(shop)
|
|
|
|
|
|
|
|
.timestamp(timestamp)
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
if (!shopifyRequestValidator.valid(shopifyCommonParameter)) {
|
|
|
|
|
|
|
|
throw new ShopifyRequestVerifyException("This request parameters is invalid");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String redirectUri = PlatformEnvironment.getEnv().concatUrl("/auth.html#/shopify/login?code=" + code + "&hmac=" + hmac + "&host=" + host + "&state=" + state + "&shop=" + shop + "×tamp=" + timestamp);
|
|
|
|
|
|
|
|
return new RedirectView(redirectUri);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|