Documentation
¶
Index ¶
- Variables
- type KeyEncoding
- type OptionFn
- func WithActor(act *vocab.Actor, prv crypto.PrivateKey) OptionFn
- func WithAlg(alg KeyEncoding) OptionFn
- func WithApplicationTag(t string) OptionFn
- func WithCoveredComponents(comp ...string) OptionFn
- func WithLogFn(fn func(string, ...any)) OptionFn
- func WithNonce(nonceFn func() (string, error)) OptionFn
- type Signer
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 WithAlg ¶
func WithAlg(alg KeyEncoding) OptionFn
func WithApplicationTag ¶
func WithCoveredComponents ¶
type Signer ¶
type Signer struct {
Alg KeyEncoding
Key crypto.PrivateKey
Actor *vocab.Actor
// contains filtered or unexported fields
}
func (*Signer) SignDraft ¶
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 ¶
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>
Click to show internal directories.
Click to hide internal directories.