Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FinClip
ligase
提交
c72a4409
ligase
项目概览
FinClip
/
ligase
通知
38
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
ligase
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c72a4409
编写于
2月 10, 2017
作者:
M
Mark Haines
提交者:
GitHub
2月 10, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move the auth checks to a separate file (#5)
上级
a3e17749
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
197 addition
and
191 deletion
+197
-191
src/github.com/matrix-org/dendrite/roomserver/input/authevents.go
...ub.com/matrix-org/dendrite/roomserver/input/authevents.go
+197
-0
src/github.com/matrix-org/dendrite/roomserver/input/authevents_test.go
...m/matrix-org/dendrite/roomserver/input/authevents_test.go
+0
-0
src/github.com/matrix-org/dendrite/roomserver/input/events.go
...github.com/matrix-org/dendrite/roomserver/input/events.go
+0
-191
未找到文件。
src/github.com/matrix-org/dendrite/roomserver/input/authevents.go
0 → 100644
浏览文件 @
c72a4409
package
input
import
(
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/gomatrixserverlib"
"sort"
)
// checkAuthEvents checks that the event passes authentication checks
// Returns the numeric IDs for the auth events.
func
checkAuthEvents
(
db
RoomEventDatabase
,
event
gomatrixserverlib
.
Event
,
authEventIDs
[]
string
)
([]
int64
,
error
)
{
// Grab the numeric IDs for the supplied auth state events from the database.
authStateEntries
,
err
:=
db
.
StateEntriesForEventIDs
(
authEventIDs
)
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.
authEvents
,
err
:=
loadAuthEvents
(
db
,
stateNeeded
,
authStateEntries
)
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.
result
:=
make
([]
int64
,
len
(
authStateEntries
))
for
i
:=
range
authStateEntries
{
result
[
i
]
=
authStateEntries
[
i
]
.
EventNID
}
return
result
,
nil
}
type
authEvents
struct
{
stateKeyNIDMap
map
[
string
]
int64
state
stateEntryMap
events
eventMap
}
// Create implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
Create
()
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEventWithEmptyStateKey
(
types
.
MRoomCreateNID
),
nil
}
// PowerLevels implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
PowerLevels
()
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEventWithEmptyStateKey
(
types
.
MRoomPowerLevelsNID
),
nil
}
// JoinRules implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
JoinRules
()
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEventWithEmptyStateKey
(
types
.
MRoomJoinRulesNID
),
nil
}
// Memmber implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
Member
(
stateKey
string
)
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEvent
(
types
.
MRoomMemberNID
,
stateKey
),
nil
}
// ThirdPartyInvite implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
ThirdPartyInvite
(
stateKey
string
)
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEvent
(
types
.
MRoomThirdPartyInviteNID
,
stateKey
),
nil
}
func
(
ae
*
authEvents
)
lookupEventWithEmptyStateKey
(
typeNID
int64
)
*
gomatrixserverlib
.
Event
{
eventNID
,
ok
:=
ae
.
state
.
lookup
(
types
.
StateKeyTuple
{
typeNID
,
types
.
EmptyStateKeyNID
})
if
!
ok
{
return
nil
}
event
,
ok
:=
ae
.
events
.
lookup
(
eventNID
)
if
!
ok
{
return
nil
}
return
&
event
.
Event
}
func
(
ae
*
authEvents
)
lookupEvent
(
typeNID
int64
,
stateKey
string
)
*
gomatrixserverlib
.
Event
{
stateKeyNID
,
ok
:=
ae
.
stateKeyNIDMap
[
stateKey
]
if
!
ok
{
return
nil
}
eventNID
,
ok
:=
ae
.
state
.
lookup
(
types
.
StateKeyTuple
{
typeNID
,
stateKeyNID
})
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
(
db
RoomEventDatabase
,
needed
gomatrixserverlib
.
StateNeeded
,
state
[]
types
.
StateEntry
,
)
(
result
authEvents
,
err
error
)
{
// Lookup the numeric IDs for the state keys needed for auth.
var
neededStateKeys
[]
string
neededStateKeys
=
append
(
neededStateKeys
,
needed
.
Member
...
)
neededStateKeys
=
append
(
neededStateKeys
,
needed
.
ThirdPartyInvite
...
)
if
result
.
stateKeyNIDMap
,
err
=
db
.
EventStateKeyNIDs
(
neededStateKeys
);
err
!=
nil
{
return
}
// Load the events we need.
result
.
state
=
state
var
eventNIDs
[]
int64
keyTuplesNeeded
:=
stateKeyTuplesNeeded
(
result
.
stateKeyNIDMap
,
needed
)
for
_
,
keyTuple
:=
range
keyTuplesNeeded
{
eventNID
,
ok
:=
result
.
state
.
lookup
(
keyTuple
)
if
ok
{
eventNIDs
=
append
(
eventNIDs
,
eventNID
)
}
}
if
result
.
events
,
err
=
db
.
Events
(
eventNIDs
);
err
!=
nil
{
return
}
return
}
// stateKeyTuplesNeeded works out which numeric state key tuples we need to authenticate some events.
func
stateKeyTuplesNeeded
(
stateKeyNIDMap
map
[
string
]
int64
,
stateNeeded
gomatrixserverlib
.
StateNeeded
)
[]
types
.
StateKeyTuple
{
var
keyTuples
[]
types
.
StateKeyTuple
if
stateNeeded
.
Create
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomCreateNID
,
types
.
EmptyStateKeyNID
})
}
if
stateNeeded
.
PowerLevels
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomPowerLevelsNID
,
types
.
EmptyStateKeyNID
})
}
if
stateNeeded
.
JoinRules
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomJoinRulesNID
,
types
.
EmptyStateKeyNID
})
}
for
_
,
member
:=
range
stateNeeded
.
Member
{
stateKeyNID
,
ok
:=
stateKeyNIDMap
[
member
]
if
ok
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomMemberNID
,
stateKeyNID
})
}
}
for
_
,
token
:=
range
stateNeeded
.
ThirdPartyInvite
{
stateKeyNID
,
ok
:=
stateKeyNIDMap
[
token
]
if
ok
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomThirdPartyInviteNID
,
stateKeyNID
})
}
}
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.
func
(
m
stateEntryMap
)
lookup
(
stateKey
types
.
StateKeyTuple
)
(
eventNID
int64
,
ok
bool
)
{
// 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.
func
(
m
eventMap
)
lookup
(
eventNID
int64
)
(
event
*
types
.
Event
,
ok
bool
)
{
// 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
}
src/github.com/matrix-org/dendrite/roomserver/input/events_test.go
→
src/github.com/matrix-org/dendrite/roomserver/input/
auth
events_test.go
浏览文件 @
c72a4409
文件已移动
src/github.com/matrix-org/dendrite/roomserver/input/events.go
浏览文件 @
c72a4409
...
...
@@ -4,7 +4,6 @@ import (
"github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/types"
"github.com/matrix-org/gomatrixserverlib"
"sort"
)
// A RoomEventDatabase has the storage APIs needed to store a room event.
...
...
@@ -62,193 +61,3 @@ func processRoomEvent(db RoomEventDatabase, input api.InputRoomEvent) error {
// - The changes to the current state of the room.
panic
(
"Not implemented"
)
}
// checkAuthEvents checks that the event passes authentication checks
// Returns the numeric IDs for the auth events.
func
checkAuthEvents
(
db
RoomEventDatabase
,
event
gomatrixserverlib
.
Event
,
authEventIDs
[]
string
)
([]
int64
,
error
)
{
// Grab the numeric IDs for the supplied auth state events from the database.
authStateEntries
,
err
:=
db
.
StateEntriesForEventIDs
(
authEventIDs
)
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.
authEvents
,
err
:=
loadAuthEvents
(
db
,
stateNeeded
,
authStateEntries
)
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.
result
:=
make
([]
int64
,
len
(
authStateEntries
))
for
i
:=
range
authStateEntries
{
result
[
i
]
=
authStateEntries
[
i
]
.
EventNID
}
return
result
,
nil
}
type
authEvents
struct
{
stateKeyNIDMap
map
[
string
]
int64
state
stateEntryMap
events
eventMap
}
// Create implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
Create
()
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEventWithEmptyStateKey
(
types
.
MRoomCreateNID
),
nil
}
// PowerLevels implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
PowerLevels
()
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEventWithEmptyStateKey
(
types
.
MRoomPowerLevelsNID
),
nil
}
// JoinRules implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
JoinRules
()
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEventWithEmptyStateKey
(
types
.
MRoomJoinRulesNID
),
nil
}
// Memmber implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
Member
(
stateKey
string
)
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEvent
(
types
.
MRoomMemberNID
,
stateKey
),
nil
}
// ThirdPartyInvite implements gomatrixserverlib.AuthEvents
func
(
ae
*
authEvents
)
ThirdPartyInvite
(
stateKey
string
)
(
*
gomatrixserverlib
.
Event
,
error
)
{
return
ae
.
lookupEvent
(
types
.
MRoomThirdPartyInviteNID
,
stateKey
),
nil
}
func
(
ae
*
authEvents
)
lookupEventWithEmptyStateKey
(
typeNID
int64
)
*
gomatrixserverlib
.
Event
{
eventNID
,
ok
:=
ae
.
state
.
lookup
(
types
.
StateKeyTuple
{
typeNID
,
types
.
EmptyStateKeyNID
})
if
!
ok
{
return
nil
}
event
,
ok
:=
ae
.
events
.
lookup
(
eventNID
)
if
!
ok
{
return
nil
}
return
&
event
.
Event
}
func
(
ae
*
authEvents
)
lookupEvent
(
typeNID
int64
,
stateKey
string
)
*
gomatrixserverlib
.
Event
{
stateKeyNID
,
ok
:=
ae
.
stateKeyNIDMap
[
stateKey
]
if
!
ok
{
return
nil
}
eventNID
,
ok
:=
ae
.
state
.
lookup
(
types
.
StateKeyTuple
{
typeNID
,
stateKeyNID
})
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
(
db
RoomEventDatabase
,
needed
gomatrixserverlib
.
StateNeeded
,
state
[]
types
.
StateEntry
,
)
(
result
authEvents
,
err
error
)
{
// Lookup the numeric IDs for the state keys needed for auth.
var
neededStateKeys
[]
string
neededStateKeys
=
append
(
neededStateKeys
,
needed
.
Member
...
)
neededStateKeys
=
append
(
neededStateKeys
,
needed
.
ThirdPartyInvite
...
)
if
result
.
stateKeyNIDMap
,
err
=
db
.
EventStateKeyNIDs
(
neededStateKeys
);
err
!=
nil
{
return
}
// Load the events we need.
result
.
state
=
state
var
eventNIDs
[]
int64
keyTuplesNeeded
:=
stateKeyTuplesNeeded
(
result
.
stateKeyNIDMap
,
needed
)
for
_
,
keyTuple
:=
range
keyTuplesNeeded
{
eventNID
,
ok
:=
result
.
state
.
lookup
(
keyTuple
)
if
ok
{
eventNIDs
=
append
(
eventNIDs
,
eventNID
)
}
}
if
result
.
events
,
err
=
db
.
Events
(
eventNIDs
);
err
!=
nil
{
return
}
return
}
// stateKeyTuplesNeeded works out which numeric state key tuples we need to authenticate some events.
func
stateKeyTuplesNeeded
(
stateKeyNIDMap
map
[
string
]
int64
,
stateNeeded
gomatrixserverlib
.
StateNeeded
)
[]
types
.
StateKeyTuple
{
var
keyTuples
[]
types
.
StateKeyTuple
if
stateNeeded
.
Create
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomCreateNID
,
types
.
EmptyStateKeyNID
})
}
if
stateNeeded
.
PowerLevels
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomPowerLevelsNID
,
types
.
EmptyStateKeyNID
})
}
if
stateNeeded
.
JoinRules
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomJoinRulesNID
,
types
.
EmptyStateKeyNID
})
}
for
_
,
member
:=
range
stateNeeded
.
Member
{
stateKeyNID
,
ok
:=
stateKeyNIDMap
[
member
]
if
ok
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomMemberNID
,
stateKeyNID
})
}
}
for
_
,
token
:=
range
stateNeeded
.
ThirdPartyInvite
{
stateKeyNID
,
ok
:=
stateKeyNIDMap
[
token
]
if
ok
{
keyTuples
=
append
(
keyTuples
,
types
.
StateKeyTuple
{
types
.
MRoomThirdPartyInviteNID
,
stateKeyNID
})
}
}
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.
func
(
m
stateEntryMap
)
lookup
(
stateKey
types
.
StateKeyTuple
)
(
eventNID
int64
,
ok
bool
)
{
// 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.
func
(
m
eventMap
)
lookup
(
eventNID
int64
)
(
event
*
types
.
Event
,
ok
bool
)
{
// 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
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录