get email using openid and python - python

I'm trying to get an e-mail address from an OpenId request using the following form below and web.py. I get an OpenId hash back, but I don't see anything related to an e-mail in my apache environment--just a openid_identity_hash.
This is my html form using web.py.
<input type="text" name="openid" value="" style="background: url(http://openid.net/login-bg.gif) no-repeat; padding-left: 18px; background-position: 0 50%%;" />
<input type="hidden" name="return_to" value="${returnUrl}" />
<input type="hidden" name="openid.ns.ext1" value="http://openid.net/srv/ax/1.0" />
<input type="hidden" name="openid.ext1.mode" value="fetch_request" />
<input type="hidden" name="openid.ext1.type.email" value="http://axschema.org/contact/email" />
<input type="hidden" name="openid.ext1.required" value="email" />
Looking at the docs linked off of Google's dev site to OpenID, I've found these attributes to include, which I feel like I'm doing.
openid.ns.ax=http://openid.net/srv/ax/1.0
openid.ax.mode=fetch_request
openid.ax.type.fname=http://example.com/schema/fullname
openid.ax.type.gender=http://example.com/schema/gender
openid.ax.type.fav_dog=http://example.com/schema/favourite_dog
openid.ax.type.fav_movie=http://example.com/schema/favourite_movie
openid.ax.count.fav_movie=3
openid.ax.required=fname,gender
openid.ax.if_available=fav_dog,fav_movie
openid.ax.update_url=http://idconsumer.com/update?transaction_id=a6b5c4
But it never actually asks to approve the e-mail request and I don't see it in my environment. Does web.py just not support it? Can I use something else like authkit to query the e-mail afterwards?

#voodo , Try setting these values.
openid.ns=http://specs.openid.net/auth/2.0&
openid.ns.alias3=http://openid.net/srv/ax/1.0&
openid.alias3.if_available=alias1,alias2,alias3&
openid.alias3.required=alias4&
openid.alias3.mode=fetch_request&
openid.alias3.type.alias1=http://schema.openid.net/namePerson&
openid.alias3.count.alias1=1&
openid.alias3.type.alias2=http://schema.openid.net/contact/email&
openid.alias3.count.alias2=1&
openid.alias3.type.alias3=http://axschema.org/namePerson&
openid.alias3.count.alias3=1&
openid.alias3.type.alias4=http://axschema.org/contact/email&
openid.alias3.count.alias4=1
I am not sure why this works and pure AX schema does not, even though google's XRDS specifies supporting AX schema

Related

Getting empty form fields with Python cgi stdlib

