Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FinClip
ligase
提交
13107c6b
ligase
项目概览
FinClip
/
ligase
通知
33
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,体验更适合开发者的 AI 搜索 >>
提交
13107c6b
编写于
10月 09, 2017
作者:
M
Marcel
提交者:
Erik Johnston
10月 09, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement /register/available API (#291)
Signed-off-by:
N
MTRNord
<
mtrnord1@gmail.com
>
上级
619fec61
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
78 addition
and
14 deletion
+78
-14
src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go
...x-org/dendrite/clientapi/auth/storage/accounts/storage.go
+10
-0
src/github.com/matrix-org/dendrite/clientapi/routing/routing.go
...thub.com/matrix-org/dendrite/clientapi/routing/routing.go
+4
-0
src/github.com/matrix-org/dendrite/clientapi/writers/register.go
...hub.com/matrix-org/dendrite/clientapi/writers/register.go
+64
-14
未找到文件。
src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go
浏览文件 @
13107c6b
...
...
@@ -314,3 +314,13 @@ func (d *Database) GetThreePIDsForLocalpart(
)
(
threepids
[]
authtypes
.
ThreePID
,
err
error
)
{
return
d
.
threepids
.
selectThreePIDsForLocalpart
(
ctx
,
localpart
)
}
// CheckAccountAvailability checks if the username/localpart is already present in the database.
// If the DB returns sql.ErrNoRows the Localpart isn't taken.
func
(
d
*
Database
)
CheckAccountAvailability
(
ctx
context
.
Context
,
localpart
string
)
(
bool
,
error
)
{
_
,
err
:=
d
.
accounts
.
selectAccountByLocalpart
(
ctx
,
localpart
)
if
err
==
sql
.
ErrNoRows
{
return
true
,
nil
}
return
false
,
err
}
src/github.com/matrix-org/dendrite/clientapi/routing/routing.go
浏览文件 @
13107c6b
...
...
@@ -131,6 +131,10 @@ func Setup(
return
writers
.
LegacyRegister
(
req
,
accountDB
,
deviceDB
,
&
cfg
)
}))
.
Methods
(
"POST"
,
"OPTIONS"
)
r0mux
.
Handle
(
"/register/available"
,
common
.
MakeExternalAPI
(
"registerAvailable"
,
func
(
req
*
http
.
Request
)
util
.
JSONResponse
{
return
writers
.
RegisterAvailable
(
req
,
accountDB
)
}))
.
Methods
(
"GET"
)
r0mux
.
Handle
(
"/directory/room/{roomAlias}"
,
common
.
MakeAuthAPI
(
"directory_room"
,
deviceDB
,
func
(
req
*
http
.
Request
,
device
*
authtypes
.
Device
)
util
.
JSONResponse
{
vars
:=
mux
.
Vars
(
req
)
...
...
src/github.com/matrix-org/dendrite/clientapi/writers/register.go
浏览文件 @
13107c6b
...
...
@@ -91,33 +91,40 @@ type registerResponse struct {
DeviceID
string
`json:"device_id"`
}
//
Validate returns an error response if the username/password are
invalid
func
validate
(
username
,
password
string
)
*
util
.
JSONResponse
{
//
validateUserName returns an error response if the username is
invalid
func
validate
UserName
(
username
string
)
*
util
.
JSONResponse
{
// https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
if
len
(
password
)
>
maxPassword
Length
{
if
len
(
username
)
>
maxUsername
Length
{
return
&
util
.
JSONResponse
{
Code
:
400
,
JSON
:
jsonerror
.
BadJSON
(
fmt
.
Sprintf
(
"'
password' >%d characters"
,
maxPassword
Length
)),
JSON
:
jsonerror
.
BadJSON
(
fmt
.
Sprintf
(
"'
username' >%d characters"
,
maxUsername
Length
)),
}
}
else
if
len
(
username
)
>
maxUsernameLength
{
}
else
if
!
validUsernameRegex
.
MatchString
(
username
)
{
return
&
util
.
JSONResponse
{
Code
:
400
,
JSON
:
jsonerror
.
BadJSON
(
fmt
.
Sprintf
(
"'username' >%d characters"
,
maxUsernameLength
)
),
JSON
:
jsonerror
.
InvalidUsername
(
"User ID can only contain characters a-z, 0-9, or '_-./'"
),
}
}
else
if
len
(
password
)
>
0
&&
len
(
password
)
<
minPasswordLength
{
}
else
if
username
[
0
]
==
'_'
{
// Regex checks its not a zero length string
return
&
util
.
JSONResponse
{
Code
:
400
,
JSON
:
jsonerror
.
WeakPassword
(
fmt
.
Sprintf
(
"password too weak: min %d chars"
,
minPasswordLength
)
),
JSON
:
jsonerror
.
InvalidUsername
(
"User ID can't start with a '_'"
),
}
}
else
if
!
validUsernameRegex
.
MatchString
(
username
)
{
}
return
nil
}
// validatePassword returns an error response if the password is invalid
func
validatePassword
(
password
string
)
*
util
.
JSONResponse
{
// https://github.com/matrix-org/synapse/blob/v0.20.0/synapse/rest/client/v2_alpha/register.py#L161
if
len
(
password
)
>
maxPasswordLength
{
return
&
util
.
JSONResponse
{
Code
:
400
,
JSON
:
jsonerror
.
InvalidUsername
(
"User ID can only contain characters a-z, 0-9, or '_-./'"
),
JSON
:
jsonerror
.
BadJSON
(
fmt
.
Sprintf
(
"'password' >%d characters"
,
maxPasswordLength
)
),
}
}
else
if
username
[
0
]
==
'_'
{
// Regex checks its not a zero length string
}
else
if
len
(
password
)
>
0
&&
len
(
password
)
<
minPasswordLength
{
return
&
util
.
JSONResponse
{
Code
:
400
,
JSON
:
jsonerror
.
InvalidUsername
(
"User ID can't start with a '_'"
),
JSON
:
jsonerror
.
WeakPassword
(
fmt
.
Sprintf
(
"password too weak: min %d chars"
,
minPasswordLength
)
),
}
}
return
nil
...
...
@@ -149,7 +156,10 @@ func Register(
}
}
if
resErr
=
validate
(
r
.
Username
,
r
.
Password
);
resErr
!=
nil
{
if
resErr
=
validateUserName
(
r
.
Username
);
resErr
!=
nil
{
return
*
resErr
}
if
resErr
=
validatePassword
(
r
.
Password
);
resErr
!=
nil
{
return
*
resErr
}
...
...
@@ -209,7 +219,10 @@ func LegacyRegister(
if
resErr
!=
nil
{
return
*
resErr
}
if
resErr
=
validate
(
r
.
Username
,
r
.
Password
);
resErr
!=
nil
{
if
resErr
=
validateUserName
(
r
.
Username
);
resErr
!=
nil
{
return
*
resErr
}
if
resErr
=
validatePassword
(
r
.
Password
);
resErr
!=
nil
{
return
*
resErr
}
...
...
@@ -344,3 +357,40 @@ func isValidMacLogin(
return
hmac
.
Equal
(
givenMac
,
expectedMAC
),
nil
}
type
availableResponse
struct
{
Available
bool
`json:"available"`
}
// RegisterAvailable checks if the username is already taken or invalid
func
RegisterAvailable
(
req
*
http
.
Request
,
accountDB
*
accounts
.
Database
,
)
util
.
JSONResponse
{
username
:=
req
.
URL
.
Query
()
.
Get
(
"username"
)
if
err
:=
validateUserName
(
username
);
err
!=
nil
{
return
*
err
}
availability
,
availabilityErr
:=
accountDB
.
CheckAccountAvailability
(
req
.
Context
(),
username
)
if
availabilityErr
!=
nil
{
return
util
.
JSONResponse
{
Code
:
500
,
JSON
:
jsonerror
.
Unknown
(
"failed to check availability: "
+
availabilityErr
.
Error
()),
}
}
if
!
availability
{
return
util
.
JSONResponse
{
Code
:
400
,
JSON
:
jsonerror
.
InvalidUsername
(
"A different user ID has already been registered for this session"
),
}
}
return
util
.
JSONResponse
{
Code
:
200
,
JSON
:
availableResponse
{
Available
:
true
,
},
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录