From e06cb2a7b7c51c6f4d6b77cbffa883fbf258d307 Mon Sep 17 00:00:00 2001 From: Daniel Aloni Date: Wed, 27 Dec 2023 16:57:48 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Post-merge=20fixes=20and=20patch?= =?UTF-8?q?es.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clientapi/auth/login.go | 8 ++++++-- clientapi/auth/login_test.go | 1 - clientapi/auth/password.go | 10 +++++----- clientapi/auth/user_interactive.go | 5 ++--- clientapi/clientapi_test.go | 3 +-- clientapi/routing/key_crosssigning.go | 5 ++--- clientapi/routing/login.go | 1 + roomserver/internal/query/query.go | 9 ++------- roomserver/storage/interface.go | 2 +- roomserver/storage/shared/storage.go | 8 ++++---- setup/jetstream/nats.go | 9 ++++++--- syncapi/routing/getevent.go | 8 -------- 12 files changed, 30 insertions(+), 39 deletions(-) diff --git a/clientapi/auth/login.go b/clientapi/auth/login.go index e07eafbce..bbf3aefd9 100644 --- a/clientapi/auth/login.go +++ b/clientapi/auth/login.go @@ -32,9 +32,13 @@ import ( // called after authorization has completed, with the result of the authorization. // If the final return value is non-nil, an error occurred and the cleanup function // is nil. +// func LoginFromJSONReader(ctx context.Context, r io.Reader, +// +// useraccountAPI uapi.ClientUserAPI, +// cfg *config.ClientAPI, rt *ratelimit.RtFailedLogin) (*Login, LoginCleanupFunc, *util.JSONResponse) { func LoginFromJSONReader( req *http.Request, - useraccountAPI uapi.UserLoginAPI, + useraccountAPI uapi.ClientUserAPI, userAPI UserInternalAPIForLogin, cfg *config.ClientAPI, rt *ratelimit.RtFailedLogin, @@ -68,7 +72,6 @@ func LoginFromJSONReader( Config: cfg, Rt: rt, InhibitDevice: header.InhibitDevice, - UserLoginAPI: useraccountAPI, } case authtypes.LoginTypeToken: typ = &LoginTypeToken{ @@ -88,6 +91,7 @@ func LoginFromJSONReader( typ = &LoginTypeApplicationService{ Config: cfg, Token: token, + } case authtypes.LoginTypeJwt: typ = &LoginTypeTokenJwt{ Config: cfg, diff --git a/clientapi/auth/login_test.go b/clientapi/auth/login_test.go index 0a781b176..1e894718a 100644 --- a/clientapi/auth/login_test.go +++ b/clientapi/auth/login_test.go @@ -283,7 +283,6 @@ func TestBadLoginFromJSONReader(t *testing.T) { type fakeUserInternalAPI struct { uapi.ClientUserAPI - UserInternalAPIForLogin DeletedTokens []string } diff --git a/clientapi/auth/password.go b/clientapi/auth/password.go index fa708a507..fbe61c900 100644 --- a/clientapi/auth/password.go +++ b/clientapi/auth/password.go @@ -29,6 +29,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/userapi/api" + uapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib/spec" "github.com/matrix-org/util" ) @@ -44,11 +45,10 @@ const email = "email" // LoginTypePassword implements https://matrix.org/docs/spec/client_server/r0.6.1#password-based type LoginTypePassword struct { - UserApi api.ClientUserAPI + UserApi uapi.ClientUserAPI Config *config.ClientAPI Rt *ratelimit.RtFailedLogin InhibitDevice bool - UserLoginAPI api.UserLoginAPI } func (t *LoginTypePassword) Name() string { @@ -195,7 +195,7 @@ func (t *LoginTypePassword) authenticateDb(ctx context.Context, localpart string // If we couldn't find the user by the lower cased localpart, try the provided // localpart as is. if !res.Exists { - err = t.UserLoginAPI.QueryAccountByPassword(ctx, &api.QueryAccountByPasswordRequest{ + err = t.UserApi.QueryAccountByPassword(ctx, &api.QueryAccountByPasswordRequest{ Localpart: localpart, ServerName: domain, PlaintextPassword: password, @@ -339,7 +339,7 @@ func (t *LoginTypePassword) isLdapAdmin(conn *ldap.Conn, username string) (bool, func (t *LoginTypePassword) getOrCreateAccount(ctx context.Context, localpart string, domain spec.ServerName, admin bool) (*api.Account, *util.JSONResponse) { var existing api.QueryAccountByLocalpartResponse - err := t.UserLoginAPI.QueryAccountByLocalpart(ctx, &api.QueryAccountByLocalpartRequest{ + err := t.UserApi.QueryAccountByLocalpart(ctx, &api.QueryAccountByLocalpartRequest{ Localpart: localpart, ServerName: domain, }, &existing) @@ -359,7 +359,7 @@ func (t *LoginTypePassword) getOrCreateAccount(ctx context.Context, localpart st accountType = api.AccountTypeAdmin } var created api.PerformAccountCreationResponse - err = t.UserLoginAPI.PerformAccountCreation(ctx, &api.PerformAccountCreationRequest{ + err = t.UserApi.PerformAccountCreation(ctx, &api.PerformAccountCreationRequest{ AppServiceID: "ldap", Localpart: localpart, Password: uuid.New().String(), diff --git a/clientapi/auth/user_interactive.go b/clientapi/auth/user_interactive.go index 2ec6c528b..928df827a 100644 --- a/clientapi/auth/user_interactive.go +++ b/clientapi/auth/user_interactive.go @@ -114,9 +114,8 @@ type UserInteractive struct { func NewUserInteractive(userAccountAPI api.ClientUserAPI, cfg *config.ClientAPI) *UserInteractive { typePassword := &LoginTypePassword{ - UserApi: userAccountAPI, - UserLoginAPI: userAccountAPI, - Config: cfg, + UserApi: userAccountAPI, + Config: cfg, } return &UserInteractive{ Flows: []userInteractiveFlow{ diff --git a/clientapi/clientapi_test.go b/clientapi/clientapi_test.go index fff45ed0a..dd112d91f 100644 --- a/clientapi/clientapi_test.go +++ b/clientapi/clientapi_test.go @@ -15,8 +15,6 @@ import ( "github.com/matrix-org/dendrite/appservice" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" - "github.com/matrix-org/dendrite/clientapi/routing" - "github.com/matrix-org/dendrite/clientapi/threepid" "github.com/matrix-org/dendrite/federationapi/statistics" "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/httputil" @@ -34,6 +32,7 @@ import ( uapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/gomatrixserverlib/spec" "github.com/matrix-org/util" "github.com/stretchr/testify/assert" "github.com/tidwall/gjson" diff --git a/clientapi/routing/key_crosssigning.go b/clientapi/routing/key_crosssigning.go index fd00c17c8..dd2d2704e 100644 --- a/clientapi/routing/key_crosssigning.go +++ b/clientapi/routing/key_crosssigning.go @@ -65,9 +65,8 @@ func UploadCrossSigningDeviceKeys( } } typePassword := auth.LoginTypePassword{ - UserApi: accountAPI, - UserLoginAPI: accountAPI, - Config: cfg, + UserApi: accountAPI, + Config: cfg, } if _, authErr := typePassword.Login(req.Context(), &uploadReq.Auth.PasswordRequest); authErr != nil { return *authErr diff --git a/clientapi/routing/login.go b/clientapi/routing/login.go index f2c7292e7..92ae4741c 100644 --- a/clientapi/routing/login.go +++ b/clientapi/routing/login.go @@ -20,6 +20,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" + "github.com/matrix-org/dendrite/clientapi/ratelimit" "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/dendrite/setup/config" userapi "github.com/matrix-org/dendrite/userapi/api" diff --git a/roomserver/internal/query/query.go b/roomserver/internal/query/query.go index fa7c6b737..74b010281 100644 --- a/roomserver/internal/query/query.go +++ b/roomserver/internal/query/query.go @@ -283,11 +283,6 @@ func (r *Queryer) queryMembershipForOptionalSenderID(ctx context.Context, roomID return err } - if membershipState == tables.MembershipStateInvite { - response.Membership = spec.Invite - response.IsInRoom = true - } - response.IsRoomForgotten = isRoomforgotten if membershipEventNID == 0 { @@ -446,7 +441,7 @@ func (r *Queryer) QueryMembershipsForRoom( return nil } - membershipEventNID, _, stillInRoom, isRoomforgotten, err := r.DB.GetMembership(ctx, info.RoomNID, request.SenderID) + membershipEventNID, stillInRoom, isRoomforgotten, err := r.DB.GetMembership(ctx, info.RoomNID, request.SenderID) if err != nil { return err } @@ -994,7 +989,7 @@ func (r *Queryer) CurrentStateEvent(ctx context.Context, roomID spec.RoomID, eve } func (r *Queryer) UserJoinedToRoom(ctx context.Context, roomNID types.RoomNID, senderID spec.SenderID) (bool, error) { - _, _, isIn, _, err := r.DB.GetMembership(ctx, roomNID, senderID) + _, isIn, _, err := r.DB.GetMembership(ctx, roomNID, senderID) return isIn, err } diff --git a/roomserver/storage/interface.go b/roomserver/storage/interface.go index be4e8ad3d..0638252b2 100644 --- a/roomserver/storage/interface.go +++ b/roomserver/storage/interface.go @@ -133,7 +133,7 @@ type Database interface { // in this room, along a boolean set to true if the user is still in this room, // false if not. // Returns an error if there was a problem talking to the database. - GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderID spec.SenderID) (membershipEventNID types.EventNID, membershipNID tables.MembershipState, stillInRoom, isRoomForgotten bool, err error) + GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderID spec.SenderID) (membershipEventNID types.EventNID, stillInRoom, isRoomForgotten bool, err error) // Lookup the membership event numeric IDs for all user that are or have // been members of a given room. Only lookup events of "join" membership if // joinOnly is set to true. diff --git a/roomserver/storage/shared/storage.go b/roomserver/storage/shared/storage.go index 5d28ec674..c9ddae838 100644 --- a/roomserver/storage/shared/storage.go +++ b/roomserver/storage/shared/storage.go @@ -491,14 +491,14 @@ func (d *Database) RemoveRoomAlias(ctx context.Context, alias string) error { }) } -func (d *Database) GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderID spec.SenderID) (membershipEventNID types.EventNID, membershipState tables.MembershipState, stillInRoom, isRoomforgotten bool, err error) { +func (d *Database) GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderID spec.SenderID) (membershipEventNID types.EventNID, GetMembershipstillInGetMembershipRoom, isRoomforgotten bool, err error) { var requestSenderUserNID types.EventStateKeyNID err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error { requestSenderUserNID, err = d.assignStateKeyNID(ctx, txn, string(requestSenderID)) return err }) if err != nil { - return 0, 0, false, false, fmt.Errorf("d.assignStateKeyNID: %w", err) + return 0, false, false, fmt.Errorf("d.assignStateKeyNID: %w", err) } senderMembershipEventNID, senderMembership, isRoomforgotten, err := @@ -507,12 +507,12 @@ func (d *Database) GetMembership(ctx context.Context, roomNID types.RoomNID, req ) if err == sql.ErrNoRows { // The user has never been a member of that room - return 0, 0, false, false, nil + return 0, false, false, nil } else if err != nil { return } - return senderMembershipEventNID, senderMembership, senderMembership == tables.MembershipStateJoin, isRoomforgotten, nil + return senderMembershipEventNID, senderMembership == tables.MembershipStateJoin, isRoomforgotten, nil } func (d *Database) GetMembershipEventNIDsForRoom( diff --git a/setup/jetstream/nats.go b/setup/jetstream/nats.go index 511a7ec33..ce1f1379a 100644 --- a/setup/jetstream/nats.go +++ b/setup/jetstream/nats.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "github.com/getsentry/sentry-go" "github.com/sirupsen/logrus" "github.com/matrix-org/dendrite/setup/config" @@ -38,7 +39,7 @@ func (s *NATSInstance) Prepare(process *process.ProcessContext, cfg *config.JetS defer natsLock.Unlock() // check if we need an in-process NATS Server if len(cfg.Addresses) != 0 { - return setupNATS(cfg, nil) + return setupNATS(process, cfg, nil) } if s.Server == nil { var err error @@ -81,7 +82,7 @@ func (s *NATSInstance) Prepare(process *process.ProcessContext, cfg *config.JetS if err != nil { logrus.Fatalln("Failed to create NATS client") } - js, _ := setupNATS(cfg, nc) + js, _ := setupNATS(process, cfg, nc) s.js = js s.nc = nc return js, nc @@ -89,6 +90,8 @@ func (s *NATSInstance) Prepare(process *process.ProcessContext, cfg *config.JetS // nolint:gocyclo func setupNATS(process *process.ProcessContext, cfg *config.JetStream, nc *natsclient.Conn) (natsclient.JetStreamContext, *natsclient.Conn) { + var s nats.JetStreamContext + var err error if nc == nil { opts := []natsclient.Option{ natsclient.DisconnectErrHandler(func(c *natsclient.Conn, err error) { @@ -222,7 +225,7 @@ func setupNATS(process *process.ProcessContext, cfg *config.JetStream, nc *natsc process.Degraded(err) } } - // // Kozi's changes that are not in the original dendrite code + // Kozi's code, which defers from the original dendrite code // err = configureStream(stream, cfg, s) // if err != nil { // logrus.WithError(err).WithField("stream", stream.Name).Fatal("unable to configure a stream") diff --git a/syncapi/routing/getevent.go b/syncapi/routing/getevent.go index 85d0a3409..d0227f4ea 100644 --- a/syncapi/routing/getevent.go +++ b/syncapi/routing/getevent.go @@ -66,14 +66,6 @@ func GetEvent( } } - roomID, err := spec.NewRoomID(rawRoomID) - if err != nil { - return util.JSONResponse{ - Code: http.StatusBadRequest, - JSON: spec.InvalidParam("invalid room ID"), - } - } - events, err := db.Events(ctx, []string{eventID}) if err != nil { logger.WithError(err).Error("GetEvent: syncDB.Events failed")