2018-01-28 14:16:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2018-09-20 13:39:22 +00:00
|
|
|
"os/signal"
|
2018-01-28 14:16:52 +00:00
|
|
|
"path"
|
2018-09-20 13:39:22 +00:00
|
|
|
"syscall"
|
2018-01-28 14:16:52 +00:00
|
|
|
|
|
|
|
"github.com/flosch/pongo2"
|
2018-09-08 17:39:59 +00:00
|
|
|
"github.com/gorilla/context"
|
2018-01-28 14:16:52 +00:00
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
2018-11-17 13:42:56 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/rconfig"
|
2018-01-28 14:16:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mainConfig struct {
|
2018-11-17 13:42:56 +00:00
|
|
|
ACL acl `yaml:"acl"`
|
|
|
|
AuditLog auditLogger `yaml:"audit_log"`
|
|
|
|
Cookie struct {
|
2018-01-28 14:16:52 +00:00
|
|
|
Domain string `yaml:"domain"`
|
|
|
|
AuthKey string `yaml:"authentication_key"`
|
|
|
|
Expire int `yaml:"expire"`
|
|
|
|
Prefix string `yaml:"prefix"`
|
|
|
|
Secure bool `yaml:"secure"`
|
|
|
|
}
|
|
|
|
Listen struct {
|
|
|
|
Addr string `yaml:"addr"`
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
} `yaml:"listen"`
|
|
|
|
Login struct {
|
|
|
|
Title string `yaml:"title"`
|
|
|
|
DefaultMethod string `yaml:"default_method"`
|
2018-12-24 09:07:49 +00:00
|
|
|
HideMFAField bool `yaml:"hide_mfa_field"`
|
2018-01-28 14:16:52 +00:00
|
|
|
Names map[string]string `yaml:"names"`
|
|
|
|
} `yaml:"login"`
|
|
|
|
}
|
|
|
|
|
2018-11-17 13:42:56 +00:00
|
|
|
func (m *mainConfig) GetSessionOpts() *sessions.Options {
|
2018-01-28 14:16:52 +00:00
|
|
|
return &sessions.Options{
|
|
|
|
Path: "/",
|
|
|
|
Domain: m.Cookie.Domain,
|
|
|
|
MaxAge: m.Cookie.Expire,
|
|
|
|
Secure: m.Cookie.Secure,
|
|
|
|
HttpOnly: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
|
|
|
ConfigFile string `flag:"config,c" default:"config.yaml" env:"CONFIG" description:"Location of the configuration file"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Level of logs to display (debug, info, warn, error)"`
|
|
|
|
TemplateDir string `flag:"frontend-dir" default:"./frontend/" env:"FRONTEND_DIR" description:"Location of the directory containing the web assets"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
mainCfg = mainConfig{}
|
|
|
|
cookieStore *sessions.CookieStore
|
|
|
|
|
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if err := rconfig.Parse(&cfg); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to parse commandline options")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to parse log level")
|
|
|
|
} else {
|
|
|
|
log.SetLevel(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("nginx-sso %s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sane defaults for main configuration
|
|
|
|
mainCfg.Cookie.Prefix = "nginx-sso"
|
|
|
|
mainCfg.Cookie.Expire = 3600
|
|
|
|
mainCfg.Listen.Addr = "127.0.0.1"
|
|
|
|
mainCfg.Listen.Port = 8082
|
2018-11-17 13:42:56 +00:00
|
|
|
mainCfg.AuditLog.TrustedIPHeaders = []string{"X-Forwarded-For", "RemoteAddr", "X-Real-IP"}
|
2018-11-18 11:24:33 +00:00
|
|
|
mainCfg.AuditLog.Headers = []string{"x-origin-uri"}
|
2018-01-28 14:16:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 13:39:22 +00:00
|
|
|
func loadConfiguration() error {
|
2018-01-28 14:16:52 +00:00
|
|
|
yamlSource, err := ioutil.ReadFile(cfg.ConfigFile)
|
|
|
|
if err != nil {
|
2018-09-20 13:39:22 +00:00
|
|
|
return fmt.Errorf("Unable to read configuration file: %s", err)
|
2018-01-28 14:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal(yamlSource, &mainCfg); err != nil {
|
2018-09-20 13:39:22 +00:00
|
|
|
return fmt.Errorf("Unable to load configuration file: %s", err)
|
2018-01-28 14:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := initializeAuthenticators(yamlSource); err != nil {
|
2018-09-20 13:39:22 +00:00
|
|
|
return fmt.Errorf("Unable to configure authentication: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-12-24 09:07:49 +00:00
|
|
|
if err = initializeMFAProviders(yamlSource); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to configure MFA providers")
|
|
|
|
}
|
|
|
|
|
2018-09-20 13:39:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := loadConfiguration(); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to load configuration")
|
2018-01-28 14:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cookieStore = sessions.NewCookieStore([]byte(mainCfg.Cookie.AuthKey))
|
|
|
|
|
|
|
|
http.HandleFunc("/auth", handleAuthRequest)
|
|
|
|
http.HandleFunc("/login", handleLoginRequest)
|
|
|
|
http.HandleFunc("/logout", handleLogoutRequest)
|
|
|
|
|
2018-09-20 13:39:22 +00:00
|
|
|
go http.ListenAndServe(
|
|
|
|
fmt.Sprintf("%s:%d", mainCfg.Listen.Addr, mainCfg.Listen.Port),
|
|
|
|
context.ClearHandler(http.DefaultServeMux),
|
|
|
|
)
|
|
|
|
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigChan, syscall.SIGHUP)
|
|
|
|
|
|
|
|
for sig := range sigChan {
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
if err := loadConfiguration(); err != nil {
|
|
|
|
log.WithError(err).Error("Unable to reload configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Fatalf("Received unexpected signal: %v", sig)
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 14:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleAuthRequest(res http.ResponseWriter, r *http.Request) {
|
2018-01-28 20:27:23 +00:00
|
|
|
user, groups, err := detectUser(res, r)
|
2018-01-28 14:16:52 +00:00
|
|
|
|
|
|
|
switch err {
|
|
|
|
case errNoValidUserFound:
|
2018-11-17 13:42:56 +00:00
|
|
|
mainCfg.AuditLog.Log(auditEventValidate, r, map[string]string{"result": "no valid user found"})
|
2018-01-28 14:16:52 +00:00
|
|
|
http.Error(res, "No valid user found", http.StatusUnauthorized)
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
if !mainCfg.ACL.HasAccess(user, groups, r) {
|
2018-11-17 13:42:56 +00:00
|
|
|
mainCfg.AuditLog.Log(auditEventAccessDenied, r, map[string]string{"username": user})
|
2018-01-28 14:16:52 +00:00
|
|
|
http.Error(res, "Access denied for this resource", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-17 13:42:56 +00:00
|
|
|
mainCfg.AuditLog.Log(auditEventValidate, r, map[string]string{"result": "valid user found", "username": user})
|
|
|
|
|
2018-01-28 14:16:52 +00:00
|
|
|
res.Header().Set("X-Username", user)
|
|
|
|
res.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.WithError(err).Error("Error while handling auth request")
|
|
|
|
http.Error(res, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLoginRequest(res http.ResponseWriter, r *http.Request) {
|
2018-01-28 20:27:23 +00:00
|
|
|
if _, _, err := detectUser(res, r); err == nil {
|
2018-01-28 14:16:52 +00:00
|
|
|
// There is already a valid user
|
|
|
|
http.Redirect(res, r, r.URL.Query().Get("go"), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-17 13:42:56 +00:00
|
|
|
auditFields := map[string]string{
|
|
|
|
"go": r.FormValue("go"),
|
|
|
|
}
|
|
|
|
|
2018-01-28 14:16:52 +00:00
|
|
|
if r.Method == "POST" {
|
2018-12-24 09:07:49 +00:00
|
|
|
// Simple authentication
|
|
|
|
user, mfaCfgs, err := loginUser(res, r)
|
|
|
|
switch err {
|
|
|
|
case errNoValidUserFound:
|
|
|
|
http.Redirect(res, r, "/login?go="+url.QueryEscape(r.FormValue("go")), http.StatusFound)
|
|
|
|
return
|
|
|
|
case nil:
|
|
|
|
// Don't handle for now, MFA validation comes first
|
|
|
|
default:
|
|
|
|
log.WithError(err).Error("Login failed with unexpected error")
|
|
|
|
http.Redirect(res, r, "/login?go="+url.QueryEscape(r.FormValue("go")), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MFA validation against configs from login
|
|
|
|
err = validateMFA(res, r, user, mfaCfgs)
|
2018-01-28 14:16:52 +00:00
|
|
|
switch err {
|
|
|
|
case errNoValidUserFound:
|
2018-11-17 13:42:56 +00:00
|
|
|
auditFields["reason"] = "invalid credentials"
|
|
|
|
mainCfg.AuditLog.Log(auditEventLoginFailure, r, auditFields)
|
2018-12-24 09:07:49 +00:00
|
|
|
res.Header().Del("Set-Cookie") // Remove login cookie
|
2018-01-28 14:16:52 +00:00
|
|
|
http.Redirect(res, r, "/login?go="+url.QueryEscape(r.FormValue("go")), http.StatusFound)
|
|
|
|
return
|
2018-11-17 13:42:56 +00:00
|
|
|
|
2018-01-28 14:16:52 +00:00
|
|
|
case nil:
|
2018-11-17 13:42:56 +00:00
|
|
|
mainCfg.AuditLog.Log(auditEventLoginSuccess, r, auditFields)
|
2018-01-28 14:16:52 +00:00
|
|
|
http.Redirect(res, r, r.FormValue("go"), http.StatusFound)
|
|
|
|
return
|
2018-11-17 13:42:56 +00:00
|
|
|
|
2018-01-28 14:16:52 +00:00
|
|
|
default:
|
2018-11-17 13:42:56 +00:00
|
|
|
auditFields["reason"] = "error"
|
|
|
|
auditFields["error"] = err.Error()
|
|
|
|
mainCfg.AuditLog.Log(auditEventLoginFailure, r, auditFields)
|
2018-01-28 14:16:52 +00:00
|
|
|
log.WithError(err).Error("Login failed with unexpected error")
|
2018-12-24 09:07:49 +00:00
|
|
|
res.Header().Del("Set-Cookie") // Remove login cookie
|
2018-01-28 14:16:52 +00:00
|
|
|
http.Redirect(res, r, "/login?go="+url.QueryEscape(r.FormValue("go")), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl := pongo2.Must(pongo2.FromFile(path.Join(cfg.TemplateDir, "index.html")))
|
|
|
|
if err := tpl.ExecuteWriter(pongo2.Context{
|
|
|
|
"active_methods": getFrontendAuthenticators(),
|
|
|
|
"go": r.URL.Query().Get("go"),
|
|
|
|
"login": mainCfg.Login,
|
|
|
|
}, res); err != nil {
|
|
|
|
log.WithError(err).Error("Unable to render template")
|
|
|
|
http.Error(res, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLogoutRequest(res http.ResponseWriter, r *http.Request) {
|
2018-11-17 13:42:56 +00:00
|
|
|
mainCfg.AuditLog.Log(auditEventLogout, r, nil)
|
2018-01-28 14:16:52 +00:00
|
|
|
if err := logoutUser(res, r); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to logout user")
|
|
|
|
http.Error(res, "Something went wrong", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(res, r, r.URL.Query().Get("go"), http.StatusFound)
|
|
|
|
}
|