42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.ilapage.cn/ila/yovision/Sense/server/common/middleware/handler"
|
|
"github.com/go-admin-team/go-admin-core/sdk/config"
|
|
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
|
)
|
|
|
|
const defaultLoginValidity = 30 * 24 * time.Hour
|
|
|
|
// AuthInit jwt验证new
|
|
func AuthInit() (*jwt.GinJWTMiddleware, error) {
|
|
timeout := resolveJWTTimeout(config.ApplicationConfig.Mode, config.JwtConfig.Timeout)
|
|
return jwt.New(&jwt.GinJWTMiddleware{
|
|
Realm: "Sense",
|
|
Key: []byte(config.JwtConfig.Secret),
|
|
Timeout: timeout,
|
|
MaxRefresh: time.Hour,
|
|
PayloadFunc: handler.PayloadFunc,
|
|
IdentityHandler: handler.IdentityHandler,
|
|
Authenticator: handler.Authenticator,
|
|
Authorizator: handler.Authorizator,
|
|
Unauthorized: handler.Unauthorized,
|
|
TokenLookup: "header: Authorization, cookie: sense_session",
|
|
TokenHeadName: "Bearer",
|
|
TimeFunc: time.Now,
|
|
})
|
|
|
|
}
|
|
|
|
func resolveJWTTimeout(mode string, configuredSeconds int64) time.Duration {
|
|
if mode == "dev" {
|
|
return time.Duration(876010) * time.Hour
|
|
}
|
|
if configuredSeconds > 0 {
|
|
return time.Duration(configuredSeconds) * time.Second
|
|
}
|
|
return defaultLoginValidity
|
|
}
|