package middleware

import (
	"cold-delivery/common/middleware/handler"
	jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
	"gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
	"time"
)

// AuthInit jwt验证new
func AuthInit() (*jwt.GinJWTMiddleware, error) {
	timeout := time.Hour
	if config.ApplicationConfig.Mode == "dev" {
		timeout = time.Duration(876000) * time.Hour
	} else {
		if config.JwtConfig.Timeout != 0 {
			timeout = time.Duration(config.JwtConfig.Timeout) * time.Second
		}
	}
	return jwt.New(&jwt.GinJWTMiddleware{
		Realm:      "bzd oauth",
		Key:        []byte(config.JwtConfig.Secret),
		Timeout:    timeout,
		SendCookie: true,
		// token 最大刷新时间
		MaxRefresh:      2 * time.Hour,
		PayloadFunc:     handler.PayloadFunc,
		IdentityHandler: handler.IdentityHandler,
		Authenticator:   handler.Authenticator,
		Authorizator:    handler.Authorizator,
		Unauthorized:    handler.Unauthorized,
		TokenLookup:     "header: Authorization, query: token, cookie: jwt",
		TokenHeadName:   "Bearer",
		TimeFunc:        time.Now,
	})

}
