refactoring

This commit is contained in:
santhoshivan23 2023-06-04 22:08:21 +05:30
parent 80ceb18c78
commit 6205ffb8c0
5 changed files with 59 additions and 47 deletions

View file

@ -25,7 +25,14 @@ import (
"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 {
Token string `json:"token"`
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 {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: spec.Unknown("Failed to decode request body: " + err.Error()),
}
return util.MatrixErrorResponse(
http.StatusBadRequest,
string(spec.ErrorBadJSON),
"Failed to decode request body:",
)
}
token := request.Token
if len(token) > 0 {
if len(token) > 64 {
return util.MatrixErrorResponse(
http.StatusBadRequest,
string(spec.ErrorInvalidParam),
"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)
if len(token) == 0 || len(token) > 64 {
return util.MatrixErrorResponse(
http.StatusBadRequest,
string(spec.ErrorInvalidParam),
"token must not be empty and must not be longer than 64")
}
uses_allowed := request.UsesAllowed
if uses_allowed < 0 {
isTokenValid, _ := regexp.MatchString("^[[:ascii:][:digit:]_]*$", token)
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(
http.StatusBadRequest,
string(spec.ErrorInvalidParam),
"uses_allowed must be a non-negative integer or null")
}
expiry_time := request.ExpiryTime
if expiry_time != 0 && expiry_time < time.Now().UnixNano()/int64(time.Millisecond) {
expiryTime := request.ExpiryTime
if expiryTime != 0 && expiryTime < time.Now().UnixNano()/int64(time.Millisecond) {
return util.MatrixErrorResponse(
http.StatusBadRequest,
string(spec.ErrorInvalidParam),
"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 {
return util.MatrixErrorResponse(
http.StatusBadRequest,
@ -91,20 +103,14 @@ func AdminCreateNewToken(req *http.Request) util.JSONResponse {
Code: 200,
JSON: map[string]interface{}{
"token": token,
"uses_allowed": uses_allowed,
"uses_allowed": usesAllowed,
"pending": 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 {
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
if err != nil {

View file

@ -164,7 +164,7 @@ func Setup(
}
dendriteAdminRouter.Handle("/admin/registrationTokens/new",
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)

View file

@ -173,6 +173,7 @@ type ClientRoomserverAPI interface {
PerformCreateRoom(ctx context.Context, userID spec.UserID, roomID spec.RoomID, createRequest *PerformCreateRoomRequest) (string, *util.JSONResponse)
// PerformRoomUpgrade upgrades a room to a newer version
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)
PerformAdminEvacuateUser(ctx context.Context, userID string) (affected []string, err error)
PerformAdminPurgeRoom(ctx context.Context, roomID string) error

View file

@ -42,6 +42,15 @@ type Admin struct {
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.
func (r *Admin) PerformAdminEvacuateRoom(
ctx context.Context,

View file

@ -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 {