auth

package
v0.0.0-...-37076da Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package auth will contains all the logic related to the authentication of the user.

Index

Constants

This section is empty.

Variables

View Source
var (
	SECRET            = []byte(os.Getenv("SESSION_SECRET"))
	AccessExpiration  = time.Minute * 10   // 10 minutes
	RefreshExpiration = time.Hour * 24 * 7 // 1 week

	AccessIssuer  = "session_provider"
	RefreshIssuer = "refresh_provider"
)
View Source
var ErrForbidden = errors.New("forbidden")
View Source
var ErrHasNotSession = errors.New("has not session")
View Source
var ErrHasNotSessionID = errors.New("has not session identifier")
View Source
var ErrHasNotStudentID = errors.New("has not student identifier")
View Source
var ErrMissingToken = errors.New("missing token")

Functions

This section is empty.

Types

type CreateSessionRepository

type CreateSessionRepository interface {
	// Run the CreateSession method
	//
	// It will receive the following params:
	//
	//		ctx context.Context
	//		session Session
	//
	// The session received will be replaced by current session if exists.
	// If current session is expired or not found, it will create a new session.
	Run(ctx context.Context, session Session) (Session, error)
}

CreateSessionRepository is a interface who defines the CreateSession method

type DeleteSessionRepository

type DeleteSessionRepository interface {
	// Run the DeleteSession method
	//
	// It will receive the following params:
	//
	//		ctx context.Context
	//		sessionID string
	//
	// The sessionID received will be used to delete the session.
	Run(ctx context.Context, sessionID string) error
}

DeleteSessionRepository is a interface who defines the DeleteSession method

type EndSession

type EndSession func(sessionID string) error

EndSession is the function that will be used to end a session.

type GetLoggedUsr

type GetLoggedUsr interface {
	// Run will receive usrID and return the user or an error
	Run(ctx context.Context, f filters.FilterConditions) (baseuser.BaseUser, error)
}

GetLoggerUsr will search and retrieve current logged usr

type GetMe

type GetMe func(GetMeOptions) (baseuser.BaseUser, error)

GetMe is the usecase to get the current student It will use the session token

type GetMeOptions

type GetMeOptions struct {
	UserID string   `json:"user_id" example:"1" format:"uuid" validate:"required,uuid"`
	Fields []string `query:"fields" example:"name,email"`
}

GetMeOptions is the options for the GetMe usecase

func (*GetMeOptions) GetErrorMap

func (g *GetMeOptions) GetErrorMap() map[string]map[string]error

GetErrorMap implements ModuleErrorMap to GetMeOptions

type GetSingleSessionRepository

type GetSingleSessionRepository interface {
	// Run the GetSingleSession method
	// The sessionID received will be used to get the session.
	Run(ctx context.Context, f filters.FilterConditions) (Session, error)
}

GetSingleSessionRepository is a interface who defines the GetSingleSession method

type Session

type Session interface {
	// GetID returns the session ID.
	GetID() string
	// GetUserID returns the user ID.
	GetUserID() string
	// IsExpired returns true if the session is expired.
	//
	// If the session has no one of:
	//		access token
	//		refresh token
	//	or
	//		expiration
	// it will return true to IsExpired call.
	//
	// Other case is:
	//
	// Access token or refresh token is expired.
	IsExpired() bool
	// GetAccessToken returns the access token.
	GetAccessToken() SessionToken
	// GetRefreshToken returns the refresh token.
	GetRefreshToken() SessionToken
	// HashToken returns the hash of the token.
	//
	// It encodes the token format and returns the encoded string.
	// Useful to return the token to the client.
	HashToken() string
}

Session is the interface that represents a session.

It will be used to store the authentication information of the user.

func NewSession

func NewSession(opts ...SessionProp) Session

NewSession will create a new Session instance.

It creates a new Valid session if only if receives the following options:

s := auth.NewSession(
	auth.WithSessionID(sessionID),
	auth.WithUserID(userID),
	auth.WithExpiration(expiration),
	auth.WithAccessToken(accessToken),
	auth.WithRefreshToken(refreshToken),
)

If any of the options is missing can return a invalid or expired session.

type SessionProp

type SessionProp func(*session) error

SessionProp is a function that will be applied to the session.

It will be used to create config options to Session.

func WithAccessToken

func WithAccessToken(token string) SessionProp

WithAccessToken sets the access token for the session

func WithExpiration

func WithExpiration(expiration time.Time) SessionProp

WithExpiration sets the expiration for the session

func WithRefreshToken

func WithRefreshToken(token string) SessionProp

WithRefreshToken sets the refresh token for the session

func WithSessionID

func WithSessionID(id string) SessionProp

WithSessionID sets the session ID for the session

func WithUserID

func WithUserID(id string) SessionProp

WithUserID sets the user ID for the session

type SessionToken

type SessionToken interface {
	// Token will return token string
	Token() string
	// Expiration will return the expiration time of the token.
	Expiration() time.Time
	// IsExpired will return true if the token is expired.
	IsExpired() bool
}

SessionToken is the interface that represents a session token.

It will be used to store the authentication token.

func NewSessionToken

func NewSessionToken(studentID string, opts ...TokenOption) SessionToken

NewSessionToken will build a new SessionToken.

It receives required studentID param and a list of options.

Hint: Use WithIssuer() to change the type of token. (default is "session_provider") Use ExpiresIn() to set the expiration for the token. (default is 10 minutes)

func SessionFromToken

func SessionFromToken(token string) SessionToken

SessionFromToken will build a SessionToken from a token string.

type SessionUpdater

type SessionUpdater func(Session) (Session, error)

SessionUpdater is a interface who defines the UpdateSession method

type StartSession

type StartSession func(username, password string) (Session, error)

StartSession is the function that will be used to start a session.

type TokenOption

type TokenOption func(*sessionToken) error

TokenOption is a function that will be applied to the session token.

It will be used to create config options to SessionToken.

func ExpiresIn

func ExpiresIn(expiresIn time.Duration) TokenOption

ExpiresIn will set the expiration for the token.

func WithIssuer

func WithIssuer(issuer string) TokenOption

WithIssuer sets the issuer for the token.

type UpdateSessionRepository

type UpdateSessionRepository interface {
	// Run the UpdateSession method
	Run(ctx context.Context, sessionID string, u SessionUpdater) error
}

type ValidateAndRefresh

type ValidateAndRefresh func(usrID, sessionID string) (baseuser.BaseUser, error)

ValidateAndRefresh is the function that will be used to validate and refresh a session.

Jump to

Keyboard shortcuts

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