middleware

package
v0.0.0-...-21d0996 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleSuperAdmin = "superadmin"
	RoleAdmin      = "admin"
	RoleUser       = "user"
	RoleModerator  = "moderator"
)

Variables

View Source
var (
	// ErrorInvalidToken - is a constant that represents an invalid token error.
	ErrorInvalidToken = errors.New("authorization failed: invalid token")

	// ErrorExpiredToken - is a constant that represents an expired token error.
	ErrorExpiredToken = errors.New("authorization failed: expired token")

	// ErrorInvalidCredentials - is a constant that represents an invalid credentials error.
	ErrorInvalidCredentials = errors.New("authorization failed: invalid credentials")

	// ErrorUnauthorizedUser - is a constant that represents an unauthorized user error.
	ErrorUnauthorizedUser = errors.New("authorization failed: unauthorized user")
)
View Source
var (
	Data     *SignedParams
	UserData *User
)

Functions

func ComparePasswords

func ComparePasswords(hash, password string) (bool, error)

ComparePasswords - ComparePasswords is a function that handles the comparison of passwords.

@param hash - string
@param password - string
@return bool
@return error

func GetCookie

func GetCookie(r *http.Request, name string) *http.Cookie

GetCookie - GetCookie is a function that gets a cookie.

@param r - *http.Request
@param name - string
@return *http.Cookie

func GetToken

func GetToken(user *User) (string, error)

GetTokens - is a function that handles the retrieval of tokens.

@param user - *store.User
@return string
@return string
@return error

func HashPassword

func HashPassword(password string) (string, error)

HashPassword - HashPassword is a function that handles the hashing of passwords.

@param password - string
@return string
@return error

func RemoveCookie

func RemoveCookie(w http.ResponseWriter, name, path, domain string)

RemoveCookie - RemoveCookie is a function that deletes a cookie.

@param w - http.ResponseWriter
@param name - string
@param path - string
@param domain - string

func SetCookie

func SetCookie(w http.ResponseWriter, name, value, path, domain string, maxAge int, secure, httpOnly bool, sameSite http.SameSite)

SetCookie - SetCookie is a function that sets a cookie.

@param w - http.ResponseWriter
@param name - string
@param value - string
@param path - string
@param domain - string
@param maxAge - int
@param secure - bool
@param httpOnly - bool
@param sameSite - http.SameSite

Types

type CtxKey

type CtxKey interface{}

Store Structs

var ContextKey CtxKey = "bookie-api-7kt2" // this is the key that will be used to store the context values

this is the store that will be used to store the context values

type CtxValues

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

func (*CtxValues) GetCtxValue

func (v *CtxValues) GetCtxValue(key string) interface{}

GetValue - returns the value of the context key provided.

@param key - string @return interface{}

func (*CtxValues) SetCtxValue

func (v *CtxValues) SetCtxValue(key string, value interface{}) error

SetValue - sets the value of the context key provided.

@param key - string @param value - interface{}

func (*CtxValues) String

func (v *CtxValues) String() string

String - returns the string representation of the context values.

type SignedParams

type SignedParams struct {
	User *User
	jwt.RegisteredClaims
	Roles []string
}

func GetVerifiedClaims

func GetVerifiedClaims(ctx context.Context, role string) (*SignedParams, error)

GetVerifiedClaims - is a function that handles the retrieval of verified claims.

@param ctx - context.Context
@param role - string
@return *SignedParams
@return error

func ValidateToken

func ValidateToken(token string) (*SignedParams, error)

ValidateToken - ValidateToken is a function that handles the verification of tokens.

@param ctx - context.Context
@param token - string
@param next - func(context.Context) (interface{}, error)
@return response
@return error

func (*SignedParams) HasRole

func (data *SignedParams) HasRole(roles ...string) bool

HasRole - is a function that checks if a user has a role.

@param ctx - context.Context
@param role - string
@return bool

type User

type User struct {
	ID          string
	Fullname    string
	Username    string
	Email       string
	DateOfBirth string
	Phone       string
	Roles       []string
}

Jump to

Keyboard shortcuts

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