示例#1
0
    def dispatch_request(self, *args, **kwargs):
        resp = kwargs['data']
        access_token = resp['access_token']
        session['access_token'] = access_token, ''

        from urllib2 import Request, urlopen, URLError
        import json
        headers = {'Authorization': 'OAuth ' + access_token}
        req = Request('https://www.googleapis.com/oauth2/v1/userinfo', None,
                      headers)
        try:
            res = urlopen(req)
        except URLError:
            return req.read()  #TODO: Handle this

        data = json.loads(res.read())
        service_account = get_service_account(name=data['email'],
                                              service=USER.GOOGLE)
        next_url = request.args.get('next') or url_for('users.login')

        if g.user:
            if service_account:
                connect_service_account_to_user(g.user, service_account)
            else:
                service_account = create_service_account(
                    name=data['email'],
                    oauth_token=access_token,
                    service=USER.GOOGLE)
                connect_service_account_to_user(g.user, service_account)
            session['session_token'] = create_session(service_account)
            flash(
                USER.ASSOCIATION_COMPLETE %
                (USER.AUTH_SERVICE[USER.GOOGLE], data['email']), 'success')
            next_url = url_for('users.home')
        else:
            if service_account is not None:
                user = get_user(service_account=service_account)
                if user is not None:
                    # create session
                    session['session_token'] = create_session(service_account)
                    flash(u'Welcome %s %s' % (user.first_name, user.last_name))
                else:
                    # Service account exists, but no user.
                    session['session_token'] = create_session(service_account)
                    flash(
                        u'Welcome back, %s. Please consider becoming a member.'
                        % service_account.name)
            else:
                service_account = create_service_account(
                    name=data['email'],
                    oauth_token=access_token,
                    service=USER.GOOGLE)
                session['session_token'] = create_session(service_account)
                flash(u'Your %s ID %s has been saved.' %
                      (USER.AUTH_SERVICE[USER.GOOGLE], data['email']))
            if resp is None:
                flash(USER.LOGIN_DENIED)
        return redirect(next_url)
示例#2
0
    def dispatch_request(self, *args, **kwargs):
        resp = kwargs['data']
        service_account = get_service_account(name=resp['screen_name'],
                                              service=USER.TWITTER)
        next_url = request.args.get('next') or url_for('users.login')

        if g.user:
            if service_account:
                connect_service_account_to_user(g.user, service_account)
            else:
                service_account = create_service_account(
                    name=resp['screen_name'],
                    oauth_token=resp['oauth_token'],
                    oauth_secret=resp['oauth_token_secret'],
                    service=USER.TWITTER)
                connect_service_account_to_user(g.user, service_account)
            session['session_token'] = create_session(service_account)
            flash(
                USER.ASSOCIATION_COMPLETE %
                (USER.AUTH_SERVICE[USER.TWITTER], resp['screen_name']),
                'success')
            next_url = url_for('users.home')
        else:
            if service_account is not None:
                user = get_user(service_account=service_account)
                if user is not None:
                    session['session_token'] = create_session(service_account)
                    flash(u'Welcome %s %s' % (user.first_name, user.last_name))
                else:
                    session['session_token'] = create_session(service_account)
                    flash(
                        u'Welcome back, %s. Please consider becoming a member.'
                        % service_account.name)
            else:
                service_account = create_service_account(
                    name=resp['screen_name'],
                    oauth_token=resp['oauth_token'],
                    oauth_secret=resp['oauth_token_secret'],
                    service=USER.TWITTER)
                session['session_token'] = create_session(service_account)
                flash(u'Your %s ID %s has been saved.' %
                      (USER.AUTH_SERVICE[USER.TWITTER], resp['screen_name']))
            if resp is None:
                flash(USER.LOGIN_DENIED)
        return redirect(next_url)
示例#3
0
 def decorator(*args, **kwargs):
     g.user = None
     g.service_account = None
     g.is_admin = False
     if 'session_token' in session:
         import application.users.constants as USER
         from application.users.storage import get_service_account, \
             get_session, get_user
         sess = get_session(token=session['session_token'])
         if sess is not None:
             service_account = get_service_account(id=sess.service_account)
             if service_account is not None:
                 g.service_account = service_account
                 user = get_user(service_account=service_account)
                 if user is not None:
                     g.user = user
                     for test_service_account in g.user.service_accounts:
                         if test_service_account.service == USER.LOCAL:
                             g.username = test_service_account.name
                     if user.getRole() == USER.ADMIN:
                         g.is_admin = True
     return f(*args, **kwargs)
示例#4
0
 def decorator(*args, **kwargs):
     g.user = None
     g.service_account = None
     g.is_admin = False
     if 'session_token' in session:
         import application.users.constants as USER
         from application.users.storage import get_service_account, \
             get_session, get_user
         sess = get_session(token=session['session_token'])
         if sess is not None:
             service_account = get_service_account(id=sess.service_account)
             if service_account is not None:
                 g.service_account = service_account
                 user = get_user(service_account=service_account)
                 if user is not None:
                     g.user = user
                     for test_service_account in g.user.service_accounts:
                         if test_service_account.service == USER.LOCAL:
                             g.username = test_service_account.name
                     if user.getRole() == USER.ADMIN:
                         g.is_admin = True
     return f(*args, **kwargs)