mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-26 08:13:09 -06:00
Implement world_readable auth with test
This commit is contained in:
parent
380ad467d2
commit
88faf7104c
|
|
@ -288,16 +288,40 @@ func (w *walker) publicRoomsChunk(roomID string) *gomatrixserverlib.PublicRoom {
|
||||||
|
|
||||||
// authorised returns true iff the user is joined this room or the room is world_readable
|
// authorised returns true iff the user is joined this room or the room is world_readable
|
||||||
func (w *walker) authorised(roomID string) bool {
|
func (w *walker) authorised(roomID string) bool {
|
||||||
var queryRes roomserver.QueryMembershipForUserResponse
|
hisVisTuple := gomatrixserverlib.StateKeyTuple{
|
||||||
err := w.rsAPI.QueryMembershipForUser(w.ctx, &roomserver.QueryMembershipForUserRequest{
|
EventType: gomatrixserverlib.MRoomHistoryVisibility,
|
||||||
|
StateKey: "",
|
||||||
|
}
|
||||||
|
roomMemberTuple := gomatrixserverlib.StateKeyTuple{
|
||||||
|
EventType: gomatrixserverlib.MRoomMember,
|
||||||
|
StateKey: w.caller.UserID,
|
||||||
|
}
|
||||||
|
var queryRes roomserver.QueryCurrentStateResponse
|
||||||
|
err := w.rsAPI.QueryCurrentState(w.ctx, &roomserver.QueryCurrentStateRequest{
|
||||||
RoomID: roomID,
|
RoomID: roomID,
|
||||||
UserID: w.caller.UserID,
|
StateTuples: []gomatrixserverlib.StateKeyTuple{
|
||||||
|
hisVisTuple, roomMemberTuple,
|
||||||
|
},
|
||||||
}, &queryRes)
|
}, &queryRes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.GetLogger(w.ctx).WithError(err).Error("failed to QueryMembershipForUser")
|
util.GetLogger(w.ctx).WithError(err).Error("failed to QueryCurrentState")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return queryRes.IsInRoom
|
memberEv := queryRes.StateEvents[roomMemberTuple]
|
||||||
|
hisVisEv := queryRes.StateEvents[hisVisTuple]
|
||||||
|
if memberEv != nil {
|
||||||
|
membership, _ := memberEv.Membership()
|
||||||
|
if membership == gomatrixserverlib.Join {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if hisVisEv != nil {
|
||||||
|
hisVis, _ := hisVisEv.HistoryVisibility()
|
||||||
|
if hisVis == "world_readable" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// references returns all references pointing to or from this room.
|
// references returns all references pointing to or from this room.
|
||||||
|
|
|
||||||
|
|
@ -60,10 +60,10 @@ var (
|
||||||
// | <-- this link is just a parent, not a child
|
// | <-- this link is just a parent, not a child
|
||||||
// R5
|
// R5
|
||||||
//
|
//
|
||||||
// TODO: Alice is not joined to R4, but R4 is "world_readable".
|
// Alice is not joined to R4, but R4 is "world_readable".
|
||||||
func TestMSC2946(t *testing.T) {
|
func TestMSC2946(t *testing.T) {
|
||||||
alice := "@alice:localhost"
|
alice := "@alice:localhost"
|
||||||
// give access tokens to all three users
|
// give access token to alice
|
||||||
nopUserAPI := &testUserAPI{
|
nopUserAPI := &testUserAPI{
|
||||||
accessTokens: make(map[string]userapi.Device),
|
accessTokens: make(map[string]userapi.Device),
|
||||||
}
|
}
|
||||||
|
|
@ -157,6 +157,31 @@ func TestMSC2946(t *testing.T) {
|
||||||
"present": true,
|
"present": true,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
// history visibility for R4
|
||||||
|
r4HisVis := mustCreateEvent(t, fledglingEvent{
|
||||||
|
RoomID: room4,
|
||||||
|
Sender: "@someone:localhost",
|
||||||
|
Type: gomatrixserverlib.MRoomHistoryVisibility,
|
||||||
|
StateKey: &empty,
|
||||||
|
Content: map[string]interface{}{
|
||||||
|
"history_visibility": "world_readable",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
var joinEvents []*gomatrixserverlib.HeaderedEvent
|
||||||
|
for _, roomID := range allRooms {
|
||||||
|
if roomID == room4 {
|
||||||
|
continue // not joined to that room
|
||||||
|
}
|
||||||
|
joinEvents = append(joinEvents, mustCreateEvent(t, fledglingEvent{
|
||||||
|
RoomID: roomID,
|
||||||
|
Sender: alice,
|
||||||
|
StateKey: &alice,
|
||||||
|
Type: gomatrixserverlib.MRoomMember,
|
||||||
|
Content: map[string]interface{}{
|
||||||
|
"membership": "join",
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
}
|
||||||
roomNameTuple := gomatrixserverlib.StateKeyTuple{
|
roomNameTuple := gomatrixserverlib.StateKeyTuple{
|
||||||
EventType: "m.room.name",
|
EventType: "m.room.name",
|
||||||
StateKey: "",
|
StateKey: "",
|
||||||
|
|
@ -166,9 +191,7 @@ func TestMSC2946(t *testing.T) {
|
||||||
StateKey: "",
|
StateKey: "",
|
||||||
}
|
}
|
||||||
nopRsAPI := &testRoomserverAPI{
|
nopRsAPI := &testRoomserverAPI{
|
||||||
userToJoinedRooms: map[string][]string{
|
joinEvents: joinEvents,
|
||||||
alice: allRooms,
|
|
||||||
},
|
|
||||||
events: map[string]*gomatrixserverlib.HeaderedEvent{
|
events: map[string]*gomatrixserverlib.HeaderedEvent{
|
||||||
rootToR1.EventID(): rootToR1,
|
rootToR1.EventID(): rootToR1,
|
||||||
rootToR2.EventID(): rootToR2,
|
rootToR2.EventID(): rootToR2,
|
||||||
|
|
@ -177,6 +200,7 @@ func TestMSC2946(t *testing.T) {
|
||||||
s1ToR4.EventID(): s1ToR4,
|
s1ToR4.EventID(): s1ToR4,
|
||||||
s1ToS2.EventID(): s1ToS2,
|
s1ToS2.EventID(): s1ToS2,
|
||||||
s2ToR5.EventID(): s2ToR5,
|
s2ToR5.EventID(): s2ToR5,
|
||||||
|
r4HisVis.EventID(): r4HisVis,
|
||||||
},
|
},
|
||||||
pubRoomState: map[string]map[gomatrixserverlib.StateKeyTuple]string{
|
pubRoomState: map[string]map[gomatrixserverlib.StateKeyTuple]string{
|
||||||
rootSpace: {
|
rootSpace: {
|
||||||
|
|
@ -208,11 +232,13 @@ func TestMSC2946(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
router := injectEvents(t, nopUserAPI, nopRsAPI, []*gomatrixserverlib.HeaderedEvent{
|
allEvents := []*gomatrixserverlib.HeaderedEvent{
|
||||||
rootToR1, rootToR2, rootToS1,
|
rootToR1, rootToR2, rootToS1,
|
||||||
s1ToR3, s1ToR4, s1ToS2,
|
s1ToR3, s1ToR4, s1ToS2,
|
||||||
s2ToR5,
|
s2ToR5, r4HisVis,
|
||||||
})
|
}
|
||||||
|
allEvents = append(allEvents, joinEvents...)
|
||||||
|
router := injectEvents(t, nopUserAPI, nopRsAPI, allEvents)
|
||||||
cancel := runServer(t, router)
|
cancel := runServer(t, router)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
|
@ -416,9 +442,9 @@ type testRoomserverAPI struct {
|
||||||
// use a trace API as it implements method stubs so we don't need to have them here.
|
// use a trace API as it implements method stubs so we don't need to have them here.
|
||||||
// We'll override the functions we care about.
|
// We'll override the functions we care about.
|
||||||
roomserver.RoomserverInternalAPITrace
|
roomserver.RoomserverInternalAPITrace
|
||||||
userToJoinedRooms map[string][]string
|
joinEvents []*gomatrixserverlib.HeaderedEvent
|
||||||
events map[string]*gomatrixserverlib.HeaderedEvent
|
events map[string]*gomatrixserverlib.HeaderedEvent
|
||||||
pubRoomState map[string]map[gomatrixserverlib.StateKeyTuple]string
|
pubRoomState map[string]map[gomatrixserverlib.StateKeyTuple]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *testRoomserverAPI) QueryBulkStateContent(ctx context.Context, req *roomserver.QueryBulkStateContentRequest, res *roomserver.QueryBulkStateContentResponse) error {
|
func (r *testRoomserverAPI) QueryBulkStateContent(ctx context.Context, req *roomserver.QueryBulkStateContentRequest, res *roomserver.QueryBulkStateContentResponse) error {
|
||||||
|
|
@ -434,12 +460,12 @@ func (r *testRoomserverAPI) QueryBulkStateContent(ctx context.Context, req *room
|
||||||
|
|
||||||
func (r *testRoomserverAPI) QueryCurrentState(ctx context.Context, req *roomserver.QueryCurrentStateRequest, res *roomserver.QueryCurrentStateResponse) error {
|
func (r *testRoomserverAPI) QueryCurrentState(ctx context.Context, req *roomserver.QueryCurrentStateRequest, res *roomserver.QueryCurrentStateResponse) error {
|
||||||
res.StateEvents = make(map[gomatrixserverlib.StateKeyTuple]*gomatrixserverlib.HeaderedEvent)
|
res.StateEvents = make(map[gomatrixserverlib.StateKeyTuple]*gomatrixserverlib.HeaderedEvent)
|
||||||
for _, he := range r.events {
|
checkEvent := func(he *gomatrixserverlib.HeaderedEvent) {
|
||||||
if he.RoomID() != req.RoomID {
|
if he.RoomID() != req.RoomID {
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
if he.StateKey() == nil {
|
if he.StateKey() == nil {
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
tuple := gomatrixserverlib.StateKeyTuple{
|
tuple := gomatrixserverlib.StateKeyTuple{
|
||||||
EventType: he.Type(),
|
EventType: he.Type(),
|
||||||
|
|
@ -451,18 +477,11 @@ func (r *testRoomserverAPI) QueryCurrentState(ctx context.Context, req *roomserv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
for _, he := range r.joinEvents {
|
||||||
}
|
checkEvent(he)
|
||||||
|
}
|
||||||
func (r *testRoomserverAPI) QueryMembershipForUser(ctx context.Context, req *roomserver.QueryMembershipForUserRequest, res *roomserver.QueryMembershipForUserResponse) error {
|
for _, he := range r.events {
|
||||||
rooms := r.userToJoinedRooms[req.UserID]
|
checkEvent(he)
|
||||||
for _, roomID := range rooms {
|
|
||||||
if roomID == req.RoomID {
|
|
||||||
res.IsInRoom = true
|
|
||||||
res.HasBeenInRoom = true
|
|
||||||
res.Membership = "join"
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue