Add /.well-known/webfinger API to lookup user hrefs
authorJessica Tallon <jessica@megworld.co.uk>
Mon, 8 Sep 2014 15:26:48 +0000 (16:26 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Mon, 8 Sep 2014 16:54:03 +0000 (17:54 +0100)
mediagoblin/federation/routing.py
mediagoblin/federation/views.py

index c1c5a2644057838c7ff459cb576fa162da1f49f5..e9fa6252c3c1dd8702a276c5a879b00b23112557 100644 (file)
@@ -72,8 +72,14 @@ add_route(
     "mediagoblin.federation.views:host_meta"
 )
 
+add_route(
+    "mediagoblin.webfinger.well-known.webfinger",
+    "/.well-known/webfinger",
+    "mediagoblin.federation.views:lrdd_lookup"
+)
+
 add_route(
     "mediagoblin.webfinger.whoami",
     "/api/whoami",
     "mediagoblin.federation.views:whoami"
-)
+)
\ No newline at end of file
index 350aa36cfaf563d52f63f44bce5746989be4f86a..724d349ce5acc24429ceb4882dbfaf4b3ab28214 100644 (file)
@@ -433,6 +433,14 @@ def host_meta(request):
     """
 
     links = [
+        {
+            "rel": "lrdd",
+            "type": "application/json",
+            "href": request.urlgen(
+                "mediagoblin.webfinger.well-known.webfinger",
+                qualified=True
+            )
+        },
         {
             "rel": "registration_endpoint",
             "href": request.urlgen(
@@ -467,7 +475,7 @@ def host_meta(request):
                 "mediagoblin.webfinger.whoami",
                 qualified=True
             ),
-        }
+        },
     ]
 
     if "application/json" in request.accept_mimetypes:
@@ -481,6 +489,59 @@ def host_meta(request):
         mimetype="application/xrd+xml"
     )
 
+def lrdd_lookup(request):
+    """
+    This is the lrdd endpoint which can lookup a user (or
+    other things such as activities). This is as specified by
+    RFC6415.
+
+    The cleint must provide a 'resource' as a GET parameter which
+    should be the query to be looked up.
+    """
+
+    if "resource" not in request.args:
+        return json_error("No resource parameter", status=400)
+
+    resource = request.args["resource"]
+
+    if "@" in resource:
+        # Lets pull out the username
+        resource = resource[5:] if resource.startswith("acct:") else resource
+        username, host = resource.split("@", 1)
+
+        # Now lookup the user
+        user = User.query.filter_by(username=username).first()
+
+        if user is None:
+            return json_error(
+                "Can't find 'user' with username '{0}'".format(username))
+
+        return json_response([
+            {
+                "rel": "http://webfinger.net/rel/profile-page",
+                "href": user.url_for_self(request.urlgen),
+                "type": "text/html"
+            },
+            {
+                "rel": "self",
+                "href": request.urlgen(
+                    "mediagoblin.federation.user",
+                    username=user.username,
+                    qualified=True
+                )
+            },
+            {
+                "rel": "activity-outbox",
+                "href": request.urlgen(
+                    "mediagoblin.federation.feed",
+                    username=user.username,
+                    qualified=True
+                )
+            }
+        ])
+    else:
+        return json_error("Unrecognized resource parameter", status=404)
+
 
 def whoami(request):
     """ /api/whoami - HTTP redirect to API profile """