backend.go (1419B)
1 package htpasswd 2 3 import ( 4 "errors" 5 "github.com/tarent/loginsrv/login" 6 "github.com/tarent/loginsrv/model" 7 "strings" 8 ) 9 10 // ProviderName const 11 const ProviderName = "htpasswd" 12 13 func init() { 14 login.RegisterProvider( 15 &login.ProviderDescription{ 16 Name: ProviderName, 17 HelpText: "Htpasswd login backend opts: files=/path/to/pwdfile,/path/to/additionalfile", 18 }, 19 BackendFactory) 20 } 21 22 // BackendFactory creates a htpasswd backend 23 func BackendFactory(config map[string]string) (login.Backend, error) { 24 var files []string 25 26 if f, exist := config["file"]; exist { 27 for _, file := range strings.Split(f, ";") { 28 files = append(files, file) 29 } 30 } 31 32 if len(files) == 0 { 33 return nil, errors.New(`missing parameter "file" for htpasswd provider`) 34 } 35 36 return NewBackend(files) 37 } 38 39 // Backend is a htpasswd based authentication backend. 40 type Backend struct { 41 auth *Auth 42 } 43 44 // NewBackend creates a new Backend and verifies the parameters. 45 func NewBackend(filenames []string) (*Backend, error) { 46 auth, err := NewAuth(filenames) 47 return &Backend{ 48 auth, 49 }, err 50 } 51 52 // Authenticate the user 53 func (sb *Backend) Authenticate(username, password string) (bool, model.UserInfo, error) { 54 authenticated, err := sb.auth.Authenticate(username, password) 55 if authenticated && err == nil { 56 return authenticated, model.UserInfo{ 57 Origin: ProviderName, 58 Sub: username, 59 }, err 60 } 61 return false, model.UserInfo{}, err 62 }