示例#1
0
    def WithMockOauth(self, issuer, private_key_file, kid):
        iat = int(datetime.datetime.now().timestamp())
        headers = {"kid": kid}
        payload = self.payload
        payload["iss"] = issuer
        payload["iat"] = iat
        payload["exp"] = iat + 3600

        try:
            token = jwt.encode(payload,
                               Reader.Read(private_key_file),
                               algorithm="RS256",
                               headers=headers)
            return token
        except Exception as ex:
            return "Error encoding token: {0}".format(ex)
示例#2
0
    def Code(logs,fp):
        mapped = []
        multi = False
        jpad = 4
        opad = 0
        tpl = "single"
    
        if not isinstance(logs,list): 
            logs = [logs]

        if len(logs) > 1:
            multi = True
            jpad = 8
            opad = 4
            tpl = "multiple"
    

        for log in logs:
            mapped.append(Generate.substitute_map(log,jpad))
        const_header = "None"
        const_cookies = "None"

        if multi: 
            if len(set(map(lambda x: x["@@HEADER"],mapped))) == 1:
                const_header = To.Code(logs[0].request.HeaderNoCookies())
                for m in mapped:
                   m["@@HEADER"] = "header"
                
            if len(set(map(lambda x: x["@@COOKIES"],mapped))) == 1:
                const_cookies =  To.Code(logs[0].request.cookies)
                for m in mapped:
                   m["@@COOKIES"] = "cookies"

        t = Reader.Read("{0}/{1}.pyt".format(os.path.dirname(__file__),tpl))
        req_objs = [Generate.gen_request_object(m,opad) for m in mapped]
        
        if multi: 
            t = Reader.Substitute(t,"@@HEADER",const_header)
            t = Reader.Substitute(t,"@@COOKIES",const_cookies)
            t = Reader.Substitute(t,"@@REQOBJS",",\n".join(req_objs))
        else: 
            t = Reader.Substitute(t,"@@REQOBJ",req_objs[0])
        
        Writer.Replace(fp,t)
        return "Python Script Written to: {0}".format(fp)
示例#3
0
    def Construct(payload, keyfile, kid, issuer, header, secret, signature):
        alg = header["alg"] if header and "alg" in header.keys() else False

        if secret:
            if not alg:
                return "Error: Header containing an alg must be present when supplying secret."
            else:
                try:
                    if alg in ("HS256", "HS512"):
                        alg_enc = hashlib.sha512 if alg == "HS512" else hashlib.sha256
                        return Jwty.SignAsHS(header, payload, secret.encode(),
                                             alg_enc)
                    header.pop("alg", None)
                    token = jwt.encode(payload,
                                       secret,
                                       algorithm=alg,
                                       headers=header)
                    return token
                except Exception as ex:
                    return "Error encoding token: {0}".format(ex)

        if signature:
            eh = Jwty.Uenc(header)
            ep = Jwty.Uenc(payload)
            return "{0}.{1}.{2}".format(eh.decode(), ep.decode(), signature)

        header = header if header else {}
        header["alg"] = "RS256"
        header["kid"] = kid
        payload["iss"] = issuer
        try:
            token = jwt.encode(payload,
                               Reader.Read(keyfile),
                               algorithm="RS256",
                               headers=header)
            return token
        except Exception as ex:
            return "Error encoding token: {0}".format(ex)