-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
39 lines (34 loc) · 829 Bytes
/
Copy pathauth.go
File metadata and controls
39 lines (34 loc) · 829 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
package main
import (
"crypto/rand"
"encoding/hex"
"net/http"
"os"
"strings"
)
func ensureWebmailToken() string {
t := os.Getenv("WEBMAIL_TOKEN")
if t != "" {
return t
}
b := make([]byte, 16)
_, _ = rand.Read(b)
t = hex.EncodeToString(b)
_ = os.Setenv("WEBMAIL_TOKEN", t)
return t
}
func webmailToken() string {
return os.Getenv("WEBMAIL_TOKEN")
}
func requestToken(r *http.Request) string {
h := r.Header.Get("Authorization")
if strings.HasPrefix(strings.ToLower(h), "bearer ") {
return strings.TrimSpace(h[7:])
}
return r.URL.Query().Get("token")
}
// authed is kept for backwards compatibility but now delegates to authMiddleware.
// New code should use authMiddleware directly (it supports per-mailbox sessions).
func authed(next http.HandlerFunc) http.HandlerFunc {
return authMiddleware(next)
}