mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-15 01:53:09 -06:00
Merge branch 'main' into senderid-userkey
This commit is contained in:
commit
1fea303625
|
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/ed25519"
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/gomatrixserverlib/spec"
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
||||||
|
|
@ -66,6 +67,9 @@ type RoomserverInternalAPI interface {
|
||||||
req *QueryAuthChainRequest,
|
req *QueryAuthChainRequest,
|
||||||
res *QueryAuthChainResponse,
|
res *QueryAuthChainResponse,
|
||||||
) error
|
) error
|
||||||
|
|
||||||
|
// GetOrCreateUserRoomPrivateKey gets the user room key for the specified user. If no key exists yet, a new one is created.
|
||||||
|
GetOrCreateUserRoomPrivateKey(ctx context.Context, userID spec.UserID, roomID spec.RoomID) (ed25519.PrivateKey, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type InputRoomEventsAPI interface {
|
type InputRoomEventsAPI interface {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/ed25519"
|
||||||
|
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
|
@ -271,3 +272,23 @@ func (r *RoomserverInternalAPI) PerformForget(
|
||||||
) error {
|
) error {
|
||||||
return r.Forgetter.PerformForget(ctx, req, resp)
|
return r.Forgetter.PerformForget(ctx, req, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetOrCreateUserRoomPrivateKey gets the user room key for the specified user. If no key exists yet, a new one is created.
|
||||||
|
func (r *RoomserverInternalAPI) GetOrCreateUserRoomPrivateKey(ctx context.Context, userID spec.UserID, roomID spec.RoomID) (ed25519.PrivateKey, error) {
|
||||||
|
key, err := r.DB.SelectUserRoomPrivateKey(ctx, userID, roomID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// no key found, create one
|
||||||
|
if len(key) == 0 {
|
||||||
|
_, key, err = ed25519.GenerateKey(nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
key, err = r.DB.InsertUserRoomPrivatePublicKey(ctx, userID, roomID, key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -353,7 +353,30 @@ func (c *Creator) PerformCreateRoom(ctx context.Context, userID spec.UserID, roo
|
||||||
SendAsServer: api.DoNotSendToOtherServers,
|
SendAsServer: api.DoNotSendToOtherServers,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if err = api.SendInputRoomEvents(ctx, c.RSAPI, userID.Domain(), inputs, false); err != nil {
|
|
||||||
|
// first send the `m.room.create` event, so we have a roomNID
|
||||||
|
if err = api.SendInputRoomEvents(ctx, c.RSAPI, userID.Domain(), inputs[:1], false); err != nil {
|
||||||
|
util.GetLogger(ctx).WithError(err).Error("roomserverAPI.SendInputRoomEvents failed")
|
||||||
|
return "", &util.JSONResponse{
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
JSON: spec.InternalServerError{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create user room key if needed
|
||||||
|
if createRequest.RoomVersion == gomatrixserverlib.RoomVersionPseudoIDs {
|
||||||
|
_, err = c.RSAPI.GetOrCreateUserRoomPrivateKey(ctx, userID, roomID)
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(ctx).WithError(err).Error("GetOrCreateUserRoomPrivateKey failed")
|
||||||
|
return "", &util.JSONResponse{
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
JSON: spec.InternalServerError{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// send the remaining events
|
||||||
|
if err = api.SendInputRoomEvents(ctx, c.RSAPI, userID.Domain(), inputs[1:], false); err != nil {
|
||||||
util.GetLogger(ctx).WithError(err).Error("roomserverAPI.SendInputRoomEvents failed")
|
util.GetLogger(ctx).WithError(err).Error("roomserverAPI.SendInputRoomEvents failed")
|
||||||
return "", &util.JSONResponse{
|
return "", &util.JSONResponse{
|
||||||
Code: http.StatusInternalServerError,
|
Code: http.StatusInternalServerError,
|
||||||
|
|
|
||||||
|
|
@ -188,6 +188,14 @@ func (r *Inviter) PerformInvite(
|
||||||
inviteEvent = event
|
inviteEvent = event
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we invited a local user, we can also create a user room key, if it doesn't exist yet.
|
||||||
|
if isTargetLocal && event.Version() == gomatrixserverlib.RoomVersionPseudoIDs {
|
||||||
|
_, err = r.RSAPI.GetOrCreateUserRoomPrivateKey(ctx, *invitedUser, *validRoomID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get user room private key: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Send the invite event to the roomserver input stream. This will
|
// Send the invite event to the roomserver input stream. This will
|
||||||
// notify existing users in the room about the invite, update the
|
// notify existing users in the room about the invite, update the
|
||||||
// membership table and ensure that the event is ready and available
|
// membership table and ensure that the event is ready and available
|
||||||
|
|
|
||||||
|
|
@ -308,6 +308,15 @@ func (r *Joiner) performJoinRoomByID(
|
||||||
|
|
||||||
switch err.(type) {
|
switch err.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
|
// create user room key if needed
|
||||||
|
if buildRes.RoomVersion == gomatrixserverlib.RoomVersionPseudoIDs {
|
||||||
|
_, err = r.RSAPI.GetOrCreateUserRoomPrivateKey(ctx, *userID, *roomID)
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).Error("GetOrCreateUserRoomPrivateKey failed")
|
||||||
|
return "", "", fmt.Errorf("failed to get user room private key: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The room join is local. Send the new join event into the
|
// The room join is local. Send the new join event into the
|
||||||
// roomserver. First of all check that the user isn't already
|
// roomserver. First of all check that the user isn't already
|
||||||
// a member of the room. This is best-effort (as in we won't
|
// a member of the room. This is best-effort (as in we won't
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ type Database interface {
|
||||||
UserRoomKeys
|
UserRoomKeys
|
||||||
// Do we support processing input events for more than one room at a time?
|
// Do we support processing input events for more than one room at a time?
|
||||||
SupportsConcurrentRoomInputs() bool
|
SupportsConcurrentRoomInputs() bool
|
||||||
|
AssignRoomNID(ctx context.Context, roomID spec.RoomID, roomVersion gomatrixserverlib.RoomVersion) (roomNID types.RoomNID, err error)
|
||||||
// RoomInfo returns room information for the given room ID, or nil if there is no room.
|
// RoomInfo returns room information for the given room ID, or nil if there is no room.
|
||||||
RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error)
|
RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error)
|
||||||
RoomInfoByNID(ctx context.Context, roomNID types.RoomNID) (*types.RoomInfo, error)
|
RoomInfoByNID(ctx context.Context, roomNID types.RoomNID) (*types.RoomInfo, error)
|
||||||
|
|
@ -213,6 +214,7 @@ type UserRoomKeys interface {
|
||||||
type RoomDatabase interface {
|
type RoomDatabase interface {
|
||||||
EventDatabase
|
EventDatabase
|
||||||
UserRoomKeys
|
UserRoomKeys
|
||||||
|
AssignRoomNID(ctx context.Context, roomID spec.RoomID, roomVersion gomatrixserverlib.RoomVersion) (roomNID types.RoomNID, err error)
|
||||||
// RoomInfo returns room information for the given room ID, or nil if there is no room.
|
// RoomInfo returns room information for the given room ID, or nil if there is no room.
|
||||||
RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error)
|
RoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error)
|
||||||
RoomInfoByNID(ctx context.Context, roomNID types.RoomNID) (*types.RoomInfo, error)
|
RoomInfoByNID(ctx context.Context, roomNID types.RoomNID) (*types.RoomInfo, error)
|
||||||
|
|
|
||||||
|
|
@ -662,6 +662,26 @@ func (d *Database) IsEventRejected(ctx context.Context, roomNID types.RoomNID, e
|
||||||
return d.EventsTable.SelectEventRejected(ctx, nil, roomNID, eventID)
|
return d.EventsTable.SelectEventRejected(ctx, nil, roomNID, eventID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Database) AssignRoomNID(ctx context.Context, roomID spec.RoomID, roomVersion gomatrixserverlib.RoomVersion) (roomNID types.RoomNID, err error) {
|
||||||
|
// This should already be checked, let's check it anyway.
|
||||||
|
_, err = gomatrixserverlib.GetRoomVersion(roomVersion)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
|
||||||
|
roomNID, err = d.assignRoomNID(ctx, txn, roomID.String(), roomVersion)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
// Not setting caches, as assignRoomNID already does this
|
||||||
|
return roomNID, err
|
||||||
|
}
|
||||||
|
|
||||||
// GetOrCreateRoomInfo gets or creates a new RoomInfo, which is only safe to use with functions only needing a roomVersion or roomNID.
|
// GetOrCreateRoomInfo gets or creates a new RoomInfo, which is only safe to use with functions only needing a roomVersion or roomNID.
|
||||||
func (d *Database) GetOrCreateRoomInfo(ctx context.Context, event gomatrixserverlib.PDU) (roomInfo *types.RoomInfo, err error) {
|
func (d *Database) GetOrCreateRoomInfo(ctx context.Context, event gomatrixserverlib.PDU) (roomInfo *types.RoomInfo, err error) {
|
||||||
// Get the default room version. If the client doesn't supply a room_version
|
// Get the default room version. If the client doesn't supply a room_version
|
||||||
|
|
@ -1692,7 +1712,7 @@ func (d *Database) SelectUserRoomPrivateKey(ctx context.Context, userID spec.Use
|
||||||
return rErr
|
return rErr
|
||||||
}
|
}
|
||||||
if roomInfo == nil {
|
if roomInfo == nil {
|
||||||
return nil
|
return eventutil.ErrRoomNoExists{}
|
||||||
}
|
}
|
||||||
|
|
||||||
key, sErr = d.UserRoomKeyTable.SelectUserRoomPrivateKey(ctx, txn, stateKeyNID, roomInfo.RoomNID)
|
key, sErr = d.UserRoomKeyTable.SelectUserRoomPrivateKey(ctx, txn, stateKeyNID, roomInfo.RoomNID)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/internal/caching"
|
"github.com/matrix-org/dendrite/internal/caching"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/gomatrixserverlib/spec"
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
@ -204,3 +205,24 @@ func TestUserRoomKeys(t *testing.T) {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAssignRoomNID(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
alice := test.NewUser(t)
|
||||||
|
room := test.NewRoom(t, alice)
|
||||||
|
|
||||||
|
roomID, err := spec.NewRoomID(room.ID)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||||
|
db, close := mustCreateRoomserverDatabase(t, dbType)
|
||||||
|
defer close()
|
||||||
|
|
||||||
|
nid, err := db.AssignRoomNID(ctx, *roomID, room.Version)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Greater(t, nid, types.EventNID(0))
|
||||||
|
|
||||||
|
_, err = db.AssignRoomNID(ctx, spec.RoomID{}, "notaroomversion")
|
||||||
|
assert.Error(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue