mirror of
https://github.com/weloe/token-go.git
synced 2025-10-05 23:46:52 +08:00
fix: fix set cookie error, lint error and update default token name to ''Tokengo'
This commit is contained in:
@@ -16,7 +16,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TokenName = "TokenGo"
|
TokenName = "Tokengo"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@@ -44,11 +44,13 @@ func (d *HttpRequest) UrlNoQuery() string {
|
|||||||
if d.source.URL.Scheme != "" {
|
if d.source.URL.Scheme != "" {
|
||||||
scheme = d.source.URL.Scheme
|
scheme = d.source.URL.Scheme
|
||||||
}
|
}
|
||||||
|
if scheme == "" {
|
||||||
if d.source.TLS != nil {
|
if d.source.TLS != nil {
|
||||||
scheme = "https"
|
scheme = "https"
|
||||||
} else {
|
} else {
|
||||||
scheme = "http"
|
scheme = "http"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return scheme + "://" + d.source.Host + d.source.URL.Path
|
return scheme + "://" + d.source.Host + d.source.URL.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -54,12 +54,18 @@ func (r *HttpResponse) DeleteCookie(name string, path string, domain string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *HttpResponse) AddCookie(name string, value string, path string, domain string, timeout int64) {
|
func (r *HttpResponse) AddCookie(name string, value string, path string, domain string, timeout int64) {
|
||||||
|
var expiration time.Time
|
||||||
|
if timeout == -1 {
|
||||||
|
expiration = time.Unix(0, 0)
|
||||||
|
} else {
|
||||||
|
expiration = time.Now().Add(time.Second * time.Duration(timeout))
|
||||||
|
}
|
||||||
cookie := http.Cookie{
|
cookie := http.Cookie{
|
||||||
Name: name,
|
Name: name,
|
||||||
Value: value,
|
Value: value,
|
||||||
Path: path,
|
Path: path,
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
Expires: time.Now().Add(time.Second * time.Duration(timeout)),
|
Expires: expiration,
|
||||||
}
|
}
|
||||||
r.AddHeader(constant.SetCookie, cookie.String())
|
r.AddHeader(constant.SetCookie, cookie.String())
|
||||||
}
|
}
|
||||||
|
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/weloe/token-go/constant"
|
"github.com/weloe/token-go/constant"
|
||||||
"github.com/weloe/token-go/ctx"
|
"github.com/weloe/token-go/ctx"
|
||||||
"github.com/weloe/token-go/model"
|
"github.com/weloe/token-go/model"
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,10 +57,20 @@ func (e *Enforcer) ResponseToken(tokenValue string, loginModel *model.Login, ctx
|
|||||||
|
|
||||||
// set token to cookie
|
// set token to cookie
|
||||||
if tokenConfig.IsReadCookie {
|
if tokenConfig.IsReadCookie {
|
||||||
cookieTimeout := tokenConfig.Timeout
|
var cookieTimeout int64
|
||||||
if loginModel.IsLastingCookie {
|
if !loginModel.IsLastingCookie {
|
||||||
cookieTimeout = -1
|
cookieTimeout = -1
|
||||||
|
} else {
|
||||||
|
if loginModel.Timeout != 0 {
|
||||||
|
cookieTimeout = loginModel.Timeout
|
||||||
|
} else {
|
||||||
|
cookieTimeout = tokenConfig.Timeout
|
||||||
}
|
}
|
||||||
|
if cookieTimeout == constant.NeverExpire {
|
||||||
|
cookieTimeout = math.MaxInt64
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add cookie use tokenConfig.CookieConfig
|
// add cookie use tokenConfig.CookieConfig
|
||||||
ctx.Response().AddCookie(tokenConfig.TokenName,
|
ctx.Response().AddCookie(tokenConfig.TokenName,
|
||||||
tokenValue,
|
tokenValue,
|
||||||
@@ -71,6 +82,7 @@ func (e *Enforcer) ResponseToken(tokenValue string, loginModel *model.Login, ctx
|
|||||||
// set token to header
|
// set token to header
|
||||||
if loginModel.IsWriteHeader {
|
if loginModel.IsWriteHeader {
|
||||||
ctx.Response().SetHeader(tokenConfig.TokenName, tokenValue)
|
ctx.Response().SetHeader(tokenConfig.TokenName, tokenValue)
|
||||||
|
ctx.Response().AddHeader(constant.AccessControlExposeHeaders, tokenConfig.TokenName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user