本文整理汇总了Python中openid.extensions.ax.FetchRequest类的典型用法代码示例。如果您正苦于以下问题:Python FetchRequest类的具体用法?Python FetchRequest怎么用?Python FetchRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FetchRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: begin
def begin(request, openid_url):
request.session['request_referer'] = urlparse.urljoin(request.META.get('HTTP_REFERER', ''), '/')
consumer = Consumer(request.session, DjangoOpenIDStore())
try:
auth_request = consumer.begin(openid_url)
except DiscoveryFailure:
return on_failure(request, _('The OpenID was invalid'))
s = SRegRequest()
for sarg in OPENID_SREG:
if sarg.lower().lstrip() == "policy_url":
s.policy_url = OPENID_SREG[sarg]
else:
for v in OPENID_SREG[sarg].split(','):
s.requestField(field_name=v.lower().lstrip(), required=(sarg.lower().lstrip() == "required"))
auth_request.addExtension(s)
axr = AXFetchRequest()
for i in OPENID_AX:
axr.add(AttrInfo(i['type_uri'], i['count'], i['required'], i['alias']))
auth_request.addExtension(axr)
redirect_url = auth_request.redirectURL(get_trusted_root(request),
request.build_absolute_uri(reverse("openid_complete")))
return HttpResponseRedirect(redirect_url)
示例2: begin
def begin(self, request, data):
try:
openid_url = data['openid_url'].strip()
except KeyError:
messages.error(request, lang.FILL_OPENID_URL)
return redirect('netauth-login')
# allow user to type openid provider without http:// prefix
if not openid_url.startswith("http"):
openid_url = "http://%s" % openid_url
return_url = request.build_absolute_uri(reverse('netauth-complete', args=[self.provider]))
request.session['openid_return_to'] = return_url
client = consumer.Consumer(request.session, None)
try:
openid_request = client.begin(openid_url)
sreg_extra = [value for __, value in self.PROFILE_MAPPING.items()]
sreg = SRegRequest(required=sreg_extra)
openid_request.addExtension(sreg)
ax_msg = FetchRequest()
for __, detail in self.PROFILE_MAPPING.items():
ax_msg.add(AttrInfo(settings.AX_URIS[detail], required=True))
openid_request.addExtension(ax_msg)
redirect_url = openid_request.redirectURL(realm='http://' + request.get_host(), return_to=return_url)
return redirect(redirect_url)
except discover.DiscoveryFailure:
messages.error(request, _('Could not find OpenID server'))
return redirect('netauth-login')
示例3: login
def login(request):
if 'openid' in request.GET or request.method == 'POST':
form = LoginForm(request.REQUEST)
if form.is_valid():
client = _openid_consumer(request)
try:
auth_request = client.begin(form.cleaned_data['openid'])
if QUERY_EMAIL:
sreg = SRegRequest()
sreg.requestField(field_name=SRegField.EMAIL, required=True)
auth_request.addExtension(sreg)
ax = FetchRequest()
ax.add(AttrInfo(AXAttribute.CONTACT_EMAIL,
required=True))
auth_request.addExtension(ax)
callback_url = reverse(callback)
state = SocialLogin.marshall_state(request)
callback_url = callback_url + '?' + urlencode(dict(state=state))
redirect_url = auth_request.redirectURL(
request.build_absolute_uri('/'),
request.build_absolute_uri(callback_url))
return HttpResponseRedirect(redirect_url)
except DiscoveryFailure, e:
if request.method == 'POST':
form._errors["openid"] = form.error_class([e])
else:
return render_authentication_error(request)
示例4: login
def login(request):
if 'openid' in request.GET or request.method == 'POST':
form = LoginForm(request.REQUEST)
if form.is_valid():
client = _openid_consumer(request)
try:
auth_request = client.begin(form.cleaned_data['openid'])
if QUERY_EMAIL:
sreg = SRegRequest()
sreg.requestField(field_name=SRegField.EMAIL,
required=True)
auth_request.addExtension(sreg)
ax = FetchRequest()
ax.add(AttrInfo(AXAttribute.CONTACT_EMAIL,
required=True))
auth_request.addExtension(ax)
callback_url = reverse(callback)
SocialLogin.stash_state(request)
redirect_url = auth_request.redirectURL(
request.build_absolute_uri('/'),
request.build_absolute_uri(callback_url))
return HttpResponseRedirect(redirect_url)
# UnicodeDecodeError:
# see https://github.com/necaris/python3-openid/issues/1
except (UnicodeDecodeError, DiscoveryFailure) as e:
if request.method == 'POST':
form._errors["openid"] = form.error_class([e])
else:
return render_authentication_error(request)
else:
form = LoginForm()
d = dict(form=form)
return render_to_response('openid/login.html',
d, context_instance=RequestContext(request))
示例5: test02
def test02(self):
interface = SQLAlchemyAXInterface()
interface.connectionString = \
SQLAlchemyAXInterfaceTestCase.DB_CONNECTION_STR
interface.attributeNames = ('firstName', 'lastName', 'emailAddress')
interface.sqlQuery = ("select firstname, lastname, emailAddress from "
"users where username = '${username}'")
axReq = FetchRequest()
for typeURI in interface.attributeNames:
axReq.add(AttrInfo(typeURI, required=True))
axResp = FetchResponse()
authnCtx = {
SQLAlchemyAXInterface.USERNAME_SESSION_KEYNAME:
SQLAlchemyAXInterfaceTestCase.USERNAME
}
interface(axReq, axResp, None, authnCtx)
axData = axResp.getExtensionArgs()
self.assert_(len(axData.keys()) > 0)
print(axData)
示例6: login
def login(request):
if 'openid' in request.GET or request.method == 'POST':
form = LoginForm(
dict(list(request.GET.items()) + list(request.POST.items()))
)
if form.is_valid():
client = _openid_consumer(request)
provider = OpenIDProvider(request)
realm = provider.get_settings().get(
'REALM',
request.build_absolute_uri('/'))
try:
auth_request = client.begin(form.cleaned_data['openid'])
if QUERY_EMAIL:
sreg = SRegRequest()
for name in SRegFields:
sreg.requestField(field_name=name,
required=True)
auth_request.addExtension(sreg)
ax = FetchRequest()
for name in AXAttributes:
ax.add(AttrInfo(name,
required=True))
provider = OpenIDProvider(request)
server_settings = \
provider.get_server_settings(request.GET.get('openid'))
extra_attributes = \
server_settings.get('extra_attributes', [])
for _, name, required in extra_attributes:
ax.add(AttrInfo(name,
required=required))
auth_request.addExtension(ax)
callback_url = reverse(callback)
SocialLogin.stash_state(request)
# Fix for issues 1523 and 2072 (github django-allauth)
if 'next' in form.cleaned_data and form.cleaned_data['next']:
auth_request.return_to_args['next'] = \
form.cleaned_data['next']
redirect_url = auth_request.redirectURL(
realm,
request.build_absolute_uri(callback_url))
return HttpResponseRedirect(redirect_url)
# UnicodeDecodeError:
# see https://github.com/necaris/python3-openid/issues/1
except (UnicodeDecodeError, DiscoveryFailure) as e:
if request.method == 'POST':
form._errors["openid"] = form.error_class([e])
else:
return render_authentication_error(
request,
OpenIDProvider.id,
exception=e)
else:
form = LoginForm(initial={'next': request.GET.get('next'),
'process': request.GET.get('process')})
d = dict(form=form)
return render(request, "openid/login.html", d)
示例7: test_ax_and_sreg_required_fields_for_trusted_site
def test_ax_and_sreg_required_fields_for_trusted_site(self):
"""The server should always return values for required fields to
trusted sites, regardless of the state of the checkbox in the UI.
Optional fields should not be returned if the user has unchecked them.
Fields are required if at least one of the SReg and AX request lists
them as such.
"""
self.rpconfig.allowed_user_attribs = (
'fullname,email,language,account_verified')
sreg_request = SRegRequest(
required=['language', 'email'], optional=['fullname'])
ax_request = FetchRequest()
ax_request.add(
AttrInfo(AX_URI_EMAIL, alias='email', required=True))
ax_request.add(
AttrInfo(AX_URI_ACCOUNT_VERIFIED, alias='account_verified',
required=True))
ax_request.add(
AttrInfo(AX_URI_LANGUAGE, alias='language', required=False))
ax_request.add(
AttrInfo(AX_URI_FULL_NAME, alias='fullname', required=False))
form = UserAttribsRequestForm(
request=self._get_request_with_post_args(),
sreg_request=sreg_request, ax_request=ax_request,
rpconfig=self.rpconfig)
self.assertIn('email', form.data_approved_for_request)
self.assertIn('account_verified', form.data_approved_for_request)
self.assertIn('language', form.data_approved_for_request)
self.assertNotIn('fullname', form.data_approved_for_request)
示例8: prepare_authentication_request
def prepare_authentication_request(self, request, redirect_to):
if not redirect_to.startswith('http://') or redirect_to.startswith('https://'):
redirect_to = get_url_host(request) + redirect_to
user_url = self.get_user_url(request)
if xri.identifierScheme(user_url) == 'XRI' and getattr(
settings, 'OPENID_DISALLOW_INAMES', False
):
raise InvalidAuthentication('i-names are not supported')
consumer = Consumer(request.session, OsqaOpenIDStore())
try:
auth_request = consumer.begin(user_url)
except DiscoveryFailure:
raise InvalidAuthentication(_('Sorry, but your input is not a valid OpenId'))
sreg = getattr(self, 'sreg_attributes', False)
if sreg:
s = SRegRequest()
for k, attr_dic in sreg.items():
if k == "policy_url":
s.policy_url = attr_dic
continue
for attr_name in attr_dic.keys():
s.requestField(field_name=attr_name, required=(k == "required"))
auth_request.addExtension(s)
ax_schema = getattr(self, 'dataype2ax_schema', False)
if ax_schema and request.session.get('force_email_request', True):
axr = AXFetchRequest()
for data_type, schema in ax_schema.items():
if isinstance(schema, tuple):
axr.add(AttrInfo(schema[0], 1, True, schema[1]))
else:
axr.add(AttrInfo(schema, 1, True, data_type))
auth_request.addExtension(axr)
trust_root = getattr(
settings, 'OPENID_TRUST_ROOT', get_url_host(request) + '/'
)
return auth_request.redirectURL(trust_root, redirect_to)
示例9: test_ax_optional_fields_for_untrusted_site
def test_ax_optional_fields_for_untrusted_site(self):
"""The server should return values for optional fields to untrusted
sites only when the user checks the checkbox in the UI.
"""
post_args = {'fullname': 'fullname'}
ax_request = FetchRequest()
for (attr, alias) in [
(AX_URI_FULL_NAME, 'fullname'),
(AX_URI_EMAIL, 'email')]:
ax_request.add(AttrInfo(attr, alias=alias, required=False))
form = UserAttribsRequestForm(
request=self._get_request_with_post_args(post_args),
sreg_request=None, ax_request=ax_request, rpconfig=None)
self.assertTrue('fullname' in form.data_approved_for_request)
self.assertFalse('email' in form.data_approved_for_request)
示例10: test_ax_required_fields_for_trusted_site
def test_ax_required_fields_for_trusted_site(self):
"""The server should always return values for required fields to
trusted sites, regardless of the state of the checkbox in the UI.
Optional fields should not be returned if the user has unchecked them.
"""
self.rpconfig.allowed_user_attribs = 'fullname,email'
ax_request = FetchRequest()
ax_request.add(
AttrInfo(AX_URI_FULL_NAME, alias='fullname', required=True))
ax_request.add(
AttrInfo(AX_URI_EMAIL, alias='email', required=False))
form = UserAttribsRequestForm(
request=self._get_request_with_post_args(), sreg_request=None,
ax_request=ax_request, rpconfig=self.rpconfig)
self.assertIn('fullname', form.data_approved_for_request)
self.assertNotIn('email', form.data_approved_for_request)
示例11: prepare_authentication_request
def prepare_authentication_request(self, request, redirect_to):
if not redirect_to.startswith('http://') or redirect_to.startswith('https://'):
redirect_to = get_url_host(request) + redirect_to
user_url = self.get_user_url(request)
if xri.identifierScheme(user_url) == 'XRI' and getattr(
settings, 'OPENID_DISALLOW_INAMES', False
):
raise InvalidAuthentication('i-names are not supported')
consumer = Consumer(request.session, OsqaOpenIDStore())
try:
auth_request = consumer.begin(user_url)
except DiscoveryFailure:
raise InvalidAuthentication(_('Sorry, but your input is not a valid OpenId'))
#sreg = getattr(settings, 'OPENID_SREG', False)
#if sreg:
# s = SRegRequest()
# for sarg in sreg:
# if sarg.lower().lstrip() == "policy_url":
# s.policy_url = sreg[sarg]
# else:
# for v in sreg[sarg].split(','):
# s.requestField(field_name=v.lower().lstrip(), required=(sarg.lower().lstrip() == "required"))
# auth_request.addExtension(s)
#auth_request.addExtension(SRegRequest(required=['email']))
if request.session.get('force_email_request', True):
axr = AXFetchRequest()
for data_type, schema in self.dataype2ax_schema.items():
if isinstance(schema, tuple):
axr.add(AttrInfo(schema[0], 1, True, schema[1]))
else:
axr.add(AttrInfo(schema, 1, True, data_type))
auth_request.addExtension(axr)
trust_root = getattr(
settings, 'OPENID_TRUST_ROOT', get_url_host(request) + '/'
)
return auth_request.redirectURL(trust_root, redirect_to)
示例12: test_no_approved_fields_without_post_request
def test_no_approved_fields_without_post_request(self):
"""The server should not generate a list of approved fields when the
request is not a POST request.
"""
sreg_request = SRegRequest(required=['fullname', 'email'])
ax_request = FetchRequest()
for (attr, alias) in [
(AX_URI_FULL_NAME, 'fullname'),
(AX_URI_EMAIL, 'email')]:
ax_request.add(AttrInfo(attr, alias=alias, required=True))
request = self._get_request_with_post_args()
request.META['REQUEST_METHOD'] = 'GET'
form = UserAttribsRequestForm(request=request,
sreg_request=sreg_request,
ax_request=ax_request,
rpconfig=self.rpconfig)
self.assertEqual(len(form.data_approved_for_request), 0)
示例13: test_ax_checkbox_status_for_trusted_site
def test_ax_checkbox_status_for_trusted_site(self):
"""Checkboxes are always checked if the site is trusted"""
self.rpconfig.allowed_user_attribs = 'fullname,email'
ax_request = FetchRequest()
ax_request.add(
AttrInfo(AX_URI_FULL_NAME, alias='fullname', required=True))
ax_request.add(
AttrInfo(AX_URI_EMAIL, alias='email', required=False))
form = UserAttribsRequestForm(
request=self._get_request_with_post_args(),
sreg_request=None, ax_request=ax_request, rpconfig=self.rpconfig)
# True because fullname required
self.assertTrue(form.check_test('fullname')(True))
# True because trusted site and no previous disapproval
self.assertTrue(form.check_test('email')(True))
# Throw in an unrequested field for good measure
self.assertFalse(form.check_test('language')(True))
示例14: test_ax_fields_for_trusted_auto_authorize_site
def test_ax_fields_for_trusted_auto_authorize_site(self):
"""The server should always return values for requested fields to
trusted sites configured to auto-authorize.
"""
self.rpconfig.allowed_user_attribs = 'fullname,email'
self.rpconfig.auto_authorize = True
ax_request = FetchRequest()
# One required attribute
ax_request.add(
AttrInfo(AX_URI_FULL_NAME, alias='fullname', required=True))
# One optional attribute
ax_request.add(
AttrInfo(AX_URI_EMAIL, alias='email', required=False))
form = UserAttribsRequestForm(
request=self._get_request_with_post_args(), sreg_request=None,
ax_request=ax_request, rpconfig=self.rpconfig)
# Both attributes should be returned
self.assertIn('fullname', form.data_approved_for_request)
self.assertIn('email', form.data_approved_for_request)
示例15: perform_openid_auth
def perform_openid_auth(self, form):
if not form.is_valid():
return form
request = self.request
client = self.get_client()
provider = self.provider(request)
realm = self.get_realm(provider)
auth_request = client.begin(form.cleaned_data['openid'])
if QUERY_EMAIL:
sreg = SRegRequest()
for name in SRegFields:
sreg.requestField(
field_name=name, required=True
)
auth_request.addExtension(sreg)
ax = FetchRequest()
for name in AXAttributes:
ax.add(AttrInfo(name,
required=True))
provider = OpenIDProvider(request)
server_settings = \
provider.get_server_settings(request.GET.get('openid'))
extra_attributes = \
server_settings.get('extra_attributes', [])
for _, name, required in extra_attributes:
ax.add(AttrInfo(name,
required=required))
auth_request.addExtension(ax)
SocialLogin.stash_state(request)
# Fix for issues 1523 and 2072 (github django-allauth)
if "next" in form.cleaned_data and form.cleaned_data["next"]:
auth_request.return_to_args['next'] = \
form.cleaned_data['next']
redirect_url = auth_request.redirectURL(
realm,
request.build_absolute_uri(self.get_callback_url()))
return HttpResponseRedirect(redirect_url)