コード例 #1
0
ファイル: router.py プロジェクト: michalpomykacz/trickster
 def __init__(self,
              id: str,
              body: Any,
              delay: Delay,
              headers: Optional[Dict[str, Any]] = None,
              status: int = 200,
              repeat: Optional[int] = None,
              weight: float = 0.5) -> None:
     Response.__init__(self, body, delay, headers, status)
     IdItem.__init__(self, id)
     self.repeat = repeat
     self.weight = weight
コード例 #2
0
ファイル: auth.py プロジェクト: michalpomykacz/trickster
 def deserialize(cls, data: Dict[str, Any]) -> Auth:
     """Convert json value to Auth."""
     if 'method' in data:
         implementation = cls._find_implementation(data['method'])
         data.pop('method')
         if response_data := data.pop('unauthorized_response', None):
             response = Response.deserialize(response_data)
         else:
             response = Response(
                 {'error': 'Unauthorized', 'message': 'Authentication failed.'},
                 Delay(0.0),
                 status=401
             )
         return implementation(response, **data)
コード例 #3
0
 def test_authenticate(self):
     auth = TokenAuth(Response('', Delay()), 'abcdefghi')
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET',
         headers={'Authorization': 'Bearer abcdefghi'}
     )
     auth.authenticate(request)
コード例 #4
0
 def test_authenticate(self):
     auth = CookieAuth(Response('', Delay()), 'name', 'value')
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET',
         cookies={'name': 'value'}
     )
     auth.authenticate(request)
コード例 #5
0
 def test_missing_cookie(self):
     auth = CookieAuth(Response('', Delay()), 'name', 'value')
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET'
     )
     with pytest.raises(AuthenticationError):
         auth.authenticate(request)
コード例 #6
0
ファイル: router.py プロジェクト: michalpomykacz/trickster
 def serialize(self) -> Dict[str, Any]:
     """Convert Response to json."""
     return {
         **Response.serialize(self),
         **IdItem.serialize(self),
         'is_active': self.is_active,
         'weight': self.weight,
         'repeat': self.repeat,
     }
コード例 #7
0
 def test_missing_auth_header(self):
     auth = BasicAuth(Response('', Delay()), 'username', 'password')
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET'
     )
     with pytest.raises(AuthenticationError):
         auth.authenticate(request)
コード例 #8
0
 def test_authenticate(self):
     auth = BasicAuth(Response('', Delay()), 'username', 'password')
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET',
         headers={'Authorization': 'dXNlcm5hbWU6cGFzc3dvcmQ='}
     )
     auth.authenticate(request)
コード例 #9
0
 def test_invalid_auth_token(self):
     auth = BasicAuth(Response('', Delay()), 'username', 'password')
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET',
         headers={'Authorization': 'xxx'}
     )
     with pytest.raises(AuthenticationError):
         auth.authenticate(request)
コード例 #10
0
 def test_missing_bearer_token(self):
     auth = TokenAuth(Response('', Delay()), 'abcdefghi')
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET',
         headers={'Authorization': 'Bearer xxxxxxx'}
     )
     with pytest.raises(AuthenticationError):
         auth.authenticate(request)
コード例 #11
0
    def test_invalid_sign(self):
        auth = HmacAuth(Response('', Delay()), 'secret')
        ts = time.time()
        request = IncomingTestRequest(
            base_url='http://localhost/',
            full_path=f'/test?hmac_timestamp={ts}&hmac_sign=invalid',
            method='GET'
        )

        with pytest.raises(AuthenticationError):
            auth.authenticate(request)
コード例 #12
0
 def test_authenticate(self):
     auth = HmacAuth(Response('', Delay()), 'secret')
     ts = time.time()
     hash_maker = hmac.new('secret'.encode('utf-8'), digestmod=hashlib.sha1) 
     hash_maker.update(f'/test?hmac_timestamp={ts}'.encode('utf-8'))
     sign = hash_maker.hexdigest() 
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path=f'/test?hmac_timestamp={ts}&hmac_sign={sign}',
         method='GET'
     )
     auth.authenticate(request)
コード例 #13
0
 def test_serialize(self):
     auth = HmacAuth(Response('', Delay()), 'secret')
     assert auth.serialize() == {
         'method': 'hmac',
         'key': 'secret',
         'unauthorized_response': {
             'body': '',
             'delay': 0.0,
             'headers': {},
             'status': 200,
             'used_count': 0
         }
     }
コード例 #14
0
 def test_serialize(self):
     auth = TokenAuth(Response('', Delay()), 'abcdefghi')
     assert auth.serialize() == {
         'method': 'token',
         'token': 'abcdefghi',
         'unauthorized_response': {
             'body': '',
             'delay': 0.0,
             'headers': {},
             'status': 200,
             'used_count': 0
         }
     }
コード例 #15
0
 def test_serialize(self):
     auth = BasicAuth(Response('', Delay()), 'username', 'password')
     assert auth.serialize() == {
         'method': 'basic',
         'username': '******',
         'password': '******',
         'unauthorized_response': {
             'body': '',
             'delay': 0.0,
             'headers': {},
             'status': 200,
             'used_count': 0
         }
     }
コード例 #16
0
    def test_signature_in_future(self):
        auth = HmacAuth(Response('', Delay()), 'secret')
        ts = 13569465661  # 01/01/2400
        hash_maker = hmac.new('secret'.encode('utf-8'), digestmod=hashlib.sha1) 
        hash_maker.update(f'/test?hmac_timestamp={ts}'.encode('utf-8'))
        sign = hash_maker.hexdigest() 
        request = IncomingTestRequest(
            base_url='http://localhost/',
            full_path=f'/test?hmac_timestamp={ts}&hmac_sign={sign}',
            method='GET'
        )

        with pytest.raises(AuthenticationError):
            auth.authenticate(request)
コード例 #17
0
 def test_serialize(self):
     auth = CookieAuth(Response('', Delay()), 'name', 'value')
     assert auth.serialize() == {
         'method': 'cookie',
         'name': 'name',
         'value': 'value',
         'unauthorized_response': {
             'body': '',
             'delay': 0.0,
             'headers': {},
             'status': 200,
             'used_count': 0
         }
     }
コード例 #18
0
 def test_missing_field(self):
     auth = FormAuth(Response('', Delay()), {
         'field1': 'value1',
         'field2': 'value2'
     })
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET',
         form={
             'field1': 'value1'
         }
     )
     with pytest.raises(AuthenticationError):
         auth.authenticate(request)
コード例 #19
0
 def test_authenticate(self):
     auth = FormAuth(Response('', Delay()), {
         'field1': 'value1',
         'field2': 'value2'
     })
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test',
         method='GET',
         form={
             'field1': 'value1',
             'field2': 'value2'
         }
     )
     auth.authenticate(request)
コード例 #20
0
 def test_serialize(self):
     auth = FormAuth(Response('', Delay()), {
         'field1': 'value1',
         'field2': 'value2'
     })
     assert auth.serialize() == {
         'method': 'form',
         'fields': {
            'field1': 'value1',
             'field2': 'value2'
         },
         'unauthorized_response': {
             'body': '',
             'delay': 0.0,
             'headers': {},
             'status': 200,
             'used_count': 0
         }
     }