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
e266d88e
Unverified
Commit
e266d88e
authored
Aug 12, 2023
by
boojack
Committed by
GitHub
Aug 12, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: add acl config (#2128)
parent
0bb5f7f9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
18 deletions
+42
-18
acl.go
api/v2/acl.go
+16
-18
acl_config.go
api/v2/acl_config.go
+26
-0
No files found.
api/v2/acl.go
View file @
e266d88e
...
...
@@ -27,30 +27,16 @@ const (
UserIDContextKey
ContextKey
=
iota
)
var
authenticationAllowlistMethods
=
map
[
string
]
bool
{
"/memos.api.v2.SystemService/GetSystemInfo"
:
true
,
"/memos.api.v2.UserService/GetUser"
:
true
,
"/memos.api.v2.MemoService/ListMemos"
:
true
,
}
// IsAuthenticationAllowed returns whether the method is exempted from authentication.
func
IsAuthenticationAllowed
(
fullMethodName
string
)
bool
{
if
strings
.
HasPrefix
(
fullMethodName
,
"/grpc.reflection"
)
{
return
true
}
return
authenticationAllowlistMethods
[
fullMethodName
]
}
// GRPCAuthInterceptor is the auth interceptor for gRPC server.
type
GRPCAuthInterceptor
struct
{
s
tore
*
store
.
Store
S
tore
*
store
.
Store
secret
string
}
// NewGRPCAuthInterceptor returns a new API auth interceptor.
func
NewGRPCAuthInterceptor
(
store
*
store
.
Store
,
secret
string
)
*
GRPCAuthInterceptor
{
return
&
GRPCAuthInterceptor
{
s
tore
:
store
,
S
tore
:
store
,
secret
:
secret
,
}
}
...
...
@@ -68,11 +54,23 @@ func (in *GRPCAuthInterceptor) AuthenticationInterceptor(ctx context.Context, re
userID
,
err
:=
in
.
authenticate
(
ctx
,
accessTokenStr
)
if
err
!=
nil
{
if
IsAuthenticationAllowe
d
(
serverInfo
.
FullMethod
)
{
if
isUnauthorizeAllowedMetho
d
(
serverInfo
.
FullMethod
)
{
return
handler
(
ctx
,
request
)
}
return
nil
,
err
}
user
,
err
:=
in
.
Store
.
GetUser
(
ctx
,
&
store
.
FindUser
{
ID
:
&
userID
,
})
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"failed to get user"
)
}
if
user
==
nil
{
return
nil
,
status
.
Errorf
(
codes
.
Unauthenticated
,
"user ID %q not exists in the access token"
,
userID
)
}
if
isOnlyForAdminAllowedMethod
(
serverInfo
.
FullMethod
)
&&
user
.
Role
!=
store
.
RoleHost
&&
user
.
Role
!=
store
.
RoleAdmin
{
return
nil
,
status
.
Errorf
(
codes
.
PermissionDenied
,
"user ID %q is not admin"
,
userID
)
}
// Stores userID into context.
childCtx
:=
context
.
WithValue
(
ctx
,
UserIDContextKey
,
userID
)
...
...
@@ -110,7 +108,7 @@ func (in *GRPCAuthInterceptor) authenticate(ctx context.Context, accessTokenStr
if
err
!=
nil
{
return
0
,
status
.
Errorf
(
codes
.
Unauthenticated
,
"malformed ID %q in the access token"
,
claims
.
Subject
)
}
user
,
err
:=
in
.
s
tore
.
GetUser
(
ctx
,
&
store
.
FindUser
{
user
,
err
:=
in
.
S
tore
.
GetUser
(
ctx
,
&
store
.
FindUser
{
ID
:
&
userID
,
})
if
err
!=
nil
{
...
...
api/v2/acl_config.go
0 → 100644
View file @
e266d88e
package
v2
import
"strings"
var
authenticationAllowlistMethods
=
map
[
string
]
bool
{
"/memos.api.v2.SystemService/GetSystemInfo"
:
true
,
"/memos.api.v2.UserService/GetUser"
:
true
,
"/memos.api.v2.MemoService/ListMemos"
:
true
,
}
// isUnauthorizeAllowedMethod returns whether the method is exempted from authentication.
func
isUnauthorizeAllowedMethod
(
fullMethodName
string
)
bool
{
if
strings
.
HasPrefix
(
fullMethodName
,
"/grpc.reflection"
)
{
return
true
}
return
authenticationAllowlistMethods
[
fullMethodName
]
}
var
allowedMethodsOnlyForAdmin
=
map
[
string
]
bool
{
"/memos.api.v2.UserService/CreateUser"
:
true
,
}
// isOnlyForAdminAllowedMethod returns true if the method is allowed to be called only by admin.
func
isOnlyForAdminAllowedMethod
(
methodName
string
)
bool
{
return
allowedMethodsOnlyForAdmin
[
methodName
]
}
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