Refactor Register Funktion
This commit is contained in:
parent
c79d1f86e4
commit
7db9c374cc
5 changed files with 129 additions and 21 deletions
|
@ -3,5 +3,6 @@ package device
|
|||
type Device struct {
|
||||
Id string `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
AccessToken string `json:"accessToken,omitempty"`
|
||||
Keys map[string]*Key `json:"keys,omitempty"`
|
||||
}
|
||||
|
|
|
@ -1,5 +1,30 @@
|
|||
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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
@ -100,8 +113,7 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
response := registerResponse{
|
||||
UserId: newUser.Id,
|
||||
// TODO: Create Funktion for Token Generation
|
||||
AccessToken: "TEST",
|
||||
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
|
||||
}
|
||||
|
|
42
utils/encryptionService.go
Normal file
42
utils/encryptionService.go
Normal 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
|
||||
}
|
|
@ -12,7 +12,7 @@ type ErrorResponse struct {
|
|||
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") {
|
||||
response = &ErrorResponse{ErrorMessage: "Content Type not JSON"}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue