s2s

package
v0.0.0-...-d9acf21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// HeadersToSign is the list of headers that will be used to generate the
	// Draft version of HTTP-Signature
	//
	// In regular builds, this list contains the "Date" header which makes it
	// compatible with the wider fediverse, at the expense of debuggability.
	HeadersToSign = []string{httpsig.RequestTarget, "host", "date"}

	// FetchCoveredComponents is the list of components to be used for generating the
	// RFC9421 Signature Base for GET and HEAD requests.
	// https://www.rfc-editor.org/rfc/rfc9421.html#name-derived-components
	FetchCoveredComponents = []string{"@method", "@target-uri", "date"}
	// AdditionalPostCoveredComponents is the list of components to be used for generating the
	// RFC9421 Signature Base for POST, PUT, DELETE requests.
	AdditionalPostCoveredComponents = []string{"content-type"}
)

Functions

This section is empty.

Types

type KeyEncoding

type KeyEncoding int
const (
	KeyTypeUnknown KeyEncoding = 0
	KeyTypePKCS    KeyEncoding = 1
	KeyTypePSS     KeyEncoding = 2
)

type OptionFn

type OptionFn func(transport *Signer)

func WithActor

func WithActor(act *vocab.Actor, prv crypto.PrivateKey) OptionFn

func WithAlg

func WithAlg(alg KeyEncoding) OptionFn

func WithApplicationTag

func WithApplicationTag(t string) OptionFn

func WithCoveredComponents

func WithCoveredComponents(comp ...string) OptionFn

func WithLogFn

func WithLogFn(fn func(string, ...any)) OptionFn

func WithNonce

func WithNonce(nonceFn func() (string, error)) OptionFn

type Signer

type Signer struct {
	Alg   KeyEncoding
	Key   crypto.PrivateKey
	Actor *vocab.Actor
	// contains filtered or unexported fields
}

func New

func New(initFns ...OptionFn) *Signer

New initializes the Signer that might come from the initialization functions.

func (*Signer) SignDraft

func (s *Signer) SignDraft(req *http.Request) error
Example
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("%s\n", r.Header.Get("Signature"))
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

signer := New(WithActor(jdoeActor, prv))

req := httptest.NewRequest(http.MethodGet, srv.URL, nil)
host := strings.TrimPrefix(srv.URL, "http://")
host = host[:strings.Index(host, ":")]
req.Header.Set("Host", host)
req.Header.Set("Date", millenium.Format(http.TimeFormat))
_ = signer.SignDraft(req)

v, err := draft.NewVerifier(req)
if err != nil {
	panic(err)
}
err = v.Verify(pub, draft.RSA_SHA256)
fmt.Printf("Verify error: %v", err)
Output:
Verify error: <nil>

func (*Signer) SignRFC9421

func (s *Signer) SignRFC9421(req *http.Request) error
Example
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	verifier, _ := httpsig.NewVerifier(
		mockKeyResolver{},
		httpsig.WithNonceChecker(mockNonceChecker(true)),
		httpsig.WithValidateAllSignatures(),
		httpsig.WithValidityTolerance(time.Hour),
		httpsig.WithMaxAge(time.Hour),
	)

	err := verifier.Verify(httpsig.MessageFromRequest(r))
	if err != nil {
		fmt.Printf("Verification failed: %s\n", err)
	} else {
		fmt.Printf("Verification succeeded\n")
	}
	w.WriteHeader(http.StatusOK)
}))
defer srv.Close()

signer := New(WithActor(&exActorRSA, prvKeyRSA), WithAlg(KeyTypePKCS), WithNonce(sameNonce("test")))
req := httptest.NewRequest(http.MethodPost, srv.URL, strings.NewReader(`{"hello": "world"}`))
req.Header.Set("Host", "example.com")
req.Header.Set("Date", millenium.Format(http.TimeFormat))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Length", "18")

_ = signer.SignRFC9421(req)
v, err := httpsig.NewVerifier(kresolver(*pubKeyRSA),
	httpsig.WithValidateAllSignatures(),
	httpsig.WithCreatedTimestampRequired(false),
	httpsig.WithExpiredTimestampRequired(false),
)
if err != nil {
	panic(err)
}
err = v.Verify(httpsig.MessageFromRequest(req))
fmt.Printf("Verification error: %v", err)
Output:
Verification error: <nil>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL