81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
package auth
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-jwt/jwt"
|
|
"github.com/spf13/viper"
|
|
"kumoly.io/kumoly/app/system"
|
|
)
|
|
|
|
const (
|
|
sys_user = "arec"
|
|
sys_pwd = "Arec82595060"
|
|
)
|
|
|
|
func init() {
|
|
// secret for jwt
|
|
viper.SetDefault("auth.secret", "secret")
|
|
viper.SetDefault("auth.name", "app")
|
|
|
|
// expires in sec, '0' to indicate token will not expire
|
|
viper.SetDefault("auth.expire", 1800)
|
|
|
|
// default admin user
|
|
viper.SetDefault("admin.user", "admin")
|
|
viper.SetDefault("admin.passwd", "admin")
|
|
|
|
}
|
|
|
|
var std *Auth
|
|
|
|
func Setup() {
|
|
std = NewAuth()
|
|
}
|
|
|
|
func Injector(router *gin.RouterGroup) *system.Inject {
|
|
return std.Injector(router)
|
|
}
|
|
|
|
// Parse tok str to token object
|
|
func Parse(tok string) (token *jwt.Token, err error) {
|
|
return std.Parse(tok)
|
|
}
|
|
|
|
// ParseClaims parse token string to claims object
|
|
func ParseClaims(tok string) (claims *Claims, err error) {
|
|
return std.ParseClaims(tok)
|
|
}
|
|
|
|
// SetToken in header and cookie(if CookieMode)
|
|
func SetToken(c *gin.Context, tok string) {
|
|
std.SetToken(c, tok)
|
|
}
|
|
|
|
// SetClaims directly from http request
|
|
func SetClaims(c *gin.Context, claims *Claims) error {
|
|
return std.SetClaims(c, claims)
|
|
}
|
|
|
|
// GetToken from header or cookie
|
|
func GetToken(c *gin.Context) (tok string, err error) {
|
|
return std.GetToken(c)
|
|
}
|
|
|
|
// GetClaims directly from http request
|
|
func GetClaims(c *gin.Context) (claims *Claims, err error) {
|
|
return std.GetClaims(c)
|
|
}
|
|
|
|
func ClearToken(c *gin.Context) {
|
|
std.ClearToken(c)
|
|
}
|
|
|
|
// New tok str
|
|
func NewToken(claims Claims) (tok string, err error) {
|
|
return std.NewToken(claims)
|
|
}
|
|
|
|
func Middleware(c *gin.Context) {
|
|
std.Middleware(c)
|
|
}
|