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
a797280e
Unverified
Commit
a797280e
authored
Jan 01, 2023
by
boojack
Committed by
GitHub
Jan 01, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update middleware skipper (#887)
* chore: update middleware skipper * chore: update
parent
293f88e4
Changes
13
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
82 additions
and
156 deletions
+82
-156
backend-tests-default.yml
.github/workflows/backend-tests-default.yml
+0
-20
backend-tests.yml
.github/workflows/backend-tests.yml
+0
-2
frontend-tests-default.yml
.github/workflows/frontend-tests-default.yml
+0
-25
frontend-tests.yml
.github/workflows/frontend-tests.yml
+0
-2
auth.go
api/auth.go
+2
-2
acl.go
server/acl.go
+19
-46
auth.go
server/auth.go
+5
-5
common.go
server/common.go
+40
-5
server.go
server/server.go
+7
-34
shortcut.go
server/shortcut.go
+1
-1
tag.go
server/tag.go
+5
-11
api.ts
web/src/helpers/api.ts
+1
-1
Auth.tsx
web/src/pages/Auth.tsx
+2
-2
No files found.
.github/workflows/backend-tests-default.yml
deleted
100644 → 0
View file @
293f88e4
name
:
Default Backend Test
on
:
pull_request
:
branches
:
-
main
-
"
release/*.*.*"
paths
:
-
"
web/**"
jobs
:
go-static-checks
:
runs-on
:
ubuntu-latest
steps
:
-
run
:
'
echo
"Not
required"'
go-tests
:
runs-on
:
ubuntu-latest
steps
:
-
run
:
'
echo
"Not
required"'
.github/workflows/backend-tests.yml
View file @
a797280e
...
@@ -5,8 +5,6 @@ on:
...
@@ -5,8 +5,6 @@ on:
branches
:
branches
:
-
main
-
main
-
"
release/*.*.*"
-
"
release/*.*.*"
paths-ignore
:
-
"
web/**"
jobs
:
jobs
:
go-static-checks
:
go-static-checks
:
...
...
.github/workflows/frontend-tests-default.yml
deleted
100644 → 0
View file @
293f88e4
name
:
Default Frontend Test
on
:
pull_request
:
branches
:
-
main
-
"
release/*.*.*"
paths-ignore
:
-
"
web/**"
jobs
:
eslint-checks
:
runs-on
:
ubuntu-latest
steps
:
-
run
:
'
echo
"Not
required"'
jest-tests
:
runs-on
:
ubuntu-latest
steps
:
-
run
:
'
echo
"Not
required"'
frontend-build
:
runs-on
:
ubuntu-latest
steps
:
-
run
:
'
echo
"Not
required"'
.github/workflows/frontend-tests.yml
View file @
a797280e
...
@@ -5,8 +5,6 @@ on:
...
@@ -5,8 +5,6 @@ on:
branches
:
branches
:
-
main
-
main
-
"
release/*.*.*"
-
"
release/*.*.*"
paths
:
-
"
web/**"
jobs
:
jobs
:
eslint-checks
:
eslint-checks
:
...
...
api/auth.go
View file @
a797280e
package
api
package
api
type
Sign
i
n
struct
{
type
Sign
I
n
struct
{
Username
string
`json:"username"`
Username
string
`json:"username"`
Password
string
`json:"password"`
Password
string
`json:"password"`
}
}
type
Sign
u
p
struct
{
type
Sign
U
p
struct
{
Username
string
`json:"username"`
Username
string
`json:"username"`
Password
string
`json:"password"`
Password
string
`json:"password"`
Role
Role
`json:"role"`
Role
Role
`json:"role"`
...
...
server/acl.go
View file @
a797280e
...
@@ -15,6 +15,7 @@ import (
...
@@ -15,6 +15,7 @@ import (
var
(
var
(
userIDContextKey
=
"user-id"
userIDContextKey
=
"user-id"
sessionName
=
"memos_session"
)
)
func
getUserIDContextKey
()
string
{
func
getUserIDContextKey
()
string
{
...
@@ -22,7 +23,7 @@ func getUserIDContextKey() string {
...
@@ -22,7 +23,7 @@ func getUserIDContextKey() string {
}
}
func
setUserSession
(
ctx
echo
.
Context
,
user
*
api
.
User
)
error
{
func
setUserSession
(
ctx
echo
.
Context
,
user
*
api
.
User
)
error
{
sess
,
_
:=
session
.
Get
(
"memos_session"
,
ctx
)
sess
,
_
:=
session
.
Get
(
sessionName
,
ctx
)
sess
.
Options
=
&
sessions
.
Options
{
sess
.
Options
=
&
sessions
.
Options
{
Path
:
"/"
,
Path
:
"/"
,
MaxAge
:
3600
*
24
*
30
,
MaxAge
:
3600
*
24
*
30
,
...
@@ -38,7 +39,7 @@ func setUserSession(ctx echo.Context, user *api.User) error {
...
@@ -38,7 +39,7 @@ func setUserSession(ctx echo.Context, user *api.User) error {
}
}
func
removeUserSession
(
ctx
echo
.
Context
)
error
{
func
removeUserSession
(
ctx
echo
.
Context
)
error
{
sess
,
_
:=
session
.
Get
(
"memos_session"
,
ctx
)
sess
,
_
:=
session
.
Get
(
sessionName
,
ctx
)
sess
.
Options
=
&
sessions
.
Options
{
sess
.
Options
=
&
sessions
.
Options
{
Path
:
"/"
,
Path
:
"/"
,
MaxAge
:
0
,
MaxAge
:
0
,
...
@@ -57,32 +58,11 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
...
@@ -57,32 +58,11 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
path
:=
c
.
Path
()
path
:=
c
.
Path
()
// Skip auth.
if
s
.
DefaultAuthSkipper
(
c
)
{
if
common
.
HasPrefixes
(
path
,
"/api/auth"
)
{
return
next
(
c
)
return
next
(
c
)
}
}
{
sess
,
_
:=
session
.
Get
(
sessionName
,
c
)
// If there is openId in query string and related user is found, then skip auth.
openID
:=
c
.
QueryParam
(
"openId"
)
if
openID
!=
""
{
userFind
:=
&
api
.
UserFind
{
OpenID
:
&
openID
,
}
user
,
err
:=
s
.
Store
.
FindUser
(
ctx
,
userFind
)
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find user by open_id"
)
.
SetInternal
(
err
)
}
if
user
!=
nil
{
// Stores userID into context.
c
.
Set
(
getUserIDContextKey
(),
user
.
ID
)
return
next
(
c
)
}
}
}
{
sess
,
_
:=
session
.
Get
(
"memos_session"
,
c
)
userIDValue
:=
sess
.
Values
[
userIDContextKey
]
userIDValue
:=
sess
.
Values
[
userIDContextKey
]
if
userIDValue
!=
nil
{
if
userIDValue
!=
nil
{
userID
,
_
:=
strconv
.
Atoi
(
fmt
.
Sprintf
(
"%v"
,
userIDValue
))
userID
,
_
:=
strconv
.
Atoi
(
fmt
.
Sprintf
(
"%v"
,
userIDValue
))
...
@@ -100,18 +80,11 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
...
@@ -100,18 +80,11 @@ func aclMiddleware(s *Server, next echo.HandlerFunc) echo.HandlerFunc {
c
.
Set
(
getUserIDContextKey
(),
userID
)
c
.
Set
(
getUserIDContextKey
(),
userID
)
}
}
}
}
}
if
common
.
HasPrefixes
(
path
,
"/api/ping"
,
"/api/status"
,
"/api/user/:id"
,
"/api/memo
/all"
,
"/api/memo/:memoId"
,
"/api/memo/amount
"
)
&&
c
.
Request
()
.
Method
==
http
.
MethodGet
{
if
common
.
HasPrefixes
(
path
,
"/api/ping"
,
"/api/status"
,
"/api/user/:id"
,
"/api/memo"
)
&&
c
.
Request
()
.
Method
==
http
.
MethodGet
{
return
next
(
c
)
return
next
(
c
)
}
}
if
common
.
HasPrefixes
(
path
,
"/api/memo"
,
"/api/tag"
,
"/api/shortcut"
)
&&
c
.
Request
()
.
Method
==
http
.
MethodGet
{
if
_
,
err
:=
strconv
.
Atoi
(
c
.
QueryParam
(
"creatorId"
));
err
==
nil
{
return
next
(
c
)
}
}
userID
:=
c
.
Get
(
getUserIDContextKey
())
userID
:=
c
.
Get
(
getUserIDContextKey
())
if
userID
==
nil
{
if
userID
==
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing user in session"
)
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing user in session"
)
...
...
server/auth.go
View file @
a797280e
...
@@ -16,7 +16,7 @@ import (
...
@@ -16,7 +16,7 @@ import (
func
(
s
*
Server
)
registerAuthRoutes
(
g
*
echo
.
Group
)
{
func
(
s
*
Server
)
registerAuthRoutes
(
g
*
echo
.
Group
)
{
g
.
POST
(
"/auth/signin"
,
func
(
c
echo
.
Context
)
error
{
g
.
POST
(
"/auth/signin"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
signin
:=
&
api
.
Sign
i
n
{}
signin
:=
&
api
.
Sign
I
n
{}
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
signin
);
err
!=
nil
{
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
signin
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted signin request"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted signin request"
)
.
SetInternal
(
err
)
}
}
...
@@ -56,7 +56,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
...
@@ -56,7 +56,7 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
g
.
POST
(
"/auth/signup"
,
func
(
c
echo
.
Context
)
error
{
g
.
POST
(
"/auth/signup"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
signup
:=
&
api
.
Sign
u
p
{}
signup
:=
&
api
.
Sign
U
p
{}
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
signup
);
err
!=
nil
{
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
signup
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted signup request"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted signup request"
)
.
SetInternal
(
err
)
}
}
...
@@ -130,14 +130,14 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
...
@@ -130,14 +130,14 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return
nil
return
nil
})
})
g
.
POST
(
"/auth/
log
out"
,
func
(
c
echo
.
Context
)
error
{
g
.
POST
(
"/auth/
sign
out"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
err
:=
removeUserSession
(
c
)
err
:=
removeUserSession
(
c
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to set
log
out session"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to set
sign
out session"
)
.
SetInternal
(
err
)
}
}
s
.
Collector
.
Collect
(
ctx
,
&
metric
.
Metric
{
s
.
Collector
.
Collect
(
ctx
,
&
metric
.
Metric
{
Name
:
"user
log
out"
,
Name
:
"user
sign
out"
,
})
})
return
c
.
JSON
(
http
.
StatusOK
,
true
)
return
c
.
JSON
(
http
.
StatusOK
,
true
)
...
...
server/common.go
View file @
a797280e
package
server
package
server
func
composeResponse
(
data
interface
{})
interface
{}
{
import
(
type
R
struct
{
"github.com/labstack/echo/v4"
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
)
type
response
struct
{
Data
interface
{}
`json:"data"`
Data
interface
{}
`json:"data"`
}
}
return
R
{
func
composeResponse
(
data
interface
{})
response
{
return
response
{
Data
:
data
,
Data
:
data
,
}
}
}
}
func
(
server
*
Server
)
DefaultAuthSkipper
(
c
echo
.
Context
)
bool
{
ctx
:=
c
.
Request
()
.
Context
()
path
:=
c
.
Path
()
// Skip auth.
if
common
.
HasPrefixes
(
path
,
"/api/auth"
)
{
return
true
}
// If there is openId in query string and related user is found, then skip auth.
openID
:=
c
.
QueryParam
(
"openId"
)
if
openID
!=
""
{
userFind
:=
&
api
.
UserFind
{
OpenID
:
&
openID
,
}
user
,
err
:=
server
.
Store
.
FindUser
(
ctx
,
userFind
)
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
false
}
if
user
!=
nil
{
// Stores userID into context.
c
.
Set
(
getUserIDContextKey
(),
user
.
ID
)
return
true
}
}
return
false
}
server/server.go
View file @
a797280e
...
@@ -4,8 +4,6 @@ import (
...
@@ -4,8 +4,6 @@ import (
"fmt"
"fmt"
"time"
"time"
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
"github.com/usememos/memos/store"
...
@@ -43,8 +41,12 @@ func NewServer(profile *profile.Profile) *Server {
...
@@ -43,8 +41,12 @@ func NewServer(profile *profile.Profile) *Server {
`"status":${status},"error":"${error}"}`
+
"
\n
"
,
`"status":${status},"error":"${error}"}`
+
"
\n
"
,
}))
}))
e
.
Use
(
middleware
.
Gzip
())
e
.
Use
(
middleware
.
CSRFWithConfig
(
middleware
.
CSRFConfig
{
e
.
Use
(
middleware
.
CSRFWithConfig
(
middleware
.
CSRFConfig
{
Skipper
:
s
.
OpenAPISkipper
,
Skipper
:
func
(
c
echo
.
Context
)
bool
{
return
s
.
DefaultAuthSkipper
(
c
)
},
TokenLookup
:
"cookie:_csrf"
,
TokenLookup
:
"cookie:_csrf"
,
}))
}))
...
@@ -92,35 +94,6 @@ func NewServer(profile *profile.Profile) *Server {
...
@@ -92,35 +94,6 @@ func NewServer(profile *profile.Profile) *Server {
return
s
return
s
}
}
func
(
server
*
Server
)
Run
()
error
{
func
(
s
*
Server
)
Run
()
error
{
return
server
.
e
.
Start
(
fmt
.
Sprintf
(
":%d"
,
server
.
Profile
.
Port
))
return
s
.
e
.
Start
(
fmt
.
Sprintf
(
":%d"
,
s
.
Profile
.
Port
))
}
func
(
server
*
Server
)
OpenAPISkipper
(
c
echo
.
Context
)
bool
{
ctx
:=
c
.
Request
()
.
Context
()
path
:=
c
.
Path
()
// Skip auth.
if
common
.
HasPrefixes
(
path
,
"/api/auth"
)
{
return
true
}
// If there is openId in query string and related user is found, then skip auth.
openID
:=
c
.
QueryParam
(
"openId"
)
if
openID
!=
""
{
userFind
:=
&
api
.
UserFind
{
OpenID
:
&
openID
,
}
user
,
err
:=
server
.
Store
.
FindUser
(
ctx
,
userFind
)
if
err
!=
nil
&&
common
.
ErrorCode
(
err
)
!=
common
.
NotFound
{
return
false
}
if
user
!=
nil
{
// Stores userID into context.
c
.
Set
(
getUserIDContextKey
(),
user
.
ID
)
return
true
}
}
return
false
}
}
server/shortcut.go
View file @
a797280e
...
@@ -91,10 +91,10 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
...
@@ -91,10 +91,10 @@ func (s *Server) registerShortcutRoutes(g *echo.Group) {
if
!
ok
{
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Missing user id to find shortcut"
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Missing user id to find shortcut"
)
}
}
shortcutFind
:=
&
api
.
ShortcutFind
{
shortcutFind
:=
&
api
.
ShortcutFind
{
CreatorID
:
&
userID
,
CreatorID
:
&
userID
,
}
}
list
,
err
:=
s
.
Store
.
FindShortcutList
(
ctx
,
shortcutFind
)
list
,
err
:=
s
.
Store
.
FindShortcutList
(
ctx
,
shortcutFind
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to fetch shortcut list"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to fetch shortcut list"
)
.
SetInternal
(
err
)
...
...
server/tag.go
View file @
a797280e
...
@@ -6,7 +6,6 @@ import (
...
@@ -6,7 +6,6 @@ import (
"net/http"
"net/http"
"regexp"
"regexp"
"sort"
"sort"
"strconv"
"github.com/usememos/memos/api"
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
"github.com/usememos/memos/common"
...
@@ -49,19 +48,14 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
...
@@ -49,19 +48,14 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
g
.
GET
(
"/tag"
,
func
(
c
echo
.
Context
)
error
{
g
.
GET
(
"/tag"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
ctx
:=
c
.
Request
()
.
Context
()
tagFind
:=
&
api
.
TagFind
{}
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
userID
,
err
:=
strconv
.
Atoi
(
c
.
QueryParam
(
"creatorId"
));
err
==
nil
{
tagFind
.
CreatorID
=
userID
}
if
tagFind
.
CreatorID
==
0
{
currentUserID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
!
ok
{
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Missing user id to find tag"
)
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Missing user id to find tag"
)
}
}
tagFind
.
CreatorID
=
currentUserID
}
tagFind
:=
&
api
.
TagFind
{
CreatorID
:
userID
,
}
tagList
,
err
:=
s
.
Store
.
FindTagList
(
ctx
,
tagFind
)
tagList
,
err
:=
s
.
Store
.
FindTagList
(
ctx
,
tagFind
)
if
err
!=
nil
{
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find tag list"
)
.
SetInternal
(
err
)
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find tag list"
)
.
SetInternal
(
err
)
...
...
web/src/helpers/api.ts
View file @
a797280e
...
@@ -34,7 +34,7 @@ export function signup(username: string, password: string, role: UserRole) {
...
@@ -34,7 +34,7 @@ export function signup(username: string, password: string, role: UserRole) {
}
}
export
function
signout
()
{
export
function
signout
()
{
return
axios
.
post
(
"/api/auth/
log
out"
);
return
axios
.
post
(
"/api/auth/
sign
out"
);
}
}
export
function
createUser
(
userCreate
:
UserCreate
)
{
export
function
createUser
(
userCreate
:
UserCreate
)
{
...
...
web/src/pages/Auth.tsx
View file @
a797280e
...
@@ -51,7 +51,7 @@ const Auth = () => {
...
@@ -51,7 +51,7 @@ const Auth = () => {
globalStore
.
setAppearance
(
appearance
);
globalStore
.
setAppearance
(
appearance
);
};
};
const
handleSign
inBtns
Click
=
async
()
=>
{
const
handleSign
InBtn
Click
=
async
()
=>
{
if
(
actionBtnLoadingState
.
isLoading
)
{
if
(
actionBtnLoadingState
.
isLoading
)
{
return
;
return
;
}
}
...
@@ -153,7 +153,7 @@ const Auth = () => {
...
@@ -153,7 +153,7 @@ const Auth = () => {
<
span
className=
"mr-2 font-mono text-gray-200"
>
/
</
span
>
<
span
className=
"mr-2 font-mono text-gray-200"
>
/
</
span
>
</>
</>
)
}
)
}
<
button
className=
{
`btn-primary ${actionBtnLoadingState.isLoading ? "requesting" : ""}`
}
onClick=
{
handleSign
inBtns
Click
}
>
<
button
className=
{
`btn-primary ${actionBtnLoadingState.isLoading ? "requesting" : ""}`
}
onClick=
{
handleSign
InBtn
Click
}
>
{
t
(
"common.sign-in"
)
}
{
t
(
"common.sign-in"
)
}
</
button
>
</
button
>
</>
</>
...
...
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