Use the Proxy Auth as a Middleware so auth works transparently

pull/1082/head
James Mills 2 weeks ago
parent 2dd218b120
commit 60acef3ddd
Signed by: prologic
GPG Key ID: AC4C014F1440EBD6
  1. 10
      internal/auth/proxy_auth.go
  2. 3
      internal/server.go

@ -5,6 +5,7 @@ package auth
import (
"net/http"
"net/http/httputil"
"git.mills.io/yarnsocial/yarn/internal/session"
"github.com/julienschmidt/httprouter"
@ -29,7 +30,16 @@ func NewProxyAuth(header string, userCreator UserCreator) Auther {
// in the session.
func (pa *ProxyAuth) MustAuth(next httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Debugf("req:\n%q\n", dump)
if username := r.Header.Get(pa.Header); username != "" {
log.Debugf("Usernaem: %q", username)
if err := pa.UserCreator.CreateUser(username, r); err != nil {
log.WithError(err).Errorf("error checking or creating user %q", username)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)

@ -1023,6 +1023,7 @@ func NewServer(bind string, options ...Option) (*Server, error) {
switch config.Auth {
case "proxy":
auther = auth.NewProxyAuth(config.AuthHeader, NewUserCreator(config, db))
router.Use(auther.MustAuth)
case "session":
auther = auth.NewSessionAuth("/logi")
default:
@ -1152,7 +1153,7 @@ func NewServer(bind string, options ...Option) (*Server, error) {
log.Infof("Instance Name: %s", server.config.Name)
log.Infof("Base URL: %s", server.config.BaseURL)
log.Infof("Using Theme: %s", server.config.Theme)
log.Infof("Using %s auth (header: %s redirect: %s)", server.config.Auth, server.config.AuthHeader, server.config.requestTimeout)
log.Infof("Using %s Auth (Header: %s)", server.config.Auth, server.config.AuthHeader)
log.Infof("Admin User: %s", server.config.AdminUser)
log.Infof("Admin Name: %s", server.config.AdminName)
log.Infof("Admin Email: %s", server.config.AdminEmail)

Loading…
Cancel
Save