Support setting cookie domain (#6288)

Signed-off-by: Tamal Saha <tamal@appscode.com>
This commit is contained in:
Tamal Saha 2019-07-12 06:57:31 -07:00 committed by techknowlogick
parent d95237b561
commit 2102f9d92d
15 changed files with 58 additions and 46 deletions

View file

@ -1,7 +1,6 @@
sudo: false
language: go
go:
- 1.5.x
- 1.6.x
- 1.7.x
- 1.8.x

View file

@ -25,7 +25,7 @@ import (
"gopkg.in/macaron.v1"
)
const _VERSION = "0.1.0"
const _VERSION = "0.1.1"
func Version() string {
return _VERSION
@ -58,6 +58,8 @@ type csrf struct {
Form string
// Cookie name value for setting and getting csrf token.
Cookie string
//Cookie domain
CookieDomain string
//Cookie path
CookiePath string
// Cookie HttpOnly flag value used for the csrf token.
@ -123,8 +125,10 @@ type Options struct {
Form string
// Cookie value used to set and get token.
Cookie string
// Cookie domain.
CookieDomain string
// Cookie path.
CookiePath string
CookiePath string
CookieHttpOnly bool
// Key used for getting the unique ID per user.
SessionKey string
@ -187,6 +191,7 @@ func Generate(options ...Options) macaron.Handler {
Header: opt.Header,
Form: opt.Form,
Cookie: opt.Cookie,
CookieDomain: opt.CookieDomain,
CookiePath: opt.CookiePath,
CookieHttpOnly: opt.CookieHttpOnly,
ErrorFunc: opt.ErrorFunc,
@ -222,7 +227,7 @@ func Generate(options ...Options) macaron.Handler {
// FIXME: actionId.
x.Token = GenerateToken(x.Secret, x.ID, "POST")
if opt.SetCookie {
ctx.SetCookie(opt.Cookie, x.Token, 0, opt.CookiePath, "", opt.Secure, opt.CookieHttpOnly, time.Now().AddDate(0, 0, 1))
ctx.SetCookie(opt.Cookie, x.Token, 0, opt.CookiePath, opt.CookieDomain, opt.Secure, opt.CookieHttpOnly, time.Now().AddDate(0, 0, 1))
}
}

View file

@ -50,7 +50,7 @@ func generateTokenAtTime(key, userID, actionID string, now time.Time) string {
h := hmac.New(sha1.New, []byte(key))
fmt.Fprintf(h, "%s:%s:%d", clean(userID), clean(actionID), now.UnixNano())
tok := fmt.Sprintf("%s:%d", h.Sum(nil), now.UnixNano())
return base64.URLEncoding.EncodeToString([]byte(tok))
return base64.RawURLEncoding.EncodeToString([]byte(tok))
}
// Valid returns true if token is a valid, unexpired token returned by Generate.
@ -61,7 +61,7 @@ func ValidToken(token, key, userID, actionID string) bool {
// validTokenAtTime is like Valid, but it uses now to check if the token is expired.
func validTokenAtTime(token, key, userID, actionID string, now time.Time) bool {
// Decode the token.
data, err := base64.URLEncoding.DecodeString(token)
data, err := base64.RawURLEncoding.DecodeString(token)
if err != nil {
return false
}

View file

@ -1,14 +1,11 @@
sudo: false
language: go
go:
- 1.3
- 1.4
- 1.5
- tip
- 1.6.x
- 1.7.x
- 1.8.x
- 1.9.x
- 1.10.x
- 1.11.x
script: go test -v -cover -race
notifications:
email:
- u@gogs.io

View file

@ -26,7 +26,7 @@ import (
"gopkg.in/macaron.v1"
)
const _VERSION = "0.3.0"
const _VERSION = "0.4.0"
func Version() string {
return _VERSION
@ -96,6 +96,8 @@ type Options struct {
TmplName string
// Configuration section name. Default is "i18n".
Section string
// Domain used for `lang` cookie. Default is ""
CookieDomain string
}
func prepareOptions(options []Options) Options {
@ -193,7 +195,7 @@ func I18n(options ...Options) macaron.Handler {
// Save language information in cookies.
if !hasCookie {
ctx.SetCookie("lang", curLang.Lang, 1<<31-1, "/"+strings.TrimPrefix(opt.SubURL, "/"))
ctx.SetCookie("lang", curLang.Lang, 1<<31-1, "/"+strings.TrimPrefix(opt.SubURL, "/"), opt.CookieDomain)
}
restLangs := make([]LangType, 0, i18n.Count()-1)

4
vendor/modules.txt vendored
View file

@ -128,9 +128,9 @@ github.com/go-macaron/cache/redis
github.com/go-macaron/captcha
# github.com/go-macaron/cors v0.0.0-20190309005821-6fd6a9bfe14e9
github.com/go-macaron/cors
# github.com/go-macaron/csrf v0.0.0-20180426211211-503617c6b372
# github.com/go-macaron/csrf v0.0.0-20190131233648-3751b136073c
github.com/go-macaron/csrf
# github.com/go-macaron/i18n v0.0.0-20160612092837-ef57533c3b0f
# github.com/go-macaron/i18n v0.0.0-20190131234336-56731837a73b
github.com/go-macaron/i18n
# github.com/go-macaron/inject v0.0.0-20160627170012-d8a0b8677191
github.com/go-macaron/inject