handle cases when request field is not present

This commit is contained in:
santhoshivan23 2023-06-06 22:21:21 +05:30
parent 8972c4b20d
commit 99fa964b62
2 changed files with 42 additions and 14 deletions

View file

@ -28,17 +28,6 @@ import (
userapi "github.com/matrix-org/dendrite/userapi/api"
)
func generateRandomToken(length int) string {
allowedChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
rand.Seed(time.Now().UnixNano())
var sb strings.Builder
for i := 0; i < length; i++ {
randomIndex := rand.Intn(len(allowedChars))
sb.WriteByte(allowedChars[randomIndex])
}
return sb.String()
}
func AdminCreateNewRegistrationToken(req *http.Request, cfg *config.ClientAPI, userAPI userapi.ClientUserAPI) util.JSONResponse {
if !cfg.RegistrationRequiresToken {
return util.MatrixErrorResponse(
@ -133,14 +122,39 @@ func AdminCreateNewRegistrationToken(req *http.Request, cfg *config.ClientAPI, u
Code: 200,
JSON: map[string]interface{}{
"token": token,
"uses_allowed": usesAllowed,
"uses_allowed": getReturnValueForUsesAllowed(usesAllowed),
"pending": pending,
"completed": completed,
"expiry_time": expiryTime,
"expiry_time": getReturnValueExpiryTime(expiryTime),
},
}
}
func generateRandomToken(length int) string {
allowedChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
rand.Seed(time.Now().UnixNano())
var sb strings.Builder
for i := 0; i < length; i++ {
randomIndex := rand.Intn(len(allowedChars))
sb.WriteByte(allowedChars[randomIndex])
}
return sb.String()
}
func getReturnValueForUsesAllowed(usesAllowed int32) interface{} {
if usesAllowed == 0 {
return nil
}
return usesAllowed
}
func getReturnValueExpiryTime(expiryTime int64) interface{} {
if expiryTime == 0 {
return nil
}
return expiryTime
}
func AdminEvacuateRoom(req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI) util.JSONResponse {
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
if err != nil {

View file

@ -58,9 +58,23 @@ func (s *registrationTokenStatements) InsertRegistrationToken(ctx context.Contex
stmt := sqlutil.TxStmt(tx, s.insertTokenStatment)
pending := 0
completed := 0
_, err := stmt.ExecContext(ctx, token, nil, expiryTime, pending, completed)
_, err := stmt.ExecContext(ctx, token, nullIfZeroInt32(usesAllowed), nullIfZero(expiryTime), pending, completed)
if err != nil {
return false, err
}
return true, nil
}
func nullIfZero(value int64) interface{} {
if value == 0 {
return nil
}
return value
}
func nullIfZeroInt32(value int32) interface{} {
if value == 0 {
return nil
}
return value
}