google / neuroglancer

WebGL-based viewer for volumetric data
Apache License 2.0
1.09k stars 299 forks source link

Error using graphene source in a local Python neuroglancer #392

Open schlegelp opened 2 years ago

schlegelp commented 2 years ago

Hi @jbms and @chrisj!

Thanks for adding support for graphen sources - I've been really excited to take this for a spin! Off the bat I'm running into an issue with a locally-run neuroglancer viewer though (probably missing something).

Here's a minimal example:

>>> import neuroglancer
>>> viewer = neuroglancer.Viewer()
>>> viewer
http://127.0.0.1:57165/v/3f43fc8559322f9408df81a43e42bf881393a163/

When I then pull up the browser and add graphene://middleauth+https://prod.flywire-daf.com/segmentation/1.0/fly_v31 as layer I get this error:

ERROR:tornado.application:Uncaught exception POST /credentials/3f43fc8559322f9408df81a43e42bf881393a163 (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:57165', method='POST', uri='/credentials/3f43fc8559322f9408df81a43e42bf881393a163', version='HTTP/1.1', remote_ip='127.0.0.1')
Traceback (most recent call last):
  File "/Users/philipps/.pyenv/versions/3.9.9/lib/python3.9/site-packages/tornado/web.py", line 1704, in _execute
    result = await result
  File "/Users/philipps/.pyenv/versions/3.9.9/lib/python3.9/site-packages/neuroglancer/server.py", line 283, in post
    provider = self.server._credentials_manager.get(msg['key'], msg.get('parameters'))
  File "/Users/philipps/.pyenv/versions/3.9.9/lib/python3.9/site-packages/neuroglancer/credentials_provider.py", line 31, in get
    return self._providers[key](parameters)
KeyError: 'middleauthapp'
Screenshot 2022-05-20 at 11 42 11

This is using the wheel for neuroglancer-2.26.post1.dev10 from build 359.

I'm guessing I need to manually add something here? Any ideas?

Thanks, Philipp

schlegelp commented 2 years ago

After opening this issue, I remembered that I had a similar issue with DVID a long while back.

I managed to get it to work like so:

from neuroglancer import credentials_provider
from neuroglancer.futures import run_on_new_thread
from neuroglancer.default_credentials_manager import default_credentials_manager

class TokenbasedDefaultCredentialsProvider(credentials_provider.CredentialsProvider):
    def __init__(self):
        super(TokenbasedDefaultCredentialsProvider, self).__init__()

        self._credentials = {}
        self._credentials['token'] = 'MY_FLYWIRE_API_TOKEN'

    def get_new(self):
        def func():
            return dict(tokenType=u'Bearer', accessToken=self._credentials['token'])

        return run_on_new_thread(func)

_global_tokenbased_application_default_credentials_provider = None

def get_tokenbased_application_default_credentials_provider():
    """Copy pasted from neuroglancer."""
    global _global_tokenbased_application_default_credentials_provider
    if _global_tokenbased_application_default_credentials_provider is None:
        _global_tokenbased_application_default_credentials_provider =\
            TokenbasedDefaultCredentialsProvider()
    return _global_tokenbased_application_default_credentials_provider

# Register authentication 
default_credentials_manager.register(
     u'middleauthapp',
     lambda _parameters: get_tokenbased_application_default_credentials_provider())

# Start viewer 
viewer = neuroglancer.Viewer()

So this works but is there a way to get the snazzy dynamic Google auth login?

jbms commented 2 years ago

@chrisj When using the Python integration, a separate implementation is required for each authentication method. The normal authentication method depends on Neuroglancer being hosted at an origin (host and port) that is specifically allowed by the authentication server.