-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
117 lines (99 loc) · 2.56 KB
/
setup.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package tmpauth
import (
"crypto/hmac"
"crypto/sha1"
"hash"
"io"
"log"
"net/http"
"os"
"runtime"
"sync"
"time"
)
const (
TmpAuthHost = "auth.tmpim.pw"
)
type CaddyHandleFunc func(w http.ResponseWriter, r *http.Request) (int, error)
func FromHTTPHandler(h http.Handler) CaddyHandleFunc {
return func(w http.ResponseWriter, r *http.Request) (int, error) {
h.ServeHTTP(w, r)
return 0, nil
}
}
func FromHTTPHandleFunc(h http.HandlerFunc) CaddyHandleFunc {
return func(w http.ResponseWriter, r *http.Request) (int, error) {
h(w, r)
return 0, nil
}
}
type StateIDSession struct {
RedirectURI string
ExpiresAt time.Time
}
type Tmpauth struct {
// We use a Caddy style HandleFunc for middleware.
Next CaddyHandleFunc
Config *Config
TokenCache map[[32]byte]*CachedToken
HttpClient *http.Client
HMAC hash.Hash
stateIDCache map[string]*StateIDSession
stateIDMutex sync.Mutex
tokenCacheMutex sync.RWMutex
hmacMutex sync.Mutex
janitorOnce sync.Once
miniServerHost string
miniConfigID string
miniConfigJSON []byte
miniClient func(req *http.Request, depth int) (*http.Response, error)
done chan struct{}
doneOnce sync.Once
}
var (
DefaultLogger = log.New(os.Stderr, "tmpauth", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
NoLogger = log.New(io.Discard, "", 0)
)
// NewTmpauth creates a new tmpauth handler. Although this can be used as a middleware, it doesn't
// have to be. For example you can leave most Config options unset, and use ParseWrappedAuthJWT
// to validate tokens.
func NewTmpauth(cfg *Config, next CaddyHandleFunc) *Tmpauth {
if cfg.Logger == nil {
cfg.Logger = DefaultLogger
}
if cfg.BaseHTTPClient == nil {
cfg.BaseHTTPClient = http.DefaultClient
}
baseTransport := cfg.BaseHTTPClient.Transport
if baseTransport == nil {
baseTransport = http.DefaultTransport
}
newClient := *cfg.BaseHTTPClient
newClient.Transport = &Transport{
config: cfg,
base: baseTransport,
}
done := make(chan struct{})
t := &Tmpauth{
Next: next,
Config: cfg,
HttpClient: &newClient,
TokenCache: make(map[[32]byte]*CachedToken),
HMAC: hmac.New(sha1.New, cfg.Secret),
stateIDCache: make(map[string]*StateIDSession),
done: done,
}
if cfg.UseFinalizer {
runtime.SetFinalizer(t, func(t *Tmpauth) {
t.Shutdown()
})
}
return t
}
// Shutdown signals background workers in tmpauth to stop. This is required for all use cases
// of tmpauth as it's used to stop the cache janitor.
func (t *Tmpauth) Shutdown() {
t.doneOnce.Do(func() {
close(t.done)
})
}