From 69c29172c39f65df51c959c6a8cbebdaf006a735 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Fri, 7 Jul 2017 14:11:32 +0100 Subject: [PATCH 1/2] Use utility methods from gomatrixserverlib. (#152) * Use utility methods from gomatrixserverlib, rather than reimplementing them * Return string rather than pointer to string * Update gomatrixserverlib --- .../clientapi/producers/roomserver.go | 14 +---- .../dendrite/clientapi/readers/directory.go | 22 +------ .../dendrite/clientapi/writers/createroom.go | 9 +-- .../dendrite/clientapi/writers/joinroom.go | 25 ++------ .../dendrite/federationapi/writers/send.go | 6 +- .../federationsender/consumers/roomserver.go | 27 ++------- .../dendrite/mediaapi/writers/upload.go | 11 +--- .../dendrite/syncapi/storage/syncserver.go | 14 ++--- .../dendrite/syncapi/sync/notifier.go | 8 +-- vendor/manifest | 2 +- .../matrix-org/gomatrixserverlib/event.go | 57 ++++++++++++++++++- .../matrix-org/gomatrixserverlib/eventauth.go | 3 + .../gomatrixserverlib/federationtypes.go | 2 +- .../gomatrixserverlib/hooks/pre-commit | 17 +++++- .../matrix-org/gomatrixserverlib/json.go | 4 +- .../matrix-org/gomatrixserverlib/keys.go | 2 +- .../matrix-org/gomatrixserverlib/request.go | 2 +- .../matrix-org/gomatrixserverlib/resolve.go | 4 +- .../gomatrixserverlib/signing_test.go | 4 +- .../gomatrixserverlib/stateresolution_test.go | 12 ++-- .../gomatrixserverlib/transaction.go | 2 +- 21 files changed, 118 insertions(+), 129 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/producers/roomserver.go b/src/github.com/matrix-org/dendrite/clientapi/producers/roomserver.go index 02a5956ce..aea912c25 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/producers/roomserver.go +++ b/src/github.com/matrix-org/dendrite/clientapi/producers/roomserver.go @@ -49,7 +49,7 @@ func (c *RoomserverProducer) SendEvents(events []gomatrixserverlib.Event, sendAs ires[i] = api.InputRoomEvent{ Kind: api.KindNew, Event: event.JSON(), - AuthEventIDs: authEventIDs(event), + AuthEventIDs: event.AuthEventIDs(), SendAsServer: string(sendAsServer), } eventIDs[i] = event.EventID() @@ -71,7 +71,7 @@ func (c *RoomserverProducer) SendEventWithState(state gomatrixserverlib.RespStat ires[i] = api.InputRoomEvent{ Kind: api.KindOutlier, Event: outlier.JSON(), - AuthEventIDs: authEventIDs(outlier), + AuthEventIDs: outlier.AuthEventIDs(), } eventIDs[i] = outlier.EventID() } @@ -84,7 +84,7 @@ func (c *RoomserverProducer) SendEventWithState(state gomatrixserverlib.RespStat ires[len(outliers)] = api.InputRoomEvent{ Kind: api.KindNew, Event: event.JSON(), - AuthEventIDs: authEventIDs(event), + AuthEventIDs: event.AuthEventIDs(), HasState: true, StateEventIDs: stateEventIDs, } @@ -93,14 +93,6 @@ func (c *RoomserverProducer) SendEventWithState(state gomatrixserverlib.RespStat return c.SendInputRoomEvents(ires, eventIDs) } -// TODO Make this a method on gomatrixserverlib.Event -func authEventIDs(event gomatrixserverlib.Event) (ids []string) { - for _, ref := range event.AuthEvents() { - ids = append(ids, ref.EventID) - } - return -} - // SendInputRoomEvents writes the given input room events to the roomserver input log. The length of both // arrays must match, and each element must correspond to the same event. func (c *RoomserverProducer) SendInputRoomEvents(ires []api.InputRoomEvent, eventIDs []string) error { diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go b/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go index db8e75247..336f29fed 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go @@ -16,6 +16,8 @@ package readers import ( "fmt" + "net/http" + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" @@ -23,8 +25,6 @@ import ( "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" - "net/http" - "strings" ) // DirectoryRoom looks up a room alias @@ -35,7 +35,7 @@ func DirectoryRoom( federation *gomatrixserverlib.FederationClient, cfg *config.Dendrite, ) util.JSONResponse { - domain, err := domainFromID(roomAlias) + _, domain, err := gomatrixserverlib.SplitID('#', roomAlias) if err != nil { return util.JSONResponse{ Code: 400, @@ -69,19 +69,3 @@ func DirectoryRoom( } } } - -// domainFromID returns everything after the first ":" character to extract -// the domain part of a matrix ID. -// TODO: duplicated from gomatrixserverlib. -func domainFromID(id string) (gomatrixserverlib.ServerName, error) { - // IDs have the format: SIGIL LOCALPART ":" DOMAIN - // Split on the first ":" character since the domain can contain ":" - // characters. - parts := strings.SplitN(id, ":", 2) - if len(parts) != 2 { - // The ID must have a ":" character. - return "", fmt.Errorf("invalid ID: %q", id) - } - // Return everything after the first ":" character. - return gomatrixserverlib.ServerName(parts[1]), nil -} diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go b/src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go index fccc42b7e..d078982f4 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go @@ -60,14 +60,7 @@ func (r createRoomRequest) Validate() *util.JSONResponse { // 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("user id must start with '@'"), - } - } - parts := strings.SplitN(userID[1:], ":", 2) - if len(parts) != 2 { + if _, _, err := gomatrixserverlib.SplitID('@', userID); err != nil { return &util.JSONResponse{ Code: 400, JSON: jsonerror.BadJSON("user id must be in the form @localpart:domain"), diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/joinroom.go b/src/github.com/matrix-org/dendrite/clientapi/writers/joinroom.go index 57bc3e86b..55cc65acb 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/writers/joinroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/joinroom.go @@ -16,6 +16,10 @@ package writers import ( "fmt" + "net/http" + "strings" + "time" + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" @@ -25,9 +29,6 @@ import ( "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" - "net/http" - "strings" - "time" ) // JoinRoomByIDOrAlias implements the "/join/{roomIDOrAlias}" API. @@ -88,7 +89,7 @@ func (r joinRoomReq) joinRoomByID() util.JSONResponse { // joinRoomByAlias joins a room using a room alias. func (r joinRoomReq) joinRoomByAlias(roomAlias string) util.JSONResponse { - domain, err := domainFromID(roomAlias) + _, domain, err := gomatrixserverlib.SplitID('#', roomAlias) if err != nil { return util.JSONResponse{ Code: 400, @@ -245,19 +246,3 @@ func (r joinRoomReq) joinRoomUsingServer(roomID string, server gomatrixserverlib }{roomID}, }, nil } - -// domainFromID returns everything after the first ":" character to extract -// the domain part of a matrix ID. -// TODO: duplicated from gomatrixserverlib. -func domainFromID(id string) (gomatrixserverlib.ServerName, error) { - // IDs have the format: SIGIL LOCALPART ":" DOMAIN - // Split on the first ":" character since the domain can contain ":" - // characters. - parts := strings.SplitN(id, ":", 2) - if len(parts) != 2 { - // The ID must have a ":" character. - return "", fmt.Errorf("invalid ID: %q", id) - } - // Return everything after the first ":" character. - return gomatrixserverlib.ServerName(parts[1]), nil -} diff --git a/src/github.com/matrix-org/dendrite/federationapi/writers/send.go b/src/github.com/matrix-org/dendrite/federationapi/writers/send.go index a6013cea2..67ee6b027 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/writers/send.go +++ b/src/github.com/matrix-org/dendrite/federationapi/writers/send.go @@ -118,11 +118,7 @@ type unknownRoomError struct { func (e unknownRoomError) Error() string { return fmt.Sprintf("unknown room %q", e.roomID) } func (t *txnReq) processEvent(e gomatrixserverlib.Event) error { - refs := e.PrevEvents() - prevEventIDs := make([]string, len(refs)) - for i := range refs { - prevEventIDs[i] = refs[i].EventID - } + prevEventIDs := e.PrevEventIDs() // Fetch the state needed to authenticate the event. needed := gomatrixserverlib.StateNeededForAuth([]gomatrixserverlib.Event{e}) diff --git a/src/github.com/matrix-org/dendrite/federationsender/consumers/roomserver.go b/src/github.com/matrix-org/dendrite/federationsender/consumers/roomserver.go index 9ac0e9880..0a0af2b32 100644 --- a/src/github.com/matrix-org/dendrite/federationsender/consumers/roomserver.go +++ b/src/github.com/matrix-org/dendrite/federationsender/consumers/roomserver.go @@ -17,7 +17,6 @@ package consumers import ( "encoding/json" "fmt" - "strings" log "github.com/Sirupsen/logrus" "github.com/matrix-org/dendrite/common" @@ -220,16 +219,14 @@ func joinedHostsFromEvents(evs []gomatrixserverlib.Event) ([]types.JoinedHost, e if ev.Type() != "m.room.member" || ev.StateKey() == nil { continue } - var content struct { - Membership string `json:"membership"` - } - if err := json.Unmarshal(ev.Content(), &content); err != nil { + membership, err := ev.Membership() + if err != nil { return nil, err } - if content.Membership != "join" { + if membership != "join" { continue } - serverName, err := domainFromID(*ev.StateKey()) + _, serverName, err := gomatrixserverlib.SplitID('@', *ev.StateKey()) if err != nil { return nil, err } @@ -343,19 +340,3 @@ func missingEventsFrom(events []gomatrixserverlib.Event, required []string) []st } return missing } - -// domainFromID returns everything after the first ":" character to extract -// the domain part of a matrix ID. -// TODO: duplicated from gomatrixserverlib. -func domainFromID(id string) (gomatrixserverlib.ServerName, error) { - // IDs have the format: SIGIL LOCALPART ":" DOMAIN - // Split on the first ":" character since the domain can contain ":" - // characters. - parts := strings.SplitN(id, ":", 2) - if len(parts) != 2 { - // The ID must have a ":" character. - return "", fmt.Errorf("invalid ID: %q", id) - } - // Return everything after the first ":" character. - return gomatrixserverlib.ServerName(parts[1]), nil -} diff --git a/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go b/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go index 163ea7b68..eaea8561e 100644 --- a/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go +++ b/src/github.com/matrix-org/dendrite/mediaapi/writers/upload.go @@ -20,7 +20,6 @@ import ( "net/http" "net/url" "path" - "strings" log "github.com/Sirupsen/logrus" "github.com/matrix-org/dendrite/clientapi/jsonerror" @@ -29,6 +28,7 @@ import ( "github.com/matrix-org/dendrite/mediaapi/storage" "github.com/matrix-org/dendrite/mediaapi/thumbnailer" "github.com/matrix-org/dendrite/mediaapi/types" + "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" ) @@ -193,14 +193,7 @@ func (r *uploadRequest) Validate(maxFileSizeBytes config.FileSizeBytes) *util.JS // 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(r.MediaMetadata.UserID) == 0 || r.MediaMetadata.UserID[0] != '@' { - return &util.JSONResponse{ - Code: 400, - JSON: jsonerror.Unknown("user id must start with '@'"), - } - } - parts := strings.SplitN(string(r.MediaMetadata.UserID[1:]), ":", 2) - if len(parts) != 2 { + if _, _, err := gomatrixserverlib.SplitID('@', string(r.MediaMetadata.UserID)); err != nil { return &util.JSONResponse{ Code: 400, JSON: jsonerror.BadJSON("user id must be in the form @localpart:domain"), diff --git a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go index ce2e7f234..a1efa1f27 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go @@ -16,11 +16,9 @@ package storage import ( "database/sql" - "encoding/json" "fmt" // Import the postgres database driver. _ "github.com/lib/pq" - "github.com/matrix-org/dendrite/clientapi/events" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" @@ -129,11 +127,11 @@ func (d *SyncServerDatabase) updateRoomState( } var membership *string if event.Type() == "m.room.member" { - var memberContent events.MemberContent - if err := json.Unmarshal(event.Content(), &memberContent); err != nil { + value, err := event.Membership() + if err != nil { return err } - membership = &memberContent.Membership + membership = &value } if err := d.roomstate.upsertRoomState(txn, event, membership, int64(streamPos)); err != nil { return err @@ -473,11 +471,11 @@ func removeDuplicates(stateEvents, recentEvents []gomatrixserverlib.Event) []gom // with type 'm.room.member' and state_key of userID. Otherwise, an empty string is returned. func getMembershipFromEvent(ev *gomatrixserverlib.Event, userID string) string { if ev.Type() == "m.room.member" && ev.StateKeyEquals(userID) { - var memberContent events.MemberContent - if err := json.Unmarshal(ev.Content(), &memberContent); err != nil { + membership, err := ev.Membership() + if err != nil { return "" } - return memberContent.Membership + return membership } return "" } diff --git a/src/github.com/matrix-org/dendrite/syncapi/sync/notifier.go b/src/github.com/matrix-org/dendrite/syncapi/sync/notifier.go index 1cc9c4e28..814660a79 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/sync/notifier.go +++ b/src/github.com/matrix-org/dendrite/syncapi/sync/notifier.go @@ -15,11 +15,9 @@ package sync import ( - "encoding/json" "sync" log "github.com/Sirupsen/logrus" - "github.com/matrix-org/dendrite/clientapi/events" "github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" @@ -68,14 +66,14 @@ func (n *Notifier) OnNewEvent(ev *gomatrixserverlib.Event, pos types.StreamPosit // If this is an invite, also add in the invitee to this list. if ev.Type() == "m.room.member" && ev.StateKey() != nil { userID := *ev.StateKey() - var memberContent events.MemberContent - if err := json.Unmarshal(ev.Content(), &memberContent); err != nil { + membership, err := ev.Membership() + if err != nil { log.WithError(err).WithField("event_id", ev.EventID()).Errorf( "Notifier.OnNewEvent: Failed to unmarshal member event", ) } else { // Keep the joined user map up-to-date - switch memberContent.Membership { + switch membership { case "invite": userIDs = append(userIDs, userID) case "join": diff --git a/vendor/manifest b/vendor/manifest index 209abd3ef..95c4b6f8b 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -98,7 +98,7 @@ { "importpath": "github.com/matrix-org/gomatrixserverlib", "repository": "https://github.com/matrix-org/gomatrixserverlib", - "revision": "0e1596ae7b0a034ec572cd1448aeaf7e96bff95a", + "revision": "30652b26ec2e83b97c941eb1c293bf7d67340f74", "branch": "master" }, { diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/event.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/event.go index 11a165131..94de35189 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/event.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/event.go @@ -18,8 +18,10 @@ package gomatrixserverlib import ( "encoding/json" "fmt" - "golang.org/x/crypto/ed25519" + "strings" "time" + + "golang.org/x/crypto/ed25519" ) // A StateKeyTuple is the combination of an event type and an event state key. @@ -54,7 +56,7 @@ type EventBuilder struct { Type string `json:"type"` // The state_key of the event if the event is a state event or nil if the event is not a state event. StateKey *string `json:"state_key,omitempty"` - // The events that immediately preceeded this event in the room history. + // The events that immediately preceded this event in the room history. PrevEvents []EventReference `json:"prev_events"` // The events needed to authenticate this event. AuthEvents []EventReference `json:"auth_events"` @@ -112,7 +114,7 @@ var emptyEventReferenceList = []EventReference{} // Build a new Event. // This is used when a local event is created on this server. // Call this after filling out the necessary fields. -// This can be called mutliple times on the same builder. +// This can be called multiple times on the same builder. // A different event ID must be supplied each time this is called. func (eb *EventBuilder) Build(eventID string, now time.Time, origin ServerName, keyID KeyID, privateKey ed25519.PrivateKey) (result Event, err error) { var event struct { @@ -467,11 +469,44 @@ func (e Event) PrevEvents() []EventReference { return e.fields.PrevEvents } +// PrevEventIDs returns the event IDs of the direct ancestors of the event. +func (e Event) PrevEventIDs() []string { + result := make([]string, len(e.fields.PrevEvents)) + for i := range e.fields.PrevEvents { + result[i] = e.fields.PrevEvents[i].EventID + } + return result +} + +// Membership returns the value of the content.membership field if this event +// is an "m.room.member" event. +// Returns an error if the event is not a m.room.member event or if the content +// is not valid m.room.member content. +func (e Event) Membership() (string, error) { + if e.fields.Type != MRoomMember { + return "", fmt.Errorf("gomatrixserverlib: not an m.room.member event") + } + var content memberContent + if err := json.Unmarshal(e.fields.Content, &content); err != nil { + return "", err + } + return content.Membership, nil +} + // AuthEvents returns references to the events needed to auth the event. func (e Event) AuthEvents() []EventReference { return e.fields.AuthEvents } +// AuthEventIDs returns the event IDs of the events needed to auth the event. +func (e Event) AuthEventIDs() []string { + result := make([]string, len(e.fields.AuthEvents)) + for i := range e.fields.AuthEvents { + result[i] = e.fields.AuthEvents[i].EventID + } + return result +} + // Redacts returns the event ID of the event this event redacts. func (e Event) Redacts() string { return e.fields.Redacts @@ -534,3 +569,19 @@ func (er EventReference) MarshalJSON() ([]byte, error) { return json.Marshal(&tuple) } + +// SplitID splits a matrix ID into a local part and a server name. +func SplitID(sigil byte, id string) (local string, domain ServerName, err error) { + // IDs have the format: SIGIL LOCALPART ":" DOMAIN + // Split on the first ":" character since the domain can contain ":" + // characters. + if len(id) == 0 || id[0] != sigil { + return "", "", fmt.Errorf("gomatriserverlib: invalid ID %q doesn't start with %q", id, sigil) + } + parts := strings.SplitN(id, ":", 2) + if len(parts) != 2 { + // The ID must have a ":" character. + return "", "", fmt.Errorf("gomatrixserverlib: invalid ID %q missing ':'", id) + } + return parts[0][1:], ServerName(parts[1]), nil +} diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/eventauth.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/eventauth.go index 15c47bfc3..f9b938a32 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/eventauth.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/eventauth.go @@ -379,6 +379,9 @@ func aliasEventAllowed(event Event, authEvents AuthEventProvider) error { // https://github.com/matrix-org/synapse/blob/v0.18.5/synapse/api/auth.py#L143-L160 create, err := newCreateContentFromAuthEvents(authEvents) + if err != nil { + return err + } senderDomain, err := domainFromID(event.Sender()) if err != nil { diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/federationtypes.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/federationtypes.go index 1f9c81eb1..1c0fff4b1 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/federationtypes.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/federationtypes.go @@ -213,7 +213,7 @@ type respSendJoinFields struct { AuthEvents []Event `json:"auth_chain"` } -// Check that a reponse to /send_join is valid. +// Check that a response to /send_join is valid. // This checks that it would be valid as a response to /state // This also checks that the join event is allowed by the state. func (r RespSendJoin) Check(keyRing KeyRing, joinEvent Event) error { diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/hooks/pre-commit b/vendor/src/github.com/matrix-org/gomatrixserverlib/hooks/pre-commit index 6ea7d146b..a7ec4d015 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/hooks/pre-commit +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/hooks/pre-commit @@ -3,7 +3,22 @@ set -eu golint ./... -go fmt +misspell -error . + +# gofmt doesn't exit with an error code if the files don't match the expected +# format. So we have to run it and see if it outputs anything. +if gofmt -l -s . 2>&1 | read +then + echo "Error: not all code had been formatted with gofmt." + echo "Fixing the following files" + gofmt -s -w -l . + echo + echo "Please add them to the commit" + git status --short + exit 1 +fi + +ineffassign . go tool vet --all --shadow . gocyclo -over 16 . go test -timeout 5s . ./... diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/json.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/json.go index 141f365bf..e29f283d5 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/json.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/json.go @@ -23,7 +23,7 @@ import ( "unicode/utf8" ) -// CanonicalJSON re-encodes the JSON in a cannonical encoding. The encoding is +// CanonicalJSON re-encodes the JSON in a canonical encoding. The encoding is // the shortest possible encoding using integer values with sorted object keys. // https://matrix.org/docs/spec/server_server/unstable.html#canonical-json func CanonicalJSON(input []byte) ([]byte, error) { @@ -223,7 +223,7 @@ func compactUnicodeEscape(input, output []byte, index int) ([]byte, int) { // Taken from https://github.com/NegativeMjark/indolentjson-rust/blob/8b959791fe2656a88f189c5d60d153be05fe3deb/src/readhex.rs#L21 func readHexDigits(input []byte) uint32 { hex := binary.BigEndian.Uint32(input) - // substract '0' + // subtract '0' hex -= 0x30303030 // strip the higher bits, maps 'a' => 'A' hex &= 0x1F1F1F1F diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/keys.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/keys.go index ffd57e59f..5fb20c7f6 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/keys.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/keys.go @@ -167,7 +167,7 @@ type KeyChecks struct { AllEd25519ChecksOK *bool // All the Ed25519 checks are ok. or null if there weren't any to check. Ed25519Checks map[KeyID]Ed25519Checks // Checks for Ed25519 keys. HasTLSFingerprint bool // The server has at least one fingerprint. - AllTLSFingerprintChecksOK *bool // All the fingerpint checks are ok. + AllTLSFingerprintChecksOK *bool // All the fingerprint checks are ok. TLSFingerprintChecks []TLSFingerprintChecks // Checks for TLS fingerprints. MatchingTLSFingerprint *bool // The TLS fingerprint for the connection matches one of the listed fingerprints. } diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/request.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/request.go index 34ada3565..f2dc5af4c 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/request.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/request.go @@ -268,7 +268,7 @@ func readHTTPRequest(req *http.Request) (*FederationRequest, error) { } result.fields.Origin = origin if result.fields.Signatures == nil { - result.fields.Signatures = map[ServerName]map[KeyID]string{origin: map[KeyID]string{key: sig}} + result.fields.Signatures = map[ServerName]map[KeyID]string{origin: {key: sig}} } else { result.fields.Signatures[origin][key] = sig } diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/resolve.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/resolve.go index c3e1ff9e0..a2af2ed83 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/resolve.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/resolve.go @@ -57,7 +57,7 @@ func LookupServer(serverName ServerName) (*DNSResult, error) { return nil, err } // If there isn't a SRV record in DNS then fallback to "serverName:8448". - hosts[string(serverName)] = []net.SRV{net.SRV{ + hosts[string(serverName)] = []net.SRV{{ Target: string(serverName), Port: 8448, }} @@ -80,7 +80,7 @@ func LookupServer(serverName ServerName) (*DNSResult, error) { if err != nil { return nil, err } - hosts[host] = []net.SRV{net.SRV{ + hosts[host] = []net.SRV{{ Target: host, Port: uint16(port), }} diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/signing_test.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/signing_test.go index ca1f955a6..e69bf31bf 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/signing_test.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/signing_test.go @@ -179,7 +179,7 @@ func TestSignJSONTestVectors(t *testing.T) { type MyMessage struct { Unsigned *json.RawMessage `json:"unsigned"` Content *json.RawMessage `json:"content"` - Signatures *json.RawMessage `json:"signature,omitempty"` + Signatures *json.RawMessage `json:"signatures,omitempty"` } func TestSignJSONWithUnsigned(t *testing.T) { @@ -215,7 +215,7 @@ func TestSignJSONWithUnsigned(t *testing.T) { t.Fatal(err) } - err = VerifyJSON(entityName, keyID, publicKey, signed) + err = VerifyJSON(entityName, keyID, publicKey, input) if err != nil { t.Errorf("VerifyJSON(%q)", signed) t.Fatal(err) diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/stateresolution_test.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/stateresolution_test.go index 289a73783..4b04a9c5c 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/stateresolution_test.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/stateresolution_test.go @@ -12,15 +12,15 @@ const ( func TestConflictEventSorter(t *testing.T) { input := []Event{ - Event{fields: eventFields{Depth: 1, EventID: "@1:a"}}, - Event{fields: eventFields{Depth: 2, EventID: "@2:a"}}, - Event{fields: eventFields{Depth: 2, EventID: "@3:b"}}, + {fields: eventFields{Depth: 1, EventID: "@1:a"}}, + {fields: eventFields{Depth: 2, EventID: "@2:a"}}, + {fields: eventFields{Depth: 2, EventID: "@3:b"}}, } got := sortConflictedEventsByDepthAndSHA1(input) want := []conflictedEvent{ - conflictedEvent{depth: 1, event: &input[0]}, - conflictedEvent{depth: 2, event: &input[2]}, - conflictedEvent{depth: 2, event: &input[1]}, + {depth: 1, event: &input[0]}, + {depth: 2, event: &input[2]}, + {depth: 2, event: &input[1]}, } copy(want[0].eventIDSHA1[:], sha1OfEventID1A) copy(want[1].eventIDSHA1[:], sha1OfEventID3B) diff --git a/vendor/src/github.com/matrix-org/gomatrixserverlib/transaction.go b/vendor/src/github.com/matrix-org/gomatrixserverlib/transaction.go index f0e2d3f75..918c18e50 100644 --- a/vendor/src/github.com/matrix-org/gomatrixserverlib/transaction.go +++ b/vendor/src/github.com/matrix-org/gomatrixserverlib/transaction.go @@ -24,7 +24,7 @@ type Transaction struct { } // A TransactionID identifies a transaction sent by a matrix server to another -// matrix server. The ID must be unique amoungst the transactions sent from the +// matrix server. The ID must be unique amongst the transactions sent from the // origin server to the destination, but doesn't have to be globally unique. // The ID must be safe to insert into a URL path segment. The ID should have a // format matching '^[0-9A-Za-z\-_]*$' From 1efbad8119f21aaa405f75d75d77cfc6bd10f06e Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 10 Jul 2017 14:52:41 +0100 Subject: [PATCH 2/2] Profile API (#151) * Profile retrieval * Saving avatar (without propagating it) * Saving display name (without propagating it) * Getters for display name and avatar URL * Doc'd * Remove unused import * Applied requested changes * Added auth on PUT /profile/{userID}/... * Improved error handling/reporting * Using utils log reporting * Removed useless checks --- .../clientapi/auth/authtypes/account.go | 1 + .../clientapi/auth/authtypes/profile.go | 22 +++ .../auth/storage/accounts/profile_table.go | 93 ++++++++++ .../auth/storage/accounts/storage.go | 34 +++- .../dendrite/clientapi/readers/profile.go | 161 ++++++++++++++++++ .../dendrite/clientapi/routing/routing.go | 46 +++-- 6 files changed, 341 insertions(+), 16 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/profile.go create mode 100644 src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/profile_table.go create mode 100644 src/github.com/matrix-org/dendrite/clientapi/readers/profile.go diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/account.go b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/account.go index ed33d0b5e..1a03590e5 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/account.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/account.go @@ -23,6 +23,7 @@ type Account struct { UserID string Localpart string ServerName gomatrixserverlib.ServerName + Profile *Profile // TODO: Other flags like IsAdmin, IsGuest // TODO: Devices // TODO: Associations (e.g. with application services) diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/profile.go b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/profile.go new file mode 100644 index 000000000..6cf508f4f --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/profile.go @@ -0,0 +1,22 @@ +// Copyright 2017 Vector Creations Ltd +// +// 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 authtypes + +// Profile represents the profile for a Matrix account on this home server. +type Profile struct { + Localpart string + DisplayName string + AvatarURL string +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/profile_table.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/profile_table.go new file mode 100644 index 000000000..36416e077 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/profile_table.go @@ -0,0 +1,93 @@ +// Copyright 2017 Vector Creations Ltd +// +// 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 accounts + +import ( + "database/sql" + + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" +) + +const profilesSchema = ` +-- Stores data about accounts profiles. +CREATE TABLE IF NOT EXISTS profiles ( + -- The Matrix user ID localpart for this account + localpart TEXT NOT NULL PRIMARY KEY, + -- The display name for this account + display_name TEXT, + -- The URL of the avatar for this account + avatar_url TEXT +); +` + +const insertProfileSQL = "" + + "INSERT INTO profiles(localpart, display_name, avatar_url) VALUES ($1, $2, $3)" + +const selectProfileByLocalpartSQL = "" + + "SELECT localpart, display_name, avatar_url FROM profiles WHERE localpart = $1" + +const setAvatarURLSQL = "" + + "UPDATE profiles SET avatar_url = $1 WHERE localpart = $2" + +const setDisplayNameSQL = "" + + "UPDATE profiles SET display_name = $1 WHERE localpart = $2" + +type profilesStatements struct { + insertProfileStmt *sql.Stmt + selectProfileByLocalpartStmt *sql.Stmt + setAvatarURLStmt *sql.Stmt + setDisplayNameStmt *sql.Stmt +} + +func (s *profilesStatements) prepare(db *sql.DB) (err error) { + _, err = db.Exec(profilesSchema) + if err != nil { + return + } + if s.insertProfileStmt, err = db.Prepare(insertProfileSQL); err != nil { + return + } + if s.selectProfileByLocalpartStmt, err = db.Prepare(selectProfileByLocalpartSQL); err != nil { + return + } + if s.setAvatarURLStmt, err = db.Prepare(setAvatarURLSQL); err != nil { + return + } + if s.setDisplayNameStmt, err = db.Prepare(setDisplayNameSQL); err != nil { + return + } + return +} + +func (s *profilesStatements) insertProfile(localpart string) (err error) { + _, err = s.insertProfileStmt.Exec(localpart, "", "") + return +} + +func (s *profilesStatements) selectProfileByLocalpart(localpart string) (*authtypes.Profile, error) { + var profile authtypes.Profile + err := s.selectProfileByLocalpartStmt.QueryRow(localpart).Scan(&profile.Localpart, &profile.DisplayName, &profile.AvatarURL) + return &profile, err +} + +func (s *profilesStatements) setAvatarURL(localpart string, avatarURL string) (err error) { + _, err = s.setAvatarURLStmt.Exec(avatarURL, localpart) + return +} + +func (s *profilesStatements) setDisplayName(localpart string, displayName string) (err error) { + _, err = s.setDisplayNameStmt.Exec(displayName, localpart) + return +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go index 1f1499bbd..cd6abc09a 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go @@ -28,9 +28,10 @@ import ( type Database struct { db *sql.DB accounts accountsStatements + profiles profilesStatements } -// NewDatabase creates a new accounts database +// NewDatabase creates a new accounts and profiles database func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) { var db *sql.DB var err error @@ -41,7 +42,11 @@ func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) if err = a.prepare(db, serverName); err != nil { return nil, err } - return &Database{db, a}, nil + p := profilesStatements{} + if err = p.prepare(db); err != nil { + return nil, err + } + return &Database{db, a, p}, nil } // GetAccountByPassword returns the account associated with the given localpart and password. @@ -57,13 +62,34 @@ func (d *Database) GetAccountByPassword(localpart, plaintextPassword string) (*a return d.accounts.selectAccountByLocalpart(localpart) } -// CreateAccount makes a new account with the given login name and password. If no password is supplied, -// the account will be a passwordless account. +// GetProfileByLocalpart returns the profile associated with the given localpart. +// Returns sql.ErrNoRows if no profile exists which matches the given localpart. +func (d *Database) GetProfileByLocalpart(localpart string) (*authtypes.Profile, error) { + return d.profiles.selectProfileByLocalpart(localpart) +} + +// SetAvatarURL updates the avatar URL of the profile associated with the given +// localpart. Returns an error if something went wrong with the SQL query +func (d *Database) SetAvatarURL(localpart string, avatarURL string) error { + return d.profiles.setAvatarURL(localpart, avatarURL) +} + +// SetDisplayName updates the display name of the profile associated with the given +// localpart. Returns an error if something went wrong with the SQL query +func (d *Database) SetDisplayName(localpart string, displayName string) error { + return d.profiles.setDisplayName(localpart, displayName) +} + +// CreateAccount makes a new account with the given login name and password, and creates an empty profile +// for this account. If no password is supplied, the account will be a passwordless account. func (d *Database) CreateAccount(localpart, plaintextPassword string) (*authtypes.Account, error) { hash, err := hashPassword(plaintextPassword) if err != nil { return nil, err } + if err := d.profiles.insertProfile(localpart); err != nil { + return nil, err + } return d.accounts.insertAccount(localpart, hash) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go b/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go new file mode 100644 index 000000000..65fa9a062 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/profile.go @@ -0,0 +1,161 @@ +// Copyright 2017 Vector Creations Ltd +// +// 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 readers + +import ( + "fmt" + "net/http" + "strings" + + "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" + "github.com/matrix-org/dendrite/clientapi/httputil" + "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/util" +) + +type profileResponse struct { + AvatarURL string `json:"avatar_url"` + DisplayName string `json:"displayname"` +} + +type avatarURL struct { + AvatarURL string `json:"avatar_url"` +} + +type displayName struct { + DisplayName string `json:"displayname"` +} + +// GetProfile implements GET /profile/{userID} +func GetProfile( + req *http.Request, accountDB *accounts.Database, userID string, +) util.JSONResponse { + if req.Method != "GET" { + return util.JSONResponse{ + Code: 405, + JSON: jsonerror.NotFound("Bad method"), + } + } + localpart := getLocalPart(userID) + profile, err := accountDB.GetProfileByLocalpart(localpart) + if err != nil { + return httputil.LogThenError(req, err) + } + res := profileResponse{ + AvatarURL: profile.AvatarURL, + DisplayName: profile.DisplayName, + } + return util.JSONResponse{ + Code: 200, + JSON: res, + } +} + +// GetAvatarURL implements GET /profile/{userID}/avatar_url +func GetAvatarURL( + req *http.Request, accountDB *accounts.Database, userID string, +) util.JSONResponse { + localpart := getLocalPart(userID) + profile, err := accountDB.GetProfileByLocalpart(localpart) + if err != nil { + return httputil.LogThenError(req, err) + } + res := avatarURL{ + AvatarURL: profile.AvatarURL, + } + return util.JSONResponse{ + Code: 200, + JSON: res, + } +} + +// SetAvatarURL implements PUT /profile/{userID}/avatar_url +func SetAvatarURL( + req *http.Request, accountDB *accounts.Database, userID string, +) util.JSONResponse { + var r avatarURL + if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil { + return *resErr + } + if r.AvatarURL == "" { + return util.JSONResponse{ + Code: 400, + JSON: jsonerror.BadJSON("'avatar_url' must be supplied."), + } + } + + localpart := getLocalPart(userID) + if err := accountDB.SetAvatarURL(localpart, r.AvatarURL); err != nil { + return httputil.LogThenError(req, err) + } + return util.JSONResponse{ + Code: 200, + JSON: struct{}{}, + } +} + +// GetDisplayName implements GET /profile/{userID}/displayname +func GetDisplayName( + req *http.Request, accountDB *accounts.Database, userID string, +) util.JSONResponse { + localpart := getLocalPart(userID) + profile, err := accountDB.GetProfileByLocalpart(localpart) + if err != nil { + return httputil.LogThenError(req, err) + } + res := displayName{ + DisplayName: profile.DisplayName, + } + return util.JSONResponse{ + Code: 200, + JSON: res, + } +} + +// SetDisplayName implements PUT /profile/{userID}/displayname +func SetDisplayName( + req *http.Request, accountDB *accounts.Database, userID string, +) util.JSONResponse { + var r displayName + if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil { + return *resErr + } + if r.DisplayName == "" { + return util.JSONResponse{ + Code: 400, + JSON: jsonerror.BadJSON("'displayname' must be supplied."), + } + } + + localpart := getLocalPart(userID) + if err := accountDB.SetDisplayName(localpart, r.DisplayName); err != nil { + return httputil.LogThenError(req, err) + } + return util.JSONResponse{ + Code: 200, + JSON: struct{}{}, + } +} + +func getLocalPart(userID string) string { + if !strings.HasPrefix(userID, "@") { + panic(fmt.Errorf("Invalid user ID")) + } + + // Get the part before ":" + username := strings.Split(userID, ":")[0] + // Return the part after the "@" + return strings.Split(username, "@")[1] +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index ce895c4bc..3482344d3 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -163,14 +163,43 @@ func Setup( r0mux.Handle("/profile/{userID}", common.MakeAPI("profile", func(req *http.Request) util.JSONResponse { - // TODO: Get profile data for user ID - return util.JSONResponse{ - Code: 200, - JSON: struct{}{}, - } + vars := mux.Vars(req) + return readers.GetProfile(req, accountDB, vars["userID"]) }), ) + r0mux.Handle("/profile/{userID}/avatar_url", + common.MakeAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse { + vars := mux.Vars(req) + return readers.GetAvatarURL(req, accountDB, vars["userID"]) + }), + ).Methods("GET") + + r0mux.Handle("/profile/{userID}/avatar_url", + common.MakeAuthAPI("profile_avatar_url", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { + vars := mux.Vars(req) + return readers.SetAvatarURL(req, accountDB, vars["userID"]) + }), + ).Methods("PUT", "OPTIONS") + // Browsers use the OPTIONS HTTP method to check if the CORS policy allows + // PUT requests, so we need to allow this method + + r0mux.Handle("/profile/{userID}/displayname", + common.MakeAPI("profile_displayname", func(req *http.Request) util.JSONResponse { + vars := mux.Vars(req) + return readers.GetDisplayName(req, accountDB, vars["userID"]) + }), + ).Methods("GET") + + r0mux.Handle("/profile/{userID}/displayname", + common.MakeAuthAPI("profile_displayname", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { + vars := mux.Vars(req) + return readers.SetDisplayName(req, accountDB, vars["userID"]) + }), + ).Methods("PUT", "OPTIONS") + // Browsers use the OPTIONS HTTP method to check if the CORS policy allows + // PUT requests, so we need to allow this method + r0mux.Handle("/account/3pid", common.MakeAPI("account_3pid", func(req *http.Request) util.JSONResponse { // TODO: Get 3pid data for user ID @@ -237,13 +266,6 @@ func Setup( }), ) - r0mux.Handle("/profile/{userID}/displayname", - common.MakeAPI("profile_displayname", func(req *http.Request) util.JSONResponse { - // TODO: Set and get the displayname - return util.JSONResponse{Code: 200, JSON: struct{}{}} - }), - ) - r0mux.Handle("/user/{userID}/account_data/{type}", common.MakeAPI("user_account_data", func(req *http.Request) util.JSONResponse { // TODO: Set and get the account_data