如何通过Django渠道将Cookie添加到标头?

时间:2019-04-26 21:35:53

标签: django react-native authentication websocket django-channels

在前端,我有以下配置。身份验证是手动的,因为在具有持久会话时,react native会失败:

...
const authLink = setContext(request =>
  storage.getCookie().then(cookie => ({
    // Before the request is sent: set cookie header
    headers: {
      cookie,
    },
    credentials: 'omit', // set cookies manually
  }))
)

const setTokenLink = new ApolloLink((operation, forward) => {
  // Send the request to the server
  return forward(operation).map(response => {
    // After response is returned: store cookie in local storage
    const context = operation.getContext()
    const {
      response: { headers },
    } = context

    if (headers) {
      storage.setCookie(headers.get('set-cookie'))
    }

    return response
  })
})
storage.getCookie().then(cookie => console.log(cookie))
// console.log('cookie', storage.getCookie().then(cookie => {cookie}))

const httpLink = createUploadLink({
  uri: `${domainApi}/graphql/`,
})

const wsClient = new SubscriptionClient(
  domainWs,
  {
    lazy: true,
    reconnect: true,
    connectionParams: {
      authToken: storage.getCookie(),
    },
  })
)
...

然而,这似乎会影响websocket,因为在后端,标头不包含cookie。

class GraphqlSubcriptionConsumer(SyncConsumer):
    def __init__(self, scope):
        super().__init__(scope)
        self.subscriptions = {}
        self.groups = {}

    def websocket_connect(self, message):
        self.send({"type": "websocket.accept", "subprotocol": "graphql-ws"})

    def websocket_disconnect(self, message):
        for group in self.groups.keys():
            group_discard = async_to_sync(self.channel_layer.group_discard)
            group_discard(f"django.{group}", self.channel_name)

        self.send({"type": "websocket.close", "code": 1000})
        raise StopConsumer()

    def websocket_receive(self, message):
        request = json.loads(message["text"])
        id = request.get("id")

        if request["type"] == "connection_init":
            return

        elif request["type"] == "start":
            print("scope user:", self.scope["user"]) //returns AnonymousUser
            print("scope headers:", self.scope["headers"]) //returns headers without the cookie
...

使用此配置,我得到

{'type': 'websocket.receive', 'text': '{"type":"connection_init","payload":{"authToken":{"_40":0,"_65":1,"_55":"sessionid=bci028bzn1cgxyuynvb7fjevc5ynqdil","_72":null}}}'}
在每个第一条类型为connection_init的消息中

。因此,我想知道是否有一种方法可以将django中的cookie手动插入(可能在if request["type"] == "connection_init":中),以便随后的start类型的消息都包含cookie。

任何小技巧/提示都会帮助您。

1 个答案:

答案 0 :(得分:0)

要在服务器的握手响应中设置cookie(已通过<html> <head> <title>Are you sure?</title> <script> function areYouSure() { var res = confirm("Are you sure?"); return res; //true or false } </script> </head> <body> <form method="POST" action="action_page.php"> <!-- ... --> <input type="submit" onclick="return areYouSure()"/> </form> </body> </html>测试):

channels==2.3.1

建立连接后,就无法通过def connect(self): # connect method of your WebsocketConsumer # called on connection headers = {} headers['Set-Cookie'] = 'myCookie=myValue' self.accept(subprotocol=(None, headers)) HTTP响应标头将cookie从服务器发送到用户代理。那是因为它是一个开放的TCP套接字,并且该协议不再是HTTP。

Set-Cookie在后​​台使用Django Channelshttps://github.com/crossbario/autobahn-python/blob/9e68328df3c23ca3eee67017404235024f265693/autobahn/websocket/types.py#L285