2026-03-24 20:08:36 +08:00
|
|
|
//
|
|
|
|
|
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// 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 service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"ragflow/internal/dao"
|
2026-03-27 19:25:18 +08:00
|
|
|
"ragflow/internal/entity"
|
2026-03-24 20:08:36 +08:00
|
|
|
"ragflow/internal/utility"
|
|
|
|
|
)
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
// APIKeyResponse key response
|
|
|
|
|
type APIKeyResponse struct {
|
2026-03-24 20:08:36 +08:00
|
|
|
TenantID string `json:"tenant_id"`
|
|
|
|
|
Token string `json:"token"`
|
|
|
|
|
DialogID *string `json:"dialog_id,omitempty"`
|
|
|
|
|
Source *string `json:"source,omitempty"`
|
|
|
|
|
Beta *string `json:"beta,omitempty"`
|
|
|
|
|
CreateTime *int64 `json:"create_time,omitempty"`
|
|
|
|
|
UpdateTime *int64 `json:"update_time,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
// ListAPIKeys list all API keys for a tenant
|
|
|
|
|
func (s *SystemService) ListAPIKeys(tenantID string) ([]*APIKeyResponse, error) {
|
2026-03-24 20:08:36 +08:00
|
|
|
APITokenDAO := dao.NewAPITokenDAO()
|
2026-06-24 16:50:40 +08:00
|
|
|
keys, err := APITokenDAO.GetByTenantID(tenantID)
|
2026-03-24 20:08:36 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
responses := make([]*APIKeyResponse, len(keys))
|
|
|
|
|
for i, key := range keys {
|
|
|
|
|
beta := key.Beta
|
2026-05-29 20:04:10 +08:00
|
|
|
if beta == nil || *beta == "" {
|
|
|
|
|
generatedBeta := utility.GenerateBetaAPIToken(utility.GenerateAPIToken())
|
2026-06-24 16:50:40 +08:00
|
|
|
if err = dao.DB.Model(&entity.APIToken{}).
|
|
|
|
|
Where("tenant_id = ? AND token = ?", tenantID, key.Token).
|
2026-05-29 20:04:10 +08:00
|
|
|
Updates(map[string]interface{}{
|
|
|
|
|
"beta": generatedBeta,
|
|
|
|
|
}).Error; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
beta = &generatedBeta
|
2026-06-24 16:50:40 +08:00
|
|
|
key.Beta = beta
|
2026-05-29 20:04:10 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
responses[i] = &APIKeyResponse{
|
|
|
|
|
TenantID: key.TenantID,
|
|
|
|
|
Token: key.Token,
|
|
|
|
|
DialogID: key.DialogID,
|
|
|
|
|
Source: key.Source,
|
2026-05-29 20:04:10 +08:00
|
|
|
Beta: beta,
|
2026-06-24 16:50:40 +08:00
|
|
|
CreateTime: key.CreateTime,
|
|
|
|
|
UpdateTime: key.UpdateTime,
|
2026-03-24 20:08:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return responses, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
// CreateAPIKeyRequest create key request
|
|
|
|
|
type CreateAPIKeyRequest struct {
|
2026-03-24 20:08:36 +08:00
|
|
|
Name string `json:"name" form:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
// CreateAPIKey creates a new API key for a tenant
|
|
|
|
|
func (s *SystemService) CreateAPIKey(tenantID string, req *CreateAPIKeyRequest) (*APIKeyResponse, error) {
|
2026-03-24 20:08:36 +08:00
|
|
|
APITokenDAO := dao.NewAPITokenDAO()
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
// Generate key and beta values
|
|
|
|
|
// key: "ragflow-" + secrets.token_urlsafe(32)
|
2026-03-24 20:08:36 +08:00
|
|
|
APIToken := utility.GenerateAPIToken()
|
|
|
|
|
// beta: generate_confirmation_token().replace("ragflow-", "")[:32]
|
2026-06-02 13:39:07 +08:00
|
|
|
betaAPIKey := utility.GenerateBetaAPIToken(utility.GenerateAPIToken())
|
2026-03-24 20:08:36 +08:00
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
APIKeyData := &entity.APIToken{
|
2026-03-24 20:08:36 +08:00
|
|
|
TenantID: tenantID,
|
|
|
|
|
Token: APIToken,
|
|
|
|
|
Beta: &betaAPIKey,
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
if err := APITokenDAO.Create(APIKeyData); err != nil {
|
2026-03-24 20:08:36 +08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
return &APIKeyResponse{
|
|
|
|
|
TenantID: APIKeyData.TenantID,
|
|
|
|
|
Token: APIKeyData.Token,
|
|
|
|
|
DialogID: APIKeyData.DialogID,
|
|
|
|
|
Source: APIKeyData.Source,
|
|
|
|
|
Beta: APIKeyData.Beta,
|
|
|
|
|
CreateTime: APIKeyData.CreateTime,
|
|
|
|
|
UpdateTime: APIKeyData.UpdateTime,
|
2026-03-24 20:08:36 +08:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 16:50:40 +08:00
|
|
|
func (s *SystemService) DeleteAPIKey(tenantID, key string) error {
|
2026-03-24 20:08:36 +08:00
|
|
|
APITokenDAO := dao.NewAPITokenDAO()
|
2026-06-24 16:50:40 +08:00
|
|
|
_, err := APITokenDAO.DeleteByTenantIDAndToken(tenantID, key)
|
2026-03-24 20:08:36 +08:00
|
|
|
return err
|
|
|
|
|
}
|