home / docs / sections

sections: plugin_hooks:plugin-hook-register-token-handler

This data as json

id page ref title content breadcrumbs references
plugin_hooks:plugin-hook-register-token-handler plugin_hooks plugin-hook-register-token-handler register_token_handler(datasette) datasette - Datasette class You can use this to access plugin configuration options via datasette.plugin_config(your_plugin_name) . Return a TokenHandler instance to provide a custom token creation and verification backend. This hook can return a single TokenHandler or a list of them. The default SignedTokenHandler uses itsdangerous signed tokens ( dstok_ prefix). Plugins can provide alternative backends such as database-backed tokens that support revocation and auditing. from datasette import hookimpl, TokenHandler class DatabaseTokenHandler(TokenHandler): name = "database" async def create_token( self, datasette, actor_id, *, expires_after=None, restrictions=None ): # Store token in database and return token string ... async def verify_token(self, datasette, token): # Look up token in database, return actor dict or None ... @hookimpl def register_token_handler(datasette): return DatabaseTokenHandler() The create_token method receives a restrictions argument which will be a TokenRestrictions instance or None . Tokens can then be created and verified using datasette.create_token() and datasette.verify_token() , which delegate to the registered handlers. If no handler is specified, the first handler is used according to pluggy call-time ordering . Use the handler parameter to select a specific backend by name: # Uses first registered handler (default) token = await datasette.create_token("user123") # Uses a specific handler by name token = await datasette.create_token( "user123", handler="database" ) # Verification tries all handlers actor = await datasette.verify_token(token) If no handlers are registered, create_token() raises RuntimeError . If the requested handler name is not found, it raises ValueError . ["Plugin hooks", "Event tracking"] [{"href": "https://pluggy.readthedocs.io/en/stable/#call-time-order", "label": "pluggy call-time ordering"}]
Powered by Datasette · Queries took 3.139ms