Repository: jupyterhub/jupyterhub

Issue:
AttributeError: 'Service' object has no attribute 'groups'
Hi,
[Running 1.2.0b1]

I am running the following code in a service to get information about my user:

```py
import requests
api_url = os.environ['JUPYTERHUB_API_URL']
api_token = os.environ['JUPYTERHUB_API_TOKEN']
r = requests.get(api_url + '/user', headers={'Authorization': 'token %s' % api_token,})
```

The request seems to authenticate ok (I would get a 403 if I put /users for example) but it returns the following:

```py
Uncaught exception GET /hub/api/user (172.17.0.2)
    HTTPServerRequest(protocol='http', host='172.17.0.2:8081', method='GET', uri='/hub/api/user', version='HTTP/1.1', remote_ip='172.17.0.2')
    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/tornado/web.py", line 1703, in _execute
        result = await result
      File "/usr/local/lib/python3.6/dist-packages/jupyterhub/apihandlers/users.py", line 38, in get
        self.write(json.dumps(self.user_model(user)))
      File "/usr/local/lib/python3.6/dist-packages/jupyterhub/apihandlers/base.py", line 193, in user_model
        'groups': [g.name for g in user.groups],
    AttributeError: 'Service' object has no attribute 'groups'
```


Discussion:
Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! :hugs:
<br>If you haven't done so already, check out [Jupyter's Code of Conduct](https://github.com/jupyter/governance/blob/master/conduct/code_of_conduct.md). Also, please try to follow the issue template as it helps other other community members to contribute more effectively.
![welcome](https://raw.githubusercontent.com/jupyterhub/.github/master/images/welcome.jpg)
You can meet the other [Jovyans](https://jupyter.readthedocs.io/en/latest/community/content-community.html?highlight=jovyan#what-is-a-jovyan) by joining our [Discourse forum](http://discourse.jupyter.org/). There is also an intro thread there where you can stop by and say Hi! :wave:
<br>Welcome to the Jupyter community! :tada:

I had a same issue, when I called /user GET API.
Another API(/users GET, /users/{name}/tokens GET, etc.) were operated well except above API.
In my case, users were authenticated by LDAP.
This should fail with 403 as it is now, but we can also make it work for services. The request being made here is asking to identify the service itself, not your user. If you want information about your users, it should send the oauth token associated with the user accessing your service, not the service's token itself.

Resolving patch:
diff --git a/jupyterhub/apihandlers/users.py b/jupyterhub/apihandlers/users.py
--- a/jupyterhub/apihandlers/users.py
+++ b/jupyterhub/apihandlers/users.py
@@ -36,7 +36,11 @@ async def get(self):
             user = self.get_current_user_oauth_token()
         if user is None:
             raise web.HTTPError(403)
-        self.write(json.dumps(self.user_model(user)))
+        if isinstance(user, orm.Service):
+            model = self.service_model(user)
+        else:
+            model = self.user_model(user)
+        self.write(json.dumps(model))
 
 
 class UserListAPIHandler(APIHandler):


Test patch:
diff --git a/jupyterhub/tests/test_api.py b/jupyterhub/tests/test_api.py
--- a/jupyterhub/tests/test_api.py
+++ b/jupyterhub/tests/test_api.py
@@ -277,6 +277,17 @@ async def test_get_self(app):
     assert r.status_code == 403
 
 
+async def test_get_self_service(app, mockservice):
+    r = await api_request(
+        app, "user", headers={"Authorization": f"token {mockservice.api_token}"}
+    )
+    r.raise_for_status()
+    service_info = r.json()
+
+    assert service_info['kind'] == 'service'
+    assert service_info['name'] == mockservice.name
+
+
 @mark.user
 async def test_add_user(app):
     db = app.db