Unverified Commit 912bd695 authored by Fernando Takagi's avatar Fernando Takagi Committed by GitHub
Browse files

Add optional override parameters client_email & private_key for nk.purchaseValidateGoogle (#804)

parent 79c08f78
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ require (
	github.com/gorilla/mux v1.8.0
	github.com/gorilla/websocket v1.4.2
	github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0
	github.com/heroiclabs/nakama-common v1.21.1-0.20220302145851-74490eeebdda
	github.com/heroiclabs/nakama-common v1.21.1-0.20220315125242-f39e5bc77bdb
	github.com/jackc/pgconn v1.10.0
	github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
	github.com/jackc/pgtype v1.8.1
+2 −2
+24 −6
Original line number Diff line number Diff line
@@ -2680,11 +2680,11 @@ func (n *RuntimeGoNakamaModule) TournamentRecordsHaystack(ctx context.Context, i
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeGoNakamaModule) PurchaseValidateApple(ctx context.Context, userID, receipt string, persist bool, passwordOverride ...string) (*api.ValidatePurchaseResponse, error) {
	if n.config.GetIAP().Apple.SharedPassword == "" && len(passwordOverride) == 0 {
		return nil, errors.New("Apple IAP is not configured.")
		return nil, errors.New("apple IAP is not configured")
	}
	password := n.config.GetIAP().Apple.SharedPassword
	if len(passwordOverride) > 1 {
		return nil, errors.New("Expects a single password override parameter")
		return nil, errors.New("expects a single password override parameter")
	} else if len(passwordOverride) == 1 {
		password = passwordOverride[0]
	}
@@ -2712,11 +2712,29 @@ func (n *RuntimeGoNakamaModule) PurchaseValidateApple(ctx context.Context, userI
// @param userId(type=string) The user ID of the owner of the receipt.
// @param receipt(type=string) JSON encoded Google receipt.
// @param persist(type=bool) Persist the purchase so that seenBefore can be computed to protect against replay attacks.
// @param overrides(type=string, optional=true) Override the iap.google.client_email and iap.google.private_key provided in your configuration.
// @return validation(*api.ValidatePurchaseResponse) The resulting successfully validated purchases. Any previously validated purchases are returned with a seenBefore flag.
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeGoNakamaModule) PurchaseValidateGoogle(ctx context.Context, userID, receipt string, persist bool) (*api.ValidatePurchaseResponse, error) {
	if n.config.GetIAP().Google.ClientEmail == "" || n.config.GetIAP().Google.PrivateKey == "" {
		return nil, errors.New("Google IAP is not configured.")
func (n *RuntimeGoNakamaModule) PurchaseValidateGoogle(ctx context.Context, userID, receipt string, persist bool, overrides ...struct {
	ClientEmail string
	PrivateKey  string
}) (*api.ValidatePurchaseResponse, error) {
	clientEmail := n.config.GetIAP().Google.ClientEmail
	privateKey := n.config.GetIAP().Google.PrivateKey

	if len(overrides) > 1 {
		return nil, errors.New("expects a single override parameter")
	} else if len(overrides) == 1 {
		if overrides[0].ClientEmail != "" {
			clientEmail = overrides[0].ClientEmail
		}
		if overrides[0].PrivateKey != "" {
			privateKey = overrides[0].PrivateKey
		}
	}

	if clientEmail == "" || privateKey == "" {
		return nil, errors.New("google IAP is not configured")
	}

	uid, err := uuid.FromString(userID)
@@ -2728,7 +2746,7 @@ func (n *RuntimeGoNakamaModule) PurchaseValidateGoogle(ctx context.Context, user
		return nil, errors.New("receipt cannot be empty string")
	}

	validation, err := ValidatePurchaseGoogle(ctx, n.logger, n.db, uid, n.config.GetIAP().Google, receipt, persist)
	validation, err := ValidatePurchaseGoogle(ctx, n.logger, n.db, uid, &IAPGoogleConfig{clientEmail, privateKey}, receipt, persist)
	if err != nil {
		return nil, err
	}
+14 −5
Original line number Diff line number Diff line
@@ -5066,7 +5066,7 @@ func (n *runtimeJavascriptNakamaModule) purchaseValidateApple(r *goja.Runtime) f
		}

		if password == "" {
			panic(r.NewGoError(errors.New("Apple IAP is not configured.")))
			panic(r.NewGoError(errors.New("apple IAP is not configured")))
		}

		userID := getJsString(r, f.Argument(0))
@@ -5108,8 +5108,18 @@ func (n *runtimeJavascriptNakamaModule) purchaseValidateApple(r *goja.Runtime) f
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) purchaseValidateGoogle(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
	return func(f goja.FunctionCall) goja.Value {
		if n.config.GetIAP().Google.ClientEmail == "" || n.config.GetIAP().Google.PrivateKey == "" {
			panic(r.NewGoError(errors.New("Google IAP is not configured.")))
		clientEmail := n.config.GetIAP().Google.ClientEmail
		privateKey := n.config.GetIAP().Google.PrivateKey

		if f.Argument(3) != goja.Undefined() {
			clientEmail = getJsString(r, f.Argument(3))
		}
		if f.Argument(4) != goja.Undefined() {
			privateKey = getJsString(r, f.Argument(4))
		}

		if clientEmail == "" || privateKey == "" {
			panic(r.NewGoError(errors.New("google IAP is not configured")))
		}

		userID := getJsString(r, f.Argument(0))
@@ -5130,8 +5140,7 @@ func (n *runtimeJavascriptNakamaModule) purchaseValidateGoogle(r *goja.Runtime)
		if f.Argument(2) != goja.Undefined() && f.Argument(2) != goja.Null() {
			persist = getJsBool(r, f.Argument(2))
		}

		validation, err := ValidatePurchaseGoogle(context.Background(), n.logger, n.db, uid, n.config.GetIAP().Google, receipt, persist)
		validation, err := ValidatePurchaseGoogle(context.Background(), n.logger, n.db, uid, &IAPGoogleConfig{clientEmail, privateKey}, receipt, persist)
		if err != nil {
			panic(r.NewGoError(fmt.Errorf("error validating Google receipt: %s", err.Error())))
		}
+8 −2
Original line number Diff line number Diff line
@@ -6629,10 +6629,15 @@ func (n *RuntimeLuaNakamaModule) purchaseValidateApple(l *lua.LState) int {
// @param userId(type=string) The user ID of the owner of the receipt.
// @param receipt(type=string) JSON encoded Google receipt.
// @param persist(type=bool, optional=true, default=true) Persist the purchase so that seenBefore can be computed to protect against replay attacks.
// @param clientEmailOverride(type=string, optional=true) Override the iap.google.client_email provided in your configuration.
// @param privateKeyOverride(type=string, optional=true) Override the iap.google.private_key provided in your configuration.
// @return validation(table) The resulting successfully validated purchases. Any previously validated purchases are returned with a seenBefore flag.
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeLuaNakamaModule) purchaseValidateGoogle(l *lua.LState) int {
	if n.config.GetIAP().Google.ClientEmail == "" || n.config.GetIAP().Google.PrivateKey == "" {
	clientEmail := l.OptString(4, n.config.GetIAP().Google.ClientEmail)
	privateKey := l.OptString(5, n.config.GetIAP().Google.PrivateKey)

	if clientEmail == "" || privateKey == "" {
		l.RaiseError("Google IAP is not configured.")
		return 0
	}
@@ -6656,7 +6661,8 @@ func (n *RuntimeLuaNakamaModule) purchaseValidateGoogle(l *lua.LState) int {

	persist := l.OptBool(3, true)

	validation, err := ValidatePurchaseGoogle(l.Context(), n.logger, n.db, userID, n.config.GetIAP().Google, receipt, persist)
	validation, err := ValidatePurchaseGoogle(l.Context(), n.logger, n.db, userID, &IAPGoogleConfig{clientEmail, privateKey}, receipt, persist)

	if err != nil {
		l.RaiseError("error validating Google receipt: %v", err.Error())
		return 0
Loading