2017-04-20 22:40:52 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-02-10 10:44:38 +00:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
2017-09-13 15:30:19 +00:00
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
|
2017-02-10 10:44:38 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
// checkAuthEvents checks that the event passes authentication checks
|
|
|
|
// Returns the numeric IDs for the auth events.
|
2017-09-13 15:30:19 +00:00
|
|
|
func checkAuthEvents(
|
|
|
|
ctx context.Context,
|
|
|
|
db RoomEventDatabase,
|
|
|
|
event gomatrixserverlib.Event,
|
|
|
|
authEventIDs []string,
|
|
|
|
) ([]types.EventNID, error) {
|
2017-02-10 10:44:38 +00:00
|
|
|
// Grab the numeric IDs for the supplied auth state events from the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
authStateEntries, err := db.StateEntriesForEventIDs(ctx, authEventIDs)
|
2017-02-10 10:44:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO: check for duplicate state keys here.
|
|
|
|
|
|
|
|
// Work out which of the state events we actually need.
|
|
|
|
stateNeeded := gomatrixserverlib.StateNeededForAuth([]gomatrixserverlib.Event{event})
|
|
|
|
|
|
|
|
// Load the actual auth events from the database.
|
2017-09-13 15:30:19 +00:00
|
|
|
authEvents, err := loadAuthEvents(ctx, db, stateNeeded, authStateEntries)
|
2017-02-10 10:44:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the event is allowed.
|
|
|
|
if err = gomatrixserverlib.Allowed(event, &authEvents); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the numeric IDs for the auth events.
|
2017-02-15 11:05:45 +00:00
|
|
|
result := make([]types.EventNID, len(authStateEntries))
|
2017-02-10 10:44:38 +00:00
|
|
|
for i := range authStateEntries {
|
|
|
|
result[i] = authStateEntries[i].EventNID
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type authEvents struct {
|
2017-02-15 11:05:45 +00:00
|
|
|
stateKeyNIDMap map[string]types.EventStateKeyNID
|
2017-02-10 10:44:38 +00:00
|
|
|
state stateEntryMap
|
|
|
|
events eventMap
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:54:17 +00:00
|
|
|
// Create implements gomatrixserverlib.AuthEventProvider
|
2017-02-10 10:44:38 +00:00
|
|
|
func (ae *authEvents) Create() (*gomatrixserverlib.Event, error) {
|
|
|
|
return ae.lookupEventWithEmptyStateKey(types.MRoomCreateNID), nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:54:17 +00:00
|
|
|
// PowerLevels implements gomatrixserverlib.AuthEventProvider
|
2017-02-10 10:44:38 +00:00
|
|
|
func (ae *authEvents) PowerLevels() (*gomatrixserverlib.Event, error) {
|
|
|
|
return ae.lookupEventWithEmptyStateKey(types.MRoomPowerLevelsNID), nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:54:17 +00:00
|
|
|
// JoinRules implements gomatrixserverlib.AuthEventProvider
|
2017-02-10 10:44:38 +00:00
|
|
|
func (ae *authEvents) JoinRules() (*gomatrixserverlib.Event, error) {
|
|
|
|
return ae.lookupEventWithEmptyStateKey(types.MRoomJoinRulesNID), nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:54:17 +00:00
|
|
|
// Memmber implements gomatrixserverlib.AuthEventProvider
|
2017-02-10 10:44:38 +00:00
|
|
|
func (ae *authEvents) Member(stateKey string) (*gomatrixserverlib.Event, error) {
|
|
|
|
return ae.lookupEvent(types.MRoomMemberNID, stateKey), nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 17:54:17 +00:00
|
|
|
// ThirdPartyInvite implements gomatrixserverlib.AuthEventProvider
|
2017-02-10 10:44:38 +00:00
|
|
|
func (ae *authEvents) ThirdPartyInvite(stateKey string) (*gomatrixserverlib.Event, error) {
|
|
|
|
return ae.lookupEvent(types.MRoomThirdPartyInviteNID, stateKey), nil
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:05:45 +00:00
|
|
|
func (ae *authEvents) lookupEventWithEmptyStateKey(typeNID types.EventTypeNID) *gomatrixserverlib.Event {
|
2017-09-13 15:30:19 +00:00
|
|
|
eventNID, ok := ae.state.lookup(types.StateKeyTuple{
|
|
|
|
EventTypeNID: typeNID,
|
|
|
|
EventStateKeyNID: types.EmptyStateKeyNID,
|
|
|
|
})
|
2017-02-10 10:44:38 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
event, ok := ae.events.lookup(eventNID)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &event.Event
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:05:45 +00:00
|
|
|
func (ae *authEvents) lookupEvent(typeNID types.EventTypeNID, stateKey string) *gomatrixserverlib.Event {
|
2017-02-10 10:44:38 +00:00
|
|
|
stateKeyNID, ok := ae.stateKeyNIDMap[stateKey]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-13 15:30:19 +00:00
|
|
|
eventNID, ok := ae.state.lookup(types.StateKeyTuple{
|
|
|
|
EventTypeNID: typeNID,
|
|
|
|
EventStateKeyNID: stateKeyNID,
|
|
|
|
})
|
2017-02-10 10:44:38 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
event, ok := ae.events.lookup(eventNID)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &event.Event
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadAuthEvents loads the events needed for authentication from the supplied room state.
|
|
|
|
func loadAuthEvents(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context,
|
2017-02-10 10:44:38 +00:00
|
|
|
db RoomEventDatabase,
|
|
|
|
needed gomatrixserverlib.StateNeeded,
|
|
|
|
state []types.StateEntry,
|
|
|
|
) (result authEvents, err error) {
|
2017-08-23 14:08:48 +00:00
|
|
|
// Look up the numeric IDs for the state keys needed for auth.
|
2017-02-10 10:44:38 +00:00
|
|
|
var neededStateKeys []string
|
|
|
|
neededStateKeys = append(neededStateKeys, needed.Member...)
|
|
|
|
neededStateKeys = append(neededStateKeys, needed.ThirdPartyInvite...)
|
2017-09-13 15:30:19 +00:00
|
|
|
if result.stateKeyNIDMap, err = db.EventStateKeyNIDs(ctx, neededStateKeys); err != nil {
|
2017-02-10 10:44:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the events we need.
|
|
|
|
result.state = state
|
2017-02-15 11:05:45 +00:00
|
|
|
var eventNIDs []types.EventNID
|
2017-02-10 10:44:38 +00:00
|
|
|
keyTuplesNeeded := stateKeyTuplesNeeded(result.stateKeyNIDMap, needed)
|
|
|
|
for _, keyTuple := range keyTuplesNeeded {
|
|
|
|
eventNID, ok := result.state.lookup(keyTuple)
|
|
|
|
if ok {
|
|
|
|
eventNIDs = append(eventNIDs, eventNID)
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 15:30:19 +00:00
|
|
|
if result.events, err = db.Events(ctx, eventNIDs); err != nil {
|
2017-02-10 10:44:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// stateKeyTuplesNeeded works out which numeric state key tuples we need to authenticate some events.
|
2017-09-13 15:30:19 +00:00
|
|
|
func stateKeyTuplesNeeded(
|
|
|
|
stateKeyNIDMap map[string]types.EventStateKeyNID,
|
|
|
|
stateNeeded gomatrixserverlib.StateNeeded,
|
|
|
|
) []types.StateKeyTuple {
|
2017-02-10 10:44:38 +00:00
|
|
|
var keyTuples []types.StateKeyTuple
|
|
|
|
if stateNeeded.Create {
|
2017-09-13 15:30:19 +00:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomCreateNID,
|
|
|
|
EventStateKeyNID: types.EmptyStateKeyNID,
|
|
|
|
})
|
2017-02-10 10:44:38 +00:00
|
|
|
}
|
|
|
|
if stateNeeded.PowerLevels {
|
2017-09-13 15:30:19 +00:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomPowerLevelsNID,
|
|
|
|
EventStateKeyNID: types.EmptyStateKeyNID,
|
|
|
|
})
|
2017-02-10 10:44:38 +00:00
|
|
|
}
|
|
|
|
if stateNeeded.JoinRules {
|
2017-09-13 15:30:19 +00:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomJoinRulesNID,
|
|
|
|
EventStateKeyNID: types.EmptyStateKeyNID,
|
|
|
|
})
|
2017-02-10 10:44:38 +00:00
|
|
|
}
|
|
|
|
for _, member := range stateNeeded.Member {
|
|
|
|
stateKeyNID, ok := stateKeyNIDMap[member]
|
|
|
|
if ok {
|
2017-09-13 15:30:19 +00:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomMemberNID,
|
|
|
|
EventStateKeyNID: stateKeyNID,
|
|
|
|
})
|
2017-02-10 10:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, token := range stateNeeded.ThirdPartyInvite {
|
|
|
|
stateKeyNID, ok := stateKeyNIDMap[token]
|
|
|
|
if ok {
|
2017-09-13 15:30:19 +00:00
|
|
|
keyTuples = append(keyTuples, types.StateKeyTuple{
|
|
|
|
EventTypeNID: types.MRoomThirdPartyInviteNID,
|
|
|
|
EventStateKeyNID: stateKeyNID,
|
|
|
|
})
|
2017-02-10 10:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return keyTuples
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map from event type, state key tuple to numeric event ID.
|
|
|
|
// Implemented using binary search on a sorted array.
|
|
|
|
type stateEntryMap []types.StateEntry
|
|
|
|
|
|
|
|
// lookup an entry in the event map.
|
2017-02-15 11:05:45 +00:00
|
|
|
func (m stateEntryMap) lookup(stateKey types.StateKeyTuple) (eventNID types.EventNID, ok bool) {
|
2017-02-10 10:44:38 +00:00
|
|
|
// Since the list is sorted we can implement this using binary search.
|
|
|
|
// This is faster than using a hash map.
|
|
|
|
// We don't have to worry about pathological cases because the keys are fixed
|
|
|
|
// size and are controlled by us.
|
|
|
|
list := []types.StateEntry(m)
|
|
|
|
i := sort.Search(len(list), func(i int) bool {
|
|
|
|
return !list[i].StateKeyTuple.LessThan(stateKey)
|
|
|
|
})
|
|
|
|
if i < len(list) && list[i].StateKeyTuple == stateKey {
|
|
|
|
ok = true
|
|
|
|
eventNID = list[i].EventNID
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map from numeric event ID to event.
|
|
|
|
// Implemented using binary search on a sorted array.
|
|
|
|
type eventMap []types.Event
|
|
|
|
|
|
|
|
// lookup an entry in the event map.
|
2017-02-15 11:05:45 +00:00
|
|
|
func (m eventMap) lookup(eventNID types.EventNID) (event *types.Event, ok bool) {
|
2017-02-10 10:44:38 +00:00
|
|
|
// Since the list is sorted we can implement this using binary search.
|
|
|
|
// This is faster than using a hash map.
|
|
|
|
// We don't have to worry about pathological cases because the keys are fixed
|
|
|
|
// size are controlled by us.
|
|
|
|
list := []types.Event(m)
|
|
|
|
i := sort.Search(len(list), func(i int) bool {
|
|
|
|
return list[i].EventNID >= eventNID
|
|
|
|
})
|
|
|
|
if i < len(list) && list[i].EventNID == eventNID {
|
|
|
|
ok = true
|
|
|
|
event = &list[i]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|