Make it possible to redirect to a login url unless authenticated (#347)

* feature: Try to navigate to UrlOnUnauthenticated if set and not authenticated

* refactor: use better naming

* feature: default to allow guest
This commit is contained in:
Simon Dahlbacka 2024-10-11 20:36:31 +03:00 committed by GitHub
parent 00856f15a7
commit 35dca50863
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 0 deletions

View File

@ -130,6 +130,8 @@ type Config struct {
DefaultIconForActions string
DefaultIconForDirectories string
DefaultIconForBack string
AuthLoginUrl string
AuthAllowGuest bool
usedConfigDir string
}
@ -177,6 +179,7 @@ func DefaultConfigWithBasePort(basePort int) *Config {
config.DefaultPermissions.Logs = true
config.AuthJwtClaimUsername = "name"
config.AuthJwtClaimUserGroup = "group"
config.AuthAllowGuest = true
config.WebUIDir = "./webui"
config.CronSupportForSeconds = false
config.SectionNavigationStyle = "sidebar"

View File

@ -269,6 +269,10 @@ func (api *oliveTinAPI) GetDashboardComponents(ctx ctx.Context, req *pb.GetDashb
res.AuthenticatedUser = user.Username
if res.AuthenticatedUser == "guest" && !cfg.AuthAllowGuest {
return nil, status.Errorf(codes.PermissionDenied, "Unauthenticated")
}
return res, nil
}

View File

@ -5,7 +5,9 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"net/http"
@ -83,10 +85,21 @@ func startRestAPIServer(globalConfig *config.Config) error {
return http.ListenAndServe(cfg.ListenAddressRestActions, cors.AllowCors(mux))
}
func errorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
log.Errorf("Error handling request: %v", err)
md, ok := runtime.ServerMetadataFromContext(ctx)
if ok && md.HeaderMD.Get("username") == nil {
err = status.Error(codes.Unauthenticated, "unauthenticated request")
}
runtime.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err)
}
func newMux() *runtime.ServeMux {
// The MarshalOptions set some important compatibility settings for the webui. See below.
mux := runtime.NewServeMux(
runtime.WithMetadata(parseRequestMetadata),
runtime.WithErrorHandler(errorHandler),
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{

View File

@ -32,6 +32,7 @@ type webUISettings struct {
SshFoundKey string
SshFoundConfig string
EnableCustomJs bool
AuthLoginUrl string
}
func findWebuiDir() string {
@ -117,6 +118,7 @@ func generateWebUISettings(w http.ResponseWriter, r *http.Request) {
SshFoundKey: installationinfo.Runtime.SshFoundKey,
SshFoundConfig: installationinfo.Runtime.SshFoundConfig,
EnableCustomJs: cfg.EnableCustomJs,
AuthLoginUrl: cfg.AuthLoginUrl,
})
w.Header().Add("Content-Type", "application/json")

View File

@ -58,6 +58,9 @@ function fetchGetDashboardComponents () {
window.fetch(window.restBaseUrl + 'GetDashboardComponents', {
cors: 'cors'
}).then(res => {
if (!res.ok && res.status === 401) {
window.location.href = window.settings.AuthLoginUrl
}
return res.json()
}).then(res => {
if (!window.restAvailable) {