no submod
This commit is contained in:
61
work/notea/server/auth.go
Normal file
61
work/notea/server/auth.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type auth interface {
|
||||
Login(string, string) (bool, error)
|
||||
Groups(string) ([]string, error)
|
||||
}
|
||||
|
||||
type FileAuth struct {
|
||||
path string
|
||||
}
|
||||
|
||||
type fileAuthContent struct {
|
||||
Users map[string]struct {
|
||||
Password string
|
||||
Groups []string
|
||||
}
|
||||
}
|
||||
|
||||
func NewFileAuth(path string) FileAuth {
|
||||
return FileAuth{path: path}
|
||||
}
|
||||
|
||||
func (fileAuth FileAuth) Login(u, p string) (bool, error) {
|
||||
content, err := fileAuth.load()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
entry, ok := content.Users[u]
|
||||
return ok && entry.Password == p, nil
|
||||
}
|
||||
|
||||
func (fileAuth FileAuth) Groups(u string) ([]string, error) {
|
||||
content, err := fileAuth.load()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entry, ok := content.Users[u]
|
||||
if !ok {
|
||||
return nil, errors.New("invalid user")
|
||||
}
|
||||
return entry.Groups, nil
|
||||
}
|
||||
|
||||
func (fileAuth FileAuth) load() (fileAuthContent, error) {
|
||||
var fileAuthContent fileAuthContent
|
||||
b, err := ioutil.ReadFile(fileAuth.path)
|
||||
if err != nil {
|
||||
return fileAuthContent, err
|
||||
}
|
||||
if err := yaml.Unmarshal(b, &fileAuthContent); err != nil {
|
||||
return fileAuthContent, err
|
||||
}
|
||||
return fileAuthContent, nil
|
||||
}
|
||||
Reference in New Issue
Block a user