/usr/share/pyshared/gluon/contrib/login_methods/oauth20_account.py is in python-gluon 1.99.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Written by Michele Comitini <mcm@glisco.it>
License: GPL v3
Adds support for OAuth 2.0 authentication to web2py.
OAuth 2.0 Draft: http://tools.ietf.org/html/draft-ietf-oauth-v2-10
"""
import time
import cgi
from urllib2 import urlopen
import urllib2
from urllib import urlencode
class OAuthAccount(object):
"""
Login will be done via OAuth Framework, instead of web2py's
login form.
Include in your model (eg db.py)::
# define the auth_table before call to auth.define_tables()
auth_table = db.define_table(
auth.settings.table_user_name,
Field('first_name', length=128, default=""),
Field('last_name', length=128, default=""),
Field('username', length=128, default="", unique=True),
Field('password', 'password', length=256,
readable=False, label='Password'),
Field('registration_key', length=128, default= "",
writable=False, readable=False))
auth_table.username.requires = IS_NOT_IN_DB(db, auth_table.username)
.
.
.
auth.define_tables()
.
.
.
CLIENT_ID=\"<put your fb application id here>\"
CLIENT_SECRET=\"<put your fb application secret here>\"
AUTH_URL="http://..."
TOKEN_URL="http://..."
from gluon.contrib.login_methods.oauth20_account import OAuthAccount
auth.settings.login_form=OAuthAccount(globals(),CLIENT_ID,CLIENT_SECRET,AUTH_URL, TOKEN_URL, **args )
Any optional arg will be passed as is to remote server for requests.
It can be used for the optional "scope" parameters for Facebook.
"""
def __redirect_uri(self, next=None):
"""Build the uri used by the authenticating server to redirect
the client back to the page originating the auth request.
Appends the _next action to the generated url so the flows continues.
"""
r = self.request
http_host=r.env.http_x_forwarded_for
if not http_host: http_host=r.env.http_host
url_scheme = r.env.wsgi_url_scheme
if next:
path_info = next
else:
path_info = r.env.path_info
uri = '%s://%s%s' %(url_scheme, http_host, path_info)
if r.get_vars and not next:
uri += '?' + urlencode(r.get_vars)
return uri
def __build_url_opener(self, uri):
"""Build the url opener for managing HTTP Basic Athentication"""
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(None,
uri,
self.client_id,
self.client_secret)
opener = urllib2.build_opener(auth_handler)
return opener
def accessToken(self):
"""Return the access token generated by the authenticating server.
If token is already in the session that one will be used.
Otherwise the token is fetched from the auth server.
"""
if self.session.token and self.session.token.has_key('expires'):
expires = self.session.token['expires']
# reuse token until expiration
if expires == 0 or expires > time.time():
return self.session.token['access_token']
if self.session.code:
data = dict(client_id=self.client_id,
client_secret=self.client_secret,
redirect_uri=self.session.redirect_uri,
response_type='token', code=self.session.code)
if self.args:
data.update(self.args)
open_url = None
opener = self.__build_url_opener(self.token_url)
try:
open_url = opener.open(self.token_url, urlencode(data))
except urllib2.HTTPError, e:
raise Exception(e.read())
finally:
del self.session.code # throw it away
if open_url:
try:
tokendata = cgi.parse_qs(open_url.read())
self.session.token = dict([(k,v[-1]) for k,v in tokendata.items()])
# set expiration absolute time try to avoid broken
# implementations where "expires_in" becomes "expires"
if self.session.token.has_key('expires_in'):
exps = 'expires_in'
else:
exps = 'expires'
self.session.token['expires'] = int(self.session.token[exps]) + \
time.time()
finally:
opener.close()
return self.session.token['access_token']
self.session.token = None
return None
def __init__(self, g, client_id, client_secret, auth_url, token_url, **args):
self.globals = g
self.client_id = client_id
self.client_secret = client_secret
self.request = g['request']
self.session = g['session']
self.auth_url = auth_url
self.token_url = token_url
self.args = args
def login_url(self, next="/"):
self.__oauth_login(next)
return next
def logout_url(self, next="/"):
del self.session.token
return next
def get_user(self):
'''Returns the user using the Graph API.
'''
raise NotImplementedError, "Must override get_user()"
if not self.accessToken():
return None
if not self.graph:
self.graph = GraphAPI((self.accessToken()))
user = None
try:
user = self.graph.get_object("me")
except GraphAPIError:
self.session.token = None
self.graph = None
if user:
return dict(first_name = user['first_name'],
last_name = user['last_name'],
username = user['id'])
def __oauth_login(self, next):
'''This method redirects the user to the authenticating form
on authentication server if the authentication code
and the authentication token are not available to the
application yet.
Once the authentication code has been received this method is
called to set the access token into the session by calling
accessToken()
'''
if not self.accessToken():
if not self.request.vars.code:
self.session.redirect_uri=self.__redirect_uri(next)
data = dict(redirect_uri=self.session.redirect_uri,
response_type='code',
client_id=self.client_id)
if self.args:
data.update(self.args)
auth_request_url = self.auth_url + "?" +urlencode(data)
HTTP = self.globals['HTTP']
raise HTTP(307,
"You are not authenticated: you are being redirected to the <a href='" + auth_request_url + "'> authentication server</a>",
Location=auth_request_url)
else:
self.session.code = self.request.vars.code
self.accessToken()
return self.session.code
return None
|