Example #1
0
  def process(self, request, environ, start_response):
    code = request.GET.get('code')
    # TODO: if not code: abort(401)

    try:
      access_token = oauth2.process_code(code)
    except ValueError:
      start_response('400 Bad request', [])
      return ['']

    if access_token is None:
      start_response('401 Unauthorized', [])
      return ['']

    try:
      info = oauth2.get_authorizations(access_token)
    except ValueError, err:
      start_response('500 Internal Server Error', [])
      return ['%s' % err]
 def test_ok(self):
   self.furlopen_result = StringIO('{"userid": "toto"}')
   self.assertEqual({'userid': 'toto'}, oauth2.get_authorizations('token'))
  def _do_process(self, req):
    """Process grant returned by user.
    """
    code = req.args.get('code')
    next = req.args.get('state') or req.base_path or "/"
    if not code: 
      raise HTTPBadRequest('"code" parameter is missing.')

    try:
      access_token = oauth2.process_code(code)
    except ValueError, err:
      raise HTTPBadRequest('Bad request. %s' % err)
    except AssertionError, err:
      raise HTTPUnauthorized("You are not authorized. %s" % err)

    try:
      info = oauth2.get_authorizations(access_token)
    except ValueError, err:
      raise HTTPInternalError(str(err))

    print "info:", info
    # TODO: 403 if not good authorizations

    # We cannot directly write into req.remote_user, so write in environ
    # and let LoginModule set cookie stuff as needed...
    req.environ['REMOTE_USER'] = info.get('userid')
    LoginModule._do_login(self, req)
    req.redirect(next)