Skip to content

Commit 912bd69

Browse files
authored
Add optional override parameters client_email & private_key for nk.purchaseValidateGoogle (heroiclabs#804)
1 parent 79c08f7 commit 912bd69

File tree

7 files changed

+53
-18
lines changed

7 files changed

+53
-18
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/gorilla/mux v1.8.0
1414
github.com/gorilla/websocket v1.4.2
1515
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0
16-
github.com/heroiclabs/nakama-common v1.21.1-0.20220302145851-74490eeebdda
16+
github.com/heroiclabs/nakama-common v1.21.1-0.20220315125242-f39e5bc77bdb
1717
github.com/jackc/pgconn v1.10.0
1818
github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
1919
github.com/jackc/pgtype v1.8.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHh
255255
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
256256
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
257257
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
258-
github.com/heroiclabs/nakama-common v1.21.1-0.20220302145851-74490eeebdda h1:3lN3FQw2Ud3QAznRfz0t6JE40aubnqYRtUTEu2wHh8g=
259-
github.com/heroiclabs/nakama-common v1.21.1-0.20220302145851-74490eeebdda/go.mod h1:WF4YG46afwY3ibzsXnkt3zvhQ3tBY03IYeU7xSLr8HE=
258+
github.com/heroiclabs/nakama-common v1.21.1-0.20220315125242-f39e5bc77bdb h1:FoxvzXI7Hna6Om/6VZCTwzEoflg+fXl+AxbRvyHu7cM=
259+
github.com/heroiclabs/nakama-common v1.21.1-0.20220315125242-f39e5bc77bdb/go.mod h1:WF4YG46afwY3ibzsXnkt3zvhQ3tBY03IYeU7xSLr8HE=
260260
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
261261
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
262262
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=

server/runtime_go_nakama.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -2680,11 +2680,11 @@ func (n *RuntimeGoNakamaModule) TournamentRecordsHaystack(ctx context.Context, i
26802680
// @return error(error) An optional error value if an error occurred.
26812681
func (n *RuntimeGoNakamaModule) PurchaseValidateApple(ctx context.Context, userID, receipt string, persist bool, passwordOverride ...string) (*api.ValidatePurchaseResponse, error) {
26822682
if n.config.GetIAP().Apple.SharedPassword == "" && len(passwordOverride) == 0 {
2683-
return nil, errors.New("Apple IAP is not configured.")
2683+
return nil, errors.New("apple IAP is not configured")
26842684
}
26852685
password := n.config.GetIAP().Apple.SharedPassword
26862686
if len(passwordOverride) > 1 {
2687-
return nil, errors.New("Expects a single password override parameter")
2687+
return nil, errors.New("expects a single password override parameter")
26882688
} else if len(passwordOverride) == 1 {
26892689
password = passwordOverride[0]
26902690
}
@@ -2712,11 +2712,29 @@ func (n *RuntimeGoNakamaModule) PurchaseValidateApple(ctx context.Context, userI
27122712
// @param userId(type=string) The user ID of the owner of the receipt.
27132713
// @param receipt(type=string) JSON encoded Google receipt.
27142714
// @param persist(type=bool) Persist the purchase so that seenBefore can be computed to protect against replay attacks.
2715+
// @param overrides(type=string, optional=true) Override the iap.google.client_email and iap.google.private_key provided in your configuration.
27152716
// @return validation(*api.ValidatePurchaseResponse) The resulting successfully validated purchases. Any previously validated purchases are returned with a seenBefore flag.
27162717
// @return error(error) An optional error value if an error occurred.
2717-
func (n *RuntimeGoNakamaModule) PurchaseValidateGoogle(ctx context.Context, userID, receipt string, persist bool) (*api.ValidatePurchaseResponse, error) {
2718-
if n.config.GetIAP().Google.ClientEmail == "" || n.config.GetIAP().Google.PrivateKey == "" {
2719-
return nil, errors.New("Google IAP is not configured.")
2718+
func (n *RuntimeGoNakamaModule) PurchaseValidateGoogle(ctx context.Context, userID, receipt string, persist bool, overrides ...struct {
2719+
ClientEmail string
2720+
PrivateKey string
2721+
}) (*api.ValidatePurchaseResponse, error) {
2722+
clientEmail := n.config.GetIAP().Google.ClientEmail
2723+
privateKey := n.config.GetIAP().Google.PrivateKey
2724+
2725+
if len(overrides) > 1 {
2726+
return nil, errors.New("expects a single override parameter")
2727+
} else if len(overrides) == 1 {
2728+
if overrides[0].ClientEmail != "" {
2729+
clientEmail = overrides[0].ClientEmail
2730+
}
2731+
if overrides[0].PrivateKey != "" {
2732+
privateKey = overrides[0].PrivateKey
2733+
}
2734+
}
2735+
2736+
if clientEmail == "" || privateKey == "" {
2737+
return nil, errors.New("google IAP is not configured")
27202738
}
27212739

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

2731-
validation, err := ValidatePurchaseGoogle(ctx, n.logger, n.db, uid, n.config.GetIAP().Google, receipt, persist)
2749+
validation, err := ValidatePurchaseGoogle(ctx, n.logger, n.db, uid, &IAPGoogleConfig{clientEmail, privateKey}, receipt, persist)
27322750
if err != nil {
27332751
return nil, err
27342752
}

server/runtime_javascript_nakama.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -5066,7 +5066,7 @@ func (n *runtimeJavascriptNakamaModule) purchaseValidateApple(r *goja.Runtime) f
50665066
}
50675067

50685068
if password == "" {
5069-
panic(r.NewGoError(errors.New("Apple IAP is not configured.")))
5069+
panic(r.NewGoError(errors.New("apple IAP is not configured")))
50705070
}
50715071

50725072
userID := getJsString(r, f.Argument(0))
@@ -5108,8 +5108,18 @@ func (n *runtimeJavascriptNakamaModule) purchaseValidateApple(r *goja.Runtime) f
51085108
// @return error(error) An optional error value if an error occurred.
51095109
func (n *runtimeJavascriptNakamaModule) purchaseValidateGoogle(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
51105110
return func(f goja.FunctionCall) goja.Value {
5111-
if n.config.GetIAP().Google.ClientEmail == "" || n.config.GetIAP().Google.PrivateKey == "" {
5112-
panic(r.NewGoError(errors.New("Google IAP is not configured.")))
5111+
clientEmail := n.config.GetIAP().Google.ClientEmail
5112+
privateKey := n.config.GetIAP().Google.PrivateKey
5113+
5114+
if f.Argument(3) != goja.Undefined() {
5115+
clientEmail = getJsString(r, f.Argument(3))
5116+
}
5117+
if f.Argument(4) != goja.Undefined() {
5118+
privateKey = getJsString(r, f.Argument(4))
5119+
}
5120+
5121+
if clientEmail == "" || privateKey == "" {
5122+
panic(r.NewGoError(errors.New("google IAP is not configured")))
51135123
}
51145124

51155125
userID := getJsString(r, f.Argument(0))
@@ -5130,8 +5140,7 @@ func (n *runtimeJavascriptNakamaModule) purchaseValidateGoogle(r *goja.Runtime)
51305140
if f.Argument(2) != goja.Undefined() && f.Argument(2) != goja.Null() {
51315141
persist = getJsBool(r, f.Argument(2))
51325142
}
5133-
5134-
validation, err := ValidatePurchaseGoogle(context.Background(), n.logger, n.db, uid, n.config.GetIAP().Google, receipt, persist)
5143+
validation, err := ValidatePurchaseGoogle(context.Background(), n.logger, n.db, uid, &IAPGoogleConfig{clientEmail, privateKey}, receipt, persist)
51355144
if err != nil {
51365145
panic(r.NewGoError(fmt.Errorf("error validating Google receipt: %s", err.Error())))
51375146
}

server/runtime_lua_nakama.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -6629,10 +6629,15 @@ func (n *RuntimeLuaNakamaModule) purchaseValidateApple(l *lua.LState) int {
66296629
// @param userId(type=string) The user ID of the owner of the receipt.
66306630
// @param receipt(type=string) JSON encoded Google receipt.
66316631
// @param persist(type=bool, optional=true, default=true) Persist the purchase so that seenBefore can be computed to protect against replay attacks.
6632+
// @param clientEmailOverride(type=string, optional=true) Override the iap.google.client_email provided in your configuration.
6633+
// @param privateKeyOverride(type=string, optional=true) Override the iap.google.private_key provided in your configuration.
66326634
// @return validation(table) The resulting successfully validated purchases. Any previously validated purchases are returned with a seenBefore flag.
66336635
// @return error(error) An optional error value if an error occurred.
66346636
func (n *RuntimeLuaNakamaModule) purchaseValidateGoogle(l *lua.LState) int {
6635-
if n.config.GetIAP().Google.ClientEmail == "" || n.config.GetIAP().Google.PrivateKey == "" {
6637+
clientEmail := l.OptString(4, n.config.GetIAP().Google.ClientEmail)
6638+
privateKey := l.OptString(5, n.config.GetIAP().Google.PrivateKey)
6639+
6640+
if clientEmail == "" || privateKey == "" {
66366641
l.RaiseError("Google IAP is not configured.")
66376642
return 0
66386643
}
@@ -6656,7 +6661,8 @@ func (n *RuntimeLuaNakamaModule) purchaseValidateGoogle(l *lua.LState) int {
66566661

66576662
persist := l.OptBool(3, true)
66586663

6659-
validation, err := ValidatePurchaseGoogle(l.Context(), n.logger, n.db, userID, n.config.GetIAP().Google, receipt, persist)
6664+
validation, err := ValidatePurchaseGoogle(l.Context(), n.logger, n.db, userID, &IAPGoogleConfig{clientEmail, privateKey}, receipt, persist)
6665+
66606666
if err != nil {
66616667
l.RaiseError("error validating Google receipt: %v", err.Error())
66626668
return 0

vendor/github.com/heroiclabs/nakama-common/runtime/runtime.go

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopena
139139
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options
140140
github.com/grpc-ecosystem/grpc-gateway/v2/runtime
141141
github.com/grpc-ecosystem/grpc-gateway/v2/utilities
142-
# github.com/heroiclabs/nakama-common v1.21.1-0.20220302145851-74490eeebdda
142+
# github.com/heroiclabs/nakama-common v1.21.1-0.20220315125242-f39e5bc77bdb
143143
## explicit; go 1.14
144144
github.com/heroiclabs/nakama-common/api
145145
github.com/heroiclabs/nakama-common/rtapi

0 commit comments

Comments
 (0)