Example #1
0
def login():
    """
    Logs in user and returns access token
    :return:
    """
    request_json = request.get_json()

    email = request_json.get('email')
    password = request_json.get('password')

    if not all([email, password]):
        return responses.missing_params()

    user = User.query.filter_by(email=email).first()

    if user is None:
        return responses.user_not_found()

    if not verify_password(password, user.password):
        return responses.invalid_password()

    jwt_token = create_access_token(identity=user.id)
    refresh_token = create_refresh_token(identity=user.id)

    return responses.user_logged_in(jwt_token, refresh_token, user.id)
Example #2
0
def login_google():
    """
    Updates google_access_token for user
    :return: Response (object), status_code (int)
    """
    incoming = request.get_json() or {}

    required_attrs = ('code', )

    if not incoming or not all(attr in incoming for attr in required_attrs):
        return responses.missing_params()

    request_origin = request.environ.get('HTTP_ORIGIN')

    token_data, access_token_info = get_google_access_token(
        auth_code=incoming.get('code'), redirect_uri=request_origin)

    if not token_data or not access_token_info:
        return jsonify({'error':
                        'Google Auth failed...'}), status.UNPROCESSABLE_ENTITY

    id_token_info = get_google_token_info(token_data.get('id_token'),
                                          'id_token')
    oauth_email = id_token_info.get('email')

    user = User.query.filter_by(email=oauth_email).first()

    if not user:
        return responses.user_not_found()

    oauth_connection = OAuthConnection.query.filter_by(
        type=OAuthConnectionType.GOOGLE, email_address=oauth_email).first()

    has_google_connection = oauth_connection is not None

    if not has_google_connection:
        oauth_connection = OAuthConnection()
        user.oauth_connections.append(oauth_connection)

        db.session.add(oauth_connection)

    oauth_connection.type = OAuthConnectionType.GOOGLE
    oauth_connection.email_address = oauth_email
    oauth_connection.ext_user_id = id_token_info.get('sub')
    oauth_connection.ext_access_token = token_data.get('access_token')
    oauth_connection.ext_refresh_token = token_data.get('refresh_token')

    db.session.commit()

    jwt_token = create_access_token(identity=user.id)
    refresh_token = create_refresh_token(identity=user.id)

    user_info = dict(auth_type='google', **user.to_dict())

    return responses.user_logged_in(jwt_token,
                                    refresh_token,
                                    user.id,
                                    user_info=user_info)
Example #3
0
def get_public_profile(user_id):
    """
    Returns user's profile information based on user_id provided
    :return:
    """
    user = User.query.get(user_id)

    if not user:
        return responses.user_not_found()

    return jsonify(user.public_dict()), status.OK
Example #4
0
def get_my_profile():
    """
    Returns current user's profile information
    :return:
    """
    user_id = get_jwt_identity()
    user = User.query.get(user_id)

    if not user:
        return responses.user_not_found()

    return jsonify(user.to_dict()), status.OK
Example #5
0
def login_facebook():
    """
    Logs in user using Facebook OAuth
    Is user type agnostic
    :return:
    """
    request_json = request.get_json()

    if not request_json:
        return responses.missing_params()

    user_token = request_json.get('user_token')

    if not user_token:
        return responses.missing_params()

    # Get the access token again?
    access_token = fb.get_access_token(
        app_id=app.config.get('FACEBOOK_APP_ID'),
        app_secret=app.config.get('FACEBOOK_APP_SECRET')
    )

    token_info = fb.debug_user_token(user_token, access_token)

    if not token_info.get('is_valid'):
        return responses.invalid_fb_token()

    long_lived_token = fb.get_long_lived_token(
        app_id=app.config.get('FACEBOOK_APP_ID'),
        app_secret=app.config.get('FACEBOOK_APP_SECRET'),
        short_lived_token=user_token
    ).get('access_token')

    # get user id to look up in database?
    facebook_user_id = token_info.get('user_id')

    user = User.query.filter_by(facebook_user_id=facebook_user_id)

    if user is None:
        return responses.user_not_found()

    # check if current user token is the same if not change it
    if user.facebook_access_token != long_lived_token:
        user.facebook_access_token = long_lived_token

        db.session.commit()

    # Return app_name user access token for access to app_name api
    jwt_token = create_access_token(identity=user.id)
    refresh_token = create_refresh_token(identity=user.id)

    return responses.user_logged_in(jwt_token, refresh_token)
Example #6
0
def delete_profile():
    """
    Deletes requester's account
    :return:
    """
    user_id = get_jwt_identity()

    user = User.query.get(user_id)

    if not user:
        return responses.user_not_found()

    db.session.delete(user)
    db.session.commit()

    return responses.resource_deleted(User.__name__)
Example #7
0
def create_resource_a():
    """
    Creates a resource A
    :return:
    """
    user_id = get_jwt_identity()
    user = User.query.get(user_id)

    if not user:
        return responses.user_not_found()

    data = request.get_json()
    resource_a = ResourceA(**data)
    resource_a.owner_id = user_id

    user.resource_a_set.append(resource_a)

    db.session.add(resource_a)
    db.session.commit()

    return responses.resource_created(ResourceA.__name__)
Example #8
0
def update_profile():
    """
    Updates the user's profile based on attributes and values sent
    Can also change password
    :return:
    """
    valid_attrs = {'first_name', 'last_name', 'email', 'phone_number', 'new_password',
                   'old_password'}

    update_attrs = request.get_json()

    if not update_attrs:
        return responses.missing_params()

    if not all(attr in valid_attrs for attr in update_attrs):
        return responses.invalid_request_keys(set(update_attrs) - valid_attrs)

    user_id = get_jwt_identity()
    user = User.query.get(user_id)

    if not user:
        return responses.user_not_found()

    changed = False
    if 'new_password' in update_attrs and 'old_password' in update_attrs:
        old_password = update_attrs.pop('old_password')
        new_password = update_attrs.pop('new_password')

        changed = user.change_password(old_password, new_password)

        if not changed:
            return responses.invalid_password()

    for attr, value in update_attrs.items():
        setattr(user, attr, value)

    db.session.commit()

    return responses.user_updated(password_changed=changed)