versia-go/pkg/versia/crypto/federation_headers.go

68 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-08-22 23:03:38 +02:00
package versiacrypto
2024-08-15 19:22:17 +02:00
import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
)
2024-08-28 00:25:25 +02:00
// FederationHeaders represents the signature header of the Versia protocol. For more information, see the [Spec].
2024-08-15 19:22:17 +02:00
//
// [Spec]: https://versia.pub/signatures#signature-definition
type FederationHeaders struct {
// SignedBy is the URL to a user
SignedBy *url.URL
// Nonce is a random string, used to prevent replay attacks
Nonce string
// Signature is the signature of the request
Signature []byte
}
func (f *FederationHeaders) Inject(h http.Header) {
h.Set("x-signed-by", f.SignedBy.String())
h.Set("x-nonce", f.Nonce)
h.Set("x-signature", base64.StdEncoding.EncodeToString(f.Signature))
}
2024-08-20 22:43:26 +02:00
func (f *FederationHeaders) Headers() map[string]string {
return map[string]string{
"x-signed-by": f.SignedBy.String(),
"x-nonce": f.Nonce,
"x-signature": base64.StdEncoding.EncodeToString(f.Signature),
}
}
2024-08-15 19:22:17 +02:00
func ExtractFederationHeaders(h http.Header) (*FederationHeaders, error) {
signedBy := h.Get("x-signed-by")
if signedBy == "" {
return nil, fmt.Errorf("missing x-signed-by header")
}
u, err := url.Parse(signedBy)
if err != nil {
return nil, err
}
nonce := h.Get("x-nonce")
if nonce == "" {
return nil, fmt.Errorf("missing x-nonce header")
}
2024-08-20 22:43:26 +02:00
rawSignature := h.Get("x-signature")
if rawSignature == "" {
2024-08-15 19:22:17 +02:00
return nil, fmt.Errorf("missing x-signature header")
}
2024-08-20 22:43:26 +02:00
signature, err := base64.StdEncoding.DecodeString(rawSignature)
if err != nil {
return nil, err
}
2024-08-15 19:22:17 +02:00
return &FederationHeaders{
SignedBy: u,
Nonce: nonce,
2024-08-20 22:43:26 +02:00
Signature: signature,
2024-08-15 19:22:17 +02:00
}, nil
}