示例#1
0
def oauth_callback(provider_name):
    try:
        if provider_name not in providers.keys():
            abort(404)

        provider = providers[provider_name]
        response = provider.authorized_response()

        if response is not None:
            if provider_name == 'twitter':
                access_token = response.get('oauth_token'), response.get(
                    'oauth_token_secret')
            if provider_name == 'github':
                access_token = response.get('access_token')
            if provider_name == 'gitee':
                access_token = response.get('access_token')
        else:
            access_token = None

        if access_token is None:
            flash('权限拒绝,请稍后再试!', 'danger')
            return redirect(url_for('auth_bp.login'))

        username, website, email, bio, avatar = get_social_profile(
            provider, access_token)

        if email is None:
            flash('未能正确的获取到您的邮箱,请到第三方社交网络设置邮箱后登录!', 'danger')
            return redirect(url_for('auth_bp.login'))

        user = User.query.filter_by(email=email).first()
        if user is None:
            tp = ThirdParty.query.filter_by(name=provider.name).first()
            user = User(username=username,
                        email=email,
                        website=website,
                        password=provider.name,
                        avatar=avatar,
                        slogan=bio,
                        confirm=1,
                        reg_way=tp.id)
            db.session.add(user)
            db.session.commit()
            login_user(user, remember=True)
            flash('使用{}社交账号登录成功!'.format(provider.name), 'success')
            return redirect(url_for('accounts_bp.profile', user_id=user.id))
        login_user(user, remember=True)
        flash('使用{}社交账号登录成功!'.format(provider.name), 'success')
        return redirect(url_for('blog_bp.index'))
    except:
        import traceback
        traceback.print_exc()
        flash('使用{}登录出现了意外了~稍后再试吧~'.format(provider.name), 'danger')
        return redirect(url_for('auth_bp.login'))
示例#2
0
文件: auth.py 项目: ArvinHe/Blogin
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        name = form.user_name.data
        pwd = form.confirm_pwd.data
        email = form.user_email.data.lower()
        user = User(username=name, email=email, password=pwd, )
        user.set_password(pwd)
        user.set_role()
        db.session.add(user)
        db.session.commit()
        token = generate_token(user, operation='confirm')
        send_confirm_email(user=user, token=token)
        flash('注册成功,欢迎加入Blogin.', 'success')
        return redirect(url_for('.login'))
    return render_template('main/register.html', form=form)
示例#3
0
    def setUp(self):
        app = create_app('testing')
        self.context = app.test_request_context()
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()
        # 数据库初始化操作
        db.drop_all()
        db.create_all()
        Role.init_role()
        States.init_states()
        ThirdParty.init_tp()
        bt = BlogType(name='Test', description='test type')
        blog1 = Blog(title='test1',
                     content='Blog test1 contents.',
                     introduce='blog1 introduce',
                     type_id=1,
                     pre_img='test.jpg',
                     delete_flag=1)
        blog2 = Blog(title='test2',
                     content='Blog test2 contents.',
                     introduce='blog2 introduce',
                     type_id=1,
                     pre_img='test.jpg',
                     delete_flag=1)
        blog3 = Blog(title='test3',
                     content='Blog test3 contents.',
                     introduce='blog3 introduce',
                     type_id=1,
                     pre_img='test.jpg',
                     delete_flag=1)

        ph1 = Photo(title='test photo1',
                    description='test photo1 description',
                    save_path='/test/save/',
                    save_path_s='/test/save/',
                    level=1)

        super_user = User(username='******',
                          email='*****@*****.**',
                          password='******',
                          confirm=1,
                          avatar='/static/img/admin/admin.jpg')
        super_user.set_password('12345678')

        user = User(username='******',
                    email='*****@*****.**',
                    password='******',
                    confirm=1)
        user.set_password('12345678')

        unconfirm_user = User(username='******',
                              email='*****@*****.**',
                              password='******',
                              confirm=1)
        unconfirm_user.set_password('12345678')
        db.session.add_all(
            [super_user, user, unconfirm_user, bt, blog1, blog2, blog3, ph1])
        db.session.commit()