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
90641201
Unverified
Commit
90641201
authored
Nov 08, 2025
by
boojack
Committed by
GitHub
Nov 08, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(api): remove test_auth.go and inline test helpers (#5235)
Co-authored-by:
Claude
<
noreply@anthropic.com
>
parent
13fea64d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
6 additions
and
25 deletions
+6
-25
acl.go
server/router/api/v1/acl.go
+4
-4
auth_service.go
server/router/api/v1/auth_service.go
+1
-1
test_helper.go
server/router/api/v1/test/test_helper.go
+1
-1
test_auth.go
server/router/api/v1/test_auth.go
+0
-19
No files found.
server/router/api/v1/acl.go
View file @
90641201
...
@@ -23,9 +23,9 @@ import (
...
@@ -23,9 +23,9 @@ import (
type
ContextKey
int
type
ContextKey
int
const
(
const
(
//
u
serIDContextKey stores the authenticated user's ID in the context.
//
U
serIDContextKey stores the authenticated user's ID in the context.
// Set for both session-based and token-based authentication.
// Set for both session-based and token-based authentication.
u
serIDContextKey
ContextKey
=
iota
U
serIDContextKey
ContextKey
=
iota
// sessionIDContextKey stores the session ID in the context.
// sessionIDContextKey stores the session ID in the context.
// Only set for session-based authentication (cookie auth).
// Only set for session-based authentication (cookie auth).
...
@@ -59,7 +59,7 @@ func NewGRPCAuthInterceptor(store *store.Store, secret string) *GRPCAuthIntercep
...
@@ -59,7 +59,7 @@ func NewGRPCAuthInterceptor(store *store.Store, secret string) *GRPCAuthIntercep
// 4. Reject: Return 401 Unauthenticated if none of the above succeed
// 4. Reject: Return 401 Unauthenticated if none of the above succeed
//
//
// On successful authentication, sets context values:
// On successful authentication, sets context values:
// -
u
serIDContextKey: The authenticated user's ID (always set)
// -
U
serIDContextKey: The authenticated user's ID (always set)
// - sessionIDContextKey: Session ID (only for cookie auth)
// - sessionIDContextKey: Session ID (only for cookie auth)
// - accessTokenContextKey: JWT token (only for Bearer token auth).
// - accessTokenContextKey: JWT token (only for Bearer token auth).
func
(
in
*
GRPCAuthInterceptor
)
AuthenticationInterceptor
(
ctx
context
.
Context
,
request
any
,
serverInfo
*
grpc
.
UnaryServerInfo
,
handler
grpc
.
UnaryHandler
)
(
any
,
error
)
{
func
(
in
*
GRPCAuthInterceptor
)
AuthenticationInterceptor
(
ctx
context
.
Context
,
request
any
,
serverInfo
*
grpc
.
UnaryServerInfo
,
handler
grpc
.
UnaryHandler
)
(
any
,
error
)
{
...
@@ -115,7 +115,7 @@ func (in *GRPCAuthInterceptor) handleAuthenticatedRequest(ctx context.Context, r
...
@@ -115,7 +115,7 @@ func (in *GRPCAuthInterceptor) handleAuthenticatedRequest(ctx context.Context, r
}
}
// Set context values
// Set context values
ctx
=
context
.
WithValue
(
ctx
,
u
serIDContextKey
,
user
.
ID
)
ctx
=
context
.
WithValue
(
ctx
,
U
serIDContextKey
,
user
.
ID
)
if
sessionID
!=
""
{
if
sessionID
!=
""
{
// Session-based authentication
// Session-based authentication
...
...
server/router/api/v1/auth_service.go
View file @
90641201
...
@@ -325,7 +325,7 @@ func (*APIV1Service) buildSessionCookie(ctx context.Context, sessionCookieValue
...
@@ -325,7 +325,7 @@ func (*APIV1Service) buildSessionCookie(ctx context.Context, sessionCookieValue
}
}
func
(
s
*
APIV1Service
)
GetCurrentUser
(
ctx
context
.
Context
)
(
*
store
.
User
,
error
)
{
func
(
s
*
APIV1Service
)
GetCurrentUser
(
ctx
context
.
Context
)
(
*
store
.
User
,
error
)
{
userID
,
ok
:=
ctx
.
Value
(
u
serIDContextKey
)
.
(
int32
)
userID
,
ok
:=
ctx
.
Value
(
U
serIDContextKey
)
.
(
int32
)
if
!
ok
{
if
!
ok
{
return
nil
,
nil
return
nil
,
nil
}
}
...
...
server/router/api/v1/test/test_helper.go
View file @
90641201
...
@@ -82,5 +82,5 @@ func (ts *TestService) CreateRegularUser(ctx context.Context, username string) (
...
@@ -82,5 +82,5 @@ func (ts *TestService) CreateRegularUser(ctx context.Context, username string) (
// CreateUserContext creates a context with the given user's ID for authentication.
// CreateUserContext creates a context with the given user's ID for authentication.
func
(
*
TestService
)
CreateUserContext
(
ctx
context
.
Context
,
userID
int32
)
context
.
Context
{
func
(
*
TestService
)
CreateUserContext
(
ctx
context
.
Context
,
userID
int32
)
context
.
Context
{
// Use the real context key from the parent package
// Use the real context key from the parent package
return
apiv1
.
CreateTestUserContext
(
ctx
,
userID
)
return
context
.
WithValue
(
ctx
,
apiv1
.
UserIDContextKey
,
userID
)
}
}
server/router/api/v1/test_auth.go
deleted
100644 → 0
View file @
13fea64d
package
v1
import
(
"context"
"github.com/usememos/memos/store"
)
// CreateTestUserContext creates a context with user's ID for testing purposes.
// This function is only intended for use in tests.
func
CreateTestUserContext
(
ctx
context
.
Context
,
userID
int32
)
context
.
Context
{
return
context
.
WithValue
(
ctx
,
userIDContextKey
,
userID
)
}
// CreateTestUserContextWithUser creates a context and ensures the user exists for testing.
// This function is only intended for use in tests.
func
CreateTestUserContextWithUser
(
ctx
context
.
Context
,
_
*
APIV1Service
,
user
*
store
.
User
)
context
.
Context
{
return
context
.
WithValue
(
ctx
,
userIDContextKey
,
user
.
ID
)
}
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