Merge branch 'main' of https://github.com/mt21625457/aicodex2api
This commit is contained in:
@@ -216,7 +216,7 @@ func (h *GatewayHandler) Messages(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxAccountSwitches = 3
|
const maxAccountSwitches = 10
|
||||||
switchCount := 0
|
switchCount := 0
|
||||||
failedAccountIDs := make(map[int64]struct{})
|
failedAccountIDs := make(map[int64]struct{})
|
||||||
lastFailoverStatus := 0
|
lastFailoverStatus := 0
|
||||||
|
|||||||
@@ -61,6 +61,13 @@ func jwtAuth(authService *service.AuthService, userService *service.UserService)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Security: Validate TokenVersion to ensure token hasn't been invalidated
|
||||||
|
// This check ensures tokens issued before a password change are rejected
|
||||||
|
if claims.TokenVersion != user.TokenVersion {
|
||||||
|
AbortWithError(c, 401, "TOKEN_REVOKED", "Token has been revoked (password changed)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.Set(string(ContextKeyUser), AuthSubject{
|
c.Set(string(ContextKeyUser), AuthSubject{
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
Concurrency: user.Concurrency,
|
Concurrency: user.Concurrency,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ var (
|
|||||||
ErrEmailExists = infraerrors.Conflict("EMAIL_EXISTS", "email already exists")
|
ErrEmailExists = infraerrors.Conflict("EMAIL_EXISTS", "email already exists")
|
||||||
ErrInvalidToken = infraerrors.Unauthorized("INVALID_TOKEN", "invalid token")
|
ErrInvalidToken = infraerrors.Unauthorized("INVALID_TOKEN", "invalid token")
|
||||||
ErrTokenExpired = infraerrors.Unauthorized("TOKEN_EXPIRED", "token has expired")
|
ErrTokenExpired = infraerrors.Unauthorized("TOKEN_EXPIRED", "token has expired")
|
||||||
|
ErrTokenRevoked = infraerrors.Unauthorized("TOKEN_REVOKED", "token has been revoked")
|
||||||
ErrEmailVerifyRequired = infraerrors.BadRequest("EMAIL_VERIFY_REQUIRED", "email verification is required")
|
ErrEmailVerifyRequired = infraerrors.BadRequest("EMAIL_VERIFY_REQUIRED", "email verification is required")
|
||||||
ErrRegDisabled = infraerrors.Forbidden("REGISTRATION_DISABLED", "registration is currently disabled")
|
ErrRegDisabled = infraerrors.Forbidden("REGISTRATION_DISABLED", "registration is currently disabled")
|
||||||
ErrServiceUnavailable = infraerrors.ServiceUnavailable("SERVICE_UNAVAILABLE", "service temporarily unavailable")
|
ErrServiceUnavailable = infraerrors.ServiceUnavailable("SERVICE_UNAVAILABLE", "service temporarily unavailable")
|
||||||
@@ -27,9 +28,10 @@ var (
|
|||||||
|
|
||||||
// JWTClaims JWT载荷数据
|
// JWTClaims JWT载荷数据
|
||||||
type JWTClaims struct {
|
type JWTClaims struct {
|
||||||
UserID int64 `json:"user_id"`
|
UserID int64 `json:"user_id"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
|
TokenVersion int64 `json:"token_version"` // Used to invalidate tokens on password change
|
||||||
jwt.RegisteredClaims
|
jwt.RegisteredClaims
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,9 +313,10 @@ func (s *AuthService) GenerateToken(user *User) (string, error) {
|
|||||||
expiresAt := now.Add(time.Duration(s.cfg.JWT.ExpireHour) * time.Hour)
|
expiresAt := now.Add(time.Duration(s.cfg.JWT.ExpireHour) * time.Hour)
|
||||||
|
|
||||||
claims := &JWTClaims{
|
claims := &JWTClaims{
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
Role: user.Role,
|
Role: user.Role,
|
||||||
|
TokenVersion: user.TokenVersion,
|
||||||
RegisteredClaims: jwt.RegisteredClaims{
|
RegisteredClaims: jwt.RegisteredClaims{
|
||||||
ExpiresAt: jwt.NewNumericDate(expiresAt),
|
ExpiresAt: jwt.NewNumericDate(expiresAt),
|
||||||
IssuedAt: jwt.NewNumericDate(now),
|
IssuedAt: jwt.NewNumericDate(now),
|
||||||
@@ -368,6 +371,12 @@ func (s *AuthService) RefreshToken(ctx context.Context, oldTokenString string) (
|
|||||||
return "", ErrUserNotActive
|
return "", ErrUserNotActive
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Security: Check TokenVersion to prevent refreshing revoked tokens
|
||||||
|
// This ensures tokens issued before a password change cannot be refreshed
|
||||||
|
if claims.TokenVersion != user.TokenVersion {
|
||||||
|
return "", ErrTokenRevoked
|
||||||
|
}
|
||||||
|
|
||||||
// 生成新token
|
// 生成新token
|
||||||
return s.GenerateToken(user)
|
return s.GenerateToken(user)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -695,6 +695,11 @@ func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *A
|
|||||||
if req.Stream {
|
if req.Stream {
|
||||||
streamResult, err := s.handleStreamingResponse(ctx, resp, c, account, startTime, originalModel, req.Model)
|
streamResult, err := s.handleStreamingResponse(ctx, resp, c, account, startTime, originalModel, req.Model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err.Error() == "have error in stream" {
|
||||||
|
return nil, &UpstreamFailoverError{
|
||||||
|
StatusCode: 403,
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
usage = streamResult.usage
|
usage = streamResult.usage
|
||||||
@@ -969,6 +974,9 @@ func (s *GatewayService) handleStreamingResponse(ctx context.Context, resp *http
|
|||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
if line == "event: error" {
|
||||||
|
return nil, errors.New("have error in stream")
|
||||||
|
}
|
||||||
|
|
||||||
// Extract data from SSE line (supports both "data: " and "data:" formats)
|
// Extract data from SSE line (supports both "data: " and "data:" formats)
|
||||||
if sseDataRe.MatchString(line) {
|
if sseDataRe.MatchString(line) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type User struct {
|
|||||||
Concurrency int
|
Concurrency int
|
||||||
Status string
|
Status string
|
||||||
AllowedGroups []int64
|
AllowedGroups []int64
|
||||||
|
TokenVersion int64 // Incremented on password change to invalidate existing tokens
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
|
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ func (s *UserService) UpdateProfile(ctx context.Context, userID int64, req Updat
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ChangePassword 修改密码
|
// ChangePassword 修改密码
|
||||||
|
// Security: Increments TokenVersion to invalidate all existing JWT tokens
|
||||||
func (s *UserService) ChangePassword(ctx context.Context, userID int64, req ChangePasswordRequest) error {
|
func (s *UserService) ChangePassword(ctx context.Context, userID int64, req ChangePasswordRequest) error {
|
||||||
user, err := s.userRepo.GetByID(ctx, userID)
|
user, err := s.userRepo.GetByID(ctx, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -131,6 +132,10 @@ func (s *UserService) ChangePassword(ctx context.Context, userID int64, req Chan
|
|||||||
return fmt.Errorf("set password: %w", err)
|
return fmt.Errorf("set password: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Increment TokenVersion to invalidate all existing tokens
|
||||||
|
// This ensures that any tokens issued before the password change become invalid
|
||||||
|
user.TokenVersion++
|
||||||
|
|
||||||
if err := s.userRepo.Update(ctx, user); err != nil {
|
if err := s.userRepo.Update(ctx, user); err != nil {
|
||||||
return fmt.Errorf("update user: %w", err)
|
return fmt.Errorf("update user: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -335,12 +335,59 @@
|
|||||||
>
|
>
|
||||||
<div>
|
<div>
|
||||||
<label class="input-label">{{ t('admin.subscriptions.form.user') }}</label>
|
<label class="input-label">{{ t('admin.subscriptions.form.user') }}</label>
|
||||||
<Select
|
<div class="relative">
|
||||||
v-model="assignForm.user_id"
|
<input
|
||||||
:options="userOptions"
|
v-model="userSearchKeyword"
|
||||||
:placeholder="t('admin.subscriptions.selectUser')"
|
type="text"
|
||||||
searchable
|
class="input pr-8"
|
||||||
/>
|
:placeholder="t('admin.usage.searchUserPlaceholder')"
|
||||||
|
@input="debounceSearchUsers"
|
||||||
|
@focus="showUserDropdown = true"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
v-if="selectedUser"
|
||||||
|
@click="clearUserSelection"
|
||||||
|
type="button"
|
||||||
|
class="absolute right-2 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300"
|
||||||
|
>
|
||||||
|
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||||
|
<path
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
stroke-width="2"
|
||||||
|
d="M6 18L18 6M6 6l12 12"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
<!-- User Dropdown -->
|
||||||
|
<div
|
||||||
|
v-if="showUserDropdown && (userSearchResults.length > 0 || userSearchKeyword)"
|
||||||
|
class="absolute z-50 mt-1 max-h-60 w-full overflow-auto rounded-lg border border-gray-200 bg-white shadow-lg dark:border-gray-700 dark:bg-gray-800"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
v-if="userSearchLoading"
|
||||||
|
class="px-4 py-3 text-sm text-gray-500 dark:text-gray-400"
|
||||||
|
>
|
||||||
|
{{ t('common.loading') }}
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
v-else-if="userSearchResults.length === 0 && userSearchKeyword"
|
||||||
|
class="px-4 py-3 text-sm text-gray-500 dark:text-gray-400"
|
||||||
|
>
|
||||||
|
{{ t('common.noOptionsFound') }}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
v-for="user in userSearchResults"
|
||||||
|
:key="user.id"
|
||||||
|
type="button"
|
||||||
|
@click="selectUser(user)"
|
||||||
|
class="w-full px-4 py-2 text-left text-sm hover:bg-gray-100 dark:hover:bg-gray-700"
|
||||||
|
>
|
||||||
|
<span class="font-medium text-gray-900 dark:text-white">{{ user.email }}</span>
|
||||||
|
<span class="ml-2 text-gray-500 dark:text-gray-400">#{{ user.id }}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label class="input-label">{{ t('admin.subscriptions.form.group') }}</label>
|
<label class="input-label">{{ t('admin.subscriptions.form.group') }}</label>
|
||||||
@@ -462,11 +509,12 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, reactive, computed, onMounted } from 'vue'
|
import { ref, reactive, computed, onMounted, onUnmounted } from 'vue'
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
import { useAppStore } from '@/stores/app'
|
import { useAppStore } from '@/stores/app'
|
||||||
import { adminAPI } from '@/api/admin'
|
import { adminAPI } from '@/api/admin'
|
||||||
import type { UserSubscription, Group, User } from '@/types'
|
import type { UserSubscription, Group } from '@/types'
|
||||||
|
import type { SimpleUser } from '@/api/admin/usage'
|
||||||
import type { Column } from '@/components/common/types'
|
import type { Column } from '@/components/common/types'
|
||||||
import { formatDateOnly } from '@/utils/format'
|
import { formatDateOnly } from '@/utils/format'
|
||||||
import AppLayout from '@/components/layout/AppLayout.vue'
|
import AppLayout from '@/components/layout/AppLayout.vue'
|
||||||
@@ -501,9 +549,17 @@ const statusOptions = computed(() => [
|
|||||||
|
|
||||||
const subscriptions = ref<UserSubscription[]>([])
|
const subscriptions = ref<UserSubscription[]>([])
|
||||||
const groups = ref<Group[]>([])
|
const groups = ref<Group[]>([])
|
||||||
const users = ref<User[]>([])
|
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
let abortController: AbortController | null = null
|
let abortController: AbortController | null = null
|
||||||
|
|
||||||
|
// User search state
|
||||||
|
const userSearchKeyword = ref('')
|
||||||
|
const userSearchResults = ref<SimpleUser[]>([])
|
||||||
|
const userSearchLoading = ref(false)
|
||||||
|
const showUserDropdown = ref(false)
|
||||||
|
const selectedUser = ref<SimpleUser | null>(null)
|
||||||
|
let userSearchTimeout: ReturnType<typeof setTimeout> | null = null
|
||||||
|
|
||||||
const filters = reactive({
|
const filters = reactive({
|
||||||
status: '',
|
status: '',
|
||||||
group_id: ''
|
group_id: ''
|
||||||
@@ -545,9 +601,6 @@ const subscriptionGroupOptions = computed(() =>
|
|||||||
.map((g) => ({ value: g.id, label: g.name }))
|
.map((g) => ({ value: g.id, label: g.name }))
|
||||||
)
|
)
|
||||||
|
|
||||||
// User options for assign
|
|
||||||
const userOptions = computed(() => users.value.map((u) => ({ value: u.id, label: u.email })))
|
|
||||||
|
|
||||||
const loadSubscriptions = async () => {
|
const loadSubscriptions = async () => {
|
||||||
if (abortController) {
|
if (abortController) {
|
||||||
abortController.abort()
|
abortController.abort()
|
||||||
@@ -590,13 +643,51 @@ const loadGroups = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadUsers = async () => {
|
// User search with debounce
|
||||||
try {
|
const debounceSearchUsers = () => {
|
||||||
const response = await adminAPI.users.list(1, 1000)
|
if (userSearchTimeout) {
|
||||||
users.value = response.items
|
clearTimeout(userSearchTimeout)
|
||||||
} catch (error) {
|
|
||||||
console.error('Error loading users:', error)
|
|
||||||
}
|
}
|
||||||
|
userSearchTimeout = setTimeout(searchUsers, 300)
|
||||||
|
}
|
||||||
|
|
||||||
|
const searchUsers = async () => {
|
||||||
|
const keyword = userSearchKeyword.value.trim()
|
||||||
|
|
||||||
|
// Clear selection if user modified the search keyword
|
||||||
|
if (selectedUser.value && keyword !== selectedUser.value.email) {
|
||||||
|
selectedUser.value = null
|
||||||
|
assignForm.user_id = null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!keyword) {
|
||||||
|
userSearchResults.value = []
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userSearchLoading.value = true
|
||||||
|
try {
|
||||||
|
userSearchResults.value = await adminAPI.usage.searchUsers(keyword)
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to search users:', error)
|
||||||
|
userSearchResults.value = []
|
||||||
|
} finally {
|
||||||
|
userSearchLoading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectUser = (user: SimpleUser) => {
|
||||||
|
selectedUser.value = user
|
||||||
|
userSearchKeyword.value = user.email
|
||||||
|
showUserDropdown.value = false
|
||||||
|
assignForm.user_id = user.id
|
||||||
|
}
|
||||||
|
|
||||||
|
const clearUserSelection = () => {
|
||||||
|
selectedUser.value = null
|
||||||
|
userSearchKeyword.value = ''
|
||||||
|
userSearchResults.value = []
|
||||||
|
assignForm.user_id = null
|
||||||
}
|
}
|
||||||
|
|
||||||
const handlePageChange = (page: number) => {
|
const handlePageChange = (page: number) => {
|
||||||
@@ -615,6 +706,11 @@ const closeAssignModal = () => {
|
|||||||
assignForm.user_id = null
|
assignForm.user_id = null
|
||||||
assignForm.group_id = null
|
assignForm.group_id = null
|
||||||
assignForm.validity_days = 30
|
assignForm.validity_days = 30
|
||||||
|
// Clear user search state
|
||||||
|
selectedUser.value = null
|
||||||
|
userSearchKeyword.value = ''
|
||||||
|
userSearchResults.value = []
|
||||||
|
showUserDropdown.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleAssignSubscription = async () => {
|
const handleAssignSubscription = async () => {
|
||||||
@@ -754,10 +850,25 @@ const formatResetTime = (windowStart: string, period: 'daily' | 'weekly' | 'mont
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle click outside to close user dropdown
|
||||||
|
const handleClickOutside = (event: MouseEvent) => {
|
||||||
|
const target = event.target as HTMLElement
|
||||||
|
if (!target.closest('.relative')) {
|
||||||
|
showUserDropdown.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
loadSubscriptions()
|
loadSubscriptions()
|
||||||
loadGroups()
|
loadGroups()
|
||||||
loadUsers()
|
document.addEventListener('click', handleClickOutside)
|
||||||
|
})
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
document.removeEventListener('click', handleClickOutside)
|
||||||
|
if (userSearchTimeout) {
|
||||||
|
clearTimeout(userSearchTimeout)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user