diff options
author | Jessica Tallon <jessica@megworld.co.uk> | 2014-09-05 14:13:49 +0100 |
---|---|---|
committer | Jessica Tallon <jessica@megworld.co.uk> | 2014-09-05 14:17:42 +0100 |
commit | 0af1b859989e6a910360b5ac87c5b7c8dc2e5a91 (patch) | |
tree | e76790cc236580c537326887e1fa9801d04ad167 | |
parent | f251d99828f30b31d8932f1046101ab332debf75 (diff) | |
download | mediagoblin-0af1b859989e6a910360b5ac87c5b7c8dc2e5a91.tar.lz mediagoblin-0af1b859989e6a910360b5ac87c5b7c8dc2e5a91.tar.xz mediagoblin-0af1b859989e6a910360b5ac87c5b7c8dc2e5a91.zip |
Add XRD+XML formatting for /.well-known/host-meta
-rw-r--r-- | mediagoblin/federation/views.py | 97 | ||||
-rw-r--r-- | mediagoblin/templates/mediagoblin/federation/host-meta.xml | 22 | ||||
-rw-r--r-- | mediagoblin/tools/response.py | 7 |
3 files changed, 88 insertions, 38 deletions
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/<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 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 <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 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 |