Пример #1
0
def add_user():
    try:
        username = request.json['username']
        if User.query.filter_by(username=username).first():
            raise ValidationError('Username already existed!')
    except KeyError as e:
        raise ValidationError('Invalid user info: missing ' + e.args[0])
    user = User()
    user.import_data(request.json)
    db.session.add(user)
    db.session.commit()

    return {}, 201, {'Location': user.get_url()}
Пример #2
0
def oauth_callback(provider):
    if provider not in app.config['OAUTH_CREDENTIALS']:
        response = jsonify({
            'status': 401,
            'error': 'provider not found',
            'message': '{0} is not supported'.format(provider)
        })
        response.status_code = 401

        return response

    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()

    if username is not None:
        user_data = {
            'username': username,
            'email': email,
            'photo_url': '',
            'password': os.urandom(24)
        }

        tmp_user = User.query.filter_by(username=user_data['username']).first()

        if tmp_user is None:
            new_user = User()
            new_user.import_data(user_data)
            db.session.add(new_user)
            db.session.commit()
            tmp_user = new_user

        return jsonify({'token': tmp_user.generate_auth_token()})

    response = jsonify({
        'status': 401,
        'error': 'access denied',
        'message': 'user does not authorized access'
    })
    response.status_code = 401

    return response