mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-27 07:28:27 +00:00
optimize imports, variable names and re-order functions
Signed-off-by: mohit kumar singh <mohitkumarsingh907@gmail.com>
This commit is contained in:
parent
50783a18ce
commit
b020ac80da
2 changed files with 27 additions and 48 deletions
|
@ -25,9 +25,6 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/common/config"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth"
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
||||||
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
|
@ -36,6 +33,7 @@ import (
|
||||||
"github.com/matrix-org/dendrite/clientapi/httputil"
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
||||||
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
"github.com/matrix-org/dendrite/clientapi/userutil"
|
"github.com/matrix-org/dendrite/clientapi/userutil"
|
||||||
|
"github.com/matrix-org/dendrite/common/config"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -109,18 +107,16 @@ type registerResponse struct {
|
||||||
DeviceID string `json:"device_id"`
|
DeviceID string `json:"device_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// recaptchaResponse represents the HTTP response from a Google Recaptcha server
|
|
||||||
type recaptchaResponse struct {
|
|
||||||
Success bool `json:"success"`
|
|
||||||
ChallengeTS time.Time `json:"challenge_ts"`
|
|
||||||
Hostname string `json:"hostname"`
|
|
||||||
ErrorCodes []int `json:"error-codes"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// validateUsername returns an error response if the username is invalid
|
// validateUsername returns an error response if the username is invalid
|
||||||
func validateUsername(username string) *util.JSONResponse {
|
func validateUsername(username string) *util.JSONResponse {
|
||||||
// https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
|
// https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
|
||||||
if len(username) > maxUsernameLength {
|
// Don't allow numeric usernames less than MAX_INT64.
|
||||||
|
if _, err := strconv.ParseInt(username, 10, 64); err == nil {
|
||||||
|
return &util.JSONResponse{
|
||||||
|
Code: http.StatusBadRequest,
|
||||||
|
JSON: jsonerror.InvalidUsername("Numeric user IDs are reserved"),
|
||||||
|
}
|
||||||
|
} else if len(username) > maxUsernameLength {
|
||||||
return &util.JSONResponse{
|
return &util.JSONResponse{
|
||||||
Code: http.StatusBadRequest,
|
Code: http.StatusBadRequest,
|
||||||
JSON: jsonerror.BadJSON(fmt.Sprintf("'username' >%d characters", maxUsernameLength)),
|
JSON: jsonerror.BadJSON(fmt.Sprintf("'username' >%d characters", maxUsernameLength)),
|
||||||
|
@ -173,15 +169,13 @@ func validatePassword(password string) *util.JSONResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateCredentials returns error if username or password is invalid
|
// validateCredentials returns error if username or password is invalid
|
||||||
func validateCredentials(username, password string) (jsonerr *util.JSONResponse) {
|
func validateCredentials(username, password string) *util.JSONResponse {
|
||||||
jsonerr = validateUserName(username)
|
if err := validateUsername(username); err != nil {
|
||||||
if jsonerr != nil {
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonerr = validatePassword(password)
|
if err := validatePassword(password); err != nil {
|
||||||
if jsonerr != nil {
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -317,23 +311,6 @@ func Register(
|
||||||
sessionID = util.RandomString(sessionIDLength)
|
sessionID = util.RandomString(sessionIDLength)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't allow numeric usernames less than MAX_INT64.
|
|
||||||
if _, err := strconv.ParseInt(r.Username, 10, 64); err == nil {
|
|
||||||
return util.JSONResponse{
|
|
||||||
Code: http.StatusBadRequest,
|
|
||||||
JSON: jsonerror.InvalidUsername("Numeric user IDs are reserved"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Auto generate a numeric username if r.Username is empty
|
|
||||||
if r.Username == "" {
|
|
||||||
id, err := accountDB.GetNewNumericLocalpart(req.Context())
|
|
||||||
if err != nil {
|
|
||||||
return httputil.LogThenError(req, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
r.Username = strconv.FormatInt(id, 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no auth type is specified by the client, send back the list of available flows
|
// If no auth type is specified by the client, send back the list of available flows
|
||||||
if r.Auth.Type == "" {
|
if r.Auth.Type == "" {
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
|
@ -350,6 +327,16 @@ func Register(
|
||||||
return *resErr
|
return *resErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Auto generate a numeric username if r.Username is empty
|
||||||
|
if r.Username == "" {
|
||||||
|
id, err := accountDB.GetNewNumericLocalpart(req.Context())
|
||||||
|
if err != nil {
|
||||||
|
return httputil.LogThenError(req, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Username = strconv.FormatInt(id, 10)
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure normal user isn't registering under an exclusive application
|
// Make sure normal user isn't registering under an exclusive application
|
||||||
// service namespace. Skip this check if no app services are registered.
|
// service namespace. Skip this check if no app services are registered.
|
||||||
if r.Auth.Type != "m.login.application_service" &&
|
if r.Auth.Type != "m.login.application_service" &&
|
||||||
|
@ -394,7 +381,7 @@ func handleRegistrationFlow(
|
||||||
return util.MessageResponse(http.StatusForbidden, "Registration has been disabled")
|
return util.MessageResponse(http.StatusForbidden, "Registration has been disabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonRes *util.JSONResponse
|
var jsonError *util.JSONResponse
|
||||||
switch r.Auth.Type {
|
switch r.Auth.Type {
|
||||||
case authtypes.LoginTypeSharedSecret:
|
case authtypes.LoginTypeSharedSecret:
|
||||||
// Check shared secret against config
|
// Check shared secret against config
|
||||||
|
@ -424,8 +411,8 @@ func handleRegistrationFlow(
|
||||||
r.Username, "", appserviceID, r.InitialDisplayName)
|
r.Username, "", appserviceID, r.InitialDisplayName)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
jsonRes = HandleUserInteractiveFlow(req, r.UserInteractiveFlowRequest, sessionID, cfg, cfg.Derived.Registration)
|
jsonError = HandleUserInteractiveFlow(req, r.UserInteractiveFlowRequest, sessionID, cfg, cfg.Derived.Registration)
|
||||||
if jsonRes == nil {
|
if jsonError == nil {
|
||||||
return completeRegistration(
|
return completeRegistration(
|
||||||
req.Context(),
|
req.Context(),
|
||||||
accountDB,
|
accountDB,
|
||||||
|
@ -436,7 +423,7 @@ func handleRegistrationFlow(
|
||||||
r.InitialDisplayName)
|
r.InitialDisplayName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return *jsonRes
|
return *jsonError
|
||||||
}
|
}
|
||||||
|
|
||||||
// LegacyRegister process register requests from the legacy v1 API
|
// LegacyRegister process register requests from the legacy v1 API
|
||||||
|
|
|
@ -182,14 +182,6 @@ func HandleUserInteractiveFlow(
|
||||||
// TODO: email / msisdn auth types.
|
// TODO: email / msisdn auth types.
|
||||||
|
|
||||||
switch r.Auth.Type {
|
switch r.Auth.Type {
|
||||||
case "":
|
|
||||||
// If no auth type is specified by the client, send back the list of available flows
|
|
||||||
return &util.JSONResponse{
|
|
||||||
Code: http.StatusUnauthorized,
|
|
||||||
JSON: newUserInteractiveResponse(sessionID,
|
|
||||||
res.Flows, res.Params),
|
|
||||||
}
|
|
||||||
|
|
||||||
case authtypes.LoginTypeRecaptcha:
|
case authtypes.LoginTypeRecaptcha:
|
||||||
// Check given captcha response
|
// Check given captcha response
|
||||||
resErr := validateRecaptcha(cfg, r.Auth.Response, req.RemoteAddr)
|
resErr := validateRecaptcha(cfg, r.Auth.Response, req.RemoteAddr)
|
||||||
|
|
Loading…
Reference in a new issue