Refactor Register Funktion

This commit is contained in:
hoernschen 2020-10-04 14:22:52 +02:00
parent c79d1f86e4
commit 7db9c374cc
5 changed files with 129 additions and 21 deletions

View file

@ -3,5 +3,6 @@ package device
type Device struct { type Device struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
AccessToken string `json:"accessToken,omitempty"`
Keys map[string]*Key `json:"keys,omitempty"` Keys map[string]*Key `json:"keys,omitempty"`
} }

View file

@ -1,5 +1,30 @@
package device package device
func New() (device *Device) { import "nutfactory.org/Matrix/utils"
func New(name string) (err error, device *Device) {
err, id := utils.CreateUUID()
if err != nil {
return
}
err, token := utils.CreateToken()
if err != nil {
return
}
device = &Device{
Id: id,
Name: name,
AccessToken: token,
Keys: make(map[string]*Key),
}
return
}
func (device *Device) RenewAccesToken() (err error) {
err, token := utils.CreateToken()
if err != nil {
return
}
device.AccessToken = token
return return
} }

View file

@ -2,13 +2,18 @@ package user
import ( import (
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
"nutfactory.org/Matrix/entities/device" "nutfactory.org/Matrix/entities/device"
"nutfactory.org/Matrix/utils" "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{ newUser = &User{
Id: id, Id: id,
Name: name, Name: name,
@ -29,7 +34,7 @@ func CheckUsernameAvailability(w http.ResponseWriter, r *http.Request) {
func Register(w http.ResponseWriter, r *http.Request) { func Register(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
request := registerRequest{} request := registerRequest{}
errResponse := utils.CheckRequest(r, request) errResponse := utils.CheckRequest(r)
if errResponse != nil { if errResponse != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode(errResponse); err != nil { if err := json.NewEncoder(w).Encode(errResponse); err != nil {
@ -46,7 +51,7 @@ func Register(w http.ResponseWriter, r *http.Request) {
} }
return return
} }
errResponse = checkAuthData(&request.Auth) errResponse = checkLoginType(request.Auth.LoginType)
if errResponse != nil { if errResponse != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode(errResponse); err != nil { 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) userDevice, err := device.ReadDevice(request.DeviceId)
if userDevice != nil { if userDevice != nil {
err = userDevice.RenewAccesToken()
if err != nil {
log.Fatalf("Unable to renew AccesToken: %s", err)
return
}
err = device.UpdateDevice(userDevice, newUser.Id) err = device.UpdateDevice(userDevice, newUser.Id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -87,8 +97,11 @@ func Register(w http.ResponseWriter, r *http.Request) {
return return
} }
} else { } else {
// TODO: Use New Function err, userDevice = device.New(request.DeviceName)
userDevice = &device.Device{} if err != nil {
log.Fatalf("Unable to create device: %s", err)
return
}
err = device.CreateDevice(userDevice, newUser.Id) err = device.CreateDevice(userDevice, newUser.Id)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -100,8 +113,7 @@ func Register(w http.ResponseWriter, r *http.Request) {
} }
response := registerResponse{ response := registerResponse{
UserId: newUser.Id, UserId: newUser.Id,
// TODO: Create Funktion for Token Generation AccessToken: userDevice.AccessToken,
AccessToken: "TEST",
DeviceId: userDevice.Id, DeviceId: userDevice.Id,
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@ -111,6 +123,34 @@ func Register(w http.ResponseWriter, r *http.Request) {
} }
func Login(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.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode("Test"); err != nil { 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) { func Deactivate(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode("Test"); err != nil { if err := json.NewEncoder(w).Encode("Not Implemented"); err != nil {
panic(err) panic(err)
} }
} }
func ChangePassword(w http.ResponseWriter, r *http.Request) { func ChangePassword(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode("Test"); err != nil { if err := json.NewEncoder(w).Encode("Not Implemented"); err != nil {
panic(err) panic(err)
} }
} }
@ -145,14 +185,14 @@ func ChangePassword(w http.ResponseWriter, r *http.Request) {
//TODO: Check if necessary //TODO: Check if necessary
func Sync(w http.ResponseWriter, r *http.Request) { func Sync(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode("Test"); err != nil { if err := json.NewEncoder(w).Encode("Not Implemented"); err != nil {
panic(err) panic(err)
} }
} }
func checkAuthData(authData *authentificationData) (errResponse *utils.ErrorResponse) { func checkLoginType(loginType string) (errResponse *utils.ErrorResponse) {
if authData.LoginType != "m.login.password" { if loginType != "m.login.password" {
errResponse = &utils.ErrorResponse{ErrorCode: "M_FORBIDDEN", ErrorMessage: "Unsupported Auth Type"} errResponse = &utils.ErrorResponse{ErrorCode: "M_FORBIDDEN", ErrorMessage: "Unsupported Auth Type"}
return return
} }

View file

@ -0,0 +1,42 @@
package utils
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
)
func CreateToken() (err error, token string) {
b := make([]byte, 8)
_, err = rand.Read(b)
if err != nil {
log.Fatal(err)
return
}
token = string(b)
return
}
func CreateUUID() (err error, uuid string) {
b := make([]byte, 16)
_, err = rand.Read(b)
if err != nil {
log.Fatal(err)
return
}
uuid = fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
func Hash(s []byte) (err error, hashString string) {
h := sha256.New()
_, err = h.Write(s)
if nil != err {
return
}
hash := h.Sum(nil)
hashString = base64.StdEncoding.EncodeToString(hash)
return
}

View file

@ -12,7 +12,7 @@ type ErrorResponse struct {
RetryTime int `json:"retry_after_ms,omitempty"` RetryTime int `json:"retry_after_ms,omitempty"`
} }
func CheckRequest(r *http.Request, request interface{}) (response *ErrorResponse) { func CheckRequest(r *http.Request) (response *ErrorResponse) {
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") { if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
response = &ErrorResponse{ErrorMessage: "Content Type not JSON"} response = &ErrorResponse{ErrorMessage: "Content Type not JSON"}
} }