Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 411 lines (326 sloc) 11.901 kb
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
#!/usr/bin/python
#
# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
A sample OpenID consumer app for Google App Engine. Allows users to log into
other OpenID providers, then displays their OpenID login. Also stores and
displays the most recent logins.

Part of http://code.google.com/p/google-app-engine-samples/.

For more about OpenID, see:
http://openid.net/
http://openid.net/about.bml

Uses JanRain's Python OpenID library, version 2.1.1, licensed under the
Apache Software License 2.0:
http://openidenabled.com/python-openid/

The JanRain library includes a reference OpenID provider that can be used to
test this consumer. After starting the dev_appserver with this app, unpack the
JanRain library and run these commands from its root directory:

setenv PYTHONPATH .
python ./examples/server.py -s localhost

Then go to http://localhost:8080/ in your browser, type in
http://localhost:8000/test as your OpenID identifier, and click Verify.
"""

import datetime
import logging
import os
import re
import sys
import urlparse
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from openid import fetchers
from openid.consumer.consumer import Consumer
from openid.consumer import discover
from openid.extensions import pape, sreg
import fetcher
import store
import string
import random

import models

# Set to True if stack traces should be shown in the browser, etc.
_DEBUG = False


def GenKeyName(length=8, chars=string.letters + string.digits):
  return ''.join([random.choice(chars) for i in xrange(length)])


class Session(db.Expando):
  """An in-progress OpenID login."""
  claimed_id = db.StringProperty()
  server_url = db.LinkProperty()


class Login(db.Model):
  """A completed OpenID login."""
  status = db.StringProperty(choices=('success', 'cancel', 'failure'))
  claimed_id = db.LinkProperty()
  server_url = db.LinkProperty()
  timestamp = db.DateTimeProperty(auto_now_add=True)
  session = db.ReferenceProperty(Session)


class Handler(webapp.RequestHandler):
  """A base handler class with a couple OpenID-specific utilities."""
  consumer = None
  session = None
  session_args = None

  def __init__(self):
    self.session_args = {}

  def get_consumer(self):
    """Returns a Consumer instance.
"""
    if not self.consumer:
      fetchers.setDefaultFetcher(fetcher.UrlfetchFetcher())
      if not self.load_session():
        return
      self.consumer = Consumer(self.session_args, store.DatastoreStore())

    return self.consumer

  def args_to_dict(self):
    """Converts the URL and POST parameters to a singly-valued dictionary.

Returns:
dict with the URL and POST body parameters
"""
    req = self.request
    return dict([(arg, req.get(arg)) for arg in req.arguments()])

  def load_session(self):
    """Loads the current session.
"""
    if not self.session:
      id = self.request.get('session_id')
      if id:
        try:
          self.session = db.get(db.Key.from_path('Session', int(id)))
          assert self.session
        except (AssertionError, db.Error), e:
          self.report_error('Invalid session id: %d' % id)
          return None

        fields = self.session.dynamic_properties()
        self.session_args = dict((f, getattr(self.session, f)) for f in fields)

      else:
        self.session_args = {}
        self.session = Session()
        self.session.claimed_id = self.request.get('openid')

      return self.session

  def store_session(self):
    """Stores the current session.
"""
    assert self.session
    for field, value in self.session_args.items():
      setattr(self.session, field, value)
    self.session.put()

  def render(self, extra_values={}):
    """Renders the page, including the extra (optional) values.

Args:
template_name: string
The template to render.

extra_values: dict
Template values to provide to the template.
"""
    logins = Login.gql('ORDER BY timestamp DESC').fetch(20)
    for login in logins:
      login.display_name = self.display_name(login.claimed_id)
      login.friendly_time = self.relative_time(login.timestamp)

    values = {
      'response': {},
      'openid': '',
      'logins': logins,
    }
    values.update(extra_values)
    cwd = os.path.dirname(__file__)
    path = os.path.join(cwd, 'templates', 'base.html')
    self.response.out.write(template.render(path, values, debug=_DEBUG))

  def report_error(self, message, exception=None):
    """Shows an error HTML page.

Args:
message: string
A detailed error message.
"""
    if exception:
      logging.exception('Error: %s' % message)
    self.render({'error': message})

  def show_front_page(self):
    """Do an internal (non-302) redirect to the front page.

Preserves the user agent's requested URL.
"""
    front_page = FrontPage()
    front_page.request = self.request
    front_page.response = self.response
    front_page.get()

  def relative_time(self, timestamp):
    """Returns a friendly string describing how long ago the timestamp was.

Args:
timestamp: a datetime

Returns:
string
"""
    def format_number(num):
      if num <= 9:
        return {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five',
                6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}[num]
      else:
        return str(num)

    delta = datetime.datetime.now() - timestamp
    minutes = delta.seconds / 60
    hours = minutes / 60

    if delta.days > 1:
      return '%s days ago' % format_number(delta.days)
    elif delta.days == 1:
      return 'yesterday'
    elif hours > 1:
      return '%s hours ago' % format_number(hours)
    elif hours == 1:
      return 'an hour ago'
    elif minutes > 25:
      return 'half an hour ago'
    elif minutes > 5:
      return '%s minutes ago' % format_number(minutes)
    else:
      return 'moments ago'

  def display_name(self, openid_url):
    """Extracts a short, representative part of an OpenID URL for display.

