def get_session():
    ctx = ApiCtx()
    token = webserver.SesTokInfo()
    ctx.session_id = token['response']['SesInfo'].split("=")[1]
    ctx.token = token['response']['TokInfo']
    lgn = user.login(ctx, USER, PASSWORD)
    pprint(lgn)
    return ctx
def quick_login(username: str, password: str, modem_host: str = None):
    ctx = ApiCtx(modem_host=modem_host)
    token = huaweisms.api.webserver.get_session_token_info(ctx.api_base_url)
    ctx.session_id = token['response']['SesInfo'].split("=")[1]
    ctx.login_token = token['response']['TokInfo']
    response = login(ctx, username, password)
    if not ctx.logged_in:
        raise ValueError(json.dumps(response))
    return ctx
Example #3
0
def quick_login(username, password, modem_host=None):
    # type: (str, str, str) -> ...
    ctx = ApiCtx(modem_host=modem_host)
    token = huaweisms.api.webserver.get_session_token_info(ctx.api_base_url)
    session_token = token['response']['SesInfo'].split("=")
    ctx.session_id = session_token[1] if len(session_token) > 1 else session_token[0]
    ctx.login_token = token['response']['TokInfo']
    response = login(ctx, username, password)
    if not ctx.logged_in:
        raise ValueError(json.dumps(response))
    return ctx
Example #4
0
def quick_login(username, password, modem_host=None, uri_scheme="http", verify=True):
    # type: (str, str, str, str, bool) -> ...
    ctx = ApiCtx(modem_host=modem_host, uri_scheme=uri_scheme, verify=verify)
    token = huaweisms.api.webserver.get_session_token_info(ctx)
    session_token = token["response"]["SesInfo"].split("=")
    ctx.session_id = session_token[1] if len(session_token) > 1 else session_token[0]
    ctx.login_token = token["response"]["TokInfo"]
    response = login(ctx, username, password)
    if not ctx.logged_in:
        raise ValueError(json.dumps(response))
    return ctx
def login(ctx: ApiCtx, user_name: str, password: str):
    headers = common_headers()
    url = "{}/user/login".format(ctx.api_base_url)

    password_value = b64_sha256(user_name + b64_sha256(password) +
                                ctx.login_token)

    xml_data = """
    <?xml version:"1.0" encoding="UTF-8"?>
    <request>
        <Username>{}</Username>
        <Password>{}</Password>
        <password_type>4</password_type>
    </request>
    """.format(user_name, password_value)

    #   setup headers
    headers['__RequestVerificationToken'] = ctx.login_token
    headers['X-Requested-With'] = 'XMLHttpRequest'

    r = post_to_url(url, xml_data, ctx, headers)

    if r['type'] == "response" and r['response'] == "OK":
        ctx.logged_in = True

    return r
def get_session_token_info(base_url=None):
    # type: (str) -> ...
    """
    Get session token information

    :param base_url: base url for the modem api
    :return:
    """
    if base_url is None:
        logger.warning(
            'calling %s.get_session_token_info without base_url argument is deprecated',
            __name__)
        base_url = 'http://{}/api'.format(MODEM_HOST)

    url = "{}/webserver/SesTokInfo".format(base_url)
    return get_from_url(url, ctx=ApiCtx(modem_host=MODEM_HOST), timeout=30)
def login(ctx: ApiCtx, user_name: str, password: str):
    headers = common_headers()
    url = "{}/user/login".format(API_URL)

    #   original JS code:
    #   psd = base64encode(
    #    SHA256(
    #        name +
    #        base64encode(
    #                       SHA256($('#password').val())
    #        ) +
    #        g_requestVerificationToken[0]
    #    )
    #   );

    password_value = b64_sha256(user_name + b64_sha256(password) + ctx.token)

    xml_data = """
    <?xml version:"1.0" encoding="UTF-8"?>
    <request>
        <Username>{}</Username>
        <Password>{}</Password>
        <password_type>4</password_type>
    </request>
    """.format(user_name, password_value)

    #   setup headers
    headers['__RequestVerificationToken'] = ctx.token
    headers['X-Requested-With'] = 'XMLHttpRequest'

    r = post_to_url(url, xml_data, ctx, headers)

    if r['type'] == "response" and r['response'] == "OK":
        ctx.logged_in = True

    return r