chore: tweak store definition

This commit is contained in:
Steven
2024-04-13 11:54:37 +08:00
parent 6ee3b0f704
commit cebc46adc7
25 changed files with 298 additions and 895 deletions

View File

@@ -0,0 +1 @@
The legacy API is deprecated and has be removed. Please use the v2 API instead.

View File

@@ -14,7 +14,6 @@ import (
"github.com/usememos/memos/internal/util"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/route/api/auth"
"github.com/usememos/memos/store"
)
@@ -84,7 +83,7 @@ func (in *GRPCAuthInterceptor) authenticate(ctx context.Context, accessToken str
if accessToken == "" {
return "", status.Errorf(codes.Unauthenticated, "access token not found")
}
claims := &auth.ClaimsMessage{}
claims := &ClaimsMessage{}
_, err := jwt.ParseWithClaims(accessToken, claims, func(t *jwt.Token) (any, error) {
if t.Method.Alg() != jwt.SigningMethodHS256.Name {
return nil, status.Errorf(codes.Unauthenticated, "unexpected access token signing method=%v, expect %v", t.Header["alg"], jwt.SigningMethodHS256)
@@ -145,7 +144,7 @@ func getTokenFromMetadata(md metadata.MD) (string, error) {
header := http.Header{}
header.Add("Cookie", t)
request := http.Request{Header: header}
if v, _ := request.Cookie(auth.AccessTokenCookieName); v != nil {
if v, _ := request.Cookie(AccessTokenCookieName); v != nil {
accessToken = v.Value
}
}

View File

@@ -939,7 +939,7 @@ paths:
type: string
format: date-time
rowStatus:
$ref: '#/definitions/apiv2RowStatus'
$ref: '#/definitions/v2RowStatus'
name:
type: string
url:
@@ -1159,7 +1159,7 @@ paths:
type: string
description: The user defined id of the memo.
rowStatus:
$ref: '#/definitions/apiv2RowStatus'
$ref: '#/definitions/v2RowStatus'
creator:
type: string
title: |-
@@ -1200,7 +1200,7 @@ paths:
type: array
items:
type: object
$ref: '#/definitions/apiv2Reaction'
$ref: '#/definitions/v2Reaction'
readOnly: true
tags:
- MemoService
@@ -1913,7 +1913,7 @@ paths:
password:
type: string
rowStatus:
$ref: '#/definitions/apiv2RowStatus'
$ref: '#/definitions/v2RowStatus'
createTime:
type: string
format: date-time
@@ -2052,45 +2052,6 @@ definitions:
type: string
fieldMapping:
$ref: '#/definitions/apiv2FieldMapping'
apiv2Reaction:
type: object
properties:
id:
type: integer
format: int32
creator:
type: string
title: |-
The name of the creator.
Format: users/{id}
contentId:
type: string
reactionType:
$ref: '#/definitions/apiv2ReactionType'
apiv2ReactionType:
type: string
enum:
- TYPE_UNSPECIFIED
- THUMBS_UP
- THUMBS_DOWN
- HEART
- FIRE
- CLAPPING_HANDS
- LAUGH
- OK_HAND
- ROCKET
- EYES
- THINKING_FACE
- CLOWN_FACE
- QUESTION_MARK
default: TYPE_UNSPECIFIED
apiv2RowStatus:
type: string
enum:
- ROW_STATUS_UNSPECIFIED
- ACTIVE
- ARCHIVED
default: ROW_STATUS_UNSPECIFIED
apiv2S3Config:
type: object
properties:
@@ -2155,27 +2116,6 @@ definitions:
telegramUserId:
type: string
description: The telegram user id of the user.
apiv2Webhook:
type: object
properties:
id:
type: integer
format: int32
creatorId:
type: integer
format: int32
createdTime:
type: string
format: date-time
updatedTime:
type: string
format: date-time
rowStatus:
$ref: '#/definitions/apiv2RowStatus'
name:
type: string
url:
type: string
apiv2WorkspaceCustomProfile:
type: object
properties:
@@ -2370,7 +2310,7 @@ definitions:
type: object
properties:
webhook:
$ref: '#/definitions/apiv2Webhook'
$ref: '#/definitions/v2Webhook'
v2DeleteIdentityProviderResponse:
type: object
v2DeleteInboxResponse:
@@ -2465,7 +2405,7 @@ definitions:
type: object
properties:
webhook:
$ref: '#/definitions/apiv2Webhook'
$ref: '#/definitions/v2Webhook'
v2GetWorkspaceProfileResponse:
type: object
properties:
@@ -2554,7 +2494,7 @@ definitions:
type: array
items:
type: object
$ref: '#/definitions/apiv2Reaction'
$ref: '#/definitions/v2Reaction'
v2ListMemoRelationsResponse:
type: object
properties:
@@ -2631,7 +2571,7 @@ definitions:
type: array
items:
type: object
$ref: '#/definitions/apiv2Webhook'
$ref: '#/definitions/v2Webhook'
v2ListWorkspaceSettingsResponse:
type: object
properties:
@@ -2653,7 +2593,7 @@ definitions:
type: string
description: The user defined id of the memo.
rowStatus:
$ref: '#/definitions/apiv2RowStatus'
$ref: '#/definitions/v2RowStatus'
creator:
type: string
title: |-
@@ -2694,7 +2634,7 @@ definitions:
type: array
items:
type: object
$ref: '#/definitions/apiv2Reaction'
$ref: '#/definitions/v2Reaction'
readOnly: true
v2MemoRelation:
type: object
@@ -2718,6 +2658,38 @@ definitions:
- REFERENCE
- COMMENT
default: TYPE_UNSPECIFIED
v2Reaction:
type: object
properties:
id:
type: integer
format: int32
creator:
type: string
title: |-
The name of the creator.
Format: users/{id}
contentId:
type: string
reactionType:
$ref: '#/definitions/v2ReactionType'
v2ReactionType:
type: string
enum:
- TYPE_UNSPECIFIED
- THUMBS_UP
- THUMBS_DOWN
- HEART
- FIRE
- CLAPPING_HANDS
- LAUGH
- OK_HAND
- ROCKET
- EYES
- THINKING_FACE
- CLOWN_FACE
- QUESTION_MARK
default: TYPE_UNSPECIFIED
v2RenameTagResponse:
type: object
properties:
@@ -2756,6 +2728,13 @@ definitions:
title: |-
The related memo.
Format: memos/{id}
v2RowStatus:
type: string
enum:
- ROW_STATUS_UNSPECIFIED
- ACTIVE
- ARCHIVED
default: ROW_STATUS_UNSPECIFIED
v2SearchMemosResponse:
type: object
properties:
@@ -2856,12 +2835,12 @@ definitions:
type: object
properties:
webhook:
$ref: '#/definitions/apiv2Webhook'
$ref: '#/definitions/v2Webhook'
v2UpsertMemoReactionResponse:
type: object
properties:
reaction:
$ref: '#/definitions/apiv2Reaction'
$ref: '#/definitions/v2Reaction'
v2UpsertTagRequest:
type: object
properties:
@@ -2899,7 +2878,7 @@ definitions:
password:
type: string
rowStatus:
$ref: '#/definitions/apiv2RowStatus'
$ref: '#/definitions/v2RowStatus'
createTime:
type: string
format: date-time
@@ -2927,6 +2906,27 @@ definitions:
- PROTECTED
- PUBLIC
default: VISIBILITY_UNSPECIFIED
v2Webhook:
type: object
properties:
id:
type: integer
format: int32
creatorId:
type: integer
format: int32
createdTime:
type: string
format: date-time
updatedTime:
type: string
format: date-time
rowStatus:
$ref: '#/definitions/v2RowStatus'
name:
type: string
url:
type: string
v2WorkspaceProfile:
type: object
properties:

View File

@@ -1,4 +1,4 @@
package auth
package v2
import (
"fmt"

View File

@@ -19,7 +19,6 @@ import (
"github.com/usememos/memos/plugin/idp/oauth2"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/route/api/auth"
"github.com/usememos/memos/store"
)
@@ -58,7 +57,7 @@ func (s *APIV2Service) SignIn(ctx context.Context, request *apiv2pb.SignInReques
return nil, status.Errorf(codes.InvalidArgument, "unmatched email and password")
}
expireTime := time.Now().Add(auth.AccessTokenDuration)
expireTime := time.Now().Add(AccessTokenDuration)
if request.NeverExpire {
// Set the expire time to 100 years.
expireTime = time.Now().Add(100 * 365 * 24 * time.Hour)
@@ -141,7 +140,7 @@ func (s *APIV2Service) SignInWithSSO(ctx context.Context, request *apiv2pb.SignI
return nil, status.Errorf(codes.PermissionDenied, fmt.Sprintf("user has been archived with username %s", userInfo.Identifier))
}
if err := s.doSignIn(ctx, user, time.Now().Add(auth.AccessTokenDuration)); err != nil {
if err := s.doSignIn(ctx, user, time.Now().Add(AccessTokenDuration)); err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to sign in, err: %s", err))
}
return &apiv2pb.SignInWithSSOResponse{
@@ -150,7 +149,7 @@ func (s *APIV2Service) SignInWithSSO(ctx context.Context, request *apiv2pb.SignI
}
func (s *APIV2Service) doSignIn(ctx context.Context, user *store.User, expireTime time.Time) error {
accessToken, err := auth.GenerateAccessToken(user.Email, user.ID, expireTime, []byte(s.Secret))
accessToken, err := GenerateAccessToken(user.Email, user.ID, expireTime, []byte(s.Secret))
if err != nil {
return status.Errorf(codes.Internal, fmt.Sprintf("failed to generate tokens, err: %s", err))
}
@@ -213,7 +212,7 @@ func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpReques
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to create user, err: %s", err))
}
if err := s.doSignIn(ctx, user, time.Now().Add(auth.AccessTokenDuration)); err != nil {
if err := s.doSignIn(ctx, user, time.Now().Add(AccessTokenDuration)); err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to sign in, err: %s", err))
}
return &apiv2pb.SignUpResponse{
@@ -243,7 +242,7 @@ func (s *APIV2Service) clearAccessTokenCookie(ctx context.Context) error {
func (*APIV2Service) buildAccessTokenCookie(ctx context.Context, accessToken string, expireTime time.Time) (string, error) {
attrs := []string{
fmt.Sprintf("%s=%s", auth.AccessTokenCookieName, accessToken),
fmt.Sprintf("%s=%s", AccessTokenCookieName, accessToken),
"Path=/",
"HttpOnly",
}

View File

@@ -21,7 +21,6 @@ import (
"github.com/usememos/memos/internal/util"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/route/api/auth"
"github.com/usememos/memos/store"
)
@@ -356,7 +355,7 @@ func (s *APIV2Service) ListUserAccessTokens(ctx context.Context, _ *apiv2pb.List
accessTokens := []*apiv2pb.UserAccessToken{}
for _, userAccessToken := range userAccessTokens {
claims := &auth.ClaimsMessage{}
claims := &ClaimsMessage{}
_, err := jwt.ParseWithClaims(userAccessToken.AccessToken, claims, func(t *jwt.Token) (any, error) {
if t.Method.Alg() != jwt.SigningMethodHS256.Name {
return nil, errors.Errorf("unexpected access token signing method=%v, expect %v", t.Header["alg"], jwt.SigningMethodHS256)
@@ -405,12 +404,12 @@ func (s *APIV2Service) CreateUserAccessToken(ctx context.Context, request *apiv2
expiresAt = request.ExpiresAt.AsTime()
}
accessToken, err := auth.GenerateAccessToken(user.Username, user.ID, expiresAt, []byte(s.Secret))
accessToken, err := GenerateAccessToken(user.Username, user.ID, expiresAt, []byte(s.Secret))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to generate access token: %v", err)
}
claims := &auth.ClaimsMessage{}
claims := &ClaimsMessage{}
_, err = jwt.ParseWithClaims(accessToken, claims, func(t *jwt.Token) (any, error) {
if t.Method.Alg() != jwt.SigningMethodHS256.Name {
return nil, errors.Errorf("unexpected access token signing method=%v, expect %v", t.Header["alg"], jwt.SigningMethodHS256)

View File

@@ -9,7 +9,6 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
@@ -19,8 +18,8 @@ func (s *APIV2Service) CreateWebhook(ctx context.Context, request *apiv2pb.Creat
return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
}
webhook, err := s.Store.CreateWebhook(ctx, &storepb.Webhook{
CreatorId: currentUser.ID,
webhook, err := s.Store.CreateWebhook(ctx, &store.Webhook{
CreatorID: currentUser.ID,
Name: request.Name,
Url: request.Url,
})
@@ -79,7 +78,7 @@ func (s *APIV2Service) UpdateWebhook(ctx context.Context, request *apiv2pb.Updat
for _, field := range request.UpdateMask.Paths {
switch field {
case "row_status":
rowStatus := storepb.RowStatus(storepb.RowStatus_value[request.Webhook.RowStatus.String()])
rowStatus := store.RowStatus(request.Webhook.RowStatus.String())
update.RowStatus = &rowStatus
case "name":
update.Name = &request.Webhook.Name
@@ -107,13 +106,13 @@ func (s *APIV2Service) DeleteWebhook(ctx context.Context, request *apiv2pb.Delet
return &apiv2pb.DeleteWebhookResponse{}, nil
}
func convertWebhookFromStore(webhook *storepb.Webhook) *apiv2pb.Webhook {
func convertWebhookFromStore(webhook *store.Webhook) *apiv2pb.Webhook {
return &apiv2pb.Webhook{
Id: webhook.Id,
Id: webhook.ID,
CreatedTime: timestamppb.New(time.Unix(webhook.CreatedTs, 0)),
UpdatedTime: timestamppb.New(time.Unix(webhook.UpdatedTs, 0)),
RowStatus: apiv2pb.RowStatus(webhook.RowStatus),
CreatorId: webhook.CreatorId,
RowStatus: convertRowStatusFromStore(webhook.RowStatus),
CreatorId: webhook.CreatorID,
Name: webhook.Name,
Url: webhook.Url,
}