mirror of
https://github.com/Fishwaldo/mouthpiece.git
synced 2025-03-15 11:31:32 +00:00
Password Checking/Setting Functions
This commit is contained in:
parent
7b26893e33
commit
8c076d3878
2 changed files with 35 additions and 8 deletions
|
@ -69,8 +69,8 @@ func (p DirectHandler) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
sessOnly := r.URL.Query().Get("sess") == "1"
|
||||
ok := true
|
||||
ok, err = p.CredChecker(creds.User, creds.Password)
|
||||
|
||||
ok, err := p.CredChecker(creds.User, creds.Password)
|
||||
if err != nil {
|
||||
rest.SendErrorJSON(w, r, p.L, http.StatusInternalServerError, err, "failed to check user credentials")
|
||||
return
|
||||
|
|
|
@ -2,6 +2,7 @@ package user
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
. "github.com/Fishwaldo/mouthpiece/internal/log"
|
||||
"github.com/Fishwaldo/mouthpiece/internal/message"
|
||||
|
@ -43,13 +44,23 @@ func CreateUser(user *User) error {
|
|||
Log.Info("User Validation Error", "Error", err)
|
||||
return err;
|
||||
}
|
||||
tx := db.Db.Create(&user)
|
||||
tx := db.Db.Omit("Password").Create(&user)
|
||||
if tx.Error != nil {
|
||||
return tx.Error
|
||||
}
|
||||
/* New Users all Start with User Role */
|
||||
if user, err := GetUser(user.Email); err == nil {
|
||||
user.addUserRole("user")
|
||||
if dbuser, err := GetUser(user.Email); err == nil {
|
||||
/* Set the Users Initial Password */
|
||||
if err := dbuser.SetPassword(user.Password); err != nil {
|
||||
if tx := db.Db.Delete(&dbuser); tx.Error != nil {
|
||||
Log.Info("Error Deleting User after failed Password", "Error", tx.Error)
|
||||
return err;
|
||||
}
|
||||
return err
|
||||
}
|
||||
/* New Users all Start with User Role */
|
||||
if !dbuser.addUserRole("user") {
|
||||
Log.Info("Error Adding User Role", "Error", err)
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
|
@ -65,11 +76,27 @@ func (u *User) addUserRole(role string) bool {
|
|||
return true;
|
||||
}
|
||||
|
||||
func (u *User) CheckPassword(string) bool {
|
||||
func (u *User) CheckPassword(password string) bool {
|
||||
Log.Info("Checking Password", "email", u.Email)
|
||||
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
||||
if err != nil {
|
||||
Log.Info("Password Check Failed", "Error", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (u *User) SetPassword(string) error {
|
||||
func (u *User) SetPassword(password string) error {
|
||||
Log.Info("Setting Password", "Email", u.Email)
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if (err != nil) {
|
||||
Log.Info("Error Generating SetPassword Hash", "Error", err)
|
||||
return err
|
||||
}
|
||||
if tx := db.Db.Model(&u).Update("password", string(hashedPassword)); tx.Error != nil {
|
||||
Log.Info("Error Setting Password", "Error", tx.Error)
|
||||
return tx.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue