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
d83f204d
Commit
d83f204d
authored
Jul 27, 2022
by
boojack
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update acl middleware
parent
873973a0
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
32 additions
and
41 deletions
+32
-41
acl.go
server/acl.go
+25
-32
memo.go
server/memo.go
+2
-2
server.go
server/server.go
+1
-1
tag.go
server/tag.go
+2
-2
user.go
server/user.go
+2
-3
user.go
store/user.go
+0
-1
No files found.
server/
basic_auth
.go
→
server/
acl
.go
View file @
d83f204d
...
@@ -51,11 +51,10 @@ func removeUserSession(ctx echo.Context) error {
...
@@ -51,11 +51,10 @@ func removeUserSession(ctx echo.Context) error {
return
nil
return
nil
}
}
// Use session to store user.id.
func
aclMiddleware
(
s
*
Server
,
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
func
BasicAuthMiddleware
(
s
*
Server
,
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
return
func
(
ctx
echo
.
Context
)
error
{
return
func
(
ctx
echo
.
Context
)
error
{
// Skip auth for some paths.
// Skip auth for some paths.
if
common
.
HasPrefixes
(
ctx
.
Path
(),
"/api/auth"
,
"/api/ping"
,
"/api/status"
,
"/api/user/:
userI
d"
)
{
if
common
.
HasPrefixes
(
ctx
.
Path
(),
"/api/auth"
,
"/api/ping"
,
"/api/status"
,
"/api/user/:
i
d"
)
{
return
next
(
ctx
)
return
next
(
ctx
)
}
}
...
@@ -76,42 +75,36 @@ func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
...
@@ -76,42 +75,36 @@ func BasicAuthMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
}
}
}
}
needAuth
:=
true
if
common
.
HasPrefixes
(
ctx
.
Path
(),
"/api/memo"
,
"/api/tag"
,
"/api/shortcut"
)
&&
ctx
.
Request
()
.
Method
==
http
.
MethodGet
{
if
_
,
err
:=
strconv
.
Atoi
(
ctx
.
QueryParam
(
"creatorId"
));
err
==
nil
{
needAuth
=
false
}
}
{
{
sess
,
_
:=
session
.
Get
(
"session"
,
ctx
)
sess
,
_
:=
session
.
Get
(
"session"
,
ctx
)
userIDValue
:=
sess
.
Values
[
userIDContextKey
]
userIDValue
:=
sess
.
Values
[
userIDContextKey
]
if
userIDValue
==
nil
&&
needAuth
{
if
userIDValue
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing userID in session"
)
userID
,
_
:=
strconv
.
Atoi
(
fmt
.
Sprintf
(
"%v"
,
userIDValue
))
}
userID
,
err
:=
strconv
.
Atoi
(
fmt
.
Sprintf
(
"%v"
,
userIDValue
))
if
err
!=
nil
&&
needAuth
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to malformatted user id in the session."
)
.
SetInternal
(
err
)
}
userFind
:=
&
api
.
UserFind
{
userFind
:=
&
api
.
UserFind
{
ID
:
&
userID
,
ID
:
&
userID
,
}
}
user
,
err
:=
s
.
Store
.
FindUser
(
userFind
)
user
,
err
:=
s
.
Store
.
FindUser
(
userFind
)
if
err
!=
nil
&&
needAuth
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
fmt
.
Sprintf
(
"Failed to find user by ID: %d"
,
userID
))
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
fmt
.
Sprintf
(
"Failed to find user by ID: %d"
,
userID
))
.
SetInternal
(
err
)
}
}
if
needAuth
{
if
user
!=
nil
{
if
user
==
nil
{
if
user
.
RowStatus
==
api
.
Archived
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
fmt
.
Sprintf
(
"Not found user ID: %d"
,
userID
))
}
else
if
user
.
RowStatus
==
api
.
Archived
{
return
echo
.
NewHTTPError
(
http
.
StatusForbidden
,
fmt
.
Sprintf
(
"User has been archived with email %s"
,
user
.
Email
))
return
echo
.
NewHTTPError
(
http
.
StatusForbidden
,
fmt
.
Sprintf
(
"User has been archived with email %s"
,
user
.
Email
))
}
}
ctx
.
Set
(
getUserIDContextKey
(),
userID
)
}
}
}
}
// Save userID into context.
if
common
.
HasPrefixes
(
ctx
.
Path
(),
"/api/memo"
,
"/api/tag"
,
"/api/shortcut"
)
&&
ctx
.
Request
()
.
Method
==
http
.
MethodGet
{
ctx
.
Set
(
getUserIDContextKey
(),
userID
)
if
_
,
err
:=
strconv
.
Atoi
(
ctx
.
QueryParam
(
"creatorId"
));
err
==
nil
{
return
next
(
ctx
)
}
}
userID
:=
ctx
.
Get
(
getUserIDContextKey
())
if
userID
==
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing userID in session"
)
}
}
return
next
(
ctx
)
return
next
(
ctx
)
...
...
server/memo.go
View file @
d83f204d
...
@@ -72,8 +72,8 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
...
@@ -72,8 +72,8 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
memoFind
.
CreatorID
=
&
userID
memoFind
.
CreatorID
=
&
userID
}
}
currentUserID
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
currentUserID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
currentUserID
==
api
.
UNKNOWN_ID
{
if
!
ok
{
if
memoFind
.
CreatorID
==
nil
{
if
memoFind
.
CreatorID
==
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Missing user id to find memo"
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Missing user id to find memo"
)
}
}
...
...
server/server.go
View file @
d83f204d
...
@@ -58,7 +58,7 @@ func NewServer(profile *profile.Profile) *Server {
...
@@ -58,7 +58,7 @@ func NewServer(profile *profile.Profile) *Server {
apiGroup
:=
e
.
Group
(
"/api"
)
apiGroup
:=
e
.
Group
(
"/api"
)
apiGroup
.
Use
(
func
(
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
apiGroup
.
Use
(
func
(
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
return
BasicAuth
Middleware
(
s
,
next
)
return
acl
Middleware
(
s
,
next
)
})
})
s
.
registerSystemRoutes
(
apiGroup
)
s
.
registerSystemRoutes
(
apiGroup
)
s
.
registerAuthRoutes
(
apiGroup
)
s
.
registerAuthRoutes
(
apiGroup
)
...
...
server/tag.go
View file @
d83f204d
...
@@ -25,8 +25,8 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
...
@@ -25,8 +25,8 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
memoFind
.
CreatorID
=
&
userID
memoFind
.
CreatorID
=
&
userID
}
}
currentUserID
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
currentUserID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
currentUserID
==
api
.
UNKNOWN_ID
{
if
!
ok
{
if
memoFind
.
CreatorID
==
nil
{
if
memoFind
.
CreatorID
==
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Missing user id to find memo"
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Missing user id to find memo"
)
}
}
...
...
server/user.go
View file @
d83f204d
...
@@ -83,12 +83,11 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
...
@@ -83,12 +83,11 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
// GET /api/user/me is used to check if the user is logged in.
// GET /api/user/me is used to check if the user is logged in.
g
.
GET
(
"/user/me"
,
func
(
c
echo
.
Context
)
error
{
g
.
GET
(
"/user/me"
,
func
(
c
echo
.
Context
)
error
{
user
SessionID
:=
c
.
Get
(
getUserIDContextKey
()
)
user
ID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
userSessionID
==
nil
{
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing auth session"
)
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing auth session"
)
}
}
userID
:=
userSessionID
.
(
int
)
userFind
:=
&
api
.
UserFind
{
userFind
:=
&
api
.
UserFind
{
ID
:
&
userID
,
ID
:
&
userID
,
}
}
...
...
store/user.go
View file @
d83f204d
...
@@ -255,7 +255,6 @@ func findUserList(db *sql.DB, find *api.UserFind) ([]*userRaw, error) {
...
@@ -255,7 +255,6 @@ func findUserList(db *sql.DB, find *api.UserFind) ([]*userRaw, error) {
&
userRaw
.
UpdatedTs
,
&
userRaw
.
UpdatedTs
,
&
userRaw
.
RowStatus
,
&
userRaw
.
RowStatus
,
);
err
!=
nil
{
);
err
!=
nil
{
fmt
.
Println
(
err
)
return
nil
,
FormatError
(
err
)
return
nil
,
FormatError
(
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