2026-03-13 16:53:54 +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 dao
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-23 12:15:58 +08:00
|
|
|
"context"
|
2026-03-27 19:25:18 +08:00
|
|
|
"ragflow/internal/entity"
|
2026-07-23 21:48:54 +08:00
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2026-03-13 16:53:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// APITokenDAO API token data access object
|
|
|
|
|
type APITokenDAO struct{}
|
|
|
|
|
|
|
|
|
|
// NewAPITokenDAO create API token DAO
|
|
|
|
|
func NewAPITokenDAO() *APITokenDAO {
|
|
|
|
|
return &APITokenDAO{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create creates a new API token
|
2026-07-23 21:48:54 +08:00
|
|
|
func (dao *APITokenDAO) Create(ctx context.Context, db *gorm.DB, apiToken *entity.APIToken) error {
|
|
|
|
|
return db.WithContext(ctx).Create(apiToken).Error
|
2026-03-13 16:53:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetByTenantID gets API tokens by tenant ID
|
2026-07-23 21:48:54 +08:00
|
|
|
func (dao *APITokenDAO) GetByTenantID(ctx context.Context, db *gorm.DB, tenantID string) ([]*entity.APIToken, error) {
|
2026-03-27 19:25:18 +08:00
|
|
|
var tokens []*entity.APIToken
|
2026-07-23 21:48:54 +08:00
|
|
|
err := db.WithContext(ctx).Where("tenant_id = ?", tenantID).Find(&tokens).Error
|
2026-03-13 16:53:54 +08:00
|
|
|
return tokens, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteByTenantID deletes all API tokens by tenant ID (hard delete)
|
2026-07-23 21:48:54 +08:00
|
|
|
func (dao *APITokenDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
|
2026-08-04 14:18:03 +08:00
|
|
|
result := db.WithContext(ctx).Where("tenant_id = ?", tenantID).Delete(&entity.APIToken{})
|
2026-03-13 16:53:54 +08:00
|
|
|
return result.RowsAffected, result.Error
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-04 14:18:03 +08:00
|
|
|
// GetByAPIToken gets user by API token
|
|
|
|
|
func (dao *APITokenDAO) GetByAPIToken(ctx context.Context, db *gorm.DB, token string) (*entity.APIToken, error) {
|
2026-03-27 19:25:18 +08:00
|
|
|
var apiToken entity.APIToken
|
2026-08-04 14:18:03 +08:00
|
|
|
tx := db.WithContext(ctx).Where("token = ?", token).Find(&apiToken)
|
|
|
|
|
if tx.Error != nil {
|
|
|
|
|
return nil, tx.Error
|
|
|
|
|
}
|
|
|
|
|
if tx.RowsAffected == 0 {
|
|
|
|
|
return nil, nil
|
2026-03-24 20:08:36 +08:00
|
|
|
}
|
|
|
|
|
return &apiToken, nil
|
2026-06-22 18:17:37 +08:00
|
|
|
}
|
|
|
|
|
|
feat(go): implement chatbots/<dialog_id>/info and searchbots/detail (#15420)
### What problem does this PR solve?
Part of #15240 (rewriting the RAGFlow API server in Go).
Implements the two public bot endpoints from
`api/apps/restful_apis/bot_api.py`:
- **`GET /api/v1/chatbots/<dialog_id>/info`** (`chatbots_inputs`) —
returns `{title, avatar, prologue, has_tavily_key}` for a dialog the
authenticated tenant owns (tenant match + `status == VALID`), otherwise
`"Authentication error: no access to this chatbot!"`.
- **`GET /api/v1/searchbots/detail`** (`detail_share_embedded`) —
returns search-app detail for a `search_id` the tenant can access.
Permission is checked across the tenant's joined tenants; denial returns
`"Has no permission for this operation."` (operating error, `data:
false`) and a missing app returns `"Can't find this Search App!"`.
Both endpoints authenticate with an SDK **beta token** (`Authorization:
Bearer <beta>`) rather than a session — the token is resolved to a
tenant via `APIToken.query(beta=token)`, backed by a new
`APITokenDAO.GetByBeta`. Because they perform their own token-based
auth, the routes are registered on the unauthenticated route group
(mirroring the Python blueprint, which has no `@login_required`).
Both live in a new `internal/handler/bot.go` + `internal/service/bot.go`
since they share the same source module. Handler unit tests cover the
auth, success, and error-mapping paths.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
---------
Co-authored-by: Claude Code <claude@anthropic.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Ling Qin <qinling0210@163.com>
2026-07-02 00:46:00 -10:00
|
|
|
// GetByBeta gets API tokens by beta key (SDK/bot authorization token).
|
|
|
|
|
// Mirrors Python's APIToken.query(beta=token), which returns a list.
|
2026-07-23 21:48:54 +08:00
|
|
|
func (dao *APITokenDAO) GetByBeta(ctx context.Context, db *gorm.DB, beta string) ([]*entity.APIToken, error) {
|
feat(go): implement chatbots/<dialog_id>/info and searchbots/detail (#15420)
### What problem does this PR solve?
Part of #15240 (rewriting the RAGFlow API server in Go).
Implements the two public bot endpoints from
`api/apps/restful_apis/bot_api.py`:
- **`GET /api/v1/chatbots/<dialog_id>/info`** (`chatbots_inputs`) —
returns `{title, avatar, prologue, has_tavily_key}` for a dialog the
authenticated tenant owns (tenant match + `status == VALID`), otherwise
`"Authentication error: no access to this chatbot!"`.
- **`GET /api/v1/searchbots/detail`** (`detail_share_embedded`) —
returns search-app detail for a `search_id` the tenant can access.
Permission is checked across the tenant's joined tenants; denial returns
`"Has no permission for this operation."` (operating error, `data:
false`) and a missing app returns `"Can't find this Search App!"`.
Both endpoints authenticate with an SDK **beta token** (`Authorization:
Bearer <beta>`) rather than a session — the token is resolved to a
tenant via `APIToken.query(beta=token)`, backed by a new
`APITokenDAO.GetByBeta`. Because they perform their own token-based
auth, the routes are registered on the unauthenticated route group
(mirroring the Python blueprint, which has no `@login_required`).
Both live in a new `internal/handler/bot.go` + `internal/service/bot.go`
since they share the same source module. Handler unit tests cover the
auth, success, and error-mapping paths.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
---------
Co-authored-by: Claude Code <claude@anthropic.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Ling Qin <qinling0210@163.com>
2026-07-02 00:46:00 -10:00
|
|
|
var tokens []*entity.APIToken
|
2026-07-23 21:48:54 +08:00
|
|
|
err := db.WithContext(ctx).Where("beta = ?", beta).Find(&tokens).Error
|
feat(go): implement chatbots/<dialog_id>/info and searchbots/detail (#15420)
### What problem does this PR solve?
Part of #15240 (rewriting the RAGFlow API server in Go).
Implements the two public bot endpoints from
`api/apps/restful_apis/bot_api.py`:
- **`GET /api/v1/chatbots/<dialog_id>/info`** (`chatbots_inputs`) —
returns `{title, avatar, prologue, has_tavily_key}` for a dialog the
authenticated tenant owns (tenant match + `status == VALID`), otherwise
`"Authentication error: no access to this chatbot!"`.
- **`GET /api/v1/searchbots/detail`** (`detail_share_embedded`) —
returns search-app detail for a `search_id` the tenant can access.
Permission is checked across the tenant's joined tenants; denial returns
`"Has no permission for this operation."` (operating error, `data:
false`) and a missing app returns `"Can't find this Search App!"`.
Both endpoints authenticate with an SDK **beta token** (`Authorization:
Bearer <beta>`) rather than a session — the token is resolved to a
tenant via `APIToken.query(beta=token)`, backed by a new
`APITokenDAO.GetByBeta`. Because they perform their own token-based
auth, the routes are registered on the unauthenticated route group
(mirroring the Python blueprint, which has no `@login_required`).
Both live in a new `internal/handler/bot.go` + `internal/service/bot.go`
since they share the same source module. Handler unit tests cover the
auth, success, and error-mapping paths.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
---------
Co-authored-by: Claude Code <claude@anthropic.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Ling Qin <qinling0210@163.com>
2026-07-02 00:46:00 -10:00
|
|
|
return tokens, err
|
2026-03-24 20:08:36 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-13 16:53:54 +08:00
|
|
|
// DeleteByDialogIDs deletes API tokens by dialog IDs (hard delete)
|
2026-07-23 21:48:54 +08:00
|
|
|
func (dao *APITokenDAO) DeleteByDialogIDs(ctx context.Context, db *gorm.DB, dialogIDs []string) (int64, error) {
|
2026-03-13 16:53:54 +08:00
|
|
|
if len(dialogIDs) == 0 {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
2026-08-04 14:18:03 +08:00
|
|
|
result := db.WithContext(ctx).Where("dialog_id IN ?", dialogIDs).Delete(&entity.APIToken{})
|
2026-03-13 16:53:54 +08:00
|
|
|
return result.RowsAffected, result.Error
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-24 20:08:36 +08:00
|
|
|
// DeleteByTenantIDAndToken deletes a specific API token by tenant ID and token value
|
2026-07-23 21:48:54 +08:00
|
|
|
func (dao *APITokenDAO) DeleteByTenantIDAndToken(ctx context.Context, db *gorm.DB, tenantID, token string) (int64, error) {
|
2026-08-04 14:18:03 +08:00
|
|
|
result := db.WithContext(ctx).Where("tenant_id = ? AND token = ?", tenantID, token).Delete(&entity.APIToken{})
|
2026-03-13 16:53:54 +08:00
|
|
|
return result.RowsAffected, result.Error
|
|
|
|
|
}
|