2017-04-13 11 views

답변

5

이 github의 문제에서 모든 정보를 찾을 수 있습니다 https://github.com/django/channels/issues/510#issuecomment-288677354

여기 토론을 요약합니다.

  1. 사본 프로젝트에이 믹스 인 : 는 https://gist.github.com/leonardoo/9574251b3c7eefccd84fc38905110ce4

  2. 은 토큰에 /auth-token보기로 이전 인증 요청을 통해 응용 프로그램에서 수신 ws_connect

에 장식을 적용 django-rest-framework. 우리는 django 채널에 토큰을 보내기 위해 쿼리 문자열을 사용합니다. 장고 - 나머지 프레임 워크를 사용하지 않는다면 자신 만의 방식으로 쿼리 스트링을 사용할 수 있습니다. 그것을 얻는 방법은 믹스 인을 읽으십시오.

  1. mixin을 사용한 후 올바른 토큰이 업그레이드/연결 요청과 함께 사용되면 메시지의 사용자는 아래 예와 같습니다. 보시다시피 User 모델에 has_permission()이 구현되어있어 인스턴스를 확인할 수 있습니다. 토큰이 없거나 유효하지 않은 토큰은 메시지에 사용자가 없습니다. 자신의 믹스 인을 공유하기위한 GitHub의 사용자 leonardoo에
 

    # get_group, get_group_category and get_id are specific to the way we named 
    # things in our implementation but I've included them for completeness. 
    # We use the URL `wss://www.website.com/ws/app_1234?token=3a5s4er34srd32` 

    def get_group(message): 
     return message.content['path'].strip('/').replace('ws/', '', 1) 


    def get_group_category(group): 
     partition = group.rpartition('_') 

     if partition[0]: 
      return partition[0] 
     else: 
      return group 


    def get_id(group): 
     return group.rpartition('_')[2] 


    def accept_connection(message, group): 
     message.reply_channel.send({'accept': True}) 
     Group(group).add(message.reply_channel) 


    # here in connect_app we access the user on message 
    # that has been set by @rest_token_user 

    def connect_app(message, group): 
     if message.user.has_permission(pk=get_id(group)): 
      accept_connection(message, group) 


    @rest_token_user 
    def ws_connect(message): 
     group = get_group(message) # returns 'app_1234' 
     category = get_group_category(group) # returns 'app' 

     if category == 'app': 
      connect_app(message, group) 


    # sends the message contents to everyone in the same group 

    def ws_message(message): 
     Group(get_group(message)).send({'text': message.content['text']}) 


    # removes this connection from its group. In this setup a 
    # connection wil only ever have one group. 

    def ws_disconnect(message): 
     Group(get_group(message)).discard(message.reply_channel) 


은 감사합니다.

+0

'get_group' 함수는 무엇을하고 있습니까? 모델 샘플을 보여 주시겠습니까? 감사합니다. – Ycon

+0

여기까지입니다, 나는 더 완벽한 예를 만들었습니다. 그것은 단지 몇 가지 기본적인 문자열 조작입니다. – ThaJay

+0

나는 꽤 오랫동안이 일을 해왔다. 그리고 나 인생에서 나는 그 일을 할 수 없다. 이걸 가지고 성공 했니? – ergusto

1

쿼리 문자열에서 토큰을 보내는 것이 HTTPS 프로토콜 내부에서도 토큰을 노출 할 수 있다고 생각합니다. 나는 다음과 같은 단계를 사용하고 같은 문제를 해결 와서 :

  1. 일시적으로 세션을 생성하는 토큰 기반의 REST API 엔드 포인트를 작성하고 (이 세션 2 분 만료되도록 설정되어있다)

    session_key에 다시 응답 채널 매개 변수 쿼리 매개 변수에
    login(request,request.user)#Create session with this user 
    request.session.set_expiry(2*60)#Make this session expire in 2Mins 
    return Response({'session_key':request.session.session_key}) 
    
  2. 사용이 session_key 내가 한 별도의 API 호출이 이해

하지만, URL 문자열에 토큰을 보내는 것보다 훨씬 안전하다고 생각합니다.