class TokenHelper: def __init__(self): self.data = jwk_from_dict(json.loads(load_key_data("oct.json", "r"))) self.octet_obj = OctetJWK(self.data.key, self.data.kid) def get_JWK(self): return self.octet_obj def get_data(self): return self.data def verify(self, token): self.octet_obj.verify()
def __init__( self, jwt_secret: bytes, duration_amount: int, duration_units: str, ): self._jwt_secret = OctetJWK(jwt_secret) self._duration_amount = duration_amount if duration_units not in {"hours", "minutes", "seconds"}: error_message: str = f"Invalid time unit string: {duration_units}" log.error(error_message) raise ValueError(error_message) self._duration_units = duration_units
def __init__(self, jwt_secret: bytes): self._jwt_secret = OctetJWK(jwt_secret)
from django.http import HttpResponse from django.template import loader from django.shortcuts import redirect from jwt import JWT from jwt.exceptions import JWTDecodeError from jwt.jwk import OctetJWK jwt = JWT() KEY = OctetJWK(b'MY secret') def login(request): if request.method == "POST": username = request.POST.get('username') password = request.POST.get('password') if username == "admin" and password == "toor": encoded = jwt.encode({'username': "******", "ip": request.META.get('HTTP_X_REAL_IP')}, KEY, 'HS256') response = redirect('/admin') response.set_cookie('auth', encoded) # response.cookies['auth']['httponly'] = True return response template = loader.get_template('login.html') response = HttpResponse(template.render({}, request)) return response def admin(request): try:
def test_from_dict_missing_k(self): key_json = self.key_json.copy() del key_json['k'] with raises(MalformedJWKError): OctetJWK.from_dict(key_json)
def setUp(self): self.key_json = json.loads(load_testdata('oct.json', 'r')) self.inst = OctetJWK.from_dict(self.key_json)
def __init__(self): self.data = jwk_from_dict(json.loads(load_key_data("oct.json", "r"))) self.octet_obj = OctetJWK(self.data.key, self.data.kid)