示例#1
0
文件: views.py 项目: P1sar/QuAsker
def oauth_authorized(resp):
    next_url = request.args.get('next') or url_for('index')
    if resp is None:
        flash('You denied the request to sign in.', "error")
        return redirect(url_for('login'))

    session['facebook_token'] = (
        resp['access_token'], "")   
    session['facebook_user'] = resp['screen_name']

    user_data = facebook.get('/me').data
    user = User.query.filter(User.email == user_data['email']).first()


    if user is None:
        new_user = User(id=user_data['id'], email=user_data['email'],
                        nickname=user_data['first_name'], reg_date = datetime.now)
        db.session.add(new_user)
        db.session.commit()
        login_user(new_user)
    else:
        login_user(user)

    flash('You were signed in as %s' % resp['screen_name'], "greetings")
    return redirect('login')
示例#2
0
def facebook_authorized(resp):
    if resp is None:
        return redirect(url_for('index'))
    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me')
    userdata = User.query.filter(User.facebook_id == me.data['id']).first()
    
    if userdata:
        session["user_email"] = userdata.email
        session["user_name"] = userdata.username
        session["user_id"] = userdata.id
        flash(u"반갑습니다, %s 님!" % session["user_name"])
    else:
        user = User(
                email = me.data['email'],
                username = me.data['name'],
                facebook_id = me.data['id'],
                access_token = session['oauth_token'][0],
                photo = 'http://graph.facebook.com/'+me.data['id']+'/picture?width=200&height=200'+'width="100%"'+'height="100%"'
            )
        db.session.add(user)
        db.session.commit()
        userdata = User.query.filter(User.facebook_id == me.data['id']).first()
        session["user_email"] = userdata.email
        session["user_name"] = userdata.username
        session["user_id"] = userdata.id
        flash(u"반갑습니다, %s 님!" % session["user_name"])

    return redirect(url_for('index'))
示例#3
0
def facebook_authorized(resp):

    next_url = request.args.get('next') or url_for('secret')

    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    
    session['facebook_token'] = (resp['access_token'], '')
    me = facebook.get('/me')

    account = Account.query.filter_by(facebook_id = me.data['id']).first()
    if account is None:
        account = create_user_facebook(facebook_id=me.data['id'], 
                facebook_token=resp['access_token'])
    else:
        # TODO don't generate the token _every_ time...
        # XXX check if expired? or keep that in header_loader?
        #account.generate_auth_token()
        db.session.add(account)
        db.session.commit()

    login_user(account)

    return redirect(next_url)
示例#4
0
def facebook_authorized(resp):

    next_url = request.args.get('next') or url_for('secret')

    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'], request.args['error_description'])

    session['facebook_token'] = (resp['access_token'], '')
    me = facebook.get('/me')

    account = Account.query.filter_by(facebook_id=me.data['id']).first()
    if account is None:
        account = create_user_facebook(facebook_id=me.data['id'],
                                       facebook_token=resp['access_token'])
    else:
        # TODO don't generate the token _every_ time...
        # XXX check if expired? or keep that in header_loader?
        #account.generate_auth_token()
        db.session.add(account)
        db.session.commit()

    login_user(account)

    return redirect(next_url)
示例#5
0
def authenticate(provider_id, access_token, **kwargs):
    provider_id = provider_id.lower()
    if provider_id == 'google':
        me = google.get('userinfo', token=(access_token, ''))
        auth_data = {
            'name': me.data['name'],
            'email': me.data['email'],
            'picture_url': me.data['picture'],
            'provider_id': 'google',
            'provider_user_id': me.data['id']
        }
    elif provider_id == 'facebook':
        me = facebook.get(
            '/me/?fields=email,name,id,picture.height(200).width(200)',
            token=(access_token, ''))
        auth_data = {
            'name': me.data['name'],
            'email': me.data['email'],
            'picture_url': me.data['picture']['data']['url'],
            'provider_id': 'facebook',
            'provider_user_id': me.data['id']
        }
    else:
        return 'Invalid Provider Id'

    return set_user(auth_data)
示例#6
0
文件: views.py 项目: JrtPec/coopkot
def facebook_authorized(resp):
    next_url = request.args.get('next') or url_for('index')
    if resp is None or 'access_token' not in resp:
        flash('Invalid login. Please try again.')
        return redirect(url_for('login'))

    session['facebook_token'] = (resp['access_token'], '')

    data = facebook.get('/me').data
    if 'id' in data and 'name' in data:
        user_id = data['id']
        user_name = data['name']
    if 'email' in data:
        user_email = data['email']

    user = User.query.filter_by(facebook_id = user_id).first()

    if user is None:
        nickname = user_name
        nickname = User.make_unique_nickname(nickname)
        if user_name in ADMIN_NAMES:
            role = ROLE_ADMIN
        else:
            role = ROLE_USER
        user = User(nickname = nickname, email = user_email,facebook_id = user_id, role = role)
        db.session.add(user)
        db.session.commit()

    login_user(user)
    return redirect(request.args.get('next') or url_for('index'))
示例#7
0
文件: views.py 项目: JrtPec/archiver
def facebook_authorized(resp):
    next_url = request.args.get('next') or url_for('index')
    if resp is None or 'access_token' not in resp:
        flash('Invalid login. Please try again.')
        return redirect(url_for('login'))

    session['facebook_token'] = (resp['access_token'], '')

    data = facebook.get('/me').data
    if 'id' in data and 'name' in data:
        user_id = data['id']
        user_name = data['name']
    if 'email' in data:
        user_email = data['email']

    user = User.query.filter_by(facebook_id = user_id).first()

    if user is None:
        name = user_name
        user = User(name = name, facebook_id = user_id)
        db.session.add(user)
        db.session.commit()

    login_user(user)
    return redirect(request.args.get('next') or url_for('index'))
示例#8
0
def authenticate(provider_name, access_token, **kwargs):

    if provider_name == 'google':
        me = google.get('userinfo', token=(access_token, ''))
        if 'error' in me.data:
            return jsonify({'error': me.data['error']})
        auth_data = {
            'name': me.data['name'],
            'email': me.data['email'],
            'picture_url': me.data['picture'],
            'provider_name': 'google',
            'provider_user_id': me.data['id']
        }

    elif provider_name == 'facebook':
        me = facebook.get('/me/?fields=email,name,id,picture.height(200).width(200)', token=(access_token, ''))
        if 'error' in me.data:
            return jsonify({'error': me.data['error']})
        auth_data = {
            'name': me.data['name'],
            'email': me.data['email'],
            'picture_url': me.data['picture']['data']['url'],
            'provider_name': 'facebook',
            'provider_user_id': me.data['id']
        }
    else:
        return jsonify({'error': 'Invalid Provider Id'})

    social_login = SocialLogin.query.filter_by(
        provider_name=auth_data['provider_name'],
        provider_user_id=auth_data['provider_user_id']
    ).first()

    # In case there's a login without a user
    if social_login is not None and social_login.user is None:
        social_login.delete()
        social_login = None

    is_new_user = False

    if social_login is None:
        is_new_user = True
        new_user = User(
            public_id=str(uuid.uuid4()),
            name=auth_data['name'],
            email=auth_data['email'],
            picture_url=auth_data['picture_url'],
        )

        social_login = SocialLogin(
            user=new_user,
            provider_name=auth_data['provider_name'],
            provider_user_id=auth_data['provider_user_id'],
        )
        db.session.add(new_user)
        db.session.commit()

    user = social_login.user
    return user
示例#9
0
def authorize_facebook():
    resp = facebook.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error%s' % (
            request.args['error_reason'], request.args['error_description'])
    if isinstance(resp, OAuthException):
        return 'Acess denied: %s' % resp.message
    session['oauth_token'] = (resp['access_token'], '')
    fb_user = facebook.get('/me/?fields=email,name,id,picture')
    return set_user(fb_user)
示例#10
0
文件: views.py 项目: Tiotao/Yikes
def facebook_callback(resp):

    next_url = request.args.get('next') or url_for('index')

    if resp is None or 'access_token' not in resp:
        flash('You denied the login')
        return redirect(next_url)

    session['fb_access_token'] = (resp['access_token'], '')
    
    remember_me = False
    
    if 'remember_me' in session:
        remember_me = session['remember_me']
        session.pop('remember_me', None)

    fb_user = facebook.get('/me')
    fb_id = me.data['id']
    fb_email = me.data['email']
    
    if fb_user.data['username']:
        fb_username = fb_user.data['username']
    else:
        fb_username = fb_user.data['name']

    user = dq.find(User, ['facebook_id'], [str(fb_id)]).first()

    #for connecting user's acc with facebook acc
    if g.user is not None and g.user.is_authenticated():
        if user is None:
            dq.update(g.user, ['facebook_id'], [str(fb_id)])
            flash('You are now linked with %s' % fb_username)
        else:
            flash('Your fb account has been linked previously')

        return redirect(url_for('settings'))

    #for new login

    if user is None:
        u = dq.find(User, ['email'], [str(fb_email)]).first()
        if u:
            login_user(u, remember = remember_me)
            return facebook.authorize(callback=url_for('facebook_callback',
                    next=request.args.get('next') or request.referrer or None,
                    _external=True))
        else:
            dq.init_user(fb_username, fb_email, ROLE_USER, None, None, str(fb_id), None, None)

    login_user(user, remember = remember_me)

    flash('You are now logged in as %s' % user.nickname)
    return redirect(url_for('index'))
示例#11
0
文件: views.py 项目: aw-redirct/pong2
def facebook_authorized():
    resp = facebook.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'], request.args['error_description'])
    if isinstance(resp, OAuthException):
        return 'Access denied: %s' % resp.message

    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get(
        '/me/?fields=email,name,id,picture.height(200).width(200)')
    return set_user('Facebook', me)
示例#12
0
def facebook_authorized(resp):
	 if resp is None:
	 # return 'Access denied: reason=%s error=%s' % (
	 #     request.args['error_reason'],
	 #     request.args['error_description']
	 # )
		  return redirect(url_for('index'))
	 session['oauth_token'] = (resp['access_token'], '')
	 me = facebook.get('/me')
	 session['username'] = me.data['name']
	 session['user_id'] = me.data['id']
	 return redirect(url_for('index'))