For example, it returns "ryan" for:
ryan.com
www.ryan.com
ryan.provider.com
provider.com/ryan
provider.com/id/path/ryan

Adapted from Net::OpenID::Consumer, by Brad Fitzpatrick. See:

http://code.sixapart.com/svn/openid/trunk/perl/Net-OpenID-Consumer/lib/Net/OpenID/VerifiedIdentity.pm

Args:
openid_url: string

Returns:
string
"""
    if not openid_url:
      return 'None'

    username_re = '[\w.+-]+'

    scheme, host, path, params, query, frag = urlparse.urlparse(openid_url)

    def sanitize(display_name):
      if '@' in display_name:
        # don't display full email addresses; use just the user name part
        display_name = display_name[:display_name.index('@')]
      return display_name

    # is the username in the params?
    match = re.search('(u|id|user|userid|user_id|profile)=(%s)' % username_re,
                      path)
    if match:
      return sanitize(match.group(2))

    # is the username in the path?
    path = path.split('/')
    if re.match(username_re, path[-1]):
      return sanitize(path[-1])

    # use the hostname
    host = host.split('.')
    if len(host) == 1:
      return host[0]

    # strip common tlds and country code tlds
    common_tlds = ('com', 'org', 'net', 'edu', 'info', 'biz', 'gov', 'mil')
    if host[-1] in common_tlds or len(host[-1]) == 2:
      host = host[:-1]
    if host[-1] == 'co':
      host = host[:-1]

    # strip www prefix
    if host[0] == 'www':
      host = host[1:]

    return sanitize('.'.join(host))


class FrontPage(Handler):
  """Show the default front page."""
  def get(self):
    self.render()


class StartHandler(Handler):
  """Handles a POST response to the OpenID login form."""

  def post(self):
    """Handles login requests."""
    logging.info(self.args_to_dict())
    openid_url = self.request.get('openid_url')
    if not openid_url:
      self.report_error('Please enter an OpenID URL.')
      return

    logging.debug('Beginning discovery for OpenID %s' % openid_url)
    try:
      consumer = self.get_consumer()
      if not consumer:
        return
      auth_request = consumer.begin(openid_url)
    except discover.DiscoveryFailure, e:
      self.report_error('Error during OpenID provider discovery.', e)
      return
    except discover.XRDSError, e:
      self.report_error('Error parsing XRDS from provider.', e)
      return

    self.session.claimed_id = auth_request.endpoint.claimed_id
    self.session.server_url = auth_request.endpoint.server_url
    self.store_session()

    sreg_request = sreg.SRegRequest(optional=['nickname', 'fullname', 'email'])
    auth_request.addExtension(sreg_request)

    pape_request = pape.Request([pape.AUTH_MULTI_FACTOR,
                                 pape.AUTH_MULTI_FACTOR_PHYSICAL,
                                 pape.AUTH_PHISHING_RESISTANT,
                                 ])
    auth_request.addExtension(pape_request)

    parts = list(urlparse.urlparse(self.request.uri))
    parts[2] = 's/finish'
    parts[4] = 'session_id=%d' % self.session.key().id()
    parts[5] = ''
    return_to = urlparse.urlunparse(parts)
    realm = urlparse.urlunparse(parts[0:2] + [''] * 4)

    redirect_url = auth_request.redirectURL(realm, return_to)
    logging.debug('Redirecting to %s' % redirect_url)
    self.response.set_status(302)
    self.response.headers['Location'] = redirect_url


class FinishHandler(Handler):
  """Handle a redirect from the provider."""
  def get(self):
    args = self.args_to_dict()
    consumer = self.get_consumer()
    if not consumer:
      return

    if self.session.login_set.get():
      self.render()
      return

    response = consumer.complete(args, self.request.uri)
    assert response.status in Login.status.choices

    if response.status == 'success':
      sreg_data = sreg.SRegResponse.fromSuccessResponse(response).items()
      pape_data = pape.Response.fromSuccessResponse(response)
      self.session.claimed_id = response.endpoint.claimed_id
      self.session.server_url = response.endpoint.server_url
    elif response.status == 'failure':
      logging.error(str(response))

    logging.debug('Login status %s for claimed_id %s' %
                  (response.status, self.session.claimed_id))

    if response.status != 'success':
      self.render(locals())
      return

    session_id = GenKeyName(length=16)

    login = Login(key_name=session_id,
                  status=response.status,
                  claimed_id=self.session.claimed_id,
                  server_url=self.session.server_url,
                  session=self.session.key())
    login.put()

    # update the login time
    user = models.User(openid_user=login.claimed_id).GetOrCreateFromDatastore()
    user.put()

    self.response.headers.add_header('Set-Cookie',
                                     'session=%s; path=/' % session_id)

    # TODO(bradfitz: redirect to proper 'next' URL
    self.redirect('/')


# Map URLs to our RequestHandler subclasses above
_URLS = [
  ('/s/openid', FrontPage),
  ('/s/startopenid', StartHandler),
  ('/s/finish', FinishHandler),
]

def main(argv):
  application = webapp.WSGIApplication(_URLS, debug=_DEBUG)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main(sys.argv)
Something went wrong with that request. Please try again.