user

package
v0.0.0-...-8ed97ff Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: ISC Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const SessionCacheTTL = 5 * time.Minute

SessionCacheTTL is the time-to-live of a session cache entry. This should be less than SessionTTL.

View Source
const SessionTTL = 7 * 24 * time.Hour

SessionTTL is the time-to-live of a session. Storages don't need to enforce this TTL, but they should be able to handle sessions that have exceeded this TTL.

Variables

View Source
var ErrSessionNotFound = hrt.NewHTTPError(401, "session not found")

ErrSessionNotFound is an error that indicates that a session was not found.

Functions

func AuthorizationEq

func AuthorizationEq(a, b *Authorization) bool

AuthorizationEq returns true if the two authorized users are equal. Unlike Authorization.Eq, this function allows nil values.

func OptionalAuthorizedUserString

func OptionalAuthorizedUserString(user *Authorization) string

OptionalAuthorizedUserString returns the string representation of the authorized user or "<nil>" if the authorized user is nil.

Types

type Authorization

type Authorization struct {
	// UserID is the ID of the user.
	// If this is nil, then the user is anonymous.
	UserID *UserID `json:"user_id,omitempty"`
	// contains filtered or unexported fields
}

Authorization is a struct that contains the session token and the user ID for an authorized user.

func NewAnonymous

func NewAnonymous(session SessionToken) Authorization

NewAnonymous creates a new anonymous authorized user.

func NewAuthorized

func NewAuthorized(session SessionToken, user UserID) Authorization

NewAuthorized creates a new authorized user.

func (Authorization) Eq

func (u Authorization) Eq(other Authorization) bool

Eq returns true if the authorized user is equal to the other authorized user. It only compares the token.

func (Authorization) Session

func (u Authorization) Session() SessionToken

Session returns the session token.

func (Authorization) String

func (u Authorization) String() string

String returns the string representation of the authorized user. The token is truncated to 8 characters.

type CachedSessionStorage

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

CachedSessionStorage is a session storage that caches session data. Sessions wrapped by this storage are cached for about 5 minutes.

func NewCachedSessionStorage

func NewCachedSessionStorage(storage SessionStorage) *CachedSessionStorage

NewCachedSessionStorage creates a new cached session storage.

func (*CachedSessionStorage) ChangeSession

func (s *CachedSessionStorage) ChangeSession(token SessionToken, userID *UserID) error

ChangeSession changes the user that the session is associated with.

func (*CachedSessionStorage) CreateSession

func (s *CachedSessionStorage) CreateSession() (SessionToken, error)

CreateSession creates a new anonymous session.

func (*CachedSessionStorage) QuerySession

func (s *CachedSessionStorage) QuerySession(token SessionToken) (*UserID, error)

QuerySession queries the session with the given session ID.

type SessionStorage

type SessionStorage interface {
	// CreateSession creates a session with the given session ID.
	CreateSession() (SessionToken, error)
	// ChangeSession changes the user that the session is associated with.
	// This is useful when a user logs in or logs out.
	ChangeSession(SessionToken, *UserID) error
	// QuerySession queries the session with the given session ID.
	QuerySession(SessionToken) (*UserID, error)
}

SessionStorage is in charge of persisting and retrieving session data from a database. A session may or may not be associated with a user.

type SessionToken

type SessionToken [24]byte

SessionToken is a type that represents a session token.

func GenerateSessionToken

func GenerateSessionToken() SessionToken

GenerateSessionToken generates a new session token.

func (SessionToken) MarshalText

func (t SessionToken) MarshalText() ([]byte, error)

MarshalText marshals the session token into text.

func (SessionToken) String

func (t SessionToken) String() string

String returns the string representation of the session token.

func (*SessionToken) UnmarshalText

func (t *SessionToken) UnmarshalText(text []byte) error

UnmarshalText unmarshals the session token from text.

type UserID

type UserID uint64

UserID is a type that represents a user UserID.

func GenerateUserID

func GenerateUserID() UserID

GenerateUserID generates a new user ID.

func (UserID) CreatedAt

func (u UserID) CreatedAt() time.Time

CreatedAt returns the time that the user ID was generated.

func (UserID) MarshalText

func (u UserID) MarshalText() ([]byte, error)

MarshalText marshals the user ID into text.

func (UserID) String

func (u UserID) String() string

String returns the string representation of the user ID.

func (*UserID) UnmarshalText

func (u *UserID) UnmarshalText(text []byte) error

UnmarshalText unmarshals the user ID from text.

Jump to

Keyboard shortcuts

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