upgrade shopify OAuth process

master
ycfxx 3 years ago
parent 20eb867b11
commit 1f8846a25a

@ -43,7 +43,7 @@ public class ShopifyAuthService {
private StringRedisTemplate stringRedisTemplate; private StringRedisTemplate stringRedisTemplate;
public ShopifyPermissionURL shopifyPermission(String shopifyStoreHost) { public ShopifyPermissionURL shopifyPermission(String shopifyStoreHost) {
String redirectUri = PlatformEnvironment.getEnv().concatUrl("/auth.html"); String redirectUri = PlatformEnvironment.getEnv().concatUrl("/shopify/auth/back");
String state = String.valueOf(new Date().getTime()).substring(0,10); String state = String.valueOf(new Date().getTime()).substring(0,10);

@ -1,14 +1,9 @@
package au.com.royalpay.payment.manage.shopify.auth.web; 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.application.ShopifyMerchantAuthApplication; import au.com.royalpay.payment.manage.shopify.auth.domain.application.ShopifyMerchantAuthApplication;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyAccessToken; import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyAccessToken;
import au.com.royalpay.payment.manage.shopify.auth.domain.service.ShopifyRequestValidator;
import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyPermissionRequest; import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyPermissionRequest;
import au.com.royalpay.payment.manage.shopify.auth.web.command.ShopifyVerifyRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -26,31 +21,6 @@ public class ShopifyAuthController {
@Autowired @Autowired
private ShopifyMerchantAuthApplication shopifyMerchantAuthApplication; private ShopifyMerchantAuthApplication shopifyMerchantAuthApplication;
@Autowired
private ShopifyRequestValidator shopifyRequestValidator;
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* shopify
*
* @param request
* @return
*/
@PostMapping("/verify")
public JSONObject verifyRequest(@RequestBody @Valid ShopifyVerifyRequest request) {
String state = stringRedisTemplate.boundValueOps("shopifyAuthState:" + request.getShop()).get();
if (!request.getState().equals(state)) {
throw new ShopifyRequestVerifyException("This request parameters is invalid");
}
stringRedisTemplate.delete("shopifyAuthState:" + request.getShop());
if (!shopifyRequestValidator.valid(request.build())) {
throw new ShopifyRequestVerifyException("This request parameters is invalid");
}
return new JSONObject();
}
/** /**
* shopifyURL * shopifyURL
* *
@ -58,10 +28,7 @@ public class ShopifyAuthController {
* @return * @return
*/ */
@PostMapping("/install") @PostMapping("/install")
public ShopifyAccessToken shopifyPermission(@RequestBody @Valid ShopifyPermissionRequest request) { public ShopifyAccessToken shopifyStoreInstall(@RequestBody @Valid ShopifyPermissionRequest request) {
if (!shopifyRequestValidator.valid(request.build())) {
throw new ShopifyRequestVerifyException("This request parameters is invalid");
}
ShopifyAccessToken shopifyAccessToken = shopifyMerchantAuthApplication.install(request); ShopifyAccessToken shopifyAccessToken = shopifyMerchantAuthApplication.install(request);
return shopifyAccessToken; return shopifyAccessToken;
} }

@ -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());
} }
/**
* shopifyURL
*
* @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 + "&timestamp=" + timestamp);
return new RedirectView(redirectUri);
}
} }

@ -1,6 +1,5 @@
package au.com.royalpay.payment.manage.shopify.auth.web.command; package au.com.royalpay.payment.manage.shopify.auth.web.command;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyCommonParameter;
import au.com.royalpay.payment.manage.shopify.store.web.command.CreateShopifyMerchantCommand; import au.com.royalpay.payment.manage.shopify.store.web.command.CreateShopifyMerchantCommand;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
@ -25,43 +24,17 @@ public class ShopifyPermissionRequest {
@NotBlank(message = "Code can not blank") @NotBlank(message = "Code can not blank")
private String code; private String code;
@NotBlank(message = "hmac can not blank")
private String hmac;
@NotBlank(message = "host can not blank")
private String host;
@NotBlank(message = "Shop can not blank") @NotBlank(message = "Shop can not blank")
@Pattern(regexp = "^[a-zA-Z0-9][a-zA-Z0-9\\-]*\\.myshopify\\.com",message = "Shop hostname is invalid") @Pattern(regexp = "^[a-zA-Z0-9][a-zA-Z0-9\\-]*\\.myshopify\\.com", message = "Shop hostname is invalid")
private String shop; private String shop;
@NotBlank(message = "state can not blank")
private String state;
@NotBlank(message = "timestamp can not blank")
private String timestamp;
public static ShopifyPermissionRequest instanceOf(CreateShopifyMerchantCommand command) { public static ShopifyPermissionRequest instanceOf(CreateShopifyMerchantCommand command) {
return ShopifyPermissionRequest.builder() return ShopifyPermissionRequest.builder()
.loginId(command.getPaymentAccount().getLoginId()) .loginId(command.getPaymentAccount().getLoginId())
.password(command.getPaymentAccount().getPassword()) .password(command.getPaymentAccount().getPassword())
.code(command.getCode()) .code(command.getCode())
.hmac(command.getHmac())
.host(command.getHost())
.shop(command.getShopifyShop()) .shop(command.getShopifyShop())
.state(command.getState())
.timestamp(command.getTimestamp())
.build(); .build();
} }
public ShopifyCommonParameter build() {
return ShopifyCommonParameter.builder()
.code(code)
.hmac(hmac)
.host(host)
.shop(shop)
.state(state)
.timestamp(timestamp)
.build();
}
} }

@ -1,6 +1,5 @@
package au.com.royalpay.payment.manage.shopify.auth.web.command; package au.com.royalpay.payment.manage.shopify.auth.web.command;
import au.com.royalpay.payment.manage.shopify.auth.domain.entity.ShopifyCommonParameter;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
@ -27,15 +26,4 @@ public class ShopifyVerifyRequest {
@NotBlank(message = "timestamp can not blank") @NotBlank(message = "timestamp can not blank")
private String timestamp; private String timestamp;
public ShopifyCommonParameter build() {
return ShopifyCommonParameter.builder()
.code(code)
.hmac(hmac)
.host(host)
.shop(shop)
.state(state)
.timestamp(timestamp)
.build();
}
} }

@ -24,6 +24,7 @@ public class ShopifyStoreController {
* *
* @param partnerCode * @param partnerCode
*/ */
@Deprecated
@GetMapping("/exist") @GetMapping("/exist")
public Boolean validPaymentAppMerchant(@RequestParam("partnerCode") String partnerCode) { public Boolean validPaymentAppMerchant(@RequestParam("partnerCode") String partnerCode) {
return shopifyStoreApplication.existMerchant(partnerCode); return shopifyStoreApplication.existMerchant(partnerCode);

@ -17,19 +17,8 @@ public class CreateShopifyMerchantCommand {
@NotBlank(message = "Auth code can not blank") @NotBlank(message = "Auth code can not blank")
private String code; private String code;
@NotBlank(message = "hmac can not blank")
private String hmac;
@NotBlank(message = "host can not blank")
private String host;
@NotBlank(message = "Shop can not blank") @NotBlank(message = "Shop can not blank")
@Pattern(regexp = "^[a-zA-Z0-9][a-zA-Z0-9\\-]*\\.myshopify\\.com",message = "Shop hostname is invalid") @Pattern(regexp = "^[a-zA-Z0-9][a-zA-Z0-9\\-]*\\.myshopify\\.com", message = "Shop hostname is invalid")
private String shopifyShop; private String shopifyShop;
@NotBlank(message = "state can not blank")
private String state;
@NotBlank(message = "timestamp can not blank")
private String timestamp;
} }

@ -70,12 +70,19 @@ define(['angular', 'uiRouter', 'uiBootstrap'], function (angular) {
module.controller('ShopifyLoginController', ['$scope', '$http', '$state', '$stateParams', '$location', function ($scope, $http, $state, $stateParams, $location) { module.controller('ShopifyLoginController', ['$scope', '$http', '$state', '$stateParams', '$location', function ($scope, $http, $state, $stateParams, $location) {
var that = $scope; var that = $scope;
var code = getQueryVariable("code") /*var code = getQueryVariable("code")
var hmac = getQueryVariable("hmac") var hmac = getQueryVariable("hmac")
var host = getQueryVariable("host") var host = getQueryVariable("host")
var shop = getQueryVariable("shop") var shop = getQueryVariable("shop")
var state = getQueryVariable("state") var state = getQueryVariable("state")
var timestamp = getQueryVariable("timestamp") var timestamp = getQueryVariable("timestamp")*/
var code = $location.search().code
var hmac = $location.search().hmac
var host = $location.search().host
var shop = $location.search().shop
var state = $location.search().state
var timestamp = $location.search().timestamp
that.model = { that.model = {
loginId: '', loginId: '',
@ -89,14 +96,14 @@ define(['angular', 'uiRouter', 'uiBootstrap'], function (angular) {
} }
that.loginDisable = false that.loginDisable = false
that.verifyRequest = function () { /*that.verifyRequest = function () {
$http.post("/shopify/auth/verify", that.model).then(function (res) { $http.post("/shopify/auth/verify", that.model).then(function (res) {
}, function (error) { }, function (error) {
that.resError = error.data.message; that.resError = error.data.message;
that.loginDisable = false that.loginDisable = false
}) })
} }
that.verifyRequest() that.verifyRequest()*/
that.activeShopifyMerchant = function () { that.activeShopifyMerchant = function () {
that.loginDisable = true that.loginDisable = true

Loading…
Cancel
Save