Exemple #1
0
def LogoutSubmit(request, default_success_url='/'):
  response = django.http.HttpResponse()
  if request.method == 'GET':
    s = openidgae.get_session(request, response)
    if s:
      s.person = None
      s.put()

    continueUrl = get_continue_url(request, default_success_url)

    return django.http.HttpResponseRedirect(continueUrl)
Exemple #2
0
def OpenIDStartSubmit(request, default_success_url='/'):
  response = django.http.HttpResponse()
  if request.method == 'POST':
    openid = request.POST.get('openid_identifier', '')
    openid = openid.strip()
    if not openid:
      return show_main_page(request)

    c = Consumer({},get_store())
    try:
      auth_request = c.begin(openid)
    except discover.DiscoveryFailure, e:
      logging.error('OpenID discovery error with begin on %s: %s'
          % (openid, str(e)))
      return show_main_page(request, 'An error occured determining your server information.  Please try again.')

    from openid.extensions import sreg
    sreg_request = sreg.SRegRequest(
        optional=['dob', 'gender', 'postcode'],
        required=['email', 'nickname', 'fullname', 'country', 'language', 'timezone'])
    auth_request.addExtension(sreg_request)

    from openid.extensions import ax
    ax_req = ax.FetchRequest()
    ax_req.add(ax.AttrInfo('http://schema.openid.net/contact/email',
          alias='email',required=True))
    ax_req.add(ax.AttrInfo('http://axschema.org/namePerson/first',
          alias='firstname',required=True))
    ax_req.add(ax.AttrInfo('http://axschema.org/namePerson/last',
          alias='lastname',required=True))
    ax_req.add(ax.AttrInfo('http://axschema.org/pref/language',
          alias='language',required=True))
    ax_req.add(ax.AttrInfo('http://axschema.org/contact/country/home',
          alias='country',required=True))
    auth_request.addExtension(ax_req)

    import urlparse
    parts = list(urlparse.urlparse(get_full_path(request)))
    # finish URL with the leading "/" character removed
    parts[2] = django.core.urlresolvers.reverse('openidgae.views.OpenIDFinish')[1:]

    continueUrl = get_continue_url(request, default_success_url)
    import urllib
    parts[4] = 'continue=%s' % urllib.quote_plus(continueUrl)
    parts[5] = ''
    return_to = urlparse.urlunparse(parts)

    realm = urlparse.urlunparse(parts[0:2] + [''] * 4)

    # save the session stuff
    session = openidgae.get_session(request, response)
    import pickle
    session.openid_stuff = pickle.dumps(c.session)
    session.put()

    # send the redirect!  we use a meta because appengine bombs out
    # sometimes with long redirect urls
    redirect_url = auth_request.redirectURL(realm, return_to)
    response.write(
        "<html><head><meta http-equiv=\"refresh\" content=\"0;url=%s\"></head><body></body></html>"
        % (redirect_url,))
    return response
Exemple #3
0
def login_succeeded(request):
  #openidgae.views.initOpenId()
  response = HttpResponse()
  logging.info("cloudcycle, login succeeded")
  if request.method == 'GET':
    args = openidgae.views.args_to_dict(request.GET)
    #url = 'http://'+request.META['HTTP_HOST']+"/"
    url = 'http://'+request.META['HTTP_HOST']+django.core.urlresolvers.reverse('auth.views.login_succeeded')
    logging.debug("cloudcycle, url for redirect: %s" % url)
    session = openidgae.get_session(request, response)
    s = {}
    try:
        s = pickle.loads(str(session.openid_stuff))
    except:
        session.openid_stuff = None

    session.put()
	
    logging.info("cloudcycle, url="+url)
    c = Consumer(s, openidgae.views.get_store())
    auth_response = c.complete(args, url)

    logging.info("cloudcycle, auth_response "+auth_response.status)
    if auth_response.status == 'success':
        openid = auth_response.getDisplayIdentifier()
        logging.info("cloudcycle, got auth_response "+openid)
        person = CyclePerson.all().filter('openid = ', openid).get()
        if not person:
            logging.info("cloudcycle, creating new person")		
            person = CyclePerson()
            person.openid = openid

        # we got an email address
        if 'openid.ext1.value.email' in args:
            person.email = db.Email(args['openid.ext1.value.email'])

        # we got an oAuth signed request token!
        if 'openid.ext2.request_token' in args:
            # note secret = "" - this is essential, as values such as None and False cause errors
            #signed_request_token = gdata.auth.OAuthToken(key=args['openid.ext2.request_token'], secret="")
            #client.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, settings.GOOGLE_CONSUMER_KEY, settings.GOOGLE_CONSUMER_SECRET)
            #access_token = client.UpgradeToOAuthAccessToken(signed_request_token)
            #logging.info("cloudcycle, access_token: %s" % str(access_token))
            #person.access_token = str(access_token)
            signed_request_token = gdata.auth.OAuthToken(key=args['openid.ext2.request_token'], secret="")
            #person.auth_token = pickle.dumps(signed_request_token)
            client.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1,
            settings.GOOGLE_CONSUMER_KEY, settings.GOOGLE_CONSUMER_SECRET)
            access_token = client.UpgradeToOAuthAccessToken(signed_request_token)
            person.access_token = str(access_token)
            #logging.info("cloudcycle, person.access_token: %s" % person.access_token)

        logging.info("cloudcycle, person: %s" % person)
        person.put()

        s = openidgae.get_session(request, response)
        s.person = person.key()
        s.put()

        request.openidgae_logged_in_person = person
	  
        #logging.info("cloudcycle, cheching: %s" % openidgae.get_current_person(request, response))
        continueUrl = request.GET.get('continue', '/')
        # Sanitize
        if continueUrl.find('//') >= 0 or not continueUrl.startswith('/'):
            continueUrl = '/'
        
        logging.info("cloudcycke, continueUrl: %s" % continueUrl)
        return HttpResponseRedirect(continueUrl)

    else:
        #	   logging.debug("OpenID failed")
        return openidgae.views.show_main_page(request, 'OpenID verification failed :(')
