2020-09-29 07:44:35 +00:00
|
|
|
package user
|
|
|
|
|
2020-10-03 12:22:29 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-10-11 21:11:30 +00:00
|
|
|
"fmt"
|
2020-10-03 12:22:29 +00:00
|
|
|
"net/http"
|
|
|
|
|
2020-10-12 14:16:28 +00:00
|
|
|
"git.nutfactory.org/hoernschen/Matrix/config"
|
|
|
|
"git.nutfactory.org/hoernschen/Matrix/entities/device"
|
|
|
|
"git.nutfactory.org/hoernschen/Matrix/utils"
|
2020-10-03 12:22:29 +00:00
|
|
|
)
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
func New(username string, name string, password string) (err error, newUser *User) {
|
2020-10-04 12:22:52 +00:00
|
|
|
err, hashedPassword := utils.Hash([]byte(password))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
id := generateUserId(username)
|
2020-10-03 12:22:29 +00:00
|
|
|
newUser = &User{
|
|
|
|
Id: id,
|
|
|
|
Name: name,
|
2020-10-11 21:11:30 +00:00
|
|
|
Password: hashedPassword,
|
|
|
|
Devices: make(map[string]*device.Device),
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
func CheckUsernameAvailabilityHandler(w http.ResponseWriter, r *http.Request) {
|
2020-10-03 12:22:29 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
2020-10-11 21:11:30 +00:00
|
|
|
request := availableRequest{}
|
|
|
|
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: fmt.Sprintf("Could not parse JSON: %s", err)}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userId := generateUserId(request.Username)
|
|
|
|
foundUser, err := ReadUser(userId)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Database Error: %s", err)}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2020-10-03 12:22:29 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-10-11 21:11:30 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(availableResponse{Available: true}); err != nil {
|
2020-10-03 12:22:29 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
|
2020-10-03 12:22:29 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
2020-10-12 14:16:28 +00:00
|
|
|
request := RegisterRequest{}
|
2020-10-04 12:22:52 +00:00
|
|
|
errResponse := utils.CheckRequest(r)
|
2020-10-03 12:22:29 +00:00
|
|
|
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)
|
2020-10-11 21:11:30 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Could not parse JSON: %s", err)}); err != nil {
|
2020-10-03 12:22:29 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-10-04 12:22:52 +00:00
|
|
|
errResponse = checkLoginType(request.Auth.LoginType)
|
2020-10-03 12:22:29 +00:00
|
|
|
if errResponse != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
err, newUser := New(request.Username, request.Username, request.Password)
|
2020-10-03 12:22:29 +00:00
|
|
|
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)
|
2020-10-11 21:11:30 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Database Error: %s", err)}); err != nil {
|
2020-10-03 12:22:29 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
userDevice, errResponse := createUserDevice(request.DeviceId, request.DeviceName, newUser.Id)
|
|
|
|
if errResponse != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
|
|
|
panic(err)
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
return
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
2020-10-12 14:16:28 +00:00
|
|
|
response := RegisterResponse{
|
2020-10-04 12:22:52 +00:00
|
|
|
UserId: newUser.Id,
|
|
|
|
AccessToken: userDevice.AccessToken,
|
2020-10-03 12:22:29 +00:00
|
|
|
DeviceId: userDevice.Id,
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
2020-10-04 12:22:52 +00:00
|
|
|
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)
|
2020-10-11 21:11:30 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Could not parse JSON: %s", err)}); err != nil {
|
2020-10-04 12:22:52 +00:00
|
|
|
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
|
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
if request.Identifier.IdentifierType != "m.id.user" && request.Identifier.User == "" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: "Username missing"}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
userId := generateUserId(request.Identifier.User)
|
|
|
|
foundUser, err := ReadUser(userId)
|
|
|
|
if err != nil || foundUser == nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorCode: "M_FORBIDDEN"}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err, hashedPassword := utils.Hash([]byte(request.Password))
|
|
|
|
if err != nil || foundUser.Password != hashedPassword {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorCode: "M_FORBIDDEN"}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userDevice, errResponse := createUserDevice(request.DeviceId, request.DeviceName, request.Identifier.User)
|
|
|
|
if errResponse != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
response := loginResponse{
|
|
|
|
UserId: foundUser.Id,
|
|
|
|
AccessToken: userDevice.AccessToken,
|
|
|
|
DeviceId: userDevice.Id,
|
|
|
|
}
|
|
|
|
response.DiscoveryInfo.Homeserver.BaseUrl = config.Homeserver
|
2020-10-03 12:22:29 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-10-11 21:11:30 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
2020-10-03 12:22:29 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
2020-10-03 12:22:29 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
2020-10-11 21:11:30 +00:00
|
|
|
errResponse := utils.CheckRequest(r)
|
|
|
|
if errResponse != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
accessToken, errResponse := utils.GetAccessToken(r)
|
|
|
|
if errResponse != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
foundDevice, err := device.ReadDeviceFromAccessToken(accessToken)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Database Error: %s", err)}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
foundDevice.AccessToken = ""
|
|
|
|
err = device.UpdateDevice(foundDevice)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Database Error: %s", err)}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-10-03 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 12:22:52 +00:00
|
|
|
func checkLoginType(loginType string) (errResponse *utils.ErrorResponse) {
|
|
|
|
if loginType != "m.login.password" {
|
2020-10-11 21:11:30 +00:00
|
|
|
errResponse = &utils.ErrorResponse{ErrorCode: "M_UNKNOWN", ErrorMessage: "Bad login type."}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateUserId(username string) string {
|
|
|
|
return fmt.Sprintf("@%s:%s", username, config.Homeserver)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createUserDevice(id string, name string, userId string) (userDevice *device.Device, errResponse *utils.ErrorResponse) {
|
|
|
|
userDevice, err := device.ReadDevice(id)
|
|
|
|
if err != nil {
|
|
|
|
errResponse = &utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Database Error: %s", err)}
|
2020-10-03 12:22:29 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-11 21:11:30 +00:00
|
|
|
if userDevice != nil {
|
|
|
|
err = userDevice.RenewAccesToken()
|
|
|
|
if name != "" {
|
|
|
|
userDevice.Name = name
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
errResponse = &utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Unable to renew AccesToken: %s", err)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = device.UpdateDevice(userDevice)
|
|
|
|
if err != nil {
|
|
|
|
errResponse = &utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Database Error: %s", err)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err, userDevice = device.New(name)
|
|
|
|
if err != nil {
|
|
|
|
errResponse = &utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Unable to create device: %s", err)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = device.CreateDevice(userDevice, userId)
|
|
|
|
if err != nil {
|
|
|
|
errResponse = &utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Database Error: %s", err)}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 15:45:57 +00:00
|
|
|
return
|
|
|
|
}
|