Commit 6cea5e48 authored by Mo Firouz's avatar Mo Firouz
Browse files

Correctly decode private key pem.

parent 32419e22
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -80,8 +80,9 @@ end
function M.google_obtain_access_token(client_email, private_key)
  local auth_url = "https://accounts.google.com/o/oauth2/token"
  local scope = "https://www.googleapis.com/auth/androidpublisher"
  local exp = nk.time() + 3600000 -- current time + 1hr added in ms
  local iat = nk.time()
  local iat = nk.time() / 1000
  local exp = iat + 3600  -- current time + 1hr added in seconds


  local algo_type = "RS256"

@@ -144,9 +145,9 @@ function M.verify_payment_google(request)
    error(access_token)
  end

  local url_template = "https://www.googleapis.com/androidpublisher/v2/applications/%q/purchases/subscriptions/%q/tokens/%q?access_token=%q"
  local url_template = "https://www.googleapis.com/androidpublisher/v2/applications/%s/purchases/subscriptions/%s/tokens/%s?access_token=%s"
  if (not request.is_subscription) then
    url_template = "https://www.googleapis.com/androidpublisher/v2/applications/%q/purchases/products/%q/tokens/%q?access_token=%q"
    url_template = "https://www.googleapis.com/androidpublisher/v2/applications/%s/purchases/products/%s/tokens/%s?access_token=%s"
  end

  local url = url_template:format(request.package_name, request.product_id, request.receipt, access_token)
+14 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import (
	"encoding/gob"
	"encoding/hex"
	"encoding/json"
	"encoding/pem"
	"fmt"
	"io"
	"io/ioutil"
@@ -740,8 +741,20 @@ func (n *RuntimeLuaNakamaModule) jwtGenerate(l *lua.LState) int {
		jwtClaims[k] = v
	}

	block, _ := pem.Decode([]byte(signingKey))
	if block == nil {
		l.RaiseError("could not parse private key: no valid blocks found")
		return 0
	}

	pk, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		l.RaiseError("could not parse private key: %v", err.Error())
		return 0
	}

	token := jwt.NewWithClaims(signingMethod, jwtClaims)
	signedToken, err := token.SignedString([]byte(signingKey))
	signedToken, err := token.SignedString(pk)
	if err != nil {
		l.RaiseError("failed to sign token: %v", err.Error())
		return 0