Documentation ¶
Index ¶
- Variables
- func AuthRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware)
- func ComparePasswords(hashedPwd []byte, plainPwd []byte) bool
- func GetAuthMiddlewareHandler() *jwt.GinJWTMiddleware
- func GetUserHandler(c *gin.Context)
- func HashAndSaltPassword(pwd []byte) ([]byte, error)
- func InitGormStore(db *gorm.DB) error
- func MeHandler(c *gin.Context)
- func PublicRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware)
- func RegisterUserHandler(c *gin.Context)
- type TokenResponse
- type User
- type UserCreationError
- type UserIdentifier
- type UserIdentifierResponse
- type UserLogin
- type UserPersonal
- type UserPersonalResponse
- type UserRegister
- type UserStore
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrUserNotFound = errors.New("the requested user was not found")
Errors
View Source
var ErrUserNotUnique = errors.New("attempted to create a user with duplicate data")
View Source
var JwtConfig = &jwt.GinJWTMiddleware{ Realm: "test zone", Key: []byte("secret key"), Timeout: time.Hour, MaxRefresh: time.Hour, IdentityKey: identityKey, PayloadFunc: func(d interface{}) jwt.MapClaims { if v, ok := d.(*UserPersonal); ok { return jwt.MapClaims{ identityKey: strconv.FormatUint(uint64(v.ID), 36), } } return jwt.MapClaims{} }, IdentityHandler: func(c *gin.Context) interface{} { claims := jwt.ExtractClaims(c) idStr := claims[identityKey].(string) id, _ := strconv.ParseUint(idStr, 36, 0) return &UserPersonal{ ID: uint(id), } }, Authenticator: func(c *gin.Context) (interface{}, error) { var loginVals UserLogin if err := c.ShouldBindJSON(&loginVals); err != nil { return nil, jwt.ErrMissingLoginValues } email := loginVals.Email password := loginVals.Password user, err := Store.GetUserByEmail(email) if err != nil { return nil, jwt.ErrFailedAuthentication } if !ComparePasswords(user.Password, []byte(password)) { return nil, jwt.ErrFailedAuthentication } return user.AsPersonal(), nil }, LoginResponse: func(c *gin.Context, code int, token string, expire time.Time) { c.JSON(http.StatusOK, request.SuccessResponse{ Data: TokenResponse{ Token: token, Expiry: expire.Format(time.RFC3339), }, }) }, Unauthorized: func(c *gin.Context, code int, message string) { c.JSON(code, gin.H{ "code": code, "message": message, }) }, TokenLookup: "header: Authorization, query: token, cookie: jwt", TokenHeadName: "Bearer", TimeFunc: time.Now, }
Functions ¶
func AuthRoutes ¶
func AuthRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware)
func ComparePasswords ¶
func GetAuthMiddlewareHandler ¶
func GetAuthMiddlewareHandler() *jwt.GinJWTMiddleware
func GetUserHandler ¶
func HashAndSaltPassword ¶
func InitGormStore ¶
Initializes a GORM user store and sets the exported user store for application use.
func PublicRoutes ¶
func PublicRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware)
func RegisterUserHandler ¶
Types ¶
type TokenResponse ¶
type User ¶
type User struct { gorm.Model Username string `gorm:"unique"` Email string `gorm:"unique"` Password []byte `gorm:"size:60"` }
func (User) AsIdentifier ¶
func (u User) AsIdentifier() *UserIdentifier
func (User) AsPersonal ¶
func (u User) AsPersonal() *UserPersonal
type UserCreationError ¶
type UserCreationError struct {
Err error
}
func (UserCreationError) Error ¶
func (e UserCreationError) Error() string
type UserIdentifier ¶
type UserIdentifierResponse ¶
type UserIdentifierResponse struct {
User *UserIdentifier `json:"user"`
}
type UserPersonal ¶
type UserPersonalResponse ¶
type UserPersonalResponse struct {
User *UserPersonal `json:"user"`
}
type UserRegister ¶
type UserStore ¶
type UserStore interface { database.DataStore GetUserIdentifierById(uint) (*UserIdentifier, error) GetUserPersonalById(uint) (*UserPersonal, error) GetUserByEmail(string) (*User, error) CreateUser(*User) error DeleteUser(uint) error }
The UserStore interface, which defines ways that the application can query for users.
var Store UserStore
The globally exported UserStore that the application will use.
Click to show internal directories.
Click to hide internal directories.