Unverified Commit 6531ac1c authored by Simon Esposito's avatar Simon Esposito Committed by GitHub
Browse files

Fix bugs in a couple of Lua nk module functions. (#457)

1. jwt_generate now correctly supports the RS256 algorithm.
2. rsa_sha256_hash now correctly decoding the private key argument.
parent b38d4dd4
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr

### Fixed
- Prevent bad presence list input to dispatcher message broadcasts from causing unexpected errors.
- Fix an issue in the Lua runtime nk module's `jwt_generate` function that would prevent it from accepting a key in 'RS256' format.
- Fix an issue in the Lua runtime nk module's `rsaSHA256Hash` function that would prevent it from parsing the input private key.   

## [2.12.0] - 2020-05-25
### Added
+26 −12
Original line number Diff line number Diff line
@@ -808,7 +808,8 @@ func (n *RuntimeLuaNakamaModule) jwtGenerate(l *lua.LState) int {
	case "RS256":
		signingMethod = jwt.SigningMethodRS256
	default:
		l.ArgError(3, "unsupported algo type - only allowed 'HS256', 'RS256'.")
		l.ArgError(1, "unsupported algo type - only allowed 'HS256', 'RS256'.")
		return 0
	}

	signingKey := l.CheckString(2)
@@ -829,17 +830,24 @@ func (n *RuntimeLuaNakamaModule) jwtGenerate(l *lua.LState) int {
		jwtClaims[k] = v
	}

	var pk interface{}
	switch signingMethod {
	case jwt.SigningMethodRS256:
		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)
		var err error
		pk, err = x509.ParsePKCS8PrivateKey(block.Bytes)
		if err != nil {
			l.RaiseError("could not parse private key: %v", err.Error())
			return 0
		}
	case jwt.SigningMethodHS256:
		pk = []byte(signingKey)
	}

	token := jwt.NewWithClaims(signingMethod, jwtClaims)
	signedToken, err := token.SignedString(pk)
@@ -1129,7 +1137,13 @@ func (n *RuntimeLuaNakamaModule) rsaSHA256Hash(l *lua.LState) int {
		return 0
	}

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

	rsaPrivateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		l.RaiseError("error parsing key: %v", err.Error())
		return 0
@@ -1138,7 +1152,7 @@ func (n *RuntimeLuaNakamaModule) rsaSHA256Hash(l *lua.LState) int {
	hashed := sha256.Sum256([]byte(input))
	signature, err := rsa.SignPKCS1v15(rand.Reader, rsaPrivateKey, crypto.SHA256, hashed[:])
	if err != nil {
		l.RaiseError("error parsing key: %v", err.Error())
		l.RaiseError("error signing input: %v", err.Error())
		return 0
	}