示例#13
0
def facebook_authorized(resp):
  if resp is None:
    return 'Access denied: reason=%s error=%s' % (request.args['error_reason'], request.args['error_description'])
  session['facebook_oauth_token'] = (resp['access_token'], '')
  me = facebook.get('/me')
  user = User.query.filter_by(username=me.data['email']).first()
  if not user:
    user = User(me.data['email'], '')
    db.session.add(user)
    db.session.commit()
  
  login_user(user)
  flash('Logged in as id=%s name=%s' % (me.data['id'], me.data['name']),'success')
  return redirect(request.args.get('next'))
示例#14
0
def facebook_authorized():
    """Manages fb response for auth request"""
    resp = facebook.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    if isinstance(resp, OAuthException):
        return 'Access denied: %s' % resp.message

    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me/?fields=email,name,id,picture.height(200).width(200)')
    return set_user(me)
示例#15
0
def facebook_authorized(resp):
    if resp is None:
        return "Access denied: reason=%s error=%s" % (request.args["error_reason"], request.args["error_description"])
    session["facebook_oauth_token"] = (resp["access_token"], "")
    me = facebook.get("/me?fields=id,name,email")
    user = User.query.filter_by(email=me.data["email"]).first()
    if not user:
        user = User(username=me.data["name"], email=me.data["email"], password=me.data["id"])
        db.session.add(user)
        db.session.commit()

    login_user(user)
    flash("Logged in as id=%s name=%s" % (me.data["id"], me.data["name"]), "success")
    return redirect(request.args.get("next"))
示例#16
0
文件: views.py 项目: alearabbit/friki
def facebook_authorized(resp):
    if resp is None:
        return redirect(url_for('index'))
    session['oauth_token']  = (resp['access_token'], '')
    me = facebook.get('/me')
    session['username'] = me.data['name']
    session['user_id'] = me.data['id']
    session['user_link'] = me.data['link']
    session['email'] = me.data['email']
    user = User(
        name = me.data['name'],
        email = me.data['email'],
        access_token_facebook = resp['access_token'])
    db.session.add(user)
    db.session.commit()
    return redirect(url_for('main_bucket'))
示例#17
0
def facebook_login():

    if not facebook.authorized:
        return redirect(url_for("facebook.login"))
    resp = facebook.get("/me")
    assert resp.ok, resp.text

    user = UserModel.query.filter_by(username=resp.json()["name"]).first()

    # Add user to the database if not already there
    if user is None:
        user = UserModel(username=resp.json()["name"])
        db.session.add(user)
        db.session.commit()
        user = UserModel.query.filter_by(username=resp.json()["name"]).first()

    login_user(user)
    return render_template('index.html')
示例#18
0
def add_new_user():
	""" Uses FB id to check for exisiting user in db. If none, adds new user."""
	fb_user = facebook.get('/me').data
	existing_user = db.session.query(User).filter(User.facebook_id == fb_user['id']).first()
	if existing_user is None:
		new_user = model.User()
		new_user.facebook_id = fb_user['id']
		new_user.first_name = fb_user['first_name']
		new_user.last_name = fb_user['last_name']
		new_user.email = fb_user['email']
		new_user.facebook_url = fb_user['link']
		new_user.avatar = get_user_photo()
		# commit new user to database
		db.session.add(new_user)
		db.session.commit()
		# Go get that new user
		new_user = db.session.query(User).filter(User.facebook_id == fb_user['id']).first()
		return new_user
	else:
		return existing_user
示例#19
0
def facebook_authorized(resp):
    print resp
    if resp is None:
        flash(' Access denied')
        return redirect(url_for('login'))
    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me')
    email = me.data['email']
    name = me.data['email'].split('@')[0]
    user = User.query.filter_by(email=email).first()
    if user.nickname != name:
        print "nickname is" + user.nickname + "name is" + name
        db.session.query(User).filter(User.email==email).\
          update({User.nickname:name},synchronize_session=False)
        print "not same"
        db.session.commit()
    if user is None:
        user = User(nickname=name, email=email)
        db.session.add(user)
        db.session.commit()
    login_user(user, remember=True)
    return redirect(request.args.get('next') or url_for('index'))
示例#20
0
def facebook_authorized(resp):
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'], request.args['error_description'])
    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me')
    user = User.query.filter_by(email=me.data['email']).first()

    if user is not None and me.data['email'] == user.email:
        login_user(user)
        flash('Logged in Successfully!')
        return redirect(url_for('main'))

    user = User(first_name=me.data['first_name'],
                last_name=me.data['last_name'],
                email=me.data['email'],
                role=ROLE_USER)
    db.session.add(user)
    db.session.commit()
    login_user(user)
    flash('User registered!')
    return redirect(url_for('main'))
示例#21
0
def get_user_photo():
	photo = facebook.get('/me/picture?redirect=0&height=1000&type=normal&width=1000').data
	photo_url = photo['data']['url']
	return photo_url