65 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package users
 | |
| 
 | |
| import "golang.org/x/crypto/bcrypt"
 | |
| 
 | |
| type User struct {
 | |
| 	ID             string `json:"id"`
 | |
| 	Username       string `json:"username"`
 | |
| 	Role           string `json:"role"`
 | |
| 	HashedPassword []byte `json:"-"`
 | |
| }
 | |
| 
 | |
| func (u *User) SetPassword(password string) error {
 | |
| 	hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	u.HashedPassword = hashed
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (u *User) ComparePassword(password string) error {
 | |
| 	return bcrypt.CompareHashAndPassword(u.HashedPassword, []byte(password))
 | |
| }
 | |
| 
 | |
| type InfoResponse struct {
 | |
| 	Version string `json:"version"`
 | |
| }
 | |
| 
 | |
| type ErrorResponse struct {
 | |
| 	Status  int    `json:"status"`
 | |
| 	Message string `json:"message"`
 | |
| }
 | |
| 
 | |
| type CreateUserRequest struct {
 | |
| 	Username string `json:"username"`
 | |
| 	Password string `json:"password"`
 | |
| 	Role     string `json:"role"`
 | |
| }
 | |
| 
 | |
| type CreateUserResponse struct {
 | |
| 	Message string `json:"message"`
 | |
| 	User    User   `json:"user"`
 | |
| }
 | |
| 
 | |
| type SetPasswordRequest struct {
 | |
| 	NewPassword string `json:"newPassword"`
 | |
| }
 | |
| 
 | |
| type VerifyRequest struct {
 | |
| 	Password string `json:"password"`
 | |
| }
 | |
| 
 | |
| // Messages
 | |
| type MsgUserUpdate struct {
 | |
| 	Message string `json:"message"`
 | |
| 	ID      string `json:"id"`
 | |
| }
 | |
| 
 | |
| type MsgUserCreate struct {
 | |
| 	Message string `json:"message"`
 | |
| 	User    User   `json:"user"`
 | |
| }
 |