2022-12-22 10:54:03 +00:00
|
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-12-23 13:11:11 +00:00
|
|
|
"errors"
|
2022-12-22 10:54:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-12-23 13:11:11 +00:00
|
|
|
"regexp"
|
2022-12-22 10:54:03 +00:00
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-12-22 10:54:03 +00:00
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2022-12-23 13:11:11 +00:00
|
|
|
const (
|
|
|
|
maxUsernameLength = 254 // http://matrix.org/speculator/spec/HEAD/intro.html#user-identifiers TODO account for domain
|
2022-12-22 10:54:03 +00:00
|
|
|
|
2022-12-23 13:11:11 +00:00
|
|
|
minPasswordLength = 8 // http://matrix.org/docs/spec/client_server/r0.2.0.html#password-based
|
|
|
|
maxPasswordLength = 512 // https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrPasswordTooLong = fmt.Errorf("password too long: max %d characters", maxPasswordLength)
|
|
|
|
ErrPasswordWeak = fmt.Errorf("password too weak: min %d characters", minPasswordLength)
|
|
|
|
ErrUsernameTooLong = fmt.Errorf("username exceeds the maximum length of %d characters", maxUsernameLength)
|
|
|
|
ErrUsernameInvalid = errors.New("username can only contain characters a-z, 0-9, or '_-./='")
|
|
|
|
ErrUsernameUnderscore = errors.New("username cannot start with a '_'")
|
|
|
|
validUsernameRegex = regexp.MustCompile(`^[0-9a-z_\-=./]+$`)
|
|
|
|
)
|
2022-12-22 10:54:03 +00:00
|
|
|
|
2022-12-23 13:11:11 +00:00
|
|
|
// ValidatePassword returns an error if the password is invalid
|
|
|
|
func ValidatePassword(password string) error {
|
2022-12-22 10:54:03 +00:00
|
|
|
// https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
|
|
|
|
if len(password) > maxPasswordLength {
|
2022-12-23 13:11:11 +00:00
|
|
|
return ErrPasswordTooLong
|
|
|
|
} else if len(password) > 0 && len(password) < minPasswordLength {
|
|
|
|
return ErrPasswordWeak
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PasswordResponse returns a util.JSONResponse for a given error, if any.
|
|
|
|
func PasswordResponse(err error) *util.JSONResponse {
|
|
|
|
switch err {
|
|
|
|
case ErrPasswordWeak:
|
2022-12-22 10:54:03 +00:00
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.WeakPassword(ErrPasswordWeak.Error()),
|
2022-12-22 10:54:03 +00:00
|
|
|
}
|
2022-12-23 13:11:11 +00:00
|
|
|
case ErrPasswordTooLong:
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.BadJSON(ErrPasswordTooLong.Error()),
|
2022-12-23 13:11:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateUsername returns an error if the username is invalid
|
2023-04-19 15:50:33 +01:00
|
|
|
func ValidateUsername(localpart string, domain spec.ServerName) error {
|
2022-12-23 13:11:11 +00:00
|
|
|
// https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
|
|
|
|
if id := fmt.Sprintf("@%s:%s", localpart, domain); len(id) > maxUsernameLength {
|
|
|
|
return ErrUsernameTooLong
|
|
|
|
} else if !validUsernameRegex.MatchString(localpart) {
|
|
|
|
return ErrUsernameInvalid
|
|
|
|
} else if localpart[0] == '_' { // Regex checks its not a zero length string
|
|
|
|
return ErrUsernameUnderscore
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UsernameResponse returns a util.JSONResponse for the given error, if any.
|
|
|
|
func UsernameResponse(err error) *util.JSONResponse {
|
|
|
|
switch err {
|
|
|
|
case ErrUsernameTooLong:
|
2022-12-22 10:54:03 +00:00
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.BadJSON(err.Error()),
|
2022-12-22 10:54:03 +00:00
|
|
|
}
|
2022-12-23 13:11:11 +00:00
|
|
|
case ErrUsernameInvalid, ErrUsernameUnderscore:
|
|
|
|
return &util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 23:46:49 +01:00
|
|
|
JSON: spec.InvalidUsername(err.Error()),
|
2022-12-23 13:11:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateApplicationServiceUsername returns an error if the username is invalid for an application service
|
2023-04-19 15:50:33 +01:00
|
|
|
func ValidateApplicationServiceUsername(localpart string, domain spec.ServerName) error {
|
2022-12-23 13:11:11 +00:00
|
|
|
if id := fmt.Sprintf("@%s:%s", localpart, domain); len(id) > maxUsernameLength {
|
|
|
|
return ErrUsernameTooLong
|
|
|
|
} else if !validUsernameRegex.MatchString(localpart) {
|
|
|
|
return ErrUsernameInvalid
|
2022-12-22 10:54:03 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|