Option to allow auto-login during development.

This commit is contained in:
Deluan 2021-06-19 10:56:39 -04:00
parent cf553ce812
commit c09468e135
2 changed files with 12 additions and 3 deletions

View File

@ -63,6 +63,7 @@ type configOptions struct {
// DevFlags. These are used to enable/disable debugging and incomplete features
DevLogSourceLine bool
DevAutoCreateAdminPassword string
DevAutoLoginUsername string
DevPreCacheAlbumArtwork bool
DevFastAccessCoverArt bool
DevOldCacheLayout bool
@ -220,6 +221,7 @@ func init() {
// DevFlags. These are used to enable/disable debugging and incomplete features
viper.SetDefault("devlogsourceline", false)
viper.SetDefault("devautocreateadminpassword", "")
viper.SetDefault("devautologinusername", "")
viper.SetDefault("devprecachealbumartwork", false)
viper.SetDefault("devoldcachelayout", false)
viper.SetDefault("devFastAccessCoverArt", false)

View File

@ -207,6 +207,10 @@ func UsernameFromReverseProxyHeader(r *http.Request) string {
return username
}
func UsernameFromConfig(r *http.Request) string {
return conf.Server.DevAutoLoginUsername
}
func contextWithUser(ctx context.Context, ds model.DataStore, username string) (context.Context, error) {
user, err := ds.User(ctx).FindByUsername(username)
if err == nil {
@ -235,7 +239,7 @@ func authenticateRequest(ds model.DataStore, r *http.Request, findUsernameFns ..
func Authenticator(ds model.DataStore) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, err := authenticateRequest(ds, r, UsernameFromToken, UsernameFromReverseProxyHeader)
ctx, err := authenticateRequest(ds, r, UsernameFromConfig, UsernameFromToken, UsernameFromReverseProxyHeader)
if err != nil {
_ = rest.RespondWithError(w, http.StatusUnauthorized, "Not authenticated")
return
@ -268,9 +272,12 @@ func JWTRefresher(next http.Handler) http.Handler {
}
func handleLoginFromHeaders(ds model.DataStore, r *http.Request) map[string]interface{} {
username := UsernameFromReverseProxyHeader(r)
username := UsernameFromConfig(r)
if username == "" {
return nil
username = UsernameFromReverseProxyHeader(r)
if username == "" {
return nil
}
}
userRepo := ds.User(r.Context())