In-line user ID parsing code for now

This commit is contained in:
Kegan Dougal 2017-03-08 11:23:14 +00:00
parent da01bd3ce4
commit 94c97ed677
2 changed files with 16 additions and 29 deletions

View file

@ -2,37 +2,12 @@ package common
import (
"encoding/json"
"fmt"
"net/http"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"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
// there was a problem unmarshalling. Calling this function consumes the request body.
func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse {

View file

@ -35,10 +35,22 @@ func (r createRoomRequest) Validate() *util.JSONResponse {
}
}
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{
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"),
}
}
}