-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added extensible auth using build tags (#339)
* added extensible auth using build tags * go mod tidied * made gavins requested changes
- Loading branch information
Showing
18 changed files
with
235 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Extensions | ||
|
||
Extensions are compile-time loaded pieces of code that impact core `kwild` functionality. Typically, extensions impact core consensus code, and therefore great care should be taken when implementing and choosing to use certain extensions. | ||
|
||
## Interfaces and Drivers | ||
|
||
Extensions can be made by implementing a driver for one of many interfaces. These implementations should be registered using Go's `init()` function, which will register the driver when the package is loaded. This is conceptually similar to Go's `database/sql` package, where users can implement custom `database/sql/driver/Driver` implementations. | ||
|
||
## Build Tags | ||
|
||
To include an extension in a build, users should use [Go's build tags](<https://www.digitalocean.com/community/tutorials/customizing-go-binaries-with-build-tags>). Users can specify what extensions they include by including their respective tags: | ||
|
||
### Tag Naming | ||
|
||
While you can give any name to your extension's tag, this repo adopts the best practice of prefixing the type of extension with the rest of the name. For example, if we were adding an extension that added standard RSA signatures for authentication, we might name the build tag `auth_rsa`. We could then include this by running: | ||
|
||
```bash | ||
go build -tags auth_rsa | ||
``` | ||
|
||
Additionally, the build tag `ext_test` is added if the extension should be included as a part of `kwild`'s automated testing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//go:build auth_ed25519_sha256 || ext_test | ||
|
||
package auth | ||
|
||
import ( | ||
"crypto/ed25519" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/kwilteam/kwil-db/pkg/auth" | ||
"github.com/kwilteam/kwil-db/pkg/crypto" | ||
) | ||
|
||
func init() { | ||
err := auth.RegisterAuthenticator(Ed25519Sha256Auth, Ed22519Sha256Authenticator{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
const ( | ||
// Ed25519Sha256Auth is the authenticator name | ||
// the "nr" suffix is for NEAR, and provides backwards compatibility | ||
Ed25519Sha256Auth = "ed25519_nr" | ||
// ed25519SignatureLength is the expected length of a signature | ||
ed25519SignatureLength = 64 | ||
) | ||
|
||
// Ed22519Sha256Authenticator is an authenticator that applies the sha256 hash to the message | ||
// before verifying the signature. This is a common standard in ecosystems like NEAR. | ||
type Ed22519Sha256Authenticator struct{} | ||
|
||
var _ auth.Authenticator = Ed22519Sha256Authenticator{} | ||
|
||
// Address generates a NEAR implicit address from a public key | ||
func (e Ed22519Sha256Authenticator) Address(publicKey []byte) (string, error) { | ||
if len(publicKey) != ed25519.PublicKeySize { | ||
return "", fmt.Errorf("invalid ed25519 public key size for generating near address: %d", len(publicKey)) | ||
} | ||
|
||
return hex.EncodeToString(publicKey), nil | ||
} | ||
|
||
// Verify verifies the signature against the given public key and data. | ||
func (e Ed22519Sha256Authenticator) Verify(publicKey []byte, msg []byte, signature []byte) error { | ||
pubkey, err := crypto.Ed25519PublicKeyFromBytes(publicKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(signature) != ed25519SignatureLength { | ||
return fmt.Errorf("invalid signature length: expected %d, received %d", ed25519SignatureLength, len(signature)) | ||
} | ||
|
||
hash := sha256.Sum256(msg) | ||
return pubkey.Verify(signature, hash[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//go:build auth_ed25519_sha256 || ext_test | ||
|
||
package auth_test | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/kwilteam/kwil-db/extensions/auth" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Ed25519Sha256Near(t *testing.T) { | ||
publicKey := "0aa611bf555596912bc6f9a9f169f8785918e7bab9924001895798ff13f05842" | ||
signature := "089bcf52220dad77abc2cfcb1639bcb2944fdf64e0b173f40cd0d144bdbf7808f4eff3716eb3e98ed40be3ab126e1449d5f57efbe5626673059edc90e9cd9801" | ||
message := []byte("foo") | ||
pubKeyBts, err := hex.DecodeString(publicKey) | ||
require.NoError(t, err, "error decode public key") | ||
|
||
signatureBts, err := hex.DecodeString(signature) | ||
require.NoError(t, err, "error decode signature") | ||
|
||
authenticator := auth.Ed22519Sha256Authenticator{} | ||
|
||
err = authenticator.Verify(pubKeyBts, message, signatureBts) | ||
require.NoError(t, err, "error verifying signature") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.