session

package
v0.0.0-...-d72dd33 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Is the session authenticated: 0: no; 1: yes
	Authenticated = "authenticated"
	// The user's ID
	UserID = "user_id"
	// The user's name
	Username = "username"
	// The user's type: admin|<nil>
	UserType = "user_type"
	// The time when the session is created
	Created = "created"
	// The time when the session is last updated
	Updated = "updated"
	// The path where the session is last updated
	Path = "path"
	// User-Agent request header
	UserAgent = "user_agent"
	// The user's IP address
	UserIPAddr = "user_ipaddr"
)

These are transient session keys

Variables

View Source
var ErrNoToken = errors.New("http: named token not present")

Functions

func ForceDelete

func ForceDelete(sessionID string)

Forcely delete a session if we know its ID. Note that it only deletes the session at server side while client could initiate a session with a same ID, which he remembered in the past. ForceDelete is usually used to forbid a session programmly, maybe upon the user's password change.

func GetUserAddr

func GetUserAddr(r *http.Request) string

GetUserAddr from the http request, considering X-Forwarded-For, Forwarded

func Middleware

func Middleware(serviceConfMode string) rest.Middleware

func SetToken

func SetToken(w http.ResponseWriter, cookie *http.Cookie)

SetToken adds a Set-Session-Token header to the provided [ResponseWriter]'s headers. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.

func Setup

func Setup(c SessionConfig, store *redis.Redis)

func Token

func Token(r *http.Request, name string) (*http.Cookie, error)

Token returns the named token provided in the request or ErrNoToken if not found. If multiple tokens match the given name, only one token will be returned.

Types

type Session

type Session struct {
	// contains filtered or unexported fields
}

func From

func From(ctx context.Context) Session

func (Session) AddFlash

func (s Session) AddFlash(value interface{}, vars ...string)

func (Session) Authenticated

func (s Session) Authenticated() bool

func (Session) Clear

func (s Session) Clear(r *http.Request, w http.ResponseWriter)

Clear is usually used on user logout. It deletes the session both at server and at client.

func (Session) CreatedAt

func (s Session) CreatedAt() time.Time

func (Session) Del

func (s Session) Del(key string)

func (Session) Flashes

func (s Session) Flashes(vars ...string) []interface{}

func (Session) Get

func (s Session) Get(key string) any

func (Session) GetInt

func (s Session) GetInt(key string) int64

func (Session) GetStr

func (s Session) GetStr(key string) string

func (Session) ID

func (s Session) ID() string

func (Session) Set

func (s Session) Set(key string, value any)

func (Session) UpdatedAt

func (s Session) UpdatedAt() time.Time

type SessionConfig

type SessionConfig struct {
	SessionSecret           string // used to authenticate session cookies using HMAC
	SessionStorageNamespace string `json:",default=sessions"`
	SessionCookieName       string `json:",default=SID"`
	SessionCookiePath       string `json:",default=/"`
	SessionCookieDomain     string `json:",optional"`
	// The duration in seconds that the session cookie/token is valid,
	// and also how long users stay logged-in to the App.
	SessionCookieTTL      int    `json:",default=600,range=[60:]"`
	SessionCookieSameSite string `json:",default=Lax,options=Strict|Lax|None"`
	SessionCookieSecure   bool   `json:",default=false"`
	// The session storage TTL is derived from its max age plus this grace period.
	SessionStorageGracePeriod               int `json:",default=10,range=[1:60]"`
	SessionStorageUnauthenticatedTTL        int `json:",default=60,range=[0:600]"`
	SessionStorageInjectedAuthenticationTTL int `json:",default=0,range=[0:60]"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL