auth

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

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const SessionContextKey sessionContextKey = 0

Variables

View Source
var AuthorizationHeader = "Authorization"

AuthorizationHeader is the name of the HTTP Header which contains the session id. Exported so that it can be changed by developers.

View Source
var SessionRedisKey = "session"

SessionRedisKey represents the key prefix (e.g. <prefix>:<actual key>) of a Session. It is set as a var in case it needs to be changed.

Functions

func HasAuthRoles

func HasAuthRoles(session *Session, authRoles ...authrole.Value) bool

HasAuthRoles checks if any auth roles specified exist on the given Session. It returns true as long as one matching auth role is found.

func IsHigherAuthority

func IsHigherAuthority(session *Session, against ...authrole.Value) bool

IsHigherAuthority checks that the authenticated user has enough authority over another set of auth roles.

func IsLoggedIn

func IsLoggedIn(session *Session) bool

IsLoggedIn checks if the user is logged in (has a session).

func Middleware

func Middleware(authService AuthServicer) func(http.Handler) http.Handler

Middleware checks for a session ID in the Authorization header and hydrates the context with the session ID of the requester.

Types

type AuthConfig

type AuthConfig struct {
	BcryptCost     int           `env:"BCRYPT_COST" envDefault:"10"`
	SessionTimeout time.Duration `env:"SESSION_TIMEOUT" envDefault:"120h"`
}

func (AuthConfig) Validate

func (authConfig AuthConfig) Validate() error

type AuthService

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

func NewAuthService

func NewAuthService(
	authConfig AuthConfig, logger logr.Logger, redisService *redis.RedisService,
) *AuthService

func (AuthService) Config

func (authService AuthService) Config() *AuthConfig

func (AuthService) CreateAccount

func (authService AuthService) CreateAccount(
	ctx context.Context, input ent.CreateAccountInput,
) (
	*CreateAccountPayload, error,
)

func (AuthService) CreateStaffAccount

func (authService AuthService) CreateStaffAccount(
	ctx context.Context, input ent.CreateStaffAccountInput,
) (*CreateStaffAccountPayload, error)

func (AuthService) GetSessionFromStore

func (authService AuthService) GetSessionFromStore(ctx context.Context, sessionToken SessionToken) (*Session, error)

GetSessionFromStore retrieves a Session from Redis given the key and session ID. It also resets the expiry time of the session.

func (AuthService) LoginToAccount

func (authService AuthService) LoginToAccount(
	ctx context.Context, input LoginToAccountInput,
) (
	*LoginToAccountPayload, error,
)

LoginToAccount logs in an account. TODO: Make sure an already logged in account from the same IP and device can't login again.

func (AuthService) LoginToStaffAccount

func (authService AuthService) LoginToStaffAccount(
	ctx context.Context, input LoginToStaffAccountInput,
) (*LoginToStaffAccountPayload, error)

func (AuthService) Logout

func (authService AuthService) Logout(ctx context.Context) error

type AuthServicer

type AuthServicer interface {
	Config() *AuthConfig
	GetSessionFromStore(ctx context.Context, sessionToken SessionToken) (*Session, error)
	CreateAccount(
		ctx context.Context, input ent.CreateAccountInput,
	) (*CreateAccountPayload, error)
	CreateStaffAccount(
		ctx context.Context, input ent.CreateStaffAccountInput,
	) (*CreateStaffAccountPayload, error)
	LoginToAccount(ctx context.Context, input LoginToAccountInput) (
		*LoginToAccountPayload, error,
	)
	LoginToStaffAccount(
		ctx context.Context, input LoginToStaffAccountInput,
	) (*LoginToStaffAccountPayload, error)
	Logout(ctx context.Context) error
}

type CreateAccountPayload

type CreateAccountPayload struct {
	Account *ent.Account
	Session *Session
}

type CreateStaffAccountPayload

type CreateStaffAccountPayload struct {
	StaffAccount *ent.StaffAccount
	Session      *Session
}

type LoginToAccountInput

type LoginToAccountInput struct {
	Username string
	Password string
}

type LoginToAccountPayload

type LoginToAccountPayload struct {
	Account *ent.Account
	Session *Session
}

type LoginToStaffAccountInput

type LoginToStaffAccountInput struct {
	Username string
	Password string
}

type LoginToStaffAccountPayload

type LoginToStaffAccountPayload struct {
	StaffAccount *ent.StaffAccount
	Session      *Session
}

type Session

type Session struct {
	Token     SessionToken     // Session ID of the logged-in user.
	UserID    pulid.PULID      // ID of the user.
	AuthRoles []authrole.Value // Auth roles of the user.
}

Session represents an authentication session on the server side, containing the user ID and their authorization roles.

func GetSessionFromContext

func GetSessionFromContext(ctx context.Context) *Session

GetSessionFromContext returns the Session from a provided context, or nil if it is not found.

type SessionToken

type SessionToken string

func NewSessionToken

func NewSessionToken() (SessionToken, error)

NewSessionToken returns a new SessionToken.

func ParseSessionToken

func ParseSessionToken(sessionTokenStr string) (SessionToken, error)

ParseSessionToken parses a session ID string and verifies that it is correct.

func (SessionToken) MarshalGQL

func (sessionToken SessionToken) MarshalGQL(w io.Writer)

MarshalGQL implements graphql.Marshaler interface.

func (SessionToken) String

func (sessionToken SessionToken) String() string

String implements fmt.Stringer interface.

func (*SessionToken) UnmarshalGQL

func (sessionToken *SessionToken) UnmarshalGQL(val interface{}) error

UnmarshalGQL implements graphql.Unmarshaler interface.

Jump to

Keyboard shortcuts

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