예제 #1
0
    def connect(self):
        headers = dict(self.scope["headers"])
        if b"authorization" not in headers:
            LOGGER.warning("WS Request without authorization header")
            raise DenyConnection()

        raw_header = headers[b"authorization"]

        token = token_from_header(raw_header)
        if not token:
            LOGGER.warning("Failed to authenticate")
            raise DenyConnection()

        self.user = token.user
예제 #2
0
    def connect(self):
        headers = dict(self.scope["headers"])
        if b"authorization" not in headers:
            LOGGER.warning("WS Request without authorization header")
            raise DenyConnection()

        raw_header = headers[b"authorization"]

        try:
            token = token_from_header(raw_header)
            # token is only None when no header was given, in which case we deny too
            if not token:
                raise DenyConnection()
        except AuthenticationFailed as exc:
            LOGGER.warning("Failed to authenticate", exc=exc)
            raise DenyConnection()

        self.user = token.user
예제 #3
0
 def test_invalid_no_token(self):
     """Test invalid with no token"""
     auth = b64encode(":abc".encode()).decode()
     self.assertIsNone(token_from_header(f"Basic :{auth}".encode()))
예제 #4
0
 def test_invalid_decode(self):
     """Test invalid bas64"""
     self.assertIsNone(token_from_header("Basic bar".encode()))
예제 #5
0
 def test_invalid_empty_password(self):
     """Test invalid with empty password"""
     self.assertIsNone(token_from_header("Basic :".encode()))
예제 #6
0
 def test_valid_bearer(self):
     """Test valid token"""
     token = Token.objects.create(intent=TokenIntents.INTENT_API,
                                  user=get_anonymous_user())
     self.assertEqual(token_from_header(f"Bearer {token.key}".encode()),
                      token)
예제 #7
0
 def test_invalid_type(self):
     """Test invalid type"""
     self.assertIsNone(token_from_header("foo bar".encode()))
예제 #8
0
 def test_valid_basic(self):
     """Test valid token"""
     token = Token.objects.create(intent=TokenIntents.INTENT_API,
                                  user=get_anonymous_user())
     auth = b64encode(f":{token.key}".encode()).decode()
     self.assertEqual(token_from_header(f"Basic {auth}".encode()), token)
예제 #9
0
 def test_invalid_no_token(self):
     """Test invalid with no token"""
     with self.assertRaises(AuthenticationFailed):
         auth = b64encode(":abc".encode()).decode()
         self.assertIsNone(token_from_header(f"Basic :{auth}".encode()))
예제 #10
0
 def test_invalid_empty_password(self):
     """Test invalid with empty password"""
     with self.assertRaises(AuthenticationFailed):
         token_from_header("Basic :".encode())
예제 #11
0
 def test_invalid_decode(self):
     """Test invalid bas64"""
     with self.assertRaises(AuthenticationFailed):
         token_from_header("Basic bar".encode())
예제 #12
0
 def test_invalid_type(self):
     """Test invalid type"""
     with self.assertRaises(AuthenticationFailed):
         token_from_header("foo bar".encode())