Ejemplo n.º 1
0
def main():
    """Obtain a mac address based on IPv4 address."""
    token = get_token()
    if test:
        print(token)

    headers = dict(
        accept='application/json', Authorization=token
    )
    if test:
        print(headers)
    url = '{0}{1}/ip2mac/{2}'.format(
        API_EARL, IP4LOGS_ENDPOINT, ip
    )
    if test:
        print(url)
    resp = requests.get(url=url, headers=headers, verify=False)
    data = resp.json()
    if test:
        print(data)

    logger.info("IP: {0}".format(ip))
    logger.info("MAC: {0}".format(data['mac']))
    for count, item in enumerate(data):
        print(count,item,data[item])
Ejemplo n.º 2
0
async def login(request: Request) -> json_response:
    try:
        user = await request.json()
        if 8 > len(user['email']) > 20:
            return failure_response(400, 'Invalid email length')
        if 8 > len(user['password']) > 20:
            return failure_response(400, 'Invalid password length')
        pool = request.app['pool']
        async with pool.acquire() as conn:
            users = await conn.fetch(
                'SELECT * FROM users WHERE email=\'{}\''.format(user['email']))
            if len(users) == 0:
                return failure_response(401, 'Invalid email or password')
            else:
                if users[0]['password'] != encode(
                        str(user['password']).encode('utf-8')):
                    return failure_response(401, 'Invalid email or password')
                else:
                    token = get_token({
                        'user': user_record_to_json(users[0]),
                        'key': users[0]['key']
                    })
                    logger.info('User {} login at site'.format(user['email']))
                    return success_response(200, 'ok', token=token)
    except Exception as e:
        return server_error_response(e)
Ejemplo n.º 3
0
async def update_user(request: Request) -> json_response:
    try:
        data = await request.json()
        if data['key'] is None or len(data['key']) < 10:
            return failure_response(401, 'Authorize please')
        if len(data['update'].items()) == 0:
            return failure_response(400, 'Nothing to update')
        pool = request.app['pool']
        async with pool.acquire() as conn:
            async with conn.transaction():
                users = await conn.fetch(
                    'SELECT * FROM users WHERE key=\'{}\''.format(data['key']))
                if len(users) == 0:
                    return failure_response(401, 'Authorize please')
                else:
                    u = user_record_to_json(users[0])
                    u.update(data['update'])
                    await conn.fetch(
                        '''UPDATE users SET name=\'{}\',email=\'{}\',country=\'{}\',age={}
                                        WHERE key=\'{}\';'''.format(
                            u['name'], u['email'], u['country'], u['age'],
                            data['key']))
                token = get_token({'user': u, 'key': data['key']})
                return success_response(200, 'ok', token=token)
    except Exception as e:
        return server_error_response(e)
def main():
    """Display the number of nodes."""
    token = get_token()
    print(token)
    headers = dict(accept='application/json', Authorization=token)
    url = '{}{}/active'.format(API_EARL, REPORTS_ENDPOINT)
    resp = requests.get(url=url, headers=headers, verify=False)
    data = resp.json()
    for c, item in enumerate(data['items']):
        print(c, item)
    print(len(data['items']))
Ejemplo n.º 5
0
async def get_token_by_key(request: Request) -> json_response:
    try:
        data = await request.json()
        if data['key'] is None or len(data['key']) < 10:
            return failure_response(401, 'Authorize please')
        pool = request.app['pool']
        async with pool.acquire() as conn:
            users = await conn.fetch(
                'SELECT * FROM users WHERE key=\'{}\''.format(data['key']))
            if len(users) == 0:
                return failure_response(401, 'Authorize please')
            else:
                token = get_token({
                    'user': user_record_to_json(users[0]),
                    'key': users[0]['key']
                })
                return success_response(200, 'ok', token=token)
    except Exception as e:
        return server_error_response(e)
Ejemplo n.º 6
0
async def register(request: Request) -> json_response:
    try:
        user = await request.json()
        if 8 > len(user['name']) > 20:
            return failure_response(400, 'Invalid name length')
        if 8 > len(user['email']) > 20:
            return failure_response(400, 'Invalid email length')
        if 8 > len(user['password']) > 20:
            return failure_response(400, 'Invalid email length')
        if 3 > len(user['country']) > 15:
            return failure_response(400, 'Invalid country')
        if user['age'] is None or 6 > int(user['age']) > 65:
            return failure_response(400, 'Invalid age')
        pool = request.app['pool']
        async with pool.acquire() as conn:
            async with conn.transaction():
                usr = await conn.fetch(
                    'SELECT * FROM users WHERE email=\'{}\''.format(
                        user['email']))
                if len(usr) == 0:
                    password_hash = encode(
                        str(user['password']).encode('utf-8'))
                    u = User(name=user['name'],
                             email=user['email'],
                             password=password_hash,
                             age=user['age'],
                             country=user['country'])
                    await conn.fetch(
                        '''INSERT INTO users (name,email,password,age,country,balance,key)
                                        VALUES (\'{}\', \'{}\', \'{}\', {}, \'{}\', {}, \'{}\')'''
                        .format(u.name, u.email, u.password, u.age, u.country,
                                u.balance, u.key))
                    token = get_token({'user': u.to_json(), 'key': u.key})
                    logger.info(
                        'Registrate new User(name={}, email={}, age={}, country={})'
                        .format(u.name, u.email, u.age, u.country))
                    return success_response(200, 'ok', token=token)
                else:
                    return failure_response(200, 'This login is already taken')
    except Exception as e:
        return server_error_response(e)