94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
package fbhttp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
fberrors "github.com/filebrowser/filebrowser/v2/errors"
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
)
|
|
|
|
// This mode is selected only by an explicit process flag, never by failed SSO.
|
|
type NoSSO struct {
|
|
store *storage.Storage
|
|
server *settings.Server
|
|
identity sessionIdentity
|
|
}
|
|
|
|
func NewNoSSO(store *storage.Storage, server *settings.Server) (*NoSSO, error) {
|
|
u, err := store.Users.Get(server.Root, false, "local:no-sso")
|
|
if errors.Is(err, fberrors.ErrNotExist) {
|
|
st, e := store.Settings.Get()
|
|
if e != nil {
|
|
return nil, e
|
|
}
|
|
u = &users.User{Username: "local:no-sso", Password: "!sso-disabled", LockPassword: true}
|
|
st.Defaults.Apply(u)
|
|
u.Scope = "/"
|
|
u.Perm.Admin = false
|
|
u.Perm.Execute = false
|
|
u.Commands = []string{}
|
|
if e = store.Users.SaveProvisioned(u, false); e != nil {
|
|
return nil, e
|
|
}
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
server.NoSSO = true
|
|
return &NoSSO{store: store, server: server, identity: sessionIdentity{ID: u.ID, Name: "Local", Admin: true, Sub: "local:no-sso", State: "local"}}, nil
|
|
}
|
|
func (n *NoSSO) Wrap(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
p := strings.TrimPrefix(r.URL.Path, n.server.BaseURL)
|
|
if strings.HasPrefix(p, "/api/") || strings.HasPrefix(p, "/auth/") {
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
}
|
|
if isMutation(r) && !csrfAllowed(r, "") {
|
|
http.Error(w, "Cross-origin request rejected", 403)
|
|
return
|
|
}
|
|
if p == "/auth/session" || p == "/api/session" {
|
|
if r.Method != "GET" {
|
|
w.WriteHeader(405)
|
|
return
|
|
}
|
|
u, err := n.store.Users.Get(n.server.Root, false, n.identity.ID)
|
|
if err != nil {
|
|
writeState(w, 503, browserState{Status: "unavailable"})
|
|
return
|
|
}
|
|
applySSOIdentity(u, n.identity)
|
|
profile := ssoUserInfo(u)
|
|
profile.AuthMode = "no-sso"
|
|
profile.SessionState = "local"
|
|
if p == "/auth/session" {
|
|
writeState(w, 200, browserState{Status: "authenticated", Authenticated: true, State: "local", User: &profile})
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(profile)
|
|
return
|
|
}
|
|
if strings.HasPrefix(p, "/auth/") {
|
|
if p == "/auth/login" && r.Method == "GET" {
|
|
http.Redirect(w, r, n.server.BaseURL+"/files/", 303)
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
return
|
|
}
|
|
for _, disabled := range []string{"/api/login", "/api/signup", "/api/renew", "/api/command"} {
|
|
if p == disabled {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
}
|
|
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), identityKey{}, n.identity)))
|
|
})
|
|
}
|