validateUsername returns an error response if the username is invalid
(username string)
| 277 | |
| 278 | // validateUsername returns an error response if the username is invalid |
| 279 | func validateUsername(username string) *util.JSONResponse { |
| 280 | // https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161 |
| 281 | if len(username) > maxUsernameLength { |
| 282 | return &util.JSONResponse{ |
| 283 | Code: http.StatusBadRequest, |
| 284 | JSON: jsonerror.BadJSON(fmt.Sprintf("'username' >%d characters", maxUsernameLength)), |
| 285 | } |
| 286 | } else if !validUsernameRegex.MatchString(username) { |
| 287 | return &util.JSONResponse{ |
| 288 | Code: http.StatusBadRequest, |
| 289 | JSON: jsonerror.InvalidUsername("Username can only contain characters a-z, 0-9, or '_-./='"), |
| 290 | } |
| 291 | } else if username[0] == '_' { // Regex checks its not a zero length string |
| 292 | return &util.JSONResponse{ |
| 293 | Code: http.StatusBadRequest, |
| 294 | JSON: jsonerror.InvalidUsername("Username cannot start with a '_'"), |
| 295 | } |
| 296 | } |
| 297 | return nil |
| 298 | } |
| 299 | |
| 300 | // validateApplicationServiceUsername returns an error response if the username is invalid for an application service |
| 301 | func validateApplicationServiceUsername(username string) *util.JSONResponse { |
no outgoing calls
no test coverage detected