Example #1
0
File: server.go Project: CPSSD/MDFS
func newUser(conn net.Conn, r *bufio.Reader, w *bufio.Writer, md *MDService) (err error) {

	// get the uuid for the new user

	var newUser utils.User
	err = md.userDB.Update(func(tx *bolt.Tx) (err error) {

		// Retrieve the users bucket.
		// This should be created when the DB is first opened.
		b := tx.Bucket([]byte("users"))

		// Generate ID for the user.
		// This returns an error only if the Tx is closed or not writeable.
		// That can't happen in an Update() call so I ignore the error check.
		id, _ := b.NextSequence()
		newUser.Uuid = uint64(id)
		idStr := strconv.FormatUint(id, 10)

		// receive the username	and public key for the new user
		uname, _ := r.ReadString('\n')
		pubKN, _ := r.ReadString('\n')
		pubKE, _ := r.ReadString('\n')

		newUser.Uname = strings.TrimSpace(uname)
		newUser.Pubkey = &rsa.PublicKey{N: big.NewInt(0)}
		newUser.Pubkey.N.SetString(strings.TrimSpace(pubKN), 10)

		newUser.Pubkey.E, err = strconv.Atoi(strings.TrimSpace(pubKE))

		fmt.Println("\tNew user: "******"\tRecieved " + newUser.Uname + "'s public key")

		// Marshal user data into bytes.
		buf, err := json.Marshal(newUser)
		if err != nil {
			return err
		}

		fmt.Println("\tSending UUID " + idStr + " to " + newUser.Uname)
		w.WriteString(idStr + "\n")
		w.Flush()

		// Persist bytes to users bucket.
		return b.Put(itob(newUser.Uuid), buf)
	})

	return err
}