2020-09-22 09:42:57 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-09-22 10:54:12 +00:00
|
|
|
"errors"
|
2020-09-22 14:39:06 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
2020-09-22 10:54:12 +00:00
|
|
|
|
2020-09-22 14:39:06 +00:00
|
|
|
"gitlab.crans.org/nounous/ghostream/auth/basic"
|
|
|
|
"gitlab.crans.org/nounous/ghostream/auth/bypass"
|
2020-09-22 09:42:57 +00:00
|
|
|
"gitlab.crans.org/nounous/ghostream/auth/ldap"
|
|
|
|
)
|
|
|
|
|
2020-09-22 10:54:12 +00:00
|
|
|
// Options holds package configuration
|
2020-09-22 09:42:57 +00:00
|
|
|
type Options struct {
|
|
|
|
Backend string
|
2020-09-22 14:39:06 +00:00
|
|
|
Basic basic.Options
|
2020-09-22 09:42:57 +00:00
|
|
|
LDAP ldap.Options
|
|
|
|
}
|
2020-09-22 10:54:12 +00:00
|
|
|
|
|
|
|
// Backend to log user in
|
|
|
|
type Backend interface {
|
|
|
|
Login(string, string) (bool, error)
|
2020-09-22 12:16:52 +00:00
|
|
|
Close()
|
2020-09-22 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New initialize authentification backend
|
|
|
|
func New(cfg *Options) (Backend, error) {
|
|
|
|
var backend Backend
|
2020-09-22 12:16:52 +00:00
|
|
|
var err error
|
2020-09-22 10:54:12 +00:00
|
|
|
|
2020-09-22 14:39:06 +00:00
|
|
|
switch strings.ToLower(cfg.Backend) {
|
|
|
|
case "basic":
|
|
|
|
backend, err = basic.New(&cfg.Basic)
|
|
|
|
case "bypass":
|
|
|
|
backend, err = bypass.New()
|
|
|
|
case "ldap":
|
|
|
|
backend, err = ldap.New(&cfg.LDAP)
|
|
|
|
default:
|
2020-09-22 10:54:12 +00:00
|
|
|
// Package is misconfigured
|
2020-09-22 14:39:06 +00:00
|
|
|
backend, err = nil, errors.New("Authentification backend not found")
|
2020-09-22 10:54:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 14:39:06 +00:00
|
|
|
if err != nil {
|
|
|
|
// Backend init failed
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("%s backend successfully initialized", cfg.Backend)
|
2020-09-22 10:54:12 +00:00
|
|
|
return backend, nil
|
|
|
|
}
|