annotate MoinMoin/auth/openidrp_ext/openidrp_sreg.py @ 5952:f6a74810da73

updated CHANGES
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 10 Mar 2013 15:28:04 +0100
parents ddfd18e00523
children
rev   line source
4883
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
2 """
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
3 MoinMoin - Simple Registration Extension for OpenID authorization
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
4
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
5 @copyright: 2009 Canonical, Inc.
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
7 """
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
8 #from MoinMoin.util.moinoid import MoinOpenIDStore
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
9 from MoinMoin import user
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
10 from MoinMoin.auth import BaseAuth
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
11 from MoinMoin.auth.openidrp import OpenIDAuth
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
12 #from openid.consumer import consumer
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
13 #from openid.yadis.discover import DiscoveryFailure
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
14 #from openid.fetchers import HTTPFetchingError
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
15 #from MoinMoin.widget import html
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
16 #from MoinMoin.auth import CancelLogin, ContinueLogin
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
17 #from MoinMoin.auth import MultistageFormLogin, MultistageRedirectLogin
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
18 #from MoinMoin.auth import get_multistage_continuation_url
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
19
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
20 from openid.extensions.sreg import *
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
21 from MoinMoin import i18n
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
22 from datetime import datetime, timedelta
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
23 from pytz import timezone
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
24 import pytz
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
25
4889
6279b8badd5f Removed no-value return statements from openid extensions. Prepared default auth login_prompt to have configurable registration url.
Rowan Kerr <rowan@stasis.org>
parents: 4888
diff changeset
26 OpenIDAuth.auth_attribs = ('name', 'email', 'aliasname', 'language', 'tz_offset')
4883
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
27
4889
6279b8badd5f Removed no-value return statements from openid extensions. Prepared default auth login_prompt to have configurable registration url.
Rowan Kerr <rowan@stasis.org>
parents: 4888
diff changeset
28 openidrp_sreg_required = ['nickname', 'email', 'timezone']
6279b8badd5f Removed no-value return statements from openid extensions. Prepared default auth login_prompt to have configurable registration url.
Rowan Kerr <rowan@stasis.org>
parents: 4888
diff changeset
29 openidrp_sreg_optional = ['fullname', 'language']
6279b8badd5f Removed no-value return statements from openid extensions. Prepared default auth login_prompt to have configurable registration url.
Rowan Kerr <rowan@stasis.org>
parents: 4888
diff changeset
30 openidrp_sreg_username_field = 'nickname'
4883
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
31
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
32 def openidrp_sreg_modify_request(oidreq, cfg):
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
33 oidreq.addExtension(SRegRequest(required=cfg.openidrp_sreg_required,
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
34 optional=cfg.openidrp_sreg_optional))
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
35
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
36 def openidrp_sreg_create_user(info, u, cfg):
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
37 sreg = _openidrp_sreg_extract_values(info)
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
38 if sreg and sreg[cfg.openidrp_sreg_username_field] != '':
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
39 u.name = sreg[cfg.openidrp_sreg_username_field]
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
40 return u
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
41
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
42 def openidrp_sreg_update_user(info, u, cfg):
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
43 sreg = _openidrp_sreg_extract_values(info)
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
44 if sreg:
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
45 u.name = sreg[cfg.openidrp_sreg_username_field]
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
46 if sreg['email'] != '':
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
47 u.email = sreg['email']
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
48 if sreg['language'] != '':
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
49 u.language = sreg['language']
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
50 if sreg['timezone'] != '':
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
51 u.tz_offset = sreg['timezone']
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
52 if sreg['fullname'] != '':
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
53 u.fullname = sreg['fullname']
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
54
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
55 def _openidrp_sreg_extract_values(info):
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
56 # Pull SREG data here instead of asking user
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
57 sreg_resp = SRegResponse.fromSuccessResponse(info)
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
58 sreg = {'nickname': '', 'email': '', 'fullname': '',
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
59 'dob': '0000-00-00', 'gender': '', 'postcode': '',
4892
802cc30f1937 PEP8 fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4889
diff changeset
60 'country': '', 'language': '', 'timezone': ''}
4883
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
61 if sreg_resp:
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
62 if sreg_resp.get('nickname'):
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
63 sreg['nickname'] = sreg_resp.get('nickname')
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
64 if sreg_resp.get('fullname'):
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
65 sreg['fullname'] = sreg_resp.get('fullname')
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
66 if sreg_resp.get('email'):
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
67 sreg['email'] = sreg_resp.get('email')
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
68 # Language must be a valid value
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
69 # check the MoinMoin list, or restrict to first 2 chars
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
70 if sreg_resp.get('language'):
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
71 # convert unknown codes to 2 char format
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
72 langs = i18n.wikiLanguages().items()
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
73 sreg['language'] = sreg_resp.get('language')
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
74 lang_found = False
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
75 for lang in langs:
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
76 if lang[0] == sreg['language']:
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
77 lang_found = True
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
78 if not lang_found:
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
79 if langs[sreg['language'][0:2]]:
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
80 sreg['language'] = sreg['language'][0:2]
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
81 # Timezone must be converted to offset in seconds
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
82 if sreg_resp.get('timezone'):
5422
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
83 try:
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
84 user_tz = timezone(sreg_resp.get('timezone').encode('ascii'))
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
85 except pytz.UnknownTimeZoneError:
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
86 pass # don't use
4883
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
87 else:
5422
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
88 if user_tz:
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
89 user_utcoffset = user_tz.utcoffset(datetime.utcnow())
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
90 sreg['timezone'] = user_utcoffset.days * 24 * 60 * 60 + user_utcoffset.seconds
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
91 else:
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
92 # XXX when does user_tz get falsy? if it does, shouldn't we just 'pass'?
ddfd18e00523 openidrp_sreg: handle UnknownTimeZoneError gracefully
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4892
diff changeset
93 sreg['timezone'] = 0
4883
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
94 return sreg
6586038c07d3 Added sreg and teams extensions for OpenID relying party authentication
Rowan Kerr <rowan@stasis.org>
parents:
diff changeset
95