def test_managed_outpost(self): """Test managed outpost""" with self.assertRaises(AuthenticationFailed): user = bearer_auth(f"Bearer {settings.SECRET_KEY}".encode()) OutpostManager().run() user = bearer_auth(f"Bearer {settings.SECRET_KEY}".encode()) self.assertEqual(user.attributes[USER_ATTRIBUTE_SA], True)
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: user = bearer_auth(raw_header) # user is only None when no header was given, in which case we deny too if not user: raise DenyConnection() except AuthenticationFailed as exc: LOGGER.warning("Failed to authenticate", exc=exc) raise DenyConnection() self.user = user
def test_invalid_no_token(self): """Test invalid with no token""" with self.assertRaises(AuthenticationFailed): auth = b64encode(":abc".encode()).decode() self.assertIsNone(bearer_auth(f"Basic :{auth}".encode()))
def test_invalid_empty_password(self): """Test invalid with empty password""" with self.assertRaises(AuthenticationFailed): bearer_auth("Basic :".encode())
def test_invalid_decode(self): """Test invalid bas64""" with self.assertRaises(AuthenticationFailed): bearer_auth("Basic bar".encode())
def test_invalid_type(self): """Test invalid type""" with self.assertRaises(AuthenticationFailed): bearer_auth("foo bar".encode())
def test_valid_bearer(self): """Test valid token""" token = Token.objects.create(intent=TokenIntents.INTENT_API, user=get_anonymous_user()) self.assertEqual(bearer_auth(f"Bearer {token.key}".encode()), token.user)
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(bearer_auth(f"Basic {auth}".encode()), token.user)