auth

package
v0.0.0-...-4b1f759 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSessionTimeout is returned when a caller attempts to access a session that's expired
	ErrSessionTimeout = errors.New("the session timed out")

	// ErrSessionDNE is returned when a caller attempts to access a session that doesn't exist
	ErrSessionDNE = errors.New("the session does not exist")
)

Functions

func CheckKeyHash

func CheckKeyHash(key Key, hash string) bool

CheckKeyHash checks whether a plaintext Key is represented by a hash

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

CheckPasswordHash checks whether a plaintext password is represented by hash

func GetBearerToken

func GetBearerToken(r *http.Request) (string, error)

GetBearerToken is a helper function to retreive a token sent in standard "Bearer" format from a request (https://tools.ietf.org/html/rfc6750#page-5). If the request doesn't contain an Authorization header or the Authorization header is improperly formatted, getBearerToken returns "". Handlers generally shouldn't call this function, and should instead call getSessionID or getApiKey (TODO) to specify which type of token they are expecting.

func HashKey

func HashKey(key Key) string

HashKey returns a stringified (64 bytes) sha256 checksum of a Key. This is sufficient for securely storing Key's in the database, see reputable StackExchange answer: https://security.stackexchange.com/a/180364

func HashPassword

func HashPassword(password string) (string, error)

HashPassword returns a stringified password hash

Types

type Key

type Key string

Key is a 32 byte, base64 encoded, cryptographically secure random string

func NewKey

func NewKey() (Key, error)

NewKey creates a new API Key. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

type Session

type Session struct {
	SessionID SessionID
	Account   model.Account
	Expires   time.Time
}

Session is an individual user's session

type SessionID

type SessionID string

SessionID is a 32 byte, base64 encoded, cryptographically secure random string

type SessionManager

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

SessionManager is an in-memory session store. The app should only ever create one of these and pass it around as a pointer

func NewSessionManager

func NewSessionManager(timeout time.Duration) *SessionManager

NewSessionManager creates a new *SessionManager

func (*SessionManager) CreateSession

func (sm *SessionManager) CreateSession(account model.Account) (Session, error)

CreateSession creates a new session in the SessionManager's store, indexed by a new randomly generated SessionID, and expiring sm.timeout from the time it's created. It will return an error if the system's secure random number generator fails to function correctly.

func (*SessionManager) DeleteSession

func (sm *SessionManager) DeleteSession(sid SessionID) bool

DeleteSession deletes a session from the session manager. Returns true if the session was found and deleted, or false if the session wasn't found

func (*SessionManager) FromContext

func (sm *SessionManager) FromContext(ctx context.Context) (Session, error)

FromContext gets a Session from a request context. WithSessionAuth-wrapped handlers should use this to access Session data from within the handler

func (*SessionManager) UpdateSession

func (sm *SessionManager) UpdateSession(session Session)

UpdateSession updates a session in the session manager with the new session passed in to it.

func (*SessionManager) WithSessionAuth

func (sm *SessionManager) WithSessionAuth(next http.Handler) http.Handler

WithSessionAuth is a middlewear function for protecting handlers for routes that require the user to be authenticated. If the user has an

Jump to

Keyboard shortcuts

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