-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.go
48 lines (40 loc) · 1.18 KB
/
backend.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 permission
import (
"net/http"
"sync"
"github.com/caddyserver/caddy"
)
// Backend is an interface for adding backend plugins
type Backend interface {
GetUsername(r *http.Request) (username string, authSuccess bool, err error)
GetPermit(username string) (*Permit, error)
GetDefaultPermit() (*Permit, error)
GetPublicPermit() (*Permit, error)
Login(w http.ResponseWriter, r *http.Request, realm string) (bool, int, error)
Name() string
}
// BackendFactory creates a plug
type BackendFactory func(c *caddy.Controller, now int64) (Backend, error)
var (
backendFactories map[string]BackendFactory
backendFactoriesLock sync.RWMutex
)
func init() {
backendFactories = make(map[string]BackendFactory)
}
// RegisterBackend registers a Permission backend for use
func RegisterBackend(name string, plugFactory BackendFactory) {
backendFactoriesLock.Lock()
defer backendFactoriesLock.Unlock()
backendFactories[name] = plugFactory
}
// GetFactory returns the factory for the given backend name
func GetFactory(name string) BackendFactory {
backendFactoriesLock.RLock()
defer backendFactoriesLock.RUnlock()
factory, ok := backendFactories[name]
if !ok {
return nil
}
return factory
}