enable auth by default with temporary random JWT

This commit is contained in:
yusing 2025-02-18 02:27:45 +08:00
parent ee27237083
commit 651a7cf83e
3 changed files with 15 additions and 3 deletions

View file

@ -82,7 +82,11 @@ func main() {
homepage.InitOverridesConfig,
favicon.InitIconCache,
)
// logging.AddHook(notif.GetDispatcher())
if common.APIJWTSecret == nil {
logging.Warn().Msg("API_JWT_SECRET is not set, using random key")
common.APIJWTSecret = common.RandomJWTKey()
}
} else {
logging.DiscardLogger()
}

View file

@ -4,7 +4,6 @@ import (
"net/http"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/net/gphttp"
)
@ -13,7 +12,6 @@ var defaultAuth Provider
// Initialize sets up authentication providers.
func Initialize() error {
if !IsEnabled() {
logging.Warn().Msg("authentication is disabled, please set API_JWT_SECRET or OIDC_* to enable authentication")
return nil
}

View file

@ -1,6 +1,7 @@
package common
import (
"crypto/rand"
"encoding/base64"
"github.com/rs/zerolog/log"
@ -16,3 +17,12 @@ func decodeJWTKey(key string) []byte {
}
return bytes
}
func RandomJWTKey() []byte {
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
log.Panic().Err(err).Msg("failed to generate random jwt key")
}
return key
}