Documentation ¶
Index ¶
- type Claims
- type JWTHandler
- func (j JWTHandler) GenerateClaims(cookieToken string) *Claims
- func (j *JWTHandler) GenerateJWT(id int, email string, roles map[string]uint8, expired time.Time) (t string, err error)
- func (j JWTHandler) HasOneRole(roles ...string) func(c *fiber.Ctx) error
- func (j JWTHandler) HasRoles(roles ...string) func(c *fiber.Ctx) error
- func (j JWTHandler) InvalidateToken(c *fiber.Ctx) error
- func (j JWTHandler) IsAuthenticated(c *fiber.Ctx) error
- func (j JWTHandler) IsBlacklisted(cookie string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Claims ¶
type Claims struct { ID int `json:"id"` Email string `json:"email"` Roles map[string]uint8 `json:"roles"` jwt.RegisteredClaims }
Claims struct will be generated as token,contains user data like ID, email and roles. You can add new field/property if you want.
type JWTHandler ¶
type JWTHandler struct {
// contains filtered or unexported fields
}
JWTHandler struct handles some key and redis connection The purpose of this is to handler and checking HTTP Header and/or checking is JWT blacklisted or not. See IsBlacklisted func.
func NewJWTHandler ¶
func NewJWTHandler() *JWTHandler
NewJWTHandler func creates new JwtHandler struct
func (JWTHandler) GenerateClaims ¶
func (j JWTHandler) GenerateClaims(cookieToken string) *Claims
GenerateClaims func generates claims struct from jwt.
func (*JWTHandler) GenerateJWT ¶
func (j *JWTHandler) GenerateJWT(id int, email string, roles map[string]uint8, expired time.Time) (t string, err error)
GenerateJWT func generate new token with expire time for user
func (JWTHandler) HasOneRole ¶
func (j JWTHandler) HasOneRole(roles ...string) func(c *fiber.Ctx) error
HasOneRole is a middleware function that checks if the user associated with the incoming request possesses at least one of the specified roles in the JWT claims. If the user has at least one role, it allows the request to proceed; otherwise, it returns an "Unauthorized" response.
func (JWTHandler) HasRoles ¶
func (j JWTHandler) HasRoles(roles ...string) func(c *fiber.Ctx) error
HasRoles is a middleware function that checks if the user associated with the incoming request possesses all the specified roles in the JWT claims. If the user lacks any of the roles, it returns an "Unauthorized" response; otherwise, it allows the request to proceed.
func (JWTHandler) InvalidateToken ¶
func (j JWTHandler) InvalidateToken(c *fiber.Ctx) error
InvalidateToken func stores (blacklistings) token to redis. After storing token in redis, the token is already blacklisted. This func is used in Logout feature.
func (JWTHandler) IsAuthenticated ¶
func (j JWTHandler) IsAuthenticated(c *fiber.Ctx) error
IsAuthenticated func extracts token from context (fiber Ctx), check is blacklisted or not. And checks the expire time too.
func (JWTHandler) IsBlacklisted ¶
func (j JWTHandler) IsBlacklisted(cookie string) bool
IsBlacklisted func check the token/cookie is blacklisted or not.