Refactor Register Funktion
This commit is contained in:
parent
c79d1f86e4
commit
7db9c374cc
5 changed files with 129 additions and 21 deletions
|
@ -2,13 +2,18 @@ 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) (newUser *User) {
|
||||
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,
|
||||
|
@ -29,7 +34,7 @@ func CheckUsernameAvailability(w http.ResponseWriter, r *http.Request) {
|
|||
func Register(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
request := registerRequest{}
|
||||
errResponse := utils.CheckRequest(r, request)
|
||||
errResponse := utils.CheckRequest(r)
|
||||
if errResponse != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
||||
|
@ -46,7 +51,7 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
return
|
||||
}
|
||||
errResponse = checkAuthData(&request.Auth)
|
||||
errResponse = checkLoginType(request.Auth.LoginType)
|
||||
if errResponse != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
||||
|
@ -78,6 +83,11 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
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)
|
||||
|
@ -87,8 +97,11 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
} else {
|
||||
// TODO: Use New Function
|
||||
userDevice = &device.Device{}
|
||||
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)
|
||||
|
@ -99,9 +112,8 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
response := registerResponse{
|
||||
UserId: newUser.Id,
|
||||
// TODO: Create Funktion for Token Generation
|
||||
AccessToken: "TEST",
|
||||
UserId: newUser.Id,
|
||||
AccessToken: userDevice.AccessToken,
|
||||
DeviceId: userDevice.Id,
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
@ -111,6 +123,34 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
@ -128,16 +168,16 @@ func Logout(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func Deactivate(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 {
|
||||
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.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode("Test"); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode("Not Implemented"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -145,14 +185,14 @@ func ChangePassword(w http.ResponseWriter, r *http.Request) {
|
|||
//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.StatusOK)
|
||||
if err := json.NewEncoder(w).Encode("Test"); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
if err := json.NewEncoder(w).Encode("Not Implemented"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func checkAuthData(authData *authentificationData) (errResponse *utils.ErrorResponse) {
|
||||
if authData.LoginType != "m.login.password" {
|
||||
func checkLoginType(loginType string) (errResponse *utils.ErrorResponse) {
|
||||
if loginType != "m.login.password" {
|
||||
errResponse = &utils.ErrorResponse{ErrorCode: "M_FORBIDDEN", ErrorMessage: "Unsupported Auth Type"}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue