Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.7 KiB

package Auth
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/JsonSerialization"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
)
func Signup(w http.ResponseWriter, r *http.Request) {
var (
userData Models.User
requestBody []byte
returnJson []byte
err error
)
requestBody, err = ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error encountered reading POST body: %s\n", err.Error())
http.Error(w, "Error", http.StatusInternalServerError)
return
}
userData, err = JsonSerialization.DeserializeUser(requestBody, []string{
"id",
}, false)
if err != nil {
log.Printf("Invalid data provided to Signup: %s\n", err.Error())
http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
return
}
if userData.Username == "" ||
userData.Password == "" ||
userData.ConfirmPassword == "" ||
len(userData.AsymmetricPrivateKey) == 0 ||
len(userData.AsymmetricPublicKey) == 0 {
http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
return
}
err = Database.CheckUniqueUsername(userData.Username)
if err != nil {
http.Error(w, "Invalid Data", http.StatusUnprocessableEntity)
return
}
userData.Password, err = HashPassword(userData.Password)
if err != nil {
http.Error(w, "Error", http.StatusInternalServerError)
return
}
err = Database.CreateUser(&userData)
if err != nil {
http.Error(w, "Error", http.StatusInternalServerError)
return
}
returnJson, err = json.MarshalIndent(userData, "", " ")
if err != nil {
http.Error(w, "Error", http.StatusInternalServerError)
return
}
// Return updated json
w.WriteHeader(http.StatusOK)
w.Write(returnJson)
}