Documentation
¶
Overview ¶
Package signed creates and verifies signed URLs: links that carry an expiry and an HMAC-SHA256 signature over their path and query, for email verification, unsubscribe links or temporary downloads.
s := signed.New(app.Key.Derive("signed.urls", 32))
link, _ := s.Sign(appURL+"/email/verify/42", time.Hour)
r.Get("/email/verify/{id}", verify).Use(s.Middleware())
Signer.Verify checks a request in constant time and returns ErrInvalid or ErrExpired; Signer.Middleware answers 403 for both.
See https://github.com/AlexGalhardo/vukem/blob/main/docs/security.md.
Example ¶
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"time"
"github.com/AlexGalhardo/vukem/signed"
)
func main() {
// In an app, derive the key: app.Key.Derive("signed.urls", 32).
s := signed.New(bytes.Repeat([]byte{3}, 32))
link, err := s.Sign("https://example.com/email/verify/42", time.Hour)
if err != nil {
fmt.Println(err)
return
}
ok := httptest.NewRequest(http.MethodGet, link, http.NoBody)
fmt.Println(s.Verify(ok) == nil)
tampered := httptest.NewRequest(http.MethodGet, strings.Replace(link, "/42", "/43", 1), http.NoBody)
fmt.Println(s.Verify(tampered) == nil)
}
Output: true false
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrInvalid = &errs.HTTPError{Status: http.StatusForbidden, Message: "Invalid signature."} ErrExpired = &errs.HTTPError{Status: http.StatusForbidden, Message: "This link has expired."} )
Errors returned by Verify (403 for clients).
Functions ¶
This section is empty.
Types ¶
type Signer ¶
type Signer struct {
// contains filtered or unexported fields
}
Signer signs and verifies URLs with one key.
func (*Signer) Middleware ¶
Middleware rejects requests whose URL signature is missing, wrong or expired with 403 (and an "url.signature_invalid" security event).
Click to show internal directories.
Click to hide internal directories.