Add XRD+XML formatting for /.well-known/host-meta
authorJessica Tallon <jessica@megworld.co.uk>
Fri, 5 Sep 2014 13:13:49 +0000 (14:13 +0100)
committerJessica Tallon <jessica@megworld.co.uk>
Fri, 5 Sep 2014 13:17:42 +0000 (14:17 +0100)
mediagoblin/federation/views.py
mediagoblin/templates/mediagoblin/federation/host-meta.xml [new file with mode: 0644]
mediagoblin/tools/response.py

index 3d6953a7c3642f124c2cd0c4013923b40e6a5a5b..350aa36cfaf563d52f63f44bce5746989be4f86a 100644 (file)
@@ -23,7 +23,8 @@ from werkzeug.datastructures import FileStorage
 from mediagoblin.decorators import oauth_required
 from mediagoblin.federation.decorators import user_has_privilege
 from mediagoblin.db.models import User, MediaEntry, MediaComment
-from mediagoblin.tools.response import redirect, json_response, json_error
+from mediagoblin.tools.response import redirect, json_response, json_error, \
+                                       render_to_response
 from mediagoblin.meddleware.csrf import csrf_exempt
 from mediagoblin.submit.lib import new_upload_entry, api_upload_request, \
                                     api_add_to_feed
@@ -70,14 +71,14 @@ def profile_endpoint(request):
 def user_endpoint(request):
     """ This is /api/user/<username> - This will get the user """
     user, user_profile = get_profile(request)
-    
+
     if user is None:
         username = request.matchdict["username"]
         return json_error(
             "No such 'user' with username '{0}'".format(username),
             status=404
         )
-    
+
     return json_response({
         "nickname": user.username,
         "updated": user.created.isoformat(),
@@ -418,42 +419,68 @@ def object_comments(request):
     return json_response(comments)
 
 ##
-# Well known
+# RFC6415 - Web Host Metadata
 ##
 def host_meta(request):
-    """ /.well-known/host-meta - provide URLs to resources """
-    links = []
+    """
+    This provides the host-meta URL information that is outlined
+    in RFC6415. By default this should provide XRD+XML however
+    if the client accepts JSON we will provide that over XRD+XML.
+    The 'Accept' header is used to decude this.
 
-    links.append({
-        "ref": "registration_endpoint",
-        "href": request.urlgen(
-            "mediagoblin.oauth.client_register",
-            qualified=True
-        ),
-    })
-    links.append({
-        "ref": "http://apinamespace.org/oauth/request_token",
-        "href": request.urlgen(
-            "mediagoblin.oauth.request_token",
-            qualified=True
-        ),
-    })
-    links.append({
-        "ref": "http://apinamespace.org/oauth/authorize",
-        "href": request.urlgen(
-            "mediagoblin.oauth.authorize",
-            qualified=True
-        ),
-    })
-    links.append({
-        "ref": "http://apinamespace.org/oauth/access_token",
-        "href": request.urlgen(
-            "mediagoblin.oauth.access_token",
-            qualified=True
-        ),
-    })
+    A client should use this endpoint to determine what URLs to
+    use for OAuth endpoints.
+    """
+
+    links = [
+        {
+            "rel": "registration_endpoint",
+            "href": request.urlgen(
+                "mediagoblin.oauth.client_register",
+                qualified=True
+            ),
+        },
+        {
+            "rel": "http://apinamespace.org/oauth/request_token",
+            "href": request.urlgen(
+                "mediagoblin.oauth.request_token",
+                qualified=True
+            ),
+        },
+        {
+            "rel": "http://apinamespace.org/oauth/authorize",
+            "href": request.urlgen(
+                "mediagoblin.oauth.authorize",
+                qualified=True
+            ),
+        },
+        {
+            "rel": "http://apinamespace.org/oauth/access_token",
+            "href": request.urlgen(
+                "mediagoblin.oauth.access_token",
+                qualified=True
+            ),
+        },
+        {
+            "rel": "http://apinamespace.org/activitypub/whoami",
+            "href": request.urlgen(
+                "mediagoblin.webfinger.whoami",
+                qualified=True
+            ),
+        }
+    ]
+
+    if "application/json" in request.accept_mimetypes:
+        return json_response({"links": links})
+
+    # provide XML+XRD
+    return render_to_response(
+        request,
+        "mediagoblin/federation/host-meta.xml",
+        {"links": links},
+        mimetype="application/xrd+xml"
+    )
 
-    return json_response({"links": links})
 
 def whoami(request):
     """ /api/whoami - HTTP redirect to API profile """
diff --git a/mediagoblin/templates/mediagoblin/federation/host-meta.xml b/mediagoblin/templates/mediagoblin/federation/host-meta.xml
new file mode 100644 (file)
index 0000000..7970a0d
--- /dev/null
@@ -0,0 +1,22 @@
+{# GNU MediaGoblin -- federated, autonomous media hosting
+# Copyright (C) 2014 MediaGoblin contributors. See AUTHORS.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+-#}
+<?xml version='1.0' encoding='UTF-8'?>
+<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
+  {% for link in links %}
+    <Link rel="{{ link.rel }}" href="{{ link.href }}" />
+  {% endfor %}
+</XRD>
\ No newline at end of file
index 57552963e80c269a7377419ce5b87b327fedbfa8..88270265a26c5ac022dd38f30d06541f816885e3 100644 (file)
@@ -29,11 +29,12 @@ class Response(wz_Response):
     default_mimetype = u'text/html'
 
 
-def render_to_response(request, template, context, status=200):
+def render_to_response(request, template, context, status=200, mimetype=None):
     """Much like Django's shortcut.render()"""
     return Response(
         render_template(request, template, context),
-        status=status)
+        status=status,
+        mimetype=mimetype)
 
 def render_error(request, status=500, title=_('Oops!'),
                  err_msg=_('An error occured')):
@@ -164,7 +165,7 @@ def json_error(error_str, status=400, *args, **kwargs):
         code to 400.
     """
     return json_response({"error": error_str}, status=status, *args, **kwargs)
-    
+
 def form_response(data, *args, **kwargs):
     """
         Responds using application/x-www-form-urlencoded and returns a werkzeug