versia-go/pkg/versia/crypto/crypto_test.go

42 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-08-22 23:03:38 +02:00
package versiacrypto
2024-08-11 03:51:22 +02:00
import (
"crypto/ed25519"
"crypto/x509"
"encoding/base64"
"net/url"
"testing"
2024-09-02 15:46:32 +02:00
"github.com/stretchr/testify/assert"
versiautils "github.com/versia-pub/versia-go/pkg/versia/utils"
2024-08-11 03:51:22 +02:00
)
func TestFederationClient_ValidateSignatureHeader(t *testing.T) {
var (
2024-08-15 19:22:17 +02:00
bobURL = &url.URL{Scheme: "https", Host: "bob.com"}
2024-08-11 03:51:22 +02:00
2024-09-02 15:46:32 +02:00
bobPrivBytes = versiautils.Must(base64.StdEncoding.DecodeString, "MC4CAQAwBQYDK2VwBCIEINOATgmaya61Ha9OEE+DD3RnOEqDaHyQ3yLf5upwskUU")
bobPriv = versiautils.Must(x509.ParsePKCS8PrivateKey, bobPrivBytes).(ed25519.PrivateKey)
2024-08-15 19:22:17 +02:00
signer = Signer{PrivateKey: bobPriv, UserURL: bobURL}
2024-08-11 03:51:22 +02:00
2024-09-02 15:46:32 +02:00
bobPubBytes = versiautils.Must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAQ08Z/FJ5f16o8mthLaFZMo4ssn0fJ7c+bipNYm3kId4=")
bobPub = versiautils.Must(x509.ParsePKIXPublicKey, bobPubBytes).(ed25519.PublicKey)
2024-08-15 19:22:17 +02:00
verifier = Verifier{PublicKey: bobPub}
2024-08-11 03:51:22 +02:00
2024-08-15 19:22:17 +02:00
method = "POST"
nonce = "myrandomnonce"
u = &url.URL{Scheme: "https", Host: "bob.com", Path: "/a/b/c", RawQuery: "z=foo&a=bar"}
body = []byte("hello")
2024-08-11 03:51:22 +02:00
)
2024-09-02 15:46:32 +02:00
toSign := NewSignatureData(method, nonce, u, SHA256(body))
2024-08-15 19:22:17 +02:00
assert.Equal(t, `post /a/b/c?z=foo&a=bar myrandomnonce LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=`, toSign.String())
2024-08-11 03:51:22 +02:00
2024-08-15 19:22:17 +02:00
signed := signer.Sign(*toSign)
assert.Equal(t, true, verifier.Verify(method, u, body, signed), "signature verification failed")
2024-08-11 03:51:22 +02:00
2024-08-15 19:22:17 +02:00
assert.Equal(t, "myrandomnonce", signed.Nonce)
assert.Equal(t, bobURL, signed.SignedBy)
assert.Equal(t, "datQHNaqJ1jeKzK3UeReUVf+B65JPq5P9LxfqUUJTMv3QNqDu5KawosKoduIRk4/D/A+EKjDhlcw0c7GzUlMCA==", base64.StdEncoding.EncodeToString(signed.Signature))
2024-08-11 03:51:22 +02:00
}