Documentation ¶
Overview ¶
Package ses provides an abstraction over varying, http session authentication systems. The basic idea is that we receive some kind of token in an http request either way. The token contains at least a session identifier that we can use to lookup session data.
Index ¶
- Constants
- func Decorate(r *http.Request, s *Session) *http.Request
- func Provide(m *Manager) func(http.Handler) http.Handler
- type Config
- type Cookie
- type Data
- type HeaderReader
- type Manager
- func (m *Manager) Clear(w http.ResponseWriter, r *http.Request)
- func (m *Manager) EncodeAndWrite(w http.ResponseWriter, s *Session) (err error)
- func (m *Manager) Read(w http.ResponseWriter, r *http.Request) (_ *Session, err error)
- func (m *Manager) ReadOrCreate(w http.ResponseWriter, r *http.Request, cookie bool) *http.Request
- func (m *Manager) Save(w http.ResponseWriter, s *Session) error
- type Provider
- type Requirer
- type Session
- type Store
- type TokenCodec
- type TokenReader
- type TokenWriter
Constants ¶
const ContextKey sesKey = "ses"
ContextKey is the http request context key for a session pointer.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { // TokenReader reads tokens from requests. Some readers do also implement token write. TokenReader // Codec is codec list that is tried from first to last both for decoding and encoding. // This allows seamless key rotations for session encodings. We try older codecs to fail // gracefully if a newly introduced codec cannot encode the token data. Codec []TokenCodec }
Config represent a session token configuration with multiple codecs and helpers.
type Cookie ¶
Cookie is a wrapped http cookie that implements token reader and writer.
func DefaultCookie ¶
DefaultCookie returns a cookie using name and secure and other default parameters. The defaults are http only, root path and same site lax mode.
func (*Cookie) WriteToken ¶
func (cc *Cookie) WriteToken(w http.ResponseWriter, tok string)
type Data ¶
type Data interface { // ID returns the session id. ID() string // Tok returns the data to be encoded in the token. // This is usually just the session id but can be any string encoded value. Tok() string // User returns the user or account id if authenticated. User() string }
Data is the user defined session data that is often coupled to a specific application and store.
type HeaderReader ¶
type HeaderReader string
HeaderReader is a token reader that reads a named http request header
type Manager ¶
type Manager struct { Config []Config Store Store Log log.Logger Decorate func(*http.Request, *Session) *http.Request }
func NewManager ¶
func (*Manager) EncodeAndWrite ¶
func (m *Manager) EncodeAndWrite(w http.ResponseWriter, s *Session) (err error)
func (*Manager) ReadOrCreate ¶
type Requirer ¶
type Requirer struct { // Allow returns whether the request is allowed to proceed. On failure Allow should write an // error or redirect to the response writer. There are usually two error classes: // unauthenticated and unauthorized requests. Unauthenticated request should be redirected // to a login page, unauthorized requests should see a simple error page with a back link. Allow func(http.ResponseWriter, *http.Request, *Session) bool Next http.Handler }
Requirer is a http handler that checks sessions before proceeding with the next handler.
type Store ¶
type Store interface { // New creates and returns session data that must at least have a unique id or an error. New() (Data, error) // Get returns session data for the given token data or an error. Get(td string) (Data, error) // Save persist the given session data or returns an error. Save(d Data, isnew bool) error // Delete deletes the session data for the given token data or returns an error. Delete(td string) error }
Store provides access to often persisted session data.
type TokenCodec ¶
type TokenCodec interface { DecodeToken(tok string) (td string, err error) EncodeToken(td string) (tok string, err error) }
TokenCodec and decode and encode a token to the underlying token data. Data is used as string in this context, but anything can be encode as string. You can wrap github.com/gorilla/securecookie as a simple an proven token codec. Or you can check your claims from any kind of bearer token you want to use.
type TokenReader ¶
TokenReader can read a token from a http request.
type TokenWriter ¶
type TokenWriter interface {
WriteToken(http.ResponseWriter, string)
}
TokenWriter can write a token to a http response, usually cookies. The token should be cleared when called with an empty token string.