This repository has been archived on 2020-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
macaron/cookie/helper.go
Andrew Thornton 0db5d45848
All checks were successful
continuous-integration/drone/push Build is passing
Create functional option for ctx.SetCookie (#14)
Merge branch 'master' into refactor-set-cookie

author attribution

Signed-off-by: Andrew Thornton <art27@cantab.net>

Add helper functions

Signed-off-by: Andrew Thornton <art27@cantab.net>

Give ctx.SetCookie a functional option to allow for more settings on the cookie

Signed-off-by: Andrew Thornton <art27@cantab.net>

run go fmt on codebase and fix linting of tree_test

Signed-off-by: Andrew Thornton <art27@cantab.net>

Reviewed-on: #14
Reviewed-by: 6543 <6543@noreply.gitea.io>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>
Co-Authored-By: Andrew Thornton <art27@cantab.net>
Co-Committed-By: Andrew Thornton <art27@cantab.net>
2020-10-28 05:36:41 +08:00

81 lines
2.0 KiB
Go

// Copyright 2020 The Macaron Authors
// Copyright 2020 The Gitea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package cookie
import (
"net/http"
"time"
)
// cookie contains helper functions for setting cookie values
// MaxAge sets the maximum age for a provided cookie
func MaxAge(maxAge int) func(*http.Cookie) {
return func(c *http.Cookie) {
c.MaxAge = maxAge
}
}
// Path sets the path for a provided cookie
func Path(path string) func(*http.Cookie) {
return func(c *http.Cookie) {
c.Path = path
}
}
// Domain sets the domain for a provided cookie
func Domain(domain string) func(*http.Cookie) {
return func(c *http.Cookie) {
c.Domain = domain
}
}
// Secure sets the secure setting for a provided cookie
func Secure(secure bool) func(*http.Cookie) {
return func(c *http.Cookie) {
c.Secure = secure
}
}
// HttpOnly sets the HttpOnly setting for a provided cookie
func HttpOnly(httpOnly bool) func(*http.Cookie) {
return func(c *http.Cookie) {
c.HttpOnly = httpOnly
}
}
// HTTPOnly sets the HttpOnly setting for a provided cookie
func HTTPOnly(httpOnly bool) func(*http.Cookie) {
return func(c *http.Cookie) {
c.HttpOnly = httpOnly
}
}
// Expires sets the expires and rawexpires for a provided cookie
func Expires(expires time.Time) func(*http.Cookie) {
return func(c *http.Cookie) {
c.Expires = expires
c.RawExpires = expires.Format(time.UnixDate)
}
}
// SameSite sets the SameSite for a provided cookie
func SameSite(sameSite http.SameSite) func(*http.Cookie) {
return func(c *http.Cookie) {
c.SameSite = sameSite
}
}