mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 02:18:08 +08:00
fix websocket cookie auth
This commit is contained in:
@ -3,8 +3,10 @@ import time
|
|||||||
|
|
||||||
from extensions.ext_redis import redis_client
|
from extensions.ext_redis import redis_client
|
||||||
from extensions.ext_socketio import sio
|
from extensions.ext_socketio import sio
|
||||||
|
from libs.token import extract_access_token
|
||||||
from libs.passport import PassportService
|
from libs.passport import PassportService
|
||||||
from services.account_service import AccountService
|
from services.account_service import AccountService
|
||||||
|
from werkzeug.wrappers import Request as WerkzeugRequest
|
||||||
|
|
||||||
|
|
||||||
@sio.on("connect")
|
@sio.on("connect")
|
||||||
@ -15,6 +17,14 @@ def socket_connect(sid, environ, auth):
|
|||||||
token = None
|
token = None
|
||||||
if auth and isinstance(auth, dict):
|
if auth and isinstance(auth, dict):
|
||||||
token = auth.get("token")
|
token = auth.get("token")
|
||||||
|
|
||||||
|
if not token:
|
||||||
|
try:
|
||||||
|
request_environ = WerkzeugRequest(environ)
|
||||||
|
token = extract_access_token(request_environ)
|
||||||
|
except Exception:
|
||||||
|
token = None
|
||||||
|
|
||||||
if not token:
|
if not token:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import type { Socket } from 'socket.io-client'
|
import type { Socket } from 'socket.io-client'
|
||||||
import { io } from 'socket.io-client'
|
import { io } from 'socket.io-client'
|
||||||
|
import { ACCESS_TOKEN_LOCAL_STORAGE_NAME } from '@/config'
|
||||||
import type { DebugInfo, WebSocketConfig } from '../types/websocket'
|
import type { DebugInfo, WebSocketConfig } from '../types/websocket'
|
||||||
|
|
||||||
export class WebSocketClient {
|
export class WebSocketClient {
|
||||||
@ -40,13 +41,25 @@ export class WebSocketClient {
|
|||||||
|
|
||||||
this.connecting.add(appId)
|
this.connecting.add(appId)
|
||||||
|
|
||||||
const authToken = localStorage.getItem('console_token')
|
const authToken = typeof window === 'undefined'
|
||||||
const socket = io(this.config.url!, {
|
? undefined
|
||||||
|
: window.localStorage.getItem(ACCESS_TOKEN_LOCAL_STORAGE_NAME) ?? undefined
|
||||||
|
|
||||||
|
const socketOptions: {
|
||||||
|
path: string
|
||||||
|
transports: WebSocketConfig['transports']
|
||||||
|
withCredentials?: boolean
|
||||||
|
auth?: { token: string }
|
||||||
|
} = {
|
||||||
path: '/socket.io',
|
path: '/socket.io',
|
||||||
transports: this.config.transports,
|
transports: this.config.transports,
|
||||||
auth: { token: authToken },
|
|
||||||
withCredentials: this.config.withCredentials,
|
withCredentials: this.config.withCredentials,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if (authToken)
|
||||||
|
socketOptions.auth = { token: authToken }
|
||||||
|
|
||||||
|
const socket = io(this.config.url!, socketOptions)
|
||||||
|
|
||||||
this.connections.set(appId, socket)
|
this.connections.set(appId, socket)
|
||||||
this.setupBaseEventListeners(socket, appId)
|
this.setupBaseEventListeners(socket, appId)
|
||||||
|
|||||||
Reference in New Issue
Block a user