forked from heroku/lumbermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logplex_drain_tokens.go
48 lines (42 loc) · 1.28 KB
/
logplex_drain_tokens.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
package authenticater
import (
"net/http"
"strings"
"sync"
)
// LogplexDrainToken ensures that the Logplex-Drain-Token header contains a
// known token and is safe for concurrent use
type LogplexDrainToken struct {
sync.RWMutex
tokens map[string]struct{}
}
// NewLogplexDrainToken creates and returns a new LogplexDrainTokens
func NewLogplexDrainToken() *LogplexDrainToken {
return &LogplexDrainToken{tokens: make(map[string]struct{})}
}
// NewLogplexDrainTokenFromString creates and returns a new LogplexDrainTokens
// instance from the provided string containing tokens in the folloing format:
// token1,token2,token3,...
func NewLogplexDrainTokenFromString(tokens string) *LogplexDrainToken {
ldt := NewLogplexDrainToken()
for _, token := range strings.Split(tokens, ",") {
ldt.AddToken(token)
}
return ldt
}
// AddToken adds a token to the list of acceptable tokens.
func (ldt *LogplexDrainToken) AddToken(token string) {
ldt.Lock()
ldt.tokens[token] = struct{}{}
ldt.Unlock()
}
// Authenticate the request if the Logplex-Drain-Token header contains a known
// token
func (ldt *LogplexDrainToken) Authenticate(r *http.Request) (exists bool) {
if token := r.Header.Get("Logplex-Drain-Token"); token != "" {
ldt.RLock()
_, exists = ldt.tokens[token]
ldt.RUnlock()
}
return
}