Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
cache.py81 linesDownload Raw Back to flask_oauth1
1from authlib.oauth1 import TemporaryCredential2 3 4def register_temporary_credential_hooks(5        authorization_server, cache, key_prefix='temporary_credential:'):6    """Register temporary credential related hooks to authorization server.7 8    :param authorization_server: AuthorizationServer instance9    :param cache: Cache instance10    :param key_prefix: key prefix for temporary credential11    """12 13    def create_temporary_credential(token, client_id, redirect_uri):14        key = key_prefix + token['oauth_token']15        token['client_id'] = client_id16        if redirect_uri:17            token['oauth_callback'] = redirect_uri18 19        cache.set(key, token, timeout=86400)  # cache for one day20        return TemporaryCredential(token)21 22    def get_temporary_credential(oauth_token):23        if not oauth_token:24            return None25        key = key_prefix + oauth_token26        value = cache.get(key)27        if value:28            return TemporaryCredential(value)29 30    def delete_temporary_credential(oauth_token):31        if oauth_token:32            key = key_prefix + oauth_token33            cache.delete(key)34 35    def create_authorization_verifier(credential, grant_user, verifier):36        key = key_prefix + credential.get_oauth_token()37        credential['oauth_verifier'] = verifier38        credential['user_id'] = grant_user.get_user_id()39        cache.set(key, credential, timeout=86400)40        return credential41 42    authorization_server.register_hook(43        'create_temporary_credential', create_temporary_credential)44    authorization_server.register_hook(45        'get_temporary_credential', get_temporary_credential)46    authorization_server.register_hook(47        'delete_temporary_credential', delete_temporary_credential)48    authorization_server.register_hook(49        'create_authorization_verifier', create_authorization_verifier)50 51 52def create_exists_nonce_func(cache, key_prefix='nonce:', expires=86400):53    """Create an ``exists_nonce`` function that can be used in hooks and54    resource protector.55 56    :param cache: Cache instance57    :param key_prefix: key prefix for temporary credential58    :param expires: Expire time for nonce59    """60    def exists_nonce(nonce, timestamp, client_id, oauth_token):61        key = f'{key_prefix}{nonce}-{timestamp}-{client_id}'62        if oauth_token:63            key = f'{key}-{oauth_token}'64        rv = cache.has(key)65        cache.set(key, 1, timeout=expires)66        return rv67    return exists_nonce68 69 70def register_nonce_hooks(71        authorization_server, cache, key_prefix='nonce:', expires=86400):72    """Register nonce related hooks to authorization server.73 74    :param authorization_server: AuthorizationServer instance75    :param cache: Cache instance76    :param key_prefix: key prefix for temporary credential77    :param expires: Expire time for nonce78    """79    exists_nonce = create_exists_nonce_func(cache, key_prefix, expires)80    authorization_server.register_hook('exists_nonce', exists_nonce)81 
codekingpro/portable-devtools · Team Ai