user

package
v0.0.0-...-13fd0b2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

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 ComparePasswords(hashedPwd []byte, plainPwd []byte) bool

func GetAuthMiddlewareHandler

func GetAuthMiddlewareHandler() *jwt.GinJWTMiddleware

func GetUserHandler

func GetUserHandler(c *gin.Context)

func HashAndSaltPassword

func HashAndSaltPassword(pwd []byte) ([]byte, error)

func InitGormStore

func InitGormStore(db *gorm.DB) error

Initializes a GORM user store and sets the exported user store for application use.

func MeHandler

func MeHandler(c *gin.Context)

func PublicRoutes

func PublicRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware)

func RegisterUserHandler

func RegisterUserHandler(c *gin.Context)

Types

type TokenResponse

type TokenResponse struct {
	Token  string `json:"token"`
	Expiry string `json:"expiry"`
}

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 UserIdentifier struct {
	ID       uint   `json:"id"`
	Username string `json:"username"`
}

type UserIdentifierResponse

type UserIdentifierResponse struct {
	User *UserIdentifier `json:"user"`
}

type UserLogin

type UserLogin struct {
	Email    string `json:"email" binding:"required,email"`
	Password string `json:"password" binding:"required"`
}

type UserPersonal

type UserPersonal struct {
	ID       uint   `json:"id"`
	Username string `json:"username"`
	Email    string `json:"email"`
}

type UserPersonalResponse

type UserPersonalResponse struct {
	User *UserPersonal `json:"user"`
}

type UserRegister

type UserRegister struct {
	Username        string `json:"username" binding:"required"`
	Email           string `json:"email" binding:"required,email"`
	Password        string `json:"password" binding:"required,min=8"`
	PasswordConfirm string `json:"password_confirm" binding:"eqfield=Password"`
}

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.

Jump to

Keyboard shortcuts

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