Back to index

moin  1.9.0~rc2
openidrp_sreg.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Simple Registration Extension for OpenID authorization
00004 
00005     @copyright: 2009 Canonical, Inc.
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 #from MoinMoin.util.moinoid import MoinOpenIDStore
00009 from MoinMoin import user
00010 from MoinMoin.auth import BaseAuth
00011 from MoinMoin.auth.openidrp import OpenIDAuth
00012 #from openid.consumer import consumer
00013 #from openid.yadis.discover import DiscoveryFailure
00014 #from openid.fetchers import HTTPFetchingError
00015 #from MoinMoin.widget import html
00016 #from MoinMoin.auth import CancelLogin, ContinueLogin
00017 #from MoinMoin.auth import MultistageFormLogin, MultistageRedirectLogin
00018 #from MoinMoin.auth import get_multistage_continuation_url
00019 
00020 from openid.extensions.sreg import *
00021 from MoinMoin import i18n
00022 from datetime import datetime, timedelta
00023 from pytz import timezone
00024 import pytz
00025 
00026 OpenIDAuth.auth_attribs = ('name', 'email', 'aliasname', 'language', 'tz_offset')
00027 
00028 openidrp_sreg_required = ['nickname', 'email', 'timezone']
00029 openidrp_sreg_optional = ['fullname', 'language']
00030 openidrp_sreg_username_field = 'nickname'
00031 
00032 def openidrp_sreg_modify_request(oidreq, cfg):
00033     oidreq.addExtension(SRegRequest(required=cfg.openidrp_sreg_required,
00034                                     optional=cfg.openidrp_sreg_optional))
00035 
00036 def openidrp_sreg_create_user(info, u, cfg):
00037     sreg = _openidrp_sreg_extract_values(info)
00038     if sreg and sreg[cfg.openidrp_sreg_username_field] != '':
00039         u.name = sreg[cfg.openidrp_sreg_username_field]
00040     return u
00041 
00042 def openidrp_sreg_update_user(info, u, cfg):
00043     sreg = _openidrp_sreg_extract_values(info)
00044     if sreg:
00045         u.name = sreg[cfg.openidrp_sreg_username_field]
00046         if sreg['email'] != '':
00047             u.email = sreg['email']
00048         if sreg['language'] != '':
00049             u.language = sreg['language']
00050         if sreg['timezone'] != '':
00051             u.tz_offset = sreg['timezone']
00052         if sreg['fullname'] != '':
00053             u.fullname = sreg['fullname']
00054 
00055 def _openidrp_sreg_extract_values(info):
00056     # Pull SREG data here instead of asking user
00057     sreg_resp = SRegResponse.fromSuccessResponse(info)
00058     sreg = {'nickname': '', 'email': '', 'fullname': '',
00059             'dob': '0000-00-00', 'gender': '', 'postcode': '',
00060             'country': '', 'language': '', 'timezone': ''}
00061     if sreg_resp:
00062         if sreg_resp.get('nickname'):
00063             sreg['nickname'] = sreg_resp.get('nickname')
00064         if sreg_resp.get('fullname'):
00065             sreg['fullname'] = sreg_resp.get('fullname')
00066         if sreg_resp.get('email'):
00067             sreg['email'] = sreg_resp.get('email')
00068         # Language must be a valid value
00069         # check the MoinMoin list, or restrict to first 2 chars
00070         if sreg_resp.get('language'):
00071             # convert unknown codes to 2 char format
00072             langs = i18n.wikiLanguages().items()
00073             sreg['language'] = sreg_resp.get('language')
00074             lang_found = False
00075             for lang in langs:
00076                 if lang[0] == sreg['language']:
00077                     lang_found = True
00078             if not lang_found:
00079                 if langs[sreg['language'][0:2]]:
00080                     sreg['language'] = sreg['language'][0:2]
00081         # Timezone must be converted to offset in seconds
00082         if sreg_resp.get('timezone'):
00083             user_tz = timezone(sreg_resp.get('timezone').encode('ascii'))
00084             if user_tz:
00085                 user_utcoffset = user_tz.utcoffset(datetime.utcnow())
00086                 sreg['timezone'] = user_utcoffset.days * 24 * 60 * 60 + user_utcoffset.seconds
00087             else:
00088                 sreg['timezone'] = 0
00089     return sreg
00090