Esempio n. 1
0
    def post(self):

        if request.remote_addr != '127.0.0.1':
            return {
                'message': "Your are'nt authorized to access this route"
            }, 401

        data = parser.parse_args()

        if UserModel.find_by_name(data['name']):
            return {'message': 'User {} already exists'.format(data['name'])}

        new_user = UserModel(name=data['name'],
                             password=UserModel.generate_hash(
                                 data['password']))

        try:
            new_user.save_to_db()
            access_token = create_access_token(
                identity=data['name'],
                expires_delta=datetime.timedelta(hours=1))
            refresh_token = create_refresh_token(identity=data['name'])
            return {
                'message': 'User {} was created'.format(data['name']),
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        except:
            return {'message': 'Something went wrong'}, 500
Esempio n. 2
0
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('name',
                            help='This field cannot be blank',
                            required=True)
        data = parser.parse_args()
        current_user = UserModel.find_by_name(data['name'])

        if not current_user:
            return {
                'message': 'User {} doesn\'t exist'.format(data['name'])
            }, 400

        user_part = UserModel.to_json(current_user)
        user_part['roles'] = list()
        user_part['keys'] = list()
        user_part['groups'] = list()

        useruserroles = UserUserRolesModel.by_user_id(current_user.id)
        for uur in useruserroles:
            role = UserRolesModel.by_id(uur.user_role_id)
            user_part['roles'].append(UserRolesModel.to_json(role))

        userkeys = UserKeysModel.by_user_id(current_user.id)
        for uk in userkeys:
            user_part['keys'].append(UserKeysModel.to_json(uk))

        usergroups = UserUserGroupsModel.by_user_id(current_user.id)
        for ug in usergroups:
            group = UserGroupsModel.by_id(ug.user_group_id)
            if group:
                user_part['groups'].append(UserGroupsModel.to_json(group))

        return user_part
Esempio n. 3
0
def exists(user):
    if UserModel.find_by_name(user):
        print('User {} already exists'.format(user))
        sys.exit(0)
    else:
        print("User don't exsist")
        sys.exit(1)
Esempio n. 4
0
    def post(self):
        data = parser.parse_args()
        current_user = UserModel.find_by_name(data['name'])

        if not current_user:
            return {
                'message': 'User {} doesn\'t exist'.format(data['name'])
            }, 400

        if UserModel.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(
                identity=data['name'],
                expires_delta=datetime.timedelta(hours=1))
            refresh_token = create_refresh_token(identity=data['name'])
            return {
                'message': 'Logged in as {}'.format(current_user.name),
                'user': current_user.name,
                'id': current_user.id,
                'email': current_user.email,
                'access_token': access_token,
                'refresh_token': refresh_token
            }, 200, {
                'jwt-token': access_token
            }
        else:
            return {'message': 'Wrong credentials'}, 401
Esempio n. 5
0
def register(user, password):
    userobj = UserModel.find_by_name(user)

    if not userobj:
        return 'User {} do not exists, create it via sshportal command line'.format(
            user)

    password = getpass('Please enter your password: '******'User {} updated'.format(user)
    except:
        return 'Something went wrong'