mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-15 18:13:09 -06:00
addressed review comments
This commit is contained in:
parent
09904290cb
commit
c5bb8f6578
|
|
@ -5,11 +5,9 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
@ -64,13 +62,13 @@ func AdminCreateNewRegistrationToken(req *http.Request, cfg *config.ClientAPI, u
|
||||||
length = 16
|
length = 16
|
||||||
}
|
}
|
||||||
// token not present in request body. Hence, generate a random token.
|
// token not present in request body. Hence, generate a random token.
|
||||||
if !(length > 0 && length <= 64) {
|
if length <= 0 || length > 64 {
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
Code: http.StatusBadRequest,
|
Code: http.StatusBadRequest,
|
||||||
JSON: spec.BadJSON("length must be greater than zero and not greater than 64"),
|
JSON: spec.BadJSON("length must be greater than zero and not greater than 64"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
token = generateRandomToken(int(length))
|
token = util.RandomString(int(length))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(token) > 64 {
|
if len(token) > 64 {
|
||||||
|
|
@ -114,18 +112,20 @@ func AdminCreateNewRegistrationToken(req *http.Request, cfg *config.ClientAPI, u
|
||||||
ExpiryTime: &expiryTime,
|
ExpiryTime: &expiryTime,
|
||||||
}
|
}
|
||||||
created, err := userAPI.PerformAdminCreateRegistrationToken(req.Context(), registrationToken)
|
created, err := userAPI.PerformAdminCreateRegistrationToken(req.Context(), registrationToken)
|
||||||
|
if !created {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusConflict,
|
||||||
|
JSON: map[string]string{
|
||||||
|
"error": fmt.Sprintf("token: %s already exists", token),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
Code: http.StatusInternalServerError,
|
Code: http.StatusInternalServerError,
|
||||||
JSON: err,
|
JSON: err,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !created {
|
|
||||||
return util.JSONResponse{
|
|
||||||
Code: http.StatusConflict,
|
|
||||||
JSON: fmt.Sprintf("Token already exists: %s", token),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
Code: 200,
|
Code: 200,
|
||||||
JSON: map[string]interface{}{
|
JSON: map[string]interface{}{
|
||||||
|
|
@ -138,17 +138,6 @@ func AdminCreateNewRegistrationToken(req *http.Request, cfg *config.ClientAPI, u
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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{} {
|
func getReturnValueForUsesAllowed(usesAllowed int32) interface{} {
|
||||||
if usesAllowed == 0 {
|
if usesAllowed == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -222,8 +211,8 @@ func AdminDeleteRegistrationToken(req *http.Request, cfg *config.ClientAPI, user
|
||||||
err = userAPI.PerformAdminDeleteRegistrationToken(req.Context(), tokenText)
|
err = userAPI.PerformAdminDeleteRegistrationToken(req.Context(), tokenText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
Code: http.StatusNotFound,
|
Code: http.StatusInternalServerError,
|
||||||
JSON: spec.NotFound(fmt.Sprintf("token: %s not found", tokenText)),
|
JSON: err,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue