2020-02-06 22:48:35 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-04-26 18:06:38 +02:00
|
|
|
"context"
|
2020-02-06 22:48:35 +01:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-03-19 01:16:18 +01:00
|
|
|
"github.com/deluan/navidrome/conf"
|
2020-02-06 22:48:35 +01:00
|
|
|
"github.com/deluan/navidrome/consts"
|
|
|
|
"github.com/deluan/navidrome/log"
|
|
|
|
"github.com/deluan/navidrome/model"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
|
"github.com/go-chi/jwtauth"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-03-19 01:16:18 +01:00
|
|
|
once sync.Once
|
|
|
|
JwtSecret []byte
|
|
|
|
TokenAuth *jwtauth.JWTAuth
|
|
|
|
sessionTimeOut time.Duration
|
2020-02-06 22:48:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func InitTokenAuth(ds model.DataStore) {
|
|
|
|
once.Do(func() {
|
2020-04-26 18:06:38 +02:00
|
|
|
secret, err := ds.Property(context.TODO()).DefaultGet(consts.JWTSecretKey, "not so secret")
|
2020-02-06 22:48:35 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("No JWT secret found in DB. Setting a temp one, but please report this error", err)
|
|
|
|
}
|
|
|
|
JwtSecret = []byte(secret)
|
|
|
|
TokenAuth = jwtauth.New("HS256", JwtSecret, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateToken(u *model.User) (string, error) {
|
|
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
|
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
|
|
claims["iss"] = consts.JWTIssuer
|
|
|
|
claims["sub"] = u.UserName
|
|
|
|
claims["adm"] = u.IsAdmin
|
|
|
|
|
|
|
|
return TouchToken(token)
|
|
|
|
}
|
|
|
|
|
2020-03-19 01:16:18 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-06 22:48:35 +01:00
|
|
|
func TouchToken(token *jwt.Token) (string, error) {
|
2020-03-19 01:16:18 +01:00
|
|
|
timeout := getSessionTimeOut()
|
|
|
|
expireIn := time.Now().Add(timeout).Unix()
|
2020-02-06 22:48:35 +01:00
|
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
|
|
claims["exp"] = expireIn
|
|
|
|
|
|
|
|
return token.SignedString(JwtSecret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Validate(tokenStr string) (jwt.MapClaims, error) {
|
|
|
|
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
// Don't forget to validate the alg is what you expect:
|
|
|
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
|
|
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
|
|
|
|
return JwtSecret, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return token.Claims.(jwt.MapClaims), err
|
|
|
|
}
|