mirror of
https://github.com/Luzifer/nginx-sso.git
synced 2024-12-20 12:51:17 +00:00
parent
7ed2b8d7cc
commit
8c9a2f6285
2 changed files with 38 additions and 7 deletions
38
main.go
38
main.go
|
@ -6,7 +6,9 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/Luzifer/rconfig"
|
"github.com/Luzifer/rconfig"
|
||||||
"github.com/flosch/pongo2"
|
"github.com/flosch/pongo2"
|
||||||
|
@ -83,18 +85,26 @@ func init() {
|
||||||
mainCfg.Listen.Port = 8082
|
mainCfg.Listen.Port = 8082
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func loadConfiguration() error {
|
||||||
yamlSource, err := ioutil.ReadFile(cfg.ConfigFile)
|
yamlSource, err := ioutil.ReadFile(cfg.ConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Fatal("Unable to read configuration file")
|
return fmt.Errorf("Unable to read configuration file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := yaml.Unmarshal(yamlSource, &mainCfg); err != nil {
|
if err := yaml.Unmarshal(yamlSource, &mainCfg); err != nil {
|
||||||
log.WithError(err).Fatal("Unable to load configuration file")
|
return fmt.Errorf("Unable to load configuration file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := initializeAuthenticators(yamlSource); err != nil {
|
if err := initializeAuthenticators(yamlSource); err != nil {
|
||||||
log.WithError(err).Fatal("Unable to configure authentication")
|
return fmt.Errorf("Unable to configure authentication: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := loadConfiguration(); err != nil {
|
||||||
|
log.WithError(err).Fatal("Unable to load configuration")
|
||||||
}
|
}
|
||||||
|
|
||||||
cookieStore = sessions.NewCookieStore([]byte(mainCfg.Cookie.AuthKey))
|
cookieStore = sessions.NewCookieStore([]byte(mainCfg.Cookie.AuthKey))
|
||||||
|
@ -103,7 +113,25 @@ func main() {
|
||||||
http.HandleFunc("/login", handleLoginRequest)
|
http.HandleFunc("/login", handleLoginRequest)
|
||||||
http.HandleFunc("/logout", handleLogoutRequest)
|
http.HandleFunc("/logout", handleLogoutRequest)
|
||||||
|
|
||||||
http.ListenAndServe(fmt.Sprintf("%s:%d", mainCfg.Listen.Addr, mainCfg.Listen.Port), context.ClearHandler(http.DefaultServeMux))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAuthRequest(res http.ResponseWriter, r *http.Request) {
|
func handleAuthRequest(res http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -72,12 +72,13 @@ func initializeAuthenticators(yamlSource []byte) error {
|
||||||
authenticatorRegistryMutex.Lock()
|
authenticatorRegistryMutex.Lock()
|
||||||
defer authenticatorRegistryMutex.Unlock()
|
defer authenticatorRegistryMutex.Unlock()
|
||||||
|
|
||||||
|
tmp := []authenticator{}
|
||||||
for _, a := range authenticatorRegistry {
|
for _, a := range authenticatorRegistry {
|
||||||
err := a.Configure(yamlSource)
|
err := a.Configure(yamlSource)
|
||||||
|
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
activeAuthenticators = append(activeAuthenticators, a)
|
tmp = append(tmp, a)
|
||||||
log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Activated authenticator")
|
log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Activated authenticator")
|
||||||
case errAuthenticatorUnconfigured:
|
case errAuthenticatorUnconfigured:
|
||||||
log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Authenticator unconfigured")
|
log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Authenticator unconfigured")
|
||||||
|
@ -87,10 +88,12 @@ func initializeAuthenticators(yamlSource []byte) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(activeAuthenticators) == 0 {
|
if len(tmp) == 0 {
|
||||||
return fmt.Errorf("No authenticator configurations supplied")
|
return fmt.Errorf("No authenticator configurations supplied")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
activeAuthenticators = tmp
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue