func (account *Account) SetPassword(pass string) error { hash, err := bcrypt.Hash(pass) if err == nil { account.PasswordHash = hash } return err }
func InitTests() { server = httptest.NewServer(createRouter()) connectToRedis = func() *redis.Client { // CONFIGURE THIS! password := "" client := redis.NewTCPClient("localhost:6379", password, 1) return client } client := connectToRedis() defer client.Close() client.FlushDb() // Bootstrap the system, with an admin hash, _ := bcrypt.Hash("asdf") client.HMSet("user:admin", "isadmin", "1", "password", hash) }
func UserPut(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["username"] password := r.FormValue("password") auth, _ := NewBasicFromRequest(r) if checkPermUsr(auth, name) { client := connectToRedis() defer client.Close() if client.Exists("user:"******"user:"******"password", hash) fmt.Fprintf(w, "") } else { http.Error(w, "", 404) } } else { http.Error(w, "", 403) } }
func (w *DatabaseWorld) CreateAccount(name, password string) (acc *Account) { passwordHash, err := bcrypt.Hash(password) if err != nil { log.Println("Couldn't hash password to create an account:", err.Error()) return nil } // TODO: Config setting for where to start new players? origin := World.ThingForId(1) char := World.CreateThing(name, PlayerThing, nil, origin) if char == nil { log.Println("Couldn't create character to create an account") return nil } tx, err := w.db.Begin() if err != nil { log.Println("Couldn't open transaction to create an account:", err.Error()) return nil } acc = &Account{name, passwordHash, char.Id, time.Unix(0, 0)} row := tx.QueryRow("INSERT INTO account (loginname, passwordhash, character) VALUES ($1, $2, $3) RETURNING created", name, passwordHash, acc.Character) err = row.Scan(&acc.Created) if err != nil { log.Println("Couldn't create new account:", err.Error()) tx.Rollback() return nil } err = tx.Commit() if err != nil { log.Println("Couldn't commit transaction to create new account:", err.Error()) tx.Rollback() return nil } return }
// Only admins can create users, for now func UserPost(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["username"] isadmin := r.FormValue("isadmin") password := r.FormValue("password") auth, _ := NewBasicFromRequest(r) fmt.Printf("isadmin %v ps %v", isadmin, password) if checkAdm(auth) { client := connectToRedis() defer client.Close() if client.Exists("user:"******"", 409) } else { hash, _ := bcrypt.Hash(password) client.HMSet("user:"******"isadmin", "1", "password", hash) fmt.Fprintf(w, "") } } else { http.Error(w, "", 403) } }
func (self *defaultAuther) Hash(password string, salt string) string { hash, _ := bcrypt.Hash(password, salt) return hash }
func (u *User) Encrypt() { u.Hash, _ = bcrypt.Hash(u.Hash) }
func Encrypt(p string) string { hash, _ := bcrypt.Hash(p) return hash }