feat: allow session timeout to be configurable. closes #101

This commit is contained in:
Deluan 2020-03-18 20:16:18 -04:00
parent ced87be57b
commit faac303eff
3 changed files with 29 additions and 13 deletions

View file

@ -13,12 +13,13 @@ import (
) )
type nd struct { type nd struct {
Port string `default:"4533"` Port string `default:"4533"`
MusicFolder string `default:"./music"` MusicFolder string `default:"./music"`
DataFolder string `default:"./"` DataFolder string `default:"./"`
ScanInterval string `default:"1m"` ScanInterval string `default:"1m"`
DbPath string DbPath string ``
LogLevel string `default:"info"` LogLevel string `default:"info"`
SessionTimeout string `default:"30s"`
IgnoredArticles string `default:"The El La Los Las Le Les Os As O A"` IgnoredArticles string `default:"The El La Los Las Le Les Os As O A"`
IndexGroups string `default:"A B C D E F G H I J K L M N O P Q R S T U V W X-Z(XYZ) [Unknown]([)"` IndexGroups string `default:"A B C D E F G H I J K L M N O P Q R S T U V W X-Z(XYZ) [Unknown]([)"`

View file

@ -9,9 +9,9 @@ const (
DefaultDbPath = "navidrome.db?cache=shared&_busy_timeout=15000&_journal_mode=WAL" DefaultDbPath = "navidrome.db?cache=shared&_busy_timeout=15000&_journal_mode=WAL"
InitialSetupFlagKey = "InitialSetup" InitialSetupFlagKey = "InitialSetup"
JWTSecretKey = "JWTSecret" JWTSecretKey = "JWTSecret"
JWTIssuer = "ND" JWTIssuer = "ND"
JWTTokenExpiration = 30 * time.Minute DefaultSessionTimeout = 30 * time.Minute
UIAssetsLocalPath = "ui/build" UIAssetsLocalPath = "ui/build"

View file

@ -5,6 +5,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/consts" "github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
@ -13,9 +14,10 @@ import (
) )
var ( var (
once sync.Once once sync.Once
JwtSecret []byte JwtSecret []byte
TokenAuth *jwtauth.JWTAuth TokenAuth *jwtauth.JWTAuth
sessionTimeOut time.Duration
) )
func InitTokenAuth(ds model.DataStore) { func InitTokenAuth(ds model.DataStore) {
@ -39,8 +41,21 @@ func CreateToken(u *model.User) (string, error) {
return TouchToken(token) return TouchToken(token)
} }
func getSessionTimeOut() time.Duration {
if sessionTimeOut == 0 {
if to, err := time.ParseDuration(conf.Server.SessionTimeout); err != nil {
sessionTimeOut = consts.DefaultSessionTimeout
} else {
sessionTimeOut = to
}
log.Info("Setting Session Timeout", "value", sessionTimeOut)
}
return sessionTimeOut
}
func TouchToken(token *jwt.Token) (string, error) { func TouchToken(token *jwt.Token) (string, error) {
expireIn := time.Now().Add(consts.JWTTokenExpiration).Unix() timeout := getSessionTimeOut()
expireIn := time.Now().Add(timeout).Unix()
claims := token.Claims.(jwt.MapClaims) claims := token.Claims.(jwt.MapClaims)
claims["exp"] = expireIn claims["exp"] = expireIn