mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-27 07:28:27 +00:00
Some more work on auth
This commit is contained in:
parent
1c117cf768
commit
bb916f93de
1 changed files with 52 additions and 14 deletions
|
@ -101,7 +101,7 @@ func (r *Inputer) processRoomEvent(
|
||||||
|
|
||||||
// First of all, check that the auth events of the event are known.
|
// First of all, check that the auth events of the event are known.
|
||||||
// If they aren't then we will ask the federation API for them.
|
// If they aren't then we will ask the federation API for them.
|
||||||
if err := r.checkForMissingAuthEvents(ctx, input); err != nil {
|
if err := r.checkForMissingAuthEvents(ctx, input.Event, map[string]types.EventNID{}); err != nil {
|
||||||
logrus.WithError(err).Error("XXX: r.checkForMissingAuthEvents")
|
logrus.WithError(err).Error("XXX: r.checkForMissingAuthEvents")
|
||||||
return "", fmt.Errorf("r.checkForMissingAuthEvents: %w", err)
|
return "", fmt.Errorf("r.checkForMissingAuthEvents: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -245,21 +245,21 @@ func (r *Inputer) processRoomEvent(
|
||||||
|
|
||||||
func (r *Inputer) checkForMissingAuthEvents(
|
func (r *Inputer) checkForMissingAuthEvents(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
input *api.InputRoomEvent,
|
event *gomatrixserverlib.HeaderedEvent,
|
||||||
|
cache map[string]types.EventNID,
|
||||||
) error {
|
) error {
|
||||||
authEventIDs := input.Event.AuthEventIDs()
|
authEventIDs := event.AuthEventIDs()
|
||||||
if len(authEventIDs) == 0 {
|
if len(authEventIDs) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Printf("XXX: Auth event IDs: %+v", authEventIDs)
|
|
||||||
|
|
||||||
knownAuthEventNIDs, err := r.DB.EventNIDs(ctx, authEventIDs)
|
knownAuthEventNIDs, err := r.DB.EventNIDs(ctx, authEventIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("r.DB.EventNIDs: %w", err)
|
return fmt.Errorf("r.DB.EventNIDs: %w", err)
|
||||||
}
|
}
|
||||||
|
for authEventID, authEventNID := range knownAuthEventNIDs {
|
||||||
logrus.Printf("XXX: Known auth event IDs: %+v", knownAuthEventNIDs)
|
cache[authEventID] = authEventNID
|
||||||
|
}
|
||||||
|
|
||||||
missingAuthEventIDs := make([]string, 0, len(authEventIDs)-len(knownAuthEventNIDs))
|
missingAuthEventIDs := make([]string, 0, len(authEventIDs)-len(knownAuthEventNIDs))
|
||||||
for _, authEventID := range authEventIDs {
|
for _, authEventID := range authEventIDs {
|
||||||
|
@ -268,21 +268,59 @@ func (r *Inputer) checkForMissingAuthEvents(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Printf("XXX: Missing auth event IDs: %+v", missingAuthEventIDs)
|
|
||||||
|
|
||||||
if len(missingAuthEventIDs) > 0 {
|
if len(missingAuthEventIDs) > 0 {
|
||||||
req := &fedapi.QueryEventAuthFromFederationRequest{
|
req := &fedapi.QueryEventAuthFromFederationRequest{
|
||||||
RoomID: input.Event.RoomID(),
|
RoomID: event.RoomID(),
|
||||||
EventID: input.Event.EventID(),
|
EventID: event.EventID(),
|
||||||
}
|
}
|
||||||
res := &fedapi.QueryEventAuthFromFederationResponse{}
|
res := &fedapi.QueryEventAuthFromFederationResponse{}
|
||||||
if err := r.FSAPI.QueryEventAuthFromFederation(ctx, req, res); err != nil {
|
if err := r.FSAPI.QueryEventAuthFromFederation(ctx, req, res); err != nil {
|
||||||
return fmt.Errorf("r.FSAPI.QueryEventAuthFromFederation: %w", err)
|
return fmt.Errorf("r.FSAPI.QueryEventAuthFromFederation: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
authEventNIDs, rejection := helpers.CheckAuthEvents(ctx, r.DB, input.Event, input.AuthEventIDs)
|
for _, event := range gomatrixserverlib.ReverseTopologicalOrdering(
|
||||||
if _, _, _, _, err := r.DB.StoreEvent(ctx, input.Event.Event, authEventNIDs, rejection != nil); err != nil {
|
res.Events,
|
||||||
return fmt.Errorf("r.DB.StoreEvent: %w", err)
|
gomatrixserverlib.TopologicalOrderByAuthEvents,
|
||||||
|
) {
|
||||||
|
// Work out which event NIDs we need to look up from the database. If
|
||||||
|
// the event NID is already in the event map in memory then we can don't
|
||||||
|
// need to ask the database again.
|
||||||
|
neededAuthEventNIDs := make([]string, 0, len(event.AuthEventIDs()))
|
||||||
|
for _, authEventID := range event.AuthEventIDs() {
|
||||||
|
if _, ok := cache[authEventID]; !ok {
|
||||||
|
neededAuthEventNIDs = append(neededAuthEventNIDs, authEventID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we need to fetch some event NIDs from the database then do that.
|
||||||
|
// We will also add those to the auth event map in memory, so that we
|
||||||
|
// can skip future database hits for the same event IDs.
|
||||||
|
if len(neededAuthEventNIDs) > 0 {
|
||||||
|
newAuthEventNIDs, err := r.DB.EventNIDs(ctx, neededAuthEventNIDs)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("r.DB.EventNIDs: %w", err)
|
||||||
|
}
|
||||||
|
for authEventID, authEventNID := range newAuthEventNIDs {
|
||||||
|
cache[authEventID] = authEventNID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now collect the event NIDs for all of the auth events.
|
||||||
|
authEventNIDsForEvent := make([]types.EventNID, 0, len(event.AuthEventIDs()))
|
||||||
|
for _, authEventID := range event.AuthEventIDs() {
|
||||||
|
authEventNIDsForEvent = append(authEventNIDsForEvent, cache[authEventID])
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we haven't accumulated all of the auth events needed for the
|
||||||
|
// event then we shouldn't persist the event as something is wrong.
|
||||||
|
if len(authEventNIDsForEvent) != len(event.AuthEventIDs()) {
|
||||||
|
return fmt.Errorf("missing auth event NIDs for event %s", event.EventID())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, store the event in the database.
|
||||||
|
if _, _, _, _, err := r.DB.StoreEvent(ctx, event, authEventNIDsForEvent, false); err != nil {
|
||||||
|
return fmt.Errorf("r.DB.StoreEvent: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue