fix(singleagent): add api key type

This commit is contained in:
lijunwen.gigoo
2025-08-13 17:19:44 +08:00
parent ece8bd3a47
commit bb2e426d90
8 changed files with 49 additions and 5 deletions

View File

@ -81,6 +81,7 @@ func (s *OpenAuthApplicationService) CreatePersonalAccessToken(ctx context.Conte
Name: req.Name,
Expire: req.ExpireAt,
UserID: *userID,
AkType: entity.AkTypeCustomer,
}
if req.DurationDay == "customize" {
@ -116,9 +117,13 @@ func (s *OpenAuthApplicationService) ImpersonateCozeUserAccessToken(ctx context.
resp := new(bot_open_api.ImpersonateCozeUserResponse)
userID := ctxutil.GetUIDFromCtx(ctx)
expiredSecond := time.Second * 60 * 15
expiredSecond := time.Now().Add(time.Duration(time.Second * 60 * 15)).Unix()
appReq := &entity.CreateApiKey{
UserID: *userID,
AkType: entity.AkTypeTemporary,
Expire: expiredSecond,
Name: "temporary access token",
}
apiKeyResp, err := openapiAuthDomainSVC.Create(ctx, appReq)
@ -128,7 +133,7 @@ func (s *OpenAuthApplicationService) ImpersonateCozeUserAccessToken(ctx context.
}
resp.Data = &bot_open_api.ImpersonateCozeUserResponseData{
AccessToken: apiKeyResp.ApiKey,
ExpiresIn: time.Now().Add(time.Duration(expiredSecond)).Unix(),
ExpiresIn: expiredSecond,
TokenType: "Bearer",
}
return resp, nil

View File

@ -32,6 +32,7 @@ type CreateApiKey struct {
Name string `json:"name"`
Expire int64 `json:"expire"`
UserID int64 `json:"user_id"`
AkType AkType `json:"ak_type"`
}
type DeleteApiKey struct {

View File

@ -0,0 +1,24 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package entity
type AkType int32
const (
AkTypeCustomer AkType = 0
AkTypeTemporary AkType = 1
)

View File

@ -72,6 +72,7 @@ func (a *ApiKeyDAO) doToPo(ctx context.Context, do *entity.CreateApiKey) (*model
Name: do.Name,
ExpiredAt: do.Expire,
UserID: do.UserID,
AkType: int32(do.AkType),
CreatedAt: time.Now().Unix(),
}
return po, nil
@ -119,7 +120,7 @@ func (a *ApiKeyDAO) FindByKey(ctx context.Context, key string) (*model.APIKey, e
func (a *ApiKeyDAO) List(ctx context.Context, userID int64, limit int, page int) ([]*model.APIKey, bool, error) {
do := a.dbQuery.APIKey.WithContext(ctx).Where(a.dbQuery.APIKey.UserID.Eq(userID))
do = do.Where(a.dbQuery.APIKey.AkType.Eq(int32(entity.AkTypeCustomer)))
do = do.Offset((page - 1) * limit).Limit(limit + 1)
list, err := do.Order(a.dbQuery.APIKey.CreatedAt.Desc()).Find()

View File

@ -17,6 +17,7 @@ type APIKey struct {
CreatedAt int64 `gorm:"column:created_at;not null;autoCreateTime:milli;comment:Create Time in Milliseconds" json:"created_at"` // Create Time in Milliseconds
UpdatedAt int64 `gorm:"column:updated_at;not null;autoUpdateTime:milli;comment:Update Time in Milliseconds" json:"updated_at"` // Update Time in Milliseconds
LastUsedAt int64 `gorm:"column:last_used_at;not null;comment:Used Time in Milliseconds" json:"last_used_at"` // Used Time in Milliseconds
AkType int32 `gorm:"column:ak_type;not null;comment:api key type" json:"ak_type"` // api key type
}
// TableName APIKey's table name

View File

@ -36,6 +36,7 @@ func newAPIKey(db *gorm.DB, opts ...gen.DOOption) aPIKey {
_aPIKey.CreatedAt = field.NewInt64(tableName, "created_at")
_aPIKey.UpdatedAt = field.NewInt64(tableName, "updated_at")
_aPIKey.LastUsedAt = field.NewInt64(tableName, "last_used_at")
_aPIKey.AkType = field.NewInt32(tableName, "ak_type")
_aPIKey.fillFieldMap()
@ -56,6 +57,7 @@ type aPIKey struct {
CreatedAt field.Int64 // Create Time in Milliseconds
UpdatedAt field.Int64 // Update Time in Milliseconds
LastUsedAt field.Int64 // Used Time in Milliseconds
AkType field.Int32 // api key type
fieldMap map[string]field.Expr
}
@ -81,6 +83,7 @@ func (a *aPIKey) updateTableName(table string) *aPIKey {
a.CreatedAt = field.NewInt64(table, "created_at")
a.UpdatedAt = field.NewInt64(table, "updated_at")
a.LastUsedAt = field.NewInt64(table, "last_used_at")
a.AkType = field.NewInt32(table, "ak_type")
a.fillFieldMap()
@ -97,7 +100,7 @@ func (a *aPIKey) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (a *aPIKey) fillFieldMap() {
a.fieldMap = make(map[string]field.Expr, 9)
a.fieldMap = make(map[string]field.Expr, 10)
a.fieldMap["id"] = a.ID
a.fieldMap["api_key"] = a.APIKey
a.fieldMap["name"] = a.Name
@ -107,6 +110,7 @@ func (a *aPIKey) fillFieldMap() {
a.fieldMap["created_at"] = a.CreatedAt
a.fieldMap["updated_at"] = a.UpdatedAt
a.fieldMap["last_used_at"] = a.LastUsedAt
a.fieldMap["ak_type"] = a.AkType
}
func (a aPIKey) clone(db *gorm.DB) aPIKey {

View File

@ -0,0 +1,7 @@
-- Modify "api_key" table
ALTER TABLE `opencoze`.`api_key` ADD COLUMN `ak_type` tinyint NOT NULL DEFAULT 0 COMMENT "api key type ";
-- Modify "single_agent_draft" table
ALTER TABLE `opencoze`.`single_agent_draft` ADD COLUMN `bot_mode` tinyint NOT NULL DEFAULT 0 COMMENT "bot mode,0:single mode 2:chatflow mode" AFTER `database_config`, ADD COLUMN `layout_info` text NULL COMMENT "chatflow layout info";
-- Modify "single_agent_version" table
ALTER TABLE `opencoze`.`single_agent_version` ADD COLUMN `bot_mode` tinyint NOT NULL DEFAULT 0 COMMENT "bot mode,0:single mode 2:chatflow mode" AFTER `database_config`, ADD COLUMN `layout_info` text NULL COMMENT "chatflow layout info";

View File

@ -1,4 +1,4 @@
h1:RChVPs+0hXzZCxc2/z5ij3ob5BG0svpnuH6xckTf/nc=
h1:0fORsjIF9mQvStz3ASawpFr3Nr3B5pqjDOgwMa7E1L4=
20250703095335_initial.sql h1:/joaeUTMhXqAEc0KwsSve5+bYM0qPOp+9OizJtsRc+U=
20250703115304_update.sql h1:cbYo6Q6Lh96hB4hu5KW2Nn/Mr0VDpg7a1WPgpIb1SOc=
20250704040445_update.sql h1:QWmoPY//oQ+GFZwET9w/oAWa8mM0KVaD5G8Yiu9bMqY=
@ -9,3 +9,4 @@ h1:RChVPs+0hXzZCxc2/z5ij3ob5BG0svpnuH6xckTf/nc=
20250730131847_update.sql h1:qIutMrXtuOA98jeucTFxXck+sQNjNTtIF2apbCYt3IY=
20250802115105_update.sql h1:irreQaMAL0LtXcDlkdHP86C7/0e2HzEVsa1hP/FkZ2M=
20250812093734_update.sql h1:uEPpf0UlG97yiOlUvuo9X2NNh2r86JYPuHk6F0Ka7Ic=
20250813081543_update.sql h1:z8sHxQrR8z+ajfBatitYpCMiTrRkd339b6dP0iz8y9Y=