Exemple #4
0
def OpenIDFinish(request, default_success_url='/'):
  response = django.http.HttpResponse()
  if request.method not in ('GET', 'POST'):
    return django.http.HttpResponseNotAllowed(['GET', 'POST'])
  else:
    args = args_to_dict(request.GET)
    assert type(args) is dict
    if request.method == 'POST':
      args.update(args_to_dict(request.POST))
    url = 'http://'+request.META['HTTP_HOST']+django.core.urlresolvers.reverse('openidgae.views.OpenIDFinish')
    session = openidgae.get_session(request, response)
    s = {}
    if session.openid_stuff:
      try:
        import pickle
        s = pickle.loads(str(session.openid_stuff))
      except:
        session.openid_stuff = None

    session.put()

    c = Consumer(s, get_store())
    auth_response = c.complete(args, url)

    sreg_response = {}
    ax_items = {}
    if auth_response.status == openid.consumer.consumer.SUCCESS:
      from openid.extensions import sreg
      sreg_response = sreg.SRegResponse.fromSuccessResponse(auth_response)
      sreg_response = dict(sreg_response.iteritems())
      logging.debug("sreg_response: %r" % sreg_response)

      from openid.extensions import ax
      ax_response = ax.FetchResponse.fromSuccessResponse(auth_response)
      logging.debug("ax_response: %r" % ax_response)
      if ax_response:
        SHORTNAMES = (
          ('email', 'http://schema.openid.net/contact/email'),
          ('firstname', 'http://axschema.org/namePerson/first'),
          ('lastname', 'http://axschema.org/namePerson/last'),
          ('language', 'http://axschema.org/pref/language'),
          ('country', 'http://axschema.org/contact/country/home'),
        )
        for short, long in SHORTNAMES:
          try:
            ax_items[short] = ax_response.get(long)
          except KeyError, e:
            pass
        logging.debug("ax_items: %r" % ax_items)

      openid_url = auth_response.getDisplayIdentifier()

      import models
      persons = models.Person.gql('WHERE openid = :1', openid_url)
      if persons.count() == 0:
        p = models.Person()
        p.openid = openid_url
        p.ax_dict().update(ax_items)
        p.sreg_dict().update(sreg_response)
        p.put()
      else:
        p = persons[0]
        changed = False
        for key in sreg_response:
          if not p.sreg_dict().has_key(key) or \
              p.sreg_dict()[key] != sreg_response[key]:
            logging.debug("Setting sreg %s" % key)
            p.sreg_dict()[key] = sreg_response[key]
            changed = True
        for key in ax_items:
          if not p.ax_dict().has_key(key) or \
              p.ax_dict()[key] != ax_items[key]:
            logging.info("Setting ax %s" % key)
            p.ax_dict()[key] = ax_items[key]
            changed = True
        if changed:
          p.put()

      s = openidgae.get_session(request, response)
      s.person = p.key()
      request.openidgae_logged_in_person = p

      s.put()

      continueUrl = get_continue_url(request, default_success_url)

      return django.http.HttpResponseRedirect(continueUrl)

    else:
Exemple #5
0
	parts[2] = django.core.urlresolvers.reverse('auth.views.login_succeeded')[1:]
	#parts[2] = '/'
	continueUrl = request.GET.get('continue', '/')
	# Sanitize
	if continueUrl.find('//') >= 0 or not continueUrl.startswith('/'):
	  continueUrl = '/'
	  
	parts[4] = 'continue=%s' % urllib.quote_plus(continueUrl)
	parts[5] = ''

	return_to = urlparse.urlunparse(parts)
	
	realm = urlparse.urlunparse(parts[0:2] + [''] * 4)
	
	# save the session stuff
	session = openidgae.get_session(request, response)
	session.openid_stuff = pickle.dumps(c.session)
	session.put()
	
	# send the redirect!  we use a meta because appengine bombs out
	# sometimes with long redirect urls
	redirect_url = auth_request.redirectURL(realm, return_to)
	exchange_params = {
				'openid.ns.ext1': 'http://openid.net/srv/ax/1.0',
				'openid.ext1.mode': 'fetch_request',
				'openid.ext1.required': 'email,first,last,country,lang',
				'openid.ext1.type.email': 'http://schema.openid.net/contact/email',
				'openid.ext1.type.first': 'http://axschema.org/namePerson/first',
				'openid.ext1.type.last': 'http://axschema.org/namePerson/last',
				'openid.ext1.type.country': 'http://axschema.org/contact/country/home',
				'openid.ext1.type.lang': 'http://axschema.org/pref/language',