login.py 5.31 KB
Newer Older
taoke committed
1
import pytest,time
fanxun committed
2
import requests
fanxun committed
3

fanxun committed
4
from config import SSOConfig
fanxun committed
5 6 7
from common.utils.encryption import Encryption
# from common.utils.getExcelData import get_excelData
from common.tools import request_main
taoke committed
8
from common.db import RedisString
9
from config import BmyConfig, BmyomsConfig
dengmaosheng committed
10
from config import BMCConfig
fanxun committed
11

taoke committed
12

fanxun committed
13
class SSOLogin():
fanxun committed
14
    """SSO登录"""
fanxun committed
15 16
    def _sso_pwd_encrypted(self, org_pwd):
        """md5加密"""
fanxun committed
17
        encrypted_password = Encryption().get_md5(org_pwd, salt=SSOConfig.sso_salt)
fanxun committed
18 19
        return encrypted_password

fanxun committed
20
    def sso_login(self,url, method='post', headers=None):
fanxun committed
21
        """SSO登录获取token"""
fanxun committed
22
        encrypted_password = self._sso_pwd_encrypted(SSOConfig.sso_password)
fanxun committed
23
        req_data = {"loginName":SSOConfig.sso_username,"password":encrypted_password}
fanxun committed
24 25
        res = request_main(url, headers, method, req_data)
        return res['data']['token']
fanxun committed
26 27


taoke committed
28 29 30
class BMY():
    """斑马云登录相关"""
    # 获取当前时间的Authorization
31 32
    def get_authorization(self,defaultToken = BmyConfig.defaultToken):
        key = BmyConfig.key  # 原始密钥
taoke committed
33 34 35 36
        m5dkey = Encryption().get_md5(key)  # AES密钥
        t = time.time()
        num = str(round(t * 1000))
        return Encryption().aes_cipher(m5dkey, defaultToken + num)
fanxun committed
37

taoke committed
38 39 40 41 42
    """
    ①AES加密  -注 AES 秘钥进行MD5(原始密钥)
    ②MD5
    ③逆序
    """
taoke committed
43
    def pwd_encrypted(self,pwd):
44
        key = BmyConfig.key  # 原始密钥
taoke committed
45 46 47 48 49 50
        m5dkey = Encryption().get_md5(key)
        encrypted_text_str = Encryption().aes_cipher(m5dkey, pwd)  # ①
        newpwd = Encryption().get_md5(encrypted_text_str)  # ②
        return newpwd[::-1]  # ③

    # 从redis获取获取图形验证码x轴百分比
taoke committed
51
    def get_imageCode(self,username, pwd):
taoke committed
52 53
        payload = {"username": username, "password": pwd}
        try:
54
            rep = requests.get(f"{BmyConfig().test_host}/website/common/graph/login-captcha", params=payload)
taoke committed
55 56 57 58 59 60 61
            imageId = rep.json()['data']['jtId']
            result = RedisString(6).get(f'bmc:captcha:{imageId}')
            imageCode = str(result)[-3:-1]
            return imageId, imageCode
        except:
            return ("imageId", "imageCode")  # 返回错误的验证码

taoke committed
62

taoke committed
63
    def bmy_login(self,indata, getToken=True):
taoke committed
64 65 66
        """企业云登录"""
        # token加密
        authorization = BMY().get_authorization()
taoke committed
67
        header = {"Authorization": authorization,"Register-Origin":indata['Register-Origin']}
taoke committed
68
        payload = {"username": "","password": "", "imageId": "", "grant_type": "passwordImageCode", "imageCode": ""}
taoke committed
69 70
        # 账号
        payload['username'] = indata['username']
fanxun committed
71

taoke committed
72 73 74 75 76 77 78 79
        # 密码加密
        password_Encrypted = BMY().pwd_encrypted(indata['password'])
        payload['password'] = password_Encrypted

        # 获取图片信息
        imageinfo = BMY().get_imageCode(payload['username'], payload['password'])
        payload['imageId'] = imageinfo[0]
        payload['imageCode'] = imageinfo[1]
taoke committed
80
        # print(payload)
taoke committed
81

82
        resp = requests.post(f"{BmyConfig().test_host}/auth/login", data=payload, headers=header)
taoke committed
83 84 85 86 87
        if getToken:
            token = resp.json()['data']['token']  # 数据权限会藏在token中
            return BMY().get_authorization(defaultToken=token)
        else:
            return resp.json()
fanxun committed
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    def bmyoms_login(self, indata, getToken=True):
        """企业云登录"""
        # token加密
        authorization = BMY().get_authorization()
        header = {"Authorization": authorization}
        payload = {"username": "","password": "", "grant_type": "intranet"}
        # 账号
        payload['username'] = indata['username']

        # 密码加密
        password_Encrypted = BMY().pwd_encrypted(indata['password'])
        payload['password'] = password_Encrypted

        resp = requests.post(f"{BmyomsConfig().test_host}/auth/login", data=payload, headers=header)
        if getToken:
            token = resp.json()['data']['token']  # 数据权限会藏在token中
            return BMY().get_authorization(defaultToken=token)
        else:
            return resp.json()

dengmaosheng committed
109 110
class BMC():
    def bmc_login(self, indata):
dengmaosheng committed
111 112 113 114 115 116 117 118 119 120 121
        """斑马信用登录获取token"""
        url = "http://testbmcapp.hikcreate.com/v1/user/login/gesture"
        header = {"Content-Type": "application/json; charset=utf-8",
                  "device-type": "Android",
                  "device-name": "vivo+X20",
                  "device-model": "vivo vivo X20",
                  "city-code": "520100",
                  "Version": "2.2.0",
                  "Device-Code": "000000001e167ed7000000001e167ed7"}

        res = requests.post(url, json=indata, headers=header)
122
        # print(res.json())
dengmaosheng committed
123
        encrypted_token = Encryption().aes_token(res.json()["data"]["token"])
124
        # print(encrypted_token)
dengmaosheng committed
125 126 127 128
        # 获取专网token
        header1 = header.copy()
        header1["Token"] = encrypted_token
        resp = requests.get("http://testbmcapp.hikcreate.com/token", headers=header1)
129
        # print(resp.json())
dengmaosheng committed
130
        pvt_token = resp.json()["data"]["token"]
131
        # print(pvt_token)
taoke committed
132
        return encrypted_token, pvt_token
dengmaosheng committed
133 134


fanxun committed
135
if __name__ == '__main__':
136 137
    indata = {"username":"888888","password":"bmy123456"}
    token = BMY().bmyoms_login(indata)
taoke committed
138 139 140
    print(token)
    # LIST1=BMY().get_imageCode( '15150000000',  '8e4b595babec901009ff84f269ee5147')
    # print(LIST1)