def __init__(self, database_manager: DatabaseManagerMongo = None): self.key_holder = KeyHolder() self.header = {'alg': 'RS512'} self.database_manager = database_manager or DatabaseManagerMongo( **SystemConfigReader().get_all_values_from_section('Database')) self.auth_module = AuthModule( SystemSettingsReader(self.database_manager))
def __init__(self, database_manager: DatabaseManagerMongo = None): self.key_holder = KeyHolder(database_manager) self.header = {'alg': 'RS512'} self.database_manager = database_manager self.auth_module = AuthModule( SystemSettingsReader( self.database_manager).get_all_values_from_section( 'auth', default=AuthModule.__DEFAULT_SETTINGS__))
def key_holder(key_dir): with open(f'{key_dir}/token_public.pem', "w+") as fpu: fpu.write(PUBLIC_KEY) fpu.close() with open(f'{key_dir}/token_private.pem', "w+") as fpr: fpr.write(PRIVATE_KEY) fpr.close() return KeyHolder(key_directory=key_dir)
class TokenValidator: def __init__(self): self.key_holder = KeyHolder() def decode_token(self, token: (JWT, str, dict)): try: decoded_token = jwt.decode(s=token, key=self.key_holder.get_public_key()) except (BadSignatureError, Exception) as err: raise ValidationError(err) return decoded_token def validate_token(self, token: (JWT, str, dict)): try: import time token.validate(time.time()) except InvalidClaimError as err: raise ValidationError(err)
class TokenGenerator: DEFAULT_CLAIMS = {'iss': {'essential': True, 'value': __title__}} def __init__(self, database_manager: DatabaseManagerMongo = None): self.key_holder = KeyHolder() self.header = {'alg': 'RS512'} self.database_manager = database_manager or DatabaseManagerMongo( **SystemConfigReader().get_all_values_from_section('Database')) self.auth_module = AuthModule( SystemSettingsReader(self.database_manager)) def get_expire_time(self) -> datetime: expire_time = int( self.auth_module.settings.get_token_lifetime( DEFAULT_TOKEN_LIFETIME)) return datetime.now() + timedelta(minutes=expire_time) def generate_token(self, payload: dict, optional_claims: dict = None) -> bytes: optional_claims = optional_claims or {} token_claims = { 'iat': int(datetime.now().timestamp()), 'exp': int(self.get_expire_time().timestamp()) } payload_claims = {'DATAGERRY': {'essential': True, 'value': payload}} claims = { **self.DEFAULT_CLAIMS, **token_claims, **payload_claims, **optional_claims } token = jwt.encode(self.header, claims, self.key_holder.get_private_key()) return token
def __init__(self): self.key_holder = KeyHolder()
def test_key_holder_instance(): key_holder = KeyHolder() assert isinstance(key_holder, KeyHolder)
def __init__(self, database_manager: DatabaseManagerMongo): self.key_holder = KeyHolder(database_manager)