I am passing multiple form fields which are optional, but which need to be associated with a user. Python's cgi.FormDict and cgi.FieldStorage both eliminate blank entries, so items get shifted "up" and associated with the wrong user.
This problem most often shows up with checkboxes (which I have), but I also have text fields.
Simplified form code:
<input type="text" name="user" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="checkbox" value="MailingList1" />
<input type="checkbox" value="MailingList2" />
<p>
<input type="text" name="user" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="checkbox" value="MailingList1" />
<input type="checkbox" value="MailingList2" />
etc...
Users are required enter EITHER email or phone (or both) but can leave the other blank.
Now say I have input like this:
john_doe john_doe#example.com (123) 555-1234 Y Y
jane_roe jane_roe#example.com Y
george_jetson george_jetson#future.com (321) 555-4321 Y
The FormDict looks something like this:
{
'username':['john_doe','jane_roe','george_jetson'],
'email':['john_doe#example.com','jane_roe#example.com','george_jetson#future.com'],
'phone':['(123) 555-1234','(321) 555-4321'],
'MailingList1':['Y','Y'],
'MailingList2':['Y','Y']
}
I'm looping through like this:
for i in range(len(myFormDict['username'])):
username = myFormDict['username'][i]
email = myFormDict['email'][i]
phone = myFormDict['phone'][i]
MailingList1 = myFormDict['MailingList1'][i]
MailingList2 = myFormDict['MailingList2'][i]
...Inserts into the database
Before you ask, Yes, I do have error traps to prevent it from running off the end of the lists and all. The code works fine, but my database ends up looking like this:
john_doe john_doe#example.com (123) 555-1234 Y Y
jane_roe jane_roe#example.com (321) 555-4321 Y Y
george_jetson george_jetson#future.com
Jane and George are going to be pretty mad at me.
So... how do I keep the blanks so the right phone numbers and list memberships line up with the right users?
All the answers I've found searching the web involve a framework like GAE, Django, Tornado, Bottle, etc.
Bottle is lightest weight, but I tried it and it requires Python 2.5 and I'm stuck on 2.4.
If these frameworks can do it, it has to be possible. So how can I manage my data properly with just the standard library?
Thanks.
Look at the doc below (namely, keep_blank_values parameter):
>>> print cgi.FieldStorage.__init__.__doc__
Constructor. Read multipart/* until last part.
Arguments, all optional:
fp : file pointer; default: sys.stdin
(not used when the request method is GET)
headers : header dictionary-like object; default:
taken from environ as per CGI spec
outerboundary : terminating multipart boundary
(for internal use only)
environ : environment dictionary; default: os.environ
keep_blank_values: flag indicating whether blank values in
percent-encoded forms should be treated as blank strings.
A true value indicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
strict_parsing: flag indicating what to do with parsing errors.
If false (the default), errors are silently ignored.
If true, errors raise a ValueError exception.
I'm not sure if you can tweak cgi.FromDict to do what you want,
but maybe a solution would be to make every input set uniqe like so:
<input type="text" name="user" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="checkbox" value="MailingList1_1" />
<input type="checkbox" value="MailingList2_1" />
<p>
<input type="text" name="user" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="checkbox" value="MailingList1_2" />
<input type="checkbox" value="MailingList2_2" />
Note thate MailingList1 and Mailinglist2 now hafe a suffix for the "entry block".
You will have to add this at generation time of you Form and then adapt your processing accordingly by reading the checkboxes like so:
lv_key = 'MailingList1_' + str(i)
MailingList1 = myFormDict[lv_key]
...
Regards,
Martin
Even if you're using wsgi based frameworks like django or bottle, you're using same <input> names for each field - cgi formdict does not eliminate empty fields, there are no empty fields in the first place. you should give different name attribute for each user.

Browse client side and server side files in Django/Python?

I have written a front end Django/Python application. It's like a syncing tool. Users can first browse the source and destination. Source is client's computer and destination is server's computer. The interface is similar to grsync. I need to implement it in the template(front-end application). How can I do this? Thanks
EDIT:
I have this in my form.html(template for Django)
<form>
<p>Session Name: <input type="text" name="session"></p>
<p>Source: <input type="text" name="target"><input type="submit" value="Open"></p>
<p>Target: <input type="text" name="target"><input type="submit" value="Open"></p>
<input type="submit" value="Save">
<input type="button" name="Cancel" value="Cancel" onclick="window.location = 'form.html' " />
</form>
Now when the user click on the open in the source the files and folders in the client's computer should be shown and when user press OK the path should be loaded in the Source field.
Try this:
<form>
<p>Session Name: <input type="text" name="session"></p>
<p>Source: <input type="file" name="source"/></p>
<p>Target: <input type="text" name="target"/></p>
<input type="submit" value="Save">
<input type="button" name="Cancel" value="Cancel" onclick="window.location = 'form.html' " />
</form>
You need to have <input type="file"> to bring up a filepicker. But if you're uploading to a server, the user won't be able to bring up a filepicker for the server the same way they can for files on their own machine. You'll have to write your own interface for searching through the remote filesystem visually, unless you can find a plugin for it somewhere. Without a filepicker, the text field input for target should at least enable the user to type in what they want the name of their file to be on the server. Hope that answers the question!

Sending POST parameters with Python using Mechanize

I want to fill out this form using Python:
<form method="post" enctype="multipart/form-data" id="uploadimage">
<input type="file" name="image" id="image" />
<input type="submit" name="button" id="button" value="Upload File" class="inputbuttons" />
<input name="newimage" type="hidden" id="image" value="1" />
<input name="path" type="hidden" id="imagepath" value="/var/www/httpdocs/images/" />
</form>
As you can see, there are two Parameters that are named exactly the same, so when I'm using Mechanize to do it, what would look like this:
import mechanize
br = mechanize.Browser()
br.open('www.site.tld/upload.php')
br.select_form(nr=0)
br.form['image'] = '/home/user/Desktop/image.jpg'
br.submit()
I am getting the Error:
mechanize._form.AmbiguityError: more than one control matching name 'image'
Every solution I found in the Internet (including this site) didn't work. Is there a different approach?
Renaming the input in the HTML form is sadly not an option.
Thanks in Advance.
You should use find_control instead; you can add a nr keyword to select a specific control if there is ambiguity. In your case, the name and type keywords should do.
Also note that a file control doesn't take a value; use add_file instead and pass in an open file object:
br.form.find_control(name='image', type='file').add_file(
open('/home/user/Desktop/image.jpg', 'rb'), 'image/jpg', 'image.jpg')
See the documentation on forms in mechanize.

cherrypy password form

I am trying to make a user registration and login for an application I'm working on using the form below.
<html><form method="get" action="login">
Email Address: <input name="email" type="text"><br/>
Password: <input name="password" type="password"><br/>
<input type="submit" value="Submit" />
</form>
</html>
However the user name and password show up in the url, as does the submit button.
http://localhost:8080/login?email=123&password=123&submit=Submit
How do I stop this happening?
They show up in the URL because your form is using the GET method, you should be using POST and processing the values on the server side.
In addition to the POST/GET confusion Shawn points out, you might be interested to look at Cherrypy's built-in authentication tool.

HTML form name array parsing in Pyramid (Python)

Is there any way for Pyramid to process HTML form input which looks like this:
<input type="text" name="someinput[]" value="" />
or even more usefully:
<input type="text" name="someinput[0][subelement1]" value="" />
<input type="text" name="someinput[0][subelement2]" value="" />
<input type="text" name="someinput[1][subelement1]" value="" />
<input type="text" name="someinput[1][subelement2]" value="" />
...and access that data easily (e.g. via a dict)?
Any help would be much appreciated!
EDIT: to make it clearer, what I need is the ability to have a form where a user can add as many 'instances' of a group of input elements, e.g. adding between 1 and n users, each containing a firstname, lastname, username (or something like that).
One solution would be to use peppercorn. Although it does not support the syntax you're looking for, it will let you send structured data to your Pyramid application through the use of forms. A more casual description exists too.

Categories

Resources