def create_user(user=None):  # noqa: E501
    """Create user

    This can only be done by the logged in user. # noqa: E501

    :param user: Created user object
    :type user: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        user = User.from_dict(connexion.request.get_json())  # noqa: E501

    # TODO: implement more intelligent checks for valid email addresses
    #       and secure passwords
    if not user.username or not user.password or not user.email:
        return ("Invalid username/password/email", 400)

    db_user = DB_User(username=user.username,
                      email=user.email,
                      password=sha256_crypt.encrypt(user.password))

    db.session.add(db_user)

    try:
        db.session.commit()
    except exc.SQLAlchemyError as e:
        print("Failed to insert user into database: ", e)
        return ("Cannot create user", 400)

    db_client = DB_OAuth2Client(
        client_name='default',
        client_uri='/',
        # TODO: get all scopes from openapi.yaml
        scope='decode:projects read:projects train:projects write:projects read:resources read:audio write:resources read:user write:user write:audio',
        redirect_uri='/',
        grant_type='password',
        response_type='code',
        token_endpoint_auth_method='client_secret_basic')
    db_client.user_id = db_user.id
    db_client.client_id = gen_salt(24)
    db_client.client_secret = gen_salt(48)

    db.session.add(db_client)
    db.session.commit()

    return (User(username=db_user.username, email=db_user.email), 201)
def create_user (body):
    global user_last_id
    global user_data
    if connexion.request.is_json:
        body = User.from_dict (connexion.request.get_json ())
    body.id = user_last_id
    user_last_id += 1
    user_data [body.id] = body
    return None, HTTPStatus.CREATED.value
def update_user (body, user_id):
    global user_data
    if connexion.request.is_json:
        body = User.from_dict (connexion.request.get_json ())
    body.id = user_id
    if body.id in user_data:
        user_data [body.id] = body
        return None, HTTPStatus.RESET_CONTENT.value
    else:
        return None, HTTPStatus.NOT_FOUND.value
Ejemplo n.º 4
0
async def create_user(request: web.Request, body) -> web.Response:
    """Create user

    This can only be done by the logged in user.

    :param body: Created user object
    :type body: dict | bytes

    """
    body = User.from_dict(body)
    return web.Response(status=200)
Ejemplo n.º 5
0
async def create_users_with_list_input(request: web.Request,
                                       body) -> web.Response:
    """Creates list of users with given input array

    

    :param body: List of user object
    :type body: list | bytes

    """
    body = [User.from_dict(d) for d in body]
    return web.Response(status=200)
    def test_create_user(self):
        """Test case for create_user

        Create user
        """
        user = User()
        response = self.client.open('/v2/user',
                                    method='POST',
                                    data=json.dumps(user),
                                    content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
Ejemplo n.º 7
0
def create_user(user=None):  # noqa: E501
    """create_user

    Creates a new user # noqa: E501

    :param user: The user to create
    :type user: dict | bytes

    :rtype: User
    """
    if connexion.request.is_json:
        user = User.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'
def create_user(body):  # noqa: E501
    """Create user

    This can only be done by the logged in user. # noqa: E501

    :param body: Created user object
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = User.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'
Ejemplo n.º 9
0
def create_user(user):  # noqa: E501
    """Create user

    This can only be done by the logged in user. # noqa: E501

    :param user: Created user object
    :type user: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        user = User.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'
    def test_update_user(self):
        """Test case for update_user

        Updated user
        """
        user = User()
        response = self.client.open(
            '/v2/user/{username}'.format(username='******'),
            method='PUT',
            data=json.dumps(user),
            content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
Ejemplo n.º 11
0
def create_users_with_list_input(user):  # noqa: E501
    """Creates list of users with given input array

     # noqa: E501

    :param user: List of user object
    :type user: list | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        user = [User.from_dict(d) for d in connexion.request.get_json()]  # noqa: E501
    return 'do some magic!'
Ejemplo n.º 12
0
async def update_user(request: web.Request, username, body) -> web.Response:
    """Updated user

    This can only be done by the logged in user.

    :param username: name that need to be deleted
    :type username: str
    :param body: Updated user object
    :type body: dict | bytes

    """
    body = User.from_dict(body)
    return web.Response(status=200)
def create_users_with_list_input(body):  # noqa: E501
    """Creates list of users with given input array

     # noqa: E501

    :param body: List of user object
    :type body: list | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = [User.from_dict(d)
                for d in connexion.request.get_json()]  # noqa: E501
    return 'do some magic!'
def get_user():  # noqa: E501
    """Get current user

    Provides info about the logged in user. # noqa: E501


    :rtype: User
    """
    current_user = connexion.context['token_info']['user']

    if not current_user:
        return ("User not authorized", 403)

    return User(username=current_user.username, email=current_user.email)
def get_user(user_id):  # noqa: E501
    """get_user

    Gets an user # noqa: E501

    :param user_id: ID of a user
    :type user_id: str

    :rtype: User
    """
    user = User(name=user_id,
                display_name='Example User',
                email='*****@*****.**')
    return user
def update_user(username, body):  # noqa: E501
    """Updated user

    This can only be done by the logged in user. # noqa: E501

    :param username: name that need to be updated
    :type username: str
    :param body: Updated user object
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = User.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'
Ejemplo n.º 17
0
def update_user(username, user):  # noqa: E501
    """Updated user

    This can only be done by the logged in user. # noqa: E501

    :param username: name that need to be deleted
    :type username: str
    :param user: Updated user object
    :type user: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        user = User.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'
Ejemplo n.º 18
0
def update_user(user_id, user=None):  # noqa: E501
    """update_user

    Updates a user # noqa: E501

    :param user_id: ID of user
    :type user_id: str
    :param user: The user to update
    :type user: dict | bytes

    :rtype: User
    """
    if connexion.request.is_json:
        user = User.from_dict(connexion.request.get_json())  # noqa: E501
    return 'do some magic!'
def user_login_post():  # noqa: E501
    """Logs user into the system

     # noqa: E501

    :param username: The user name for login
    :type username: str
    :param password: The password for login in clear text
    :type password: str

    :rtype: str
    """
    if connexion.request.is_json:
        user = User.from_dict(connexion.request.get_json())  # noqa: E501

    response = auth_with_password(user.username, user.password)
    print(response)
    if not response.ok:
        return "Invalid User or Password", 401, {}
    return json.loads(response.text)
def create_users_with_array_input():  # noqa: E501
    """Creates list of users with given input array

     # noqa: E501

    :param body: List of user object
    :type body: list | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = [User.from_dict(d)
                for d in connexion.request.get_json()]  # noqa: E501

        for user in body:
            id = user.id
            id_user = petstore.User.query.filter_by(id=id).first()
            if id is None:
                return 'User ID is Required', 404
            if id_user:
                return 'User Already Exists', 409
            username = user.username
            user_n = petstore.User.query.filter_by(username=username).first()
            if username is None:
                return 'User Name is Required', 404
            if user_n:
                return "User Already Exists", 409
            firstname = user.first_name
            lastname = user.last_name
            email = user.email
            password = generate_password_hash(user.password)
            phone = user.phone
            user_status = user.user_status

            new_user = petstore.User(id, username, firstname, lastname, email,
                                     password, phone, user_status)
            petstore.db.session.add(new_user)
            petstore.db.session.commit()

        return body
def create_user():  # noqa: E501
    """Create user

    This can only be done by the logged in user. # noqa: E501

    :param body: Created user object
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = User.from_dict(connexion.request.get_json())  # noqa: E501
        id = body.id
        id_user = petstore.User.query.filter_by(id=id).first()
        if id is None:
            return 'User ID is Required', 404
        if id_user:
            return 'User Already Exists', 409
        username = body.username
        user = petstore.User.query.filter_by(username=username).first()
        if username is None:
            return 'User Name is Required', 404
        if user:
            return 'User Name is Already Exists', 409
        firstname = body.first_name
        lastname = body.last_name
        email = body.email
        password = generate_password_hash(body.password, method='sha256')
        phone = body.phone
        user_status = body.user_status

        new_user = petstore.User(id, username, firstname, lastname, email,
                                 password, phone, user_status)
        petstore.db.session.add(new_user)
        petstore.db.session.commit()

        return body
Ejemplo n.º 22
0
def user_login_post():  # noqa: E501
    """Logs user into the system

     # noqa: E501

    :param username: The user name for login
    :type username: str
    :param password: The password for login in clear text
    :type password: str

    :rtype: str
    """
    if connexion.request.is_json:
        user = User.from_dict(connexion.request.get_json())  # noqa: E501
    if not auth_with_password(user.username, user.password):
        return "Invalid User or Password", 401, {}

    timestamp = _current_timestamp()
    payload = {
        "iss": JWT_ISSUER,
        "iat": int(timestamp),
        "exp": int(timestamp + JWT_LIFETIME_SECONDS),
        "sub": str(user.username),
    }

    access_token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
    return {
        "access_token": access_token,
        "token_type": "bearer",
        "expires_in": JWT_LIFETIME_SECONDS,
        "refresh_token": "IwOGYzYTlmM2YxOTQ5MGE3YmNmMDFkNTVk",
        "scope": "create"
    }, 200, {
        'X-Expires-After': JWT_LIFETIME_SECONDS,
        'X-Rate-Limit': 1000
    }
Ejemplo n.º 23
0
def db_user_to_front(db_user):
    return User(username=db_user.username, email=db_user.email)