auth

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

Auth

The majority of this code is based off of The Copenhagen Book.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encrypter

type Encrypter interface {
	// Encrypt encrypts the given string and returns the encrypted string. The
	// returned string should be base64 encoded.
	Encrypt(string) (string, error)
	// Decrypt decrypts the given string and returns the decrypted string. The
	// given string should be base64 encoded.
	Decrypt(string) (string, error)
}

Encrypter is responsible for encrypting and decrypting strings. This can be used to encrypt and decrypt session IDs, user IDs, etc. It is up to you to implement this interface for your specific use case.

type Generator

type Generator interface {
	// Generate generates a random string with the given number of bytes. This can
	// be used to generate a session ID or other random tokens. It is recommended
	// that the tokens have an entropy of at least 120 bits. This is outlined in
	// The Coopenhagen Book.
	Generate() (string, error)
}

Generator is an interface for generating random strings. This can be used to generate session IDs or random tokens. These implementations are based of of [The Copenhagen Book](https://thecopenhagenbook.com/). We recommend reading the book to better understand authentication and security, and why these are implemented the way they are.

type NewSessionServiceOptions

type NewSessionServiceOptions struct {
	Adapter   SessionAdapter
	Encrypter Encrypter
	Generator Generator
}

type Session

type Session interface {
	GetSessionID() string
	SetSessionID(string)
	GetUserID() string
	GetExpiresAt() time.Time
	SetExpiresAt(time.Time)
	GetRefreshUntil() time.Time
	Copy() Session
}

Session is an interface that represents a user session. It contains the functions required for this package to interact with the session. The Session interface is used to allow for different implementations of the session to be used.

We recommend storing the user agent and IP address so you can verify that the session is being used by the same device and from the same location.

type SessionAdapter added in v0.3.1

type SessionAdapter interface {
	// GetSession retrieves the session with the given sessionID from the
	// database and returns it.
	GetSession(ctx context.Context, sessionID string) (Session, error)
	// InsertSession inserts a new session into the database with the given
	// values.
	InsertSession(ctx context.Context, newSession Session) error
	// UpdateSession updates the session with the given sessionID to have a new
	// expiration time.
	UpdateSession(ctx context.Context, newSession Session) error
	// DeleteSessionsByUserID invalidates all sessions for the given user.
	DeleteSessionsByUserID(ctx context.Context, userID string) error
	// DeleteSession invalidates the session with the given sessionID.
	DeleteSession(ctx context.Context, sessionID string) error
}

SessionAdapter is responsible for creating, updating, and deleting sessions from a datastore of your choice. This can be a SQL database, a NoSQL database, an in-memory cache, etc. It is up to you to implement this interface for your specific use case. The adapter should just be simple operations. The logic for wether for calling these operations are handled elsewhere.

type SessionService

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

SessionService is a struct that contains all of the necessary components to manage server side sessions. It is responsible for creating, retrieving, and invalidating sessions. It is also responsible for encrypting and decrypting session IDs. It is up to you to choose the implementation of the Adapter, Encrypter, and Generator interfaces. The following links provide implementations.

func NewSessionService

func NewSessionService(options NewSessionServiceOptions) *SessionService

NewSessionService returns a new instance of Auth.

func (*SessionService) CreateSession

func (a *SessionService) CreateSession(ctx context.Context, newSession Session) (Session, error)

4. Return the session and cookie

func (*SessionService) DecryptSessionID

func (a *SessionService) DecryptSessionID(encryptedSessionID string) (string, error)

func (*SessionService) DeleteSession

func (a *SessionService) DeleteSession(ctx context.Context, sessionID string) error

func (*SessionService) DeleteSessionsByUserID

func (a *SessionService) DeleteSessionsByUserID(ctx context.Context, userID string) error

func (*SessionService) EncryptSessionID

func (a *SessionService) EncryptSessionID(sessionID string) (string, error)

func (*SessionService) GetSession

func (a *SessionService) GetSession(ctx context.Context, sessionID string) (Session, error)

func (*SessionService) UpdateSession

func (a *SessionService) UpdateSession(ctx context.Context, session Session) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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