Run code with `auth_state` when user logs in

I have a TLJH setup that is connected via LTI authenticator with some other services. Now when a user logs in via LTI and/or their private server is spawned, I would like to run a python script. For example, to enable certain extensions if the user is an instructor, or to fill in name and course ID in some configuration files.

I tried hooking c.Authenticator.post_auth_hook, c.Spawner.pre_spawn_hook or auth_state_hook, and subclassing the spawner. The problem is, these are all run before the instance is spawned, in the context of the hub. I also can’t retrieve the path of the user’s home directory. IPython startup scripts also wouldn’t work, since I need to run my code before the file listing is shown, and not just when a notebook is opened.

Ideal would be a simple callback in the context of the user’s server, that has access to auth_state (which includes data passed from the LTI consmer). Any idea how to achive this?

JupyterHub is decoupled from the singleuser server (for instance, it may spawn a container for each server), so any callbacks would have to be executed by the singleuser server and not JupyterHub.

You could write a wrapper script around jupyterhub-singleuser or whatever command your spawner is currently configured to use, and pass the content of auth_state in an environment variable.