It's 2012 all up in here
[mediagoblin.git] / mediagoblin / webfinger / views.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 '''
17 For references, see docstring in mediagoblin/webfinger/__init__.py
18 '''
19
20 import re
21
22 from urlparse import urlparse
23
24 from mediagoblin.tools.response import render_to_response, render_404
25
26 def host_meta(request):
27 '''
28 Webfinger host-meta
29 '''
30
31 placeholder = 'MG_LRDD_PLACEHOLDER'
32
33 lrdd_title = 'GNU MediaGoblin - User lookup'
34
35 lrdd_template = request.urlgen(
36 'mediagoblin.webfinger.xrd',
37 uri=placeholder,
38 qualified=True)
39
40 return render_to_response(
41 request,
42 'mediagoblin/webfinger/host-meta.xml',
43 {'request': request,
44 'lrdd_template': lrdd_template,
45 'lrdd_title': lrdd_title,
46 'placeholder': placeholder})
47
48 MATCH_SCHEME_PATTERN = re.compile(r'^acct:')
49
50 def xrd(request):
51 '''
52 Find user data based on a webfinger URI
53 '''
54 param_uri = request.GET.get('uri')
55
56 if not param_uri:
57 return render_404(request)
58
59 '''
60 :py:module:`urlparse` does not recognize usernames in URIs of the
61 form ``acct:user@example.org`` or ``user@example.org``.
62 '''
63 if not MATCH_SCHEME_PATTERN.search(param_uri):
64 # Assume the URI is in the form ``user@example.org``
65 uri = 'acct://' + param_uri
66 else:
67 # Assumes the URI looks like ``acct:user@example.org
68 uri = MATCH_SCHEME_PATTERN.sub(
69 'acct://', param_uri)
70
71 parsed = urlparse(uri)
72
73 xrd_subject = param_uri
74
75 # TODO: Verify that the user exists
76 # Q: Does webfinger support error handling in this case?
77 # Returning 404 seems intuitive, need to check.
78 if parsed.username:
79 # The user object
80 # TODO: Fetch from database instead of using the MockUser
81 user = MockUser()
82 user.username = parsed.username
83
84 xrd_links = [
85 {'attrs': {
86 'rel': 'http://microformats.org/profile/hcard',
87 'href': request.urlgen(
88 'mediagoblin.user_pages.user_home',
89 user=user.username,
90 qualified=True)}},
91 {'attrs': {
92 'rel': 'http://schemas.google.com/g/2010#updates-from',
93 'href': request.urlgen(
94 'mediagoblin.user_pages.atom_feed',
95 user=user.username,
96 qualified=True)}}]
97
98 xrd_alias = request.urlgen(
99 'mediagoblin.user_pages.user_home',
100 user=user.username,
101 qualified=True)
102
103 return render_to_response(
104 request,
105 'mediagoblin/webfinger/xrd.xml',
106 {'request': request,
107 'subject': xrd_subject,
108 'alias': xrd_alias,
109 'links': xrd_links })
110 else:
111 return render_404(request)
112
113 class MockUser(object):
114 '''
115 TEMPORARY user object
116 '''
117 username = None