mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-14 09:33:09 -06:00
refactoring
This commit is contained in:
parent
80ceb18c78
commit
6205ffb8c0
|
|
@ -25,7 +25,14 @@ import (
|
||||||
"github.com/matrix-org/dendrite/userapi/api"
|
"github.com/matrix-org/dendrite/userapi/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AdminCreateNewToken(req *http.Request) util.JSONResponse {
|
func AdminCreateNewRegistrationToken(req *http.Request, cfg *config.ClientAPI, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
||||||
|
if !cfg.RegistrationRequiresToken {
|
||||||
|
return util.MatrixErrorResponse(
|
||||||
|
http.StatusForbidden,
|
||||||
|
string(spec.ErrorForbidden),
|
||||||
|
"Registration via tokens is not enabled on this homeserver",
|
||||||
|
)
|
||||||
|
}
|
||||||
request := struct {
|
request := struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
UsesAllowed int32 `json:"uses_allowed"`
|
UsesAllowed int32 `json:"uses_allowed"`
|
||||||
|
|
@ -34,53 +41,58 @@ func AdminCreateNewToken(req *http.Request) util.JSONResponse {
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
return util.JSONResponse{
|
return util.MatrixErrorResponse(
|
||||||
Code: http.StatusBadRequest,
|
http.StatusBadRequest,
|
||||||
JSON: spec.Unknown("Failed to decode request body: " + err.Error()),
|
string(spec.ErrorBadJSON),
|
||||||
}
|
"Failed to decode request body:",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
token := request.Token
|
token := request.Token
|
||||||
if len(token) > 0 {
|
if len(token) == 0 || len(token) > 64 {
|
||||||
if len(token) > 64 {
|
return util.MatrixErrorResponse(
|
||||||
return util.MatrixErrorResponse(
|
http.StatusBadRequest,
|
||||||
http.StatusBadRequest,
|
string(spec.ErrorInvalidParam),
|
||||||
string(spec.ErrorInvalidParam),
|
"token must not be empty and must not be longer than 64")
|
||||||
"token must not be empty and must not be longer than 64")
|
|
||||||
}
|
|
||||||
is_token_valid, _ := regexp.MatchString("^[[:ascii:][:digit:]_]*$", token)
|
|
||||||
if !is_token_valid {
|
|
||||||
return util.MatrixErrorResponse(
|
|
||||||
http.StatusBadRequest,
|
|
||||||
string(spec.ErrorInvalidParam),
|
|
||||||
"token must consist only of characters matched by the regex [A-Za-z0-9-_]")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
length := request.Length
|
|
||||||
if length > 0 && length <= 64 {
|
|
||||||
return util.MatrixErrorResponse(
|
|
||||||
http.StatusBadRequest,
|
|
||||||
string(spec.ErrorInvalidParam),
|
|
||||||
"length must be greater than zero and not greater than 64")
|
|
||||||
}
|
|
||||||
// TODO: Generate Random Token
|
|
||||||
// token = GenerateRandomToken(length)
|
|
||||||
}
|
}
|
||||||
uses_allowed := request.UsesAllowed
|
isTokenValid, _ := regexp.MatchString("^[[:ascii:][:digit:]_]*$", token)
|
||||||
if uses_allowed < 0 {
|
if !isTokenValid {
|
||||||
|
return util.MatrixErrorResponse(
|
||||||
|
http.StatusBadRequest,
|
||||||
|
string(spec.ErrorInvalidParam),
|
||||||
|
"token must consist only of characters matched by the regex [A-Za-z0-9-_]")
|
||||||
|
}
|
||||||
|
length := request.Length
|
||||||
|
if !(length > 0 && length <= 64) {
|
||||||
|
return util.MatrixErrorResponse(
|
||||||
|
http.StatusBadRequest,
|
||||||
|
string(spec.ErrorInvalidParam),
|
||||||
|
"length must be greater than zero and not greater than 64")
|
||||||
|
}
|
||||||
|
// TODO: Generate Random Token
|
||||||
|
// token = GenerateRandomToken(length)
|
||||||
|
usesAllowed := request.UsesAllowed
|
||||||
|
if usesAllowed < 0 {
|
||||||
return util.MatrixErrorResponse(
|
return util.MatrixErrorResponse(
|
||||||
http.StatusBadRequest,
|
http.StatusBadRequest,
|
||||||
string(spec.ErrorInvalidParam),
|
string(spec.ErrorInvalidParam),
|
||||||
"uses_allowed must be a non-negative integer or null")
|
"uses_allowed must be a non-negative integer or null")
|
||||||
}
|
}
|
||||||
|
|
||||||
expiry_time := request.ExpiryTime
|
expiryTime := request.ExpiryTime
|
||||||
if expiry_time != 0 && expiry_time < time.Now().UnixNano()/int64(time.Millisecond) {
|
if expiryTime != 0 && expiryTime < time.Now().UnixNano()/int64(time.Millisecond) {
|
||||||
return util.MatrixErrorResponse(
|
return util.MatrixErrorResponse(
|
||||||
http.StatusBadRequest,
|
http.StatusBadRequest,
|
||||||
string(spec.ErrorInvalidParam),
|
string(spec.ErrorInvalidParam),
|
||||||
"expiry_time must not be in the past")
|
"expiry_time must not be in the past")
|
||||||
}
|
}
|
||||||
created := CreateToken(token, uses_allowed, expiry_time)
|
created, err := rsAPI.PerformCreateToken(req.Context(), token, usesAllowed, expiryTime)
|
||||||
|
if err != nil {
|
||||||
|
return util.MatrixErrorResponse(
|
||||||
|
http.StatusInternalServerError,
|
||||||
|
string(spec.ErrorUnknown),
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
}
|
||||||
if !created {
|
if !created {
|
||||||
return util.MatrixErrorResponse(
|
return util.MatrixErrorResponse(
|
||||||
http.StatusBadRequest,
|
http.StatusBadRequest,
|
||||||
|
|
@ -91,20 +103,14 @@ func AdminCreateNewToken(req *http.Request) util.JSONResponse {
|
||||||
Code: 200,
|
Code: 200,
|
||||||
JSON: map[string]interface{}{
|
JSON: map[string]interface{}{
|
||||||
"token": token,
|
"token": token,
|
||||||
"uses_allowed": uses_allowed,
|
"uses_allowed": usesAllowed,
|
||||||
"pending": 0,
|
"pending": 0,
|
||||||
"completed": 0,
|
"completed": 0,
|
||||||
"expiry_time": expiry_time,
|
"expiry_time": expiryTime,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateToken(token string, uses_allowed int32, expiryTime int64) bool {
|
|
||||||
// TODO: Implement Create Token -> Inserts token into table registration_tokens.
|
|
||||||
// Returns true if token created, false if token already exists.
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func AdminEvacuateRoom(req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
func AdminEvacuateRoom(req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
|
||||||
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -164,7 +164,7 @@ func Setup(
|
||||||
}
|
}
|
||||||
dendriteAdminRouter.Handle("/admin/registrationTokens/new",
|
dendriteAdminRouter.Handle("/admin/registrationTokens/new",
|
||||||
httputil.MakeAdminAPI("admin_registration_tokens_new", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
httputil.MakeAdminAPI("admin_registration_tokens_new", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
||||||
return AdminCreateNewToken(req)
|
return AdminCreateNewRegistrationToken(req, cfg, rsAPI)
|
||||||
}),
|
}),
|
||||||
).Methods(http.MethodPost, http.MethodOptions)
|
).Methods(http.MethodPost, http.MethodOptions)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -173,6 +173,7 @@ type ClientRoomserverAPI interface {
|
||||||
PerformCreateRoom(ctx context.Context, userID spec.UserID, roomID spec.RoomID, createRequest *PerformCreateRoomRequest) (string, *util.JSONResponse)
|
PerformCreateRoom(ctx context.Context, userID spec.UserID, roomID spec.RoomID, createRequest *PerformCreateRoomRequest) (string, *util.JSONResponse)
|
||||||
// PerformRoomUpgrade upgrades a room to a newer version
|
// PerformRoomUpgrade upgrades a room to a newer version
|
||||||
PerformRoomUpgrade(ctx context.Context, roomID, userID string, roomVersion gomatrixserverlib.RoomVersion) (newRoomID string, err error)
|
PerformRoomUpgrade(ctx context.Context, roomID, userID string, roomVersion gomatrixserverlib.RoomVersion) (newRoomID string, err error)
|
||||||
|
PerformAdminCreateRegistrationToken(ctx context.Context, token string, usesAllowed, pending, completed int32, expiryTime int64) (bool, error)
|
||||||
PerformAdminEvacuateRoom(ctx context.Context, roomID string) (affected []string, err error)
|
PerformAdminEvacuateRoom(ctx context.Context, roomID string) (affected []string, err error)
|
||||||
PerformAdminEvacuateUser(ctx context.Context, userID string) (affected []string, err error)
|
PerformAdminEvacuateUser(ctx context.Context, userID string) (affected []string, err error)
|
||||||
PerformAdminPurgeRoom(ctx context.Context, roomID string) error
|
PerformAdminPurgeRoom(ctx context.Context, roomID string) error
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,15 @@ type Admin struct {
|
||||||
Leaver *Leaver
|
Leaver *Leaver
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Admin) PerformAdminCreateRegistrationToken(
|
||||||
|
ctx context.Context, token string,
|
||||||
|
usesAllowed, pending, completed int32,
|
||||||
|
expiryTime int64) (bool, error) {
|
||||||
|
//TODO: Implement logic to save token in DB.
|
||||||
|
//Return false, if token already exists, else true.
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
// PerformAdminEvacuateRoom will remove all local users from the given room.
|
// PerformAdminEvacuateRoom will remove all local users from the given room.
|
||||||
func (r *Admin) PerformAdminEvacuateRoom(
|
func (r *Admin) PerformAdminEvacuateRoom(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
|
|
|
||||||
|
|
@ -105,10 +105,6 @@ func (c *ClientAPI) Verify(configErrs *ConfigErrors) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.RegistrationDisabled && c.RegistrationRequiresToken {
|
|
||||||
configErrs.Add("registration_requires_token cannot be set to true when registration_disabled is true")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TURN struct {
|
type TURN struct {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue