def _setup(alg, priv_type, pub_type, exp, iat_skew, nbf, jti_size, keyless, expected):
privk = None if keyless else priv_keys[alg][priv_type]<EOL>pubk = None if keyless else pub_keys[alg][pub_type]<EOL>jtis = {}<EOL>@Vows.batch<EOL>class GenerateJWT(Vows.Context):<EOL><INDENT>"""<STR_LIT>"""<EOL>def topic(self):<EOL><INDENT>"""<STR_LIT>"""<EOL>lt = timedelta(seconds=exp)<EOL>now = datetime.utcnow()<EOL>not_before = (now + timedelta(minutes=nbf)) if nbf else None<EOL>if callable(privk):<EOL><INDENT>token = privk(payload, alg, lt, not_before=not_before)<EOL><DEDENT>else:<EOL><INDENT>token = jwt.generate_jwt(payload, privk, alg, lt,<EOL>not_before=not_before,<EOL>jti_size=jti_size)<EOL><DEDENT>yield clock_tick(timedelta(milliseconds=<NUM_LIT>)), token<EOL>now = datetime.utcnow()<EOL>not_before = (now + timedelta(minutes=nbf)) if nbf else None<EOL>if callable(privk):<EOL><INDENT>token = privk(payload, alg,<EOL>expires=(now + lt),<EOL>not_before=not_before)<EOL><DEDENT>else:<EOL><INDENT>token = jwt.generate_jwt(payload, privk, alg,<EOL>expires=(now + lt),<EOL>not_before=not_before,<EOL>jti_size=jti_size)<EOL><DEDENT>yield clock_tick(timedelta(milliseconds=<NUM_LIT>)), token<EOL><DEDENT>class ProcessJWT(Vows.Context):<EOL><INDENT>"""<STR_LIT>"""<EOL>def topic(self, topic):<EOL><INDENT>"""<STR_LIT>"""<EOL>_, sjwt = topic<EOL>return jwt.process_jwt(sjwt)<EOL><DEDENT>class CheckClaims(Vows.Context):<EOL><INDENT>"""<STR_LIT>"""<EOL>def topic(self, token):<EOL><INDENT>"""<STR_LIT>"""<EOL>_, claims = token<EOL>return claims<EOL><DEDENT>def payload_keys_should_be_as_expected(self, claims):<EOL><INDENT>"""<STR_LIT>"""<EOL>expect(list(claims.keys())).to_be_like(keys if jti_size or callable(privk) else [key for key in keys if key != '<STR_LIT>'])<EOL><DEDENT>def payload_values_should_match(self, claims):<EOL><INDENT>"""<STR_LIT>"""<EOL>for x in payload:<EOL><INDENT>expect(claims[x]).to_equal(payload[x])<EOL><DEDENT><DEDENT>def jtis_should_be_unique(self, claims):<EOL><INDENT>"""<STR_LIT>"""<EOL>if jti_size or callable(privk):<EOL><INDENT>expect(is_string(claims['<STR_LIT>'])).to_be_true()<EOL>expect(jtis).Not.to_include(claims['<STR_LIT>'])<EOL>jtis[claims['<STR_LIT>']] = True<EOL><DEDENT><DEDENT>def jti_size_should_be_as_expected(self, claims):<EOL><INDENT>"""<STR_LIT>"""<EOL>if jti_size and not callable(privk): <EOL><INDENT>expect(len(base64url_decode(claims['<STR_LIT>']))).to_equal(jti_size)<EOL><DEDENT><DEDENT><DEDENT>def header_should_be_as_expected(self, token):<EOL><INDENT>"""<STR_LIT>"""<EOL>header, _ = token<EOL>expect(header).to_equal({<EOL>'<STR_LIT>': '<STR_LIT:none>' if keyless else alg,<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>})<EOL><DEDENT><DEDENT>class VerifyJWTWithGeneratedKey(Vows.Context):<EOL><INDENT>"""<STR_LIT>"""<EOL>@Vows.capture_error<EOL>def topic(self, topic):<EOL><INDENT>"""<STR_LIT>"""<EOL>clock, sjwt = topic<EOL>clock_load(clock)<EOL>pubk = None if keyless else generated_keys[alg]<EOL>return jwt.verify_jwt(sjwt, pubk, ['<STR_LIT:none>'] if keyless else [alg],<EOL>timedelta(seconds=iat_skew))<EOL><DEDENT>def should_fail_to_verify(self, r):<EOL><INDENT>"""<STR_LIT>"""<EOL>if keyless and expected:<EOL><INDENT>expect(r).to_be_instance_of(tuple)<EOL><DEDENT>else:<EOL><INDENT>expect(r).to_be_an_error()<EOL><DEDENT><DEDENT><DEDENT>class VerifyJWT(Vows.Context):<EOL><INDENT>"""<STR_LIT>"""<EOL>@Vows.capture_error<EOL>def topic(self, topic):<EOL><INDENT>"""<STR_LIT>"""<EOL>clock, sjwt = topic<EOL>clock_load(clock)<EOL>if callable(pubk):<EOL><INDENT>return pubk(sjwt, timedelta(seconds=iat_skew))<EOL><DEDENT>return jwt.verify_jwt(sjwt, pubk, ['<STR_LIT:none>'] if keyless else [alg],<EOL>timedelta(seconds=iat_skew))<EOL><DEDENT>def should_verify_as_expected(self, r):<EOL><INDENT>"""<STR_LIT>"""<EOL>if expected:<EOL><INDENT>try:<EOL><INDENT>expect(r).to_be_instance_of(tuple)<EOL><DEDENT>except:<EOL><INDENT>print(alg, priv_type, pub_type, exp, iat_skew, nbf, keyless, expected)<EOL>raise<EOL><DEDENT><DEDENT>else:<EOL><INDENT>expect(r).to_be_an_error()<EOL><DEDENT><DEDENT><DEDENT><DEDENT>
setup tests