Documentation ¶
Index ¶
- Constants
- func AddFlash(c *kelly.Context, msg string)
- func AuthMiddleware(options *AuthOptions) kelly.HandlerFunc
- func Flashes(c *kelly.Context) []interface{}
- func InitFlash(keyPairs []byte) bool
- func InitPermission(options *PermissionOptions) error
- func IsAuthenticated(c *kelly.Context) bool
- func LoggedUser(c *kelly.Context) interface{}
- func Login(c *kelly.Context, user interface{}) error
- func LoginRequired() kelly.HandlerFunc
- func Logout(c *kelly.Context) error
- func NewCookie(name, value string, options *Options) *http.Cookie
- func PermissionRequired(perm string) kelly.HandlerFunc
- func Save(c *kelly.Context) error
- func SessionMiddleware(store Store, key string) kelly.HandlerFunc
- type AllPermisionsGetter
- type AuthOptions
- type CastUser
- type CookieStore
- func (s *CookieStore) Delete(c *kelly.Context, name string) error
- func (s *CookieStore) Get(c *kelly.Context, name string) (*SessionImp, error)
- func (s *CookieStore) MaxAge(age int)
- func (s *CookieStore) New(c *kelly.Context, name string) (*SessionImp, error)
- func (s *CookieStore) Save(c *kelly.Context, session *SessionImp) error
- type FilesystemStore
- func (s *FilesystemStore) Delete(c *kelly.Context, name string) error
- func (s *FilesystemStore) Get(c *kelly.Context, name string) (*SessionImp, error)
- func (s *FilesystemStore) MaxAge(age int)
- func (s *FilesystemStore) MaxLength(l int)
- func (s *FilesystemStore) New(c *kelly.Context, name string) (*SessionImp, error)
- func (s *FilesystemStore) Save(c *kelly.Context, session *SessionImp) error
- type GobSerializer
- type JSONSerializer
- type MultiError
- type Options
- type PermissionOptions
- type RediStore
- func (s *RediStore) Delete(c *kelly.Context, name string) error
- func (s *RediStore) Get(c *kelly.Context, name string) (*SessionImp, error)
- func (s *RediStore) New(c *kelly.Context, name string) (*SessionImp, error)
- func (s *RediStore) Save(c *kelly.Context, session *SessionImp) error
- func (s *RediStore) SetKeyPrefix(p string)
- func (s *RediStore) SetMaxAge(v int)
- func (s *RediStore) SetMaxLength(l int)
- func (s *RediStore) SetSerializer(ss SessionSerializer)
- type Registry
- type Session
- type SessionImp
- func (s *SessionImp) AddFlash(value interface{}, vars ...string)
- func (s *SessionImp) Clear()
- func (s *SessionImp) Delete(key interface{})
- func (s *SessionImp) Flashes(vars ...string) []interface{}
- func (s *SessionImp) Get(key interface{}) interface{}
- func (s *SessionImp) Save() error
- func (s *SessionImp) Set(key interface{}, val interface{})
- func (s *SessionImp) Written() bool
- type SessionSerializer
- type Store
- type UsePermissionGetter
Constants ¶
const ( AUTH_SESSION_NAME = "session" AUTH_SESSION_KEY = "_user_" )
const ( DefaultRegistryKey = "registrys" FlashKey = "_flash" )
const (
PERMISSION_SESSION_KEY = "_permission_"
)
Variables ¶
This section is empty.
Functions ¶
func NewCookie ¶
NewCookie returns an http.Cookie with the options set. It also sets the Expires field calculated based on the MaxAge value, for Internet Explorer compatibility.
func SessionMiddleware ¶
func SessionMiddleware(store Store, key string) kelly.HandlerFunc
Types ¶
type AuthOptions ¶
type AuthOptions struct { ErrorFunc kelly.HandlerFunc User interface{} CastUserFunc CastUser }
Options stores configurations for a CSRF middleware.
type CookieStore ¶
type CookieStore struct { Codecs []securecookie.Codec Options *Options // default configuration }
CookieStore stores sessions using secure cookies.
func NewCookieStore ¶
func NewCookieStore(keyPairs ...[]byte) *CookieStore
NewCookieStore returns a new CookieStore.
Keys are defined in pairs to allow key rotation, but the common case is to set a single authentication key and optionally an encryption key.
The first key in a pair is used for authentication and the second for encryption. The encryption key can be set to nil or omitted in the last pair, but the authentication key is required in all pairs.
It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.
Use the convenience function securecookie.GenerateRandomKey() to create strong keys.
func (*CookieStore) Delete ¶
func (s *CookieStore) Delete(c *kelly.Context, name string) error
Save adds a single session to the response.
func (*CookieStore) Get ¶
func (s *CookieStore) Get(c *kelly.Context, name string) (*SessionImp, error)
Get returns a session for the given name after adding it to the registry.
It returns a new session if the sessions doesn't exist. Access IsNew on the session to check if it is an existing session or a new one.
It returns a new session and an error if the session exists but could not be decoded.
func (*CookieStore) MaxAge ¶
func (s *CookieStore) MaxAge(age int)
MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.
func (*CookieStore) New ¶
func (s *CookieStore) New(c *kelly.Context, name string) (*SessionImp, error)
New returns a session for the given name without adding it to the registry.
The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same decoded session after the first call.
func (*CookieStore) Save ¶
func (s *CookieStore) Save(c *kelly.Context, session *SessionImp) error
Save adds a single session to the response.
type FilesystemStore ¶
type FilesystemStore struct { Codecs []securecookie.Codec Options *Options // default configuration // contains filtered or unexported fields }
FilesystemStore stores sessions in the filesystem.
It also serves as a reference for custom stores.
This store is still experimental and not well tested. Feedback is welcome.
func NewFilesystemStore ¶
func NewFilesystemStore(path string, keyPairs ...[]byte) *FilesystemStore
NewFilesystemStore returns a new FilesystemStore.
The path argument is the directory where sessions will be saved. If empty it will use os.TempDir().
See NewCookieStore() for a description of the other parameters.
func (*FilesystemStore) Delete ¶
func (s *FilesystemStore) Delete(c *kelly.Context, name string) error
Save adds a single session to the response.
func (*FilesystemStore) Get ¶
func (s *FilesystemStore) Get(c *kelly.Context, name string) (*SessionImp, error)
Get returns a session for the given name after adding it to the registry.
See CookieStore.Get().
func (*FilesystemStore) MaxAge ¶
func (s *FilesystemStore) MaxAge(age int)
MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.
func (*FilesystemStore) MaxLength ¶
func (s *FilesystemStore) MaxLength(l int)
MaxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new FilesystemStore is 4096.
func (*FilesystemStore) New ¶
func (s *FilesystemStore) New(c *kelly.Context, name string) (*SessionImp, error)
New returns a session for the given name without adding it to the registry.
See CookieStore.New().
func (*FilesystemStore) Save ¶
func (s *FilesystemStore) Save(c *kelly.Context, session *SessionImp) error
Save adds a single session to the response.
If the Options.MaxAge of the session is <= 0 then the session file will be deleted from the store path. With this process it enforces the properly session cookie handling so no need to trust in the cookie management in the web browser.
type GobSerializer ¶
type GobSerializer struct{}
GobSerializer uses gob package to encode the session map
func (GobSerializer) Deserialize ¶
func (s GobSerializer) Deserialize(d []byte, session *SessionImp) error
Deserialize back to map[interface{}]interface{}
func (GobSerializer) Serialize ¶
func (s GobSerializer) Serialize(session *SessionImp) ([]byte, error)
Serialize using gob
type JSONSerializer ¶
type JSONSerializer struct{}
JSONSerializer encode the session map to JSON.
func (JSONSerializer) Deserialize ¶
func (s JSONSerializer) Deserialize(d []byte, session *SessionImp) error
Deserialize back to map[string]interface{}
func (JSONSerializer) Serialize ¶
func (s JSONSerializer) Serialize(session *SessionImp) ([]byte, error)
Serialize to JSON. Will err if there are unmarshalable key values
type MultiError ¶
type MultiError []error
MultiError stores multiple errors.
Borrowed from the App Engine SDK.
func (MultiError) Error ¶
func (m MultiError) Error() string
type Options ¶
type Options struct { Path string Domain string // MaxAge=0 means no 'Max-Age' attribute specified. // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'. // MaxAge>0 means Max-Age attribute present and given in seconds. MaxAge int Secure bool HttpOnly bool }
Options stores configuration for a session or session store. Fields are a subset of http.Cookie fields.
type PermissionOptions ¶
type PermissionOptions struct { ErrorFunc kelly.HandlerFunc UserPermissionGetter UsePermissionGetter AllPermisionsGetter AllPermisionsGetter }
选项,对外
type RediStore ¶
type RediStore struct { Pool *redis.Client Codecs []securecookie.Codec Options *Options // default configuration DefaultMaxAge int // default Redis TTL for a MaxAge == 0 session // contains filtered or unexported fields }
RediStore stores sessions in a redis backend.
func NewRediStore ¶
NewRediStore returns a new RediStore. size: maximum number of idle connections.
func NewRediStoreWithPool ¶
NewRediStoreWithPool instantiates a RediStore with a *redis.Pool passed in.
func (*RediStore) Get ¶
Get returns a session for the given name after adding it to the registry.
See gorilla/sessions FilesystemStore.Get().
func (*RediStore) New ¶
New returns a session for the given name without adding it to the registry.
See gorilla/sessions FilesystemStore.New().
func (*RediStore) Save ¶
func (s *RediStore) Save(c *kelly.Context, session *SessionImp) error
Save adds a single session to the response.
func (*RediStore) SetKeyPrefix ¶
SetKeyPrefix set the prefix
func (*RediStore) SetMaxAge ¶
SetMaxAge restricts the maximum age, in seconds, of the session record both in database and a browser. This is to change session storage configuration. If you want just to remove session use your session `s` object and change it's `Options.MaxAge` to -1, as specified in
http://godoc.org/github.com/gorilla/sessions#Options
Default is the one provided by this package value - `sessionExpire`. Set it to 0 for no restriction. Because we use `MaxAge` also in SecureCookie crypting algorithm you should use this function to change `MaxAge` value.
func (*RediStore) SetMaxLength ¶
SetMaxLength sets RediStore.maxLength if the `l` argument is greater or equal 0 maxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new RediStore is 4096. Redis allows for max. value sizes of up to 512MB (http://redis.io/topics/data-types) Default: 4096,
func (*RediStore) SetSerializer ¶
func (s *RediStore) SetSerializer(ss SessionSerializer)
SetSerializer sets the serializer
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry stores sessions used during a request.
func GetRegistry ¶
type Session ¶
type Session interface { // Get returns the session value associated to the given key. Get(key interface{}) interface{} // Set sets the session value associated to the given key. Set(key interface{}, val interface{}) // Delete removes the session value associated to the given key. Delete(key interface{}) // Clear deletes all values in the session. Clear() // AddFlash adds a flash message to the session. // A single variadic argument is accepted, and it is optional: it defines the flash key. // If not defined "_flash" is used by default. AddFlash(value interface{}, vars ...string) // Flashes returns a slice of flash messages from the session. // A single variadic argument is accepted, and it is optional: it defines the flash key. // If not defined "_flash" is used by default. Flashes(vars ...string) []interface{} // Options sets confuguration for a session. // Options(Options) // Save saves all sessions used during the current request. Save() error }
Wraps thinly gorilla-session methods. Session stores the values and optional configuration for a session.
type SessionImp ¶
type SessionImp struct { // The ID of the session, generated by stores. It should not be used for // user data. ID string // Values contains the user-data for the session. Values map[interface{}]interface{} Options *Options IsNew bool // contains filtered or unexported fields }
func NewSession ¶
func NewSession(store Store, name string) *SessionImp
NewSession is called by session stores to create a new session instance.
func (*SessionImp) AddFlash ¶
func (s *SessionImp) AddFlash(value interface{}, vars ...string)
func (*SessionImp) Clear ¶
func (s *SessionImp) Clear()
func (*SessionImp) Delete ¶
func (s *SessionImp) Delete(key interface{})
func (*SessionImp) Flashes ¶
func (s *SessionImp) Flashes(vars ...string) []interface{}
func (*SessionImp) Get ¶
func (s *SessionImp) Get(key interface{}) interface{}
func (*SessionImp) Save ¶
func (s *SessionImp) Save() error
func (*SessionImp) Set ¶
func (s *SessionImp) Set(key interface{}, val interface{})
func (*SessionImp) Written ¶
func (s *SessionImp) Written() bool
type SessionSerializer ¶
type SessionSerializer interface { Deserialize(d []byte, session *SessionImp) error Serialize(session *SessionImp) ([]byte, error) }
SessionSerializer provides an interface hook for alternative serializers
type Store ¶
type Store interface { // Get should return a cached session. Get(c *kelly.Context, name string) (*SessionImp, error) // New should create and return a new session. // // Note that New should never return a nil session, even in the case of // an error if using the Registry infrastructure to cache the session. New(c *kelly.Context, name string) (*SessionImp, error) // Save should persist session to the underlying store implementation. Save(c *kelly.Context, s *SessionImp) error // 删除cookie Delete(c *kelly.Context, name string) error }
type UsePermissionGetter ¶
获取用户的所有权限