mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 23:13:11 -06:00
In-line user ID parsing code for now
This commit is contained in:
parent
da01bd3ce4
commit
94c97ed677
|
|
@ -2,37 +2,12 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"net/http"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserID represents a parsed User ID string
|
|
||||||
type UserID struct {
|
|
||||||
Domain string
|
|
||||||
Localpart string
|
|
||||||
}
|
|
||||||
|
|
||||||
// UserIDFromString creates a UserID from an input string. Returns an error if
|
|
||||||
// the string is not a valid user ID.
|
|
||||||
func UserIDFromString(id string) (uid UserID, err error) {
|
|
||||||
// https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/types.py#L92
|
|
||||||
if len(id) == 0 || id[0] != '@' {
|
|
||||||
err = fmt.Errorf("user id must start with '@'")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
parts := strings.SplitN(id[1:], ":", 2)
|
|
||||||
if len(parts) != 2 {
|
|
||||||
err = fmt.Errorf("user id must be in the form @localpart:domain")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
uid.Localpart = parts[0]
|
|
||||||
uid.Domain = parts[1]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSONRequest into the given interface pointer. Returns an error JSON response if
|
// UnmarshalJSONRequest into the given interface pointer. Returns an error JSON response if
|
||||||
// there was a problem unmarshalling. Calling this function consumes the request body.
|
// there was a problem unmarshalling. Calling this function consumes the request body.
|
||||||
func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse {
|
func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse {
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,22 @@ func (r createRoomRequest) Validate() *util.JSONResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, userID := range r.Invite {
|
for _, userID := range r.Invite {
|
||||||
if _, err := common.UserIDFromString(userID); err != nil {
|
// TODO: We should put user ID parsing code into gomatrixserverlib and use that instead
|
||||||
|
// (see https://github.com/matrix-org/gomatrixserverlib/blob/3394e7c7003312043208aa73727d2256eea3d1f6/eventcontent.go#L347 )
|
||||||
|
// It should be a struct (with pointers into a single string to avoid copying) and
|
||||||
|
// we should update all refs to use UserID types rather than strings.
|
||||||
|
// https://github.com/matrix-org/synapse/blob/v0.19.2/synapse/types.py#L92
|
||||||
|
if len(userID) == 0 || userID[0] != '@' {
|
||||||
return &util.JSONResponse{
|
return &util.JSONResponse{
|
||||||
Code: 400,
|
Code: 400,
|
||||||
JSON: jsonerror.BadJSON("Entries in 'invite' must be valid user IDs"),
|
JSON: jsonerror.BadJSON("user id must start with '@'"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parts := strings.SplitN(userID[1:], ":", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return &util.JSONResponse{
|
||||||
|
Code: 400,
|
||||||
|
JSON: jsonerror.BadJSON("user id must be in the form @localpart:domain"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue