package user import ( "encoding/json" "log" "net/http" "nutfactory.org/Matrix/entities/device" "nutfactory.org/Matrix/utils" ) func New(id string, name, string, password string, devices map[string]*device.Device) (err error, newUser *User) { err, hashedPassword := utils.Hash([]byte(password)) if err != nil { return } newUser = &User{ Id: id, Name: name, Password: password, Devices: devices, } return } func CheckUsernameAvailability(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode("Test"); err != nil { panic(err) } } func Register(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") request := registerRequest{} errResponse := utils.CheckRequest(r) if errResponse != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(errResponse); err != nil { panic(err) } return } decoder := json.NewDecoder(r.Body) err := decoder.Decode(&request) if err != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Could not parse JSON"}); err != nil { panic(err) } return } errResponse = checkLoginType(request.Auth.LoginType) if errResponse != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(errResponse); err != nil { panic(err) } return } // TODO: Use New Function newUser := &User{ Id: request.Username, Name: request.Username, Password: request.Password, } foundUser, err := ReadUser(newUser.Id) if foundUser != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorCode: "M_USER_IN_USE", ErrorMessage: "Username already in use"}); err != nil { panic(err) } return } err = CreateUser(newUser) if err != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Database Error"}); err != nil { panic(err) } return } userDevice, err := device.ReadDevice(request.DeviceId) if userDevice != nil { err = userDevice.RenewAccesToken() if err != nil { log.Fatalf("Unable to renew AccesToken: %s", err) return } err = device.UpdateDevice(userDevice, newUser.Id) if err != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Database Error"}); err != nil { panic(err) } return } } else { err, userDevice = device.New(request.DeviceName) if err != nil { log.Fatalf("Unable to create device: %s", err) return } err = device.CreateDevice(userDevice, newUser.Id) if err != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Database Error"}); err != nil { panic(err) } return } } response := registerResponse{ UserId: newUser.Id, AccessToken: userDevice.AccessToken, DeviceId: userDevice.Id, } w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode(response); err != nil { panic(err) } } func Login(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") request := loginRequest{} errResponse := utils.CheckRequest(r) if errResponse != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(errResponse); err != nil { panic(err) } return } decoder := json.NewDecoder(r.Body) err := decoder.Decode(&request) if err != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Could not parse JSON"}); err != nil { panic(err) } return } errResponse = checkLoginType(request.LoginType) if errResponse != nil { w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode(errResponse); err != nil { panic(err) } return } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode("Test"); err != nil { panic(err) } } func Logout(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode("Test"); err != nil { panic(err) } } func Deactivate(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode("Not Implemented"); err != nil { panic(err) } } func ChangePassword(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode("Not Implemented"); err != nil { panic(err) } } //TODO: Check if necessary func Sync(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusBadRequest) if err := json.NewEncoder(w).Encode("Not Implemented"); err != nil { panic(err) } } func checkLoginType(loginType string) (errResponse *utils.ErrorResponse) { if loginType != "m.login.password" { errResponse = &utils.ErrorResponse{ErrorCode: "M_FORBIDDEN", ErrorMessage: "Unsupported Auth Type"} return } return }