forked from leg100/otf
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsigner.go
More file actions
42 lines (36 loc) · 959 Bytes
/
signer.go
File metadata and controls
42 lines (36 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package internal
import (
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/leg100/surl"
)
// NewSigner constructs a signer for signing and verifying URLs
func NewSigner(secret []byte) *surl.Signer {
return surl.New(secret,
surl.PrefixPath("/signed"),
surl.WithPathFormatter(),
surl.WithBase58Expiry(),
surl.SkipQuery(),
)
}
// Signer cryptographically signs URLs with a limited lifespan.
type Signer interface {
Sign(string, time.Duration) (string, error)
}
// Verifier verifies signed URLs
type Verifier interface {
Verify(string) error
}
// VerifySignedURL is middleware that verifies signed URLs
func VerifySignedURL(v Verifier) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := v.Verify(r.URL.String()); err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
}