From 0af1b859989e6a910360b5ac87c5b7c8dc2e5a91 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Fri, 5 Sep 2014 14:13:49 +0100 Subject: [PATCH] Add XRD+XML formatting for /.well-known/host-meta --- mediagoblin/federation/views.py | 97 ++++++++++++------- .../mediagoblin/federation/host-meta.xml | 22 +++++ mediagoblin/tools/response.py | 7 +- 3 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 mediagoblin/templates/mediagoblin/federation/host-meta.xml diff --git a/mediagoblin/federation/views.py b/mediagoblin/federation/views.py index 3d6953a7..350aa36c 100644 --- a/mediagoblin/federation/views.py +++ b/mediagoblin/federation/views.py @@ -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/ - 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 index 00000000..7970a0d2 --- /dev/null +++ b/mediagoblin/templates/mediagoblin/federation/host-meta.xml @@ -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 . +-#} + + + {% for link in links %} + + {% endfor %} + \ No newline at end of file diff --git a/mediagoblin/tools/response.py b/mediagoblin/tools/response.py index 57552963..88270265 100644 --- a/mediagoblin/tools/response.py +++ b/mediagoblin/tools/response.py @@ -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 -- 2.25.1