forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviderauth.go
64 lines (51 loc) · 1.08 KB
/
providerauth.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
package util
import "sync"
type AuthCollection struct {
mu sync.Mutex
paramC chan<- Param
vehicles map[string]*AuthProvider
}
func NewAuthCollection(paramC chan<- Param) *AuthCollection {
return &AuthCollection{
paramC: paramC,
vehicles: make(map[string]*AuthProvider),
}
}
func (ac *AuthCollection) Register(baseURI, title string) *AuthProvider {
ap := &AuthProvider{
ac: ac,
Uri: baseURI,
}
ac.mu.Lock()
ac.vehicles[title] = ap
ac.mu.Unlock()
return ap
}
// publish routes and status
func (ac *AuthCollection) Publish() {
ac.mu.Lock()
defer ac.mu.Unlock()
val := struct {
Vehicles map[string]*AuthProvider `json:"vehicles"`
}{
Vehicles: ac.vehicles,
}
ac.paramC <- Param{Key: "auth", Val: val}
}
type AuthProvider struct {
ac *AuthCollection
Uri string `json:"uri"`
Authenticated bool `json:"authenticated"`
}
func (ap *AuthProvider) Handler() chan<- bool {
c := make(chan bool)
go func() {
for auth := range c {
ap.ac.mu.Lock()
ap.Authenticated = auth
ap.ac.mu.Unlock()
ap.ac.Publish()
}
}()
return c
}