Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
05a5c59a
Commit
05a5c59a
authored
Aug 20, 2022
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update user create validator
parent
734d5f3a
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
88 additions
and
18 deletions
+88
-18
user.go
api/user.go
+20
-0
error.go
common/error.go
+0
-5
util.go
common/util.go
+9
-0
util_test.go
common/util_test.go
+31
-0
auth.go
server/auth.go
+10
-13
user.go
server/user.go
+18
-0
No files found.
api/user.go
View file @
05a5c59a
package
api
package
api
import
(
"fmt"
"github.com/usememos/memos/common"
)
// Role is the type of a role.
// Role is the type of a role.
type
Role
string
type
Role
string
...
@@ -47,6 +53,20 @@ type UserCreate struct {
...
@@ -47,6 +53,20 @@ type UserCreate struct {
OpenID
string
OpenID
string
}
}
func
(
create
UserCreate
)
Validate
()
error
{
if
!
common
.
ValidateEmail
(
create
.
Email
)
{
return
fmt
.
Errorf
(
"invalid email format"
)
}
if
len
(
create
.
Email
)
<
6
{
return
fmt
.
Errorf
(
"email is too short, minimum length is 6"
)
}
if
len
(
create
.
Password
)
<
6
{
return
fmt
.
Errorf
(
"password is too short, minimum length is 6"
)
}
return
nil
}
type
UserPatch
struct
{
type
UserPatch
struct
{
ID
int
ID
int
...
...
common/error.go
View file @
05a5c59a
...
@@ -17,11 +17,6 @@ const (
...
@@ -17,11 +17,6 @@ const (
NotFound
Code
=
4
NotFound
Code
=
4
Conflict
Code
=
5
Conflict
Code
=
5
NotImplemented
Code
=
6
NotImplemented
Code
=
6
// 101 ~ 199 db error
DbConnectionFailure
Code
=
101
DbStatementSyntaxError
Code
=
102
DbExecutionError
Code
=
103
)
)
// Error represents an application-specific error. Application errors can be
// Error represents an application-specific error. Application errors can be
...
...
common/util.go
View file @
05a5c59a
package
common
package
common
import
(
import
(
"net/mail"
"strings"
"strings"
"github.com/google/uuid"
"github.com/google/uuid"
...
@@ -16,6 +17,14 @@ func HasPrefixes(src string, prefixes ...string) bool {
...
@@ -16,6 +17,14 @@ func HasPrefixes(src string, prefixes ...string) bool {
return
false
return
false
}
}
// ValidateEmail validates the email.
func
ValidateEmail
(
email
string
)
bool
{
if
_
,
err
:=
mail
.
ParseAddress
(
email
);
err
!=
nil
{
return
false
}
return
true
}
func
GenUUID
()
string
{
func
GenUUID
()
string
{
return
uuid
.
New
()
.
String
()
return
uuid
.
New
()
.
String
()
}
}
common/util_test.go
0 → 100644
View file @
05a5c59a
package
common
import
(
"testing"
)
func
TestValidateEmail
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
email
string
want
bool
}{
{
email
:
"t@gmail.com"
,
want
:
true
,
},
{
email
:
"@qq.com"
,
want
:
false
,
},
{
email
:
"1@gmail"
,
want
:
true
,
},
}
for
_
,
test
:=
range
tests
{
result
:=
ValidateEmail
(
test
.
email
)
if
result
!=
test
.
want
{
t
.
Errorf
(
"Validate Email %s: got result %v, want %v."
,
test
.
email
,
result
,
test
.
want
)
}
}
}
server/auth.go
View file @
05a5c59a
...
@@ -80,13 +80,15 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
...
@@ -80,13 +80,15 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted signup request"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted signup request"
)
.
SetInternal
(
err
)
}
}
// Validate signup form.
userCreate
:=
&
api
.
UserCreate
{
// We can do stricter checks later.
Email
:
signup
.
Email
,
if
len
(
signup
.
Email
)
<
6
{
Role
:
api
.
Role
(
signup
.
Role
),
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Email is too short, minimum length is 6."
)
Name
:
signup
.
Name
,
Password
:
signup
.
Password
,
OpenID
:
common
.
GenUUID
(),
}
}
if
len
(
signup
.
Password
)
<
6
{
if
err
:=
userCreate
.
Validate
();
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"
Password is too short, minimum length is 6."
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"
Invalid user create format."
)
.
SetInternal
(
err
)
}
}
passwordHash
,
err
:=
bcrypt
.
GenerateFromPassword
([]
byte
(
signup
.
Password
),
bcrypt
.
DefaultCost
)
passwordHash
,
err
:=
bcrypt
.
GenerateFromPassword
([]
byte
(
signup
.
Password
),
bcrypt
.
DefaultCost
)
...
@@ -94,13 +96,8 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
...
@@ -94,13 +96,8 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to generate password hash"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to generate password hash"
)
.
SetInternal
(
err
)
}
}
userCreate
:=
&
api
.
UserCreate
{
userCreate
.
PasswordHash
=
string
(
passwordHash
)
Email
:
signup
.
Email
,
Role
:
api
.
Role
(
signup
.
Role
),
Name
:
signup
.
Name
,
PasswordHash
:
string
(
passwordHash
),
OpenID
:
common
.
GenUUID
(),
}
user
,
err
:=
s
.
Store
.
CreateUser
(
ctx
,
userCreate
)
user
,
err
:=
s
.
Store
.
CreateUser
(
ctx
,
userCreate
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to create user"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to create user"
)
.
SetInternal
(
err
)
...
...
server/user.go
View file @
05a5c59a
...
@@ -16,11 +16,29 @@ import (
...
@@ -16,11 +16,29 @@ import (
func
(
s
*
Server
)
registerUserRoutes
(
g
*
echo
.
Group
)
{
func
(
s
*
Server
)
registerUserRoutes
(
g
*
echo
.
Group
)
{
g
.
POST
(
"/user"
,
func
(
c
echo
.
Context
)
error
{
g
.
POST
(
"/user"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing auth session"
)
}
currentUser
,
err
:=
s
.
Store
.
FindUser
(
ctx
,
&
api
.
UserFind
{
ID
:
&
userID
,
})
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find user by id"
)
.
SetInternal
(
err
)
}
if
currentUser
.
Role
!=
api
.
Host
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Only Host user can create member."
)
}
userCreate
:=
&
api
.
UserCreate
{}
userCreate
:=
&
api
.
UserCreate
{}
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
userCreate
);
err
!=
nil
{
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
userCreate
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post user request"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post user request"
)
.
SetInternal
(
err
)
}
}
if
err
:=
userCreate
.
Validate
();
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Invalid user create format."
)
.
SetInternal
(
err
)
}
passwordHash
,
err
:=
bcrypt
.
GenerateFromPassword
([]
byte
(
userCreate
.
Password
),
bcrypt
.
DefaultCost
)
passwordHash
,
err
:=
bcrypt
.
GenerateFromPassword
([]
byte
(
userCreate
.
Password
),
bcrypt
.
DefaultCost
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to generate password hash"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to generate password hash"
)
.
SetInternal
(
err
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment