def requestic1(key): key = int("{}".format(key)) data = json.loads(request.data) Jwt1 = request.headers['Authorization'] message = data.get("message") try: jwt_Obj = PyJWT() decode_token = jwt_Obj.decode(str(Jwt1), key=Key) if decode_token['role'] == "write": if key == None or message == None: return Response(status=400) else: cache = redis.Redis(host=HOST, port=6379) cache.ping() if cache.exists(key): cache.delete(key) cache.set(key, json.dumps(message)) return make_response("changed", 200) else: cache.set(key, json.dumps(message)) return make_response({key: message}, 201) else: return make_response("invalid1 tiket", 400) except Exception: return make_response("invalid2 tiket", 400)
def requestic4(): user = request.authorization.username password = request.authorization.password if d.get(user) != None and d[str(user)] == password: payload = {"role": str(user)} jwt_Obj = PyJWT() jwt_token = jwt_Obj.encode(payload=payload, key=Key) rez = make_response(str(jwt_token, 'UTF-8'), 200) rez.headers['Authorization'] = str(jwt_token, 'UTF-8') return rez else: return make_response( "invalid user or password" + str(user) + ' ' + str(password), 400)
def requestic2(key): key = int("{}".format(key)) Jwt1 = request.headers['Authorization'] try: jwt_Obj = PyJWT() decode_token = jwt_Obj.decode(str(Jwt1), key=Key) if decode_token['role'] == "read": cache = redis.Redis(host=HOST, port=6379) cache.ping() if cache.exists(key): res = json.loads(cache.get(key)) return make_response({"message": res}, 200) else: return Response(status=400) else: return make_response("invalid1 tiket", 400) except Exception: return make_response("invalid2 tiket", 400)
def post(self, request, *args, **kwargs): if not request.data: return Response({'Error': "Please provide username/password"}, status="400") email = request.data['email'] password = request.data['password'] print(email, password) user = models.UserProfile.objects.get(email=email) payload = {'email': user.email, 'password': user.password} key = 'secretmustbecomplex' jwt_Obj = PyJWT() jwt_token = {'token': jwt_Obj.encode(payload=payload, key=key)} return Response(jwt_token)
def authenticate(self, request): print('sadas') rawToken = get_authorization_header(request) print(request.data) print(rawToken) key = 'secretmustbecomplex' jwt_Obj = PyJWT() if not rawToken: return None payload = jwt_Obj.decode(rawToken, key=key) email = payload['email'] password = payload['password'] try: user = UserProfile.objects.get(email=email, password=password) except UserProfile.DoesNotExist: raise exceptions.AuthenticationFailed('No such user') return (user, None)
def _token_errors(encoded_token): try: decoded_payload, signing, header, signature = PyJWT()._load(encoded_token) except jwt.exceptions.DecodeError: return "Invalid token format" kid = header['kid'] issuer = "https://course-api-auth.herokuapp.com/" audience = issuer try: jwt.decode( encoded_token, secret_keys[kid], issuer=issuer, audience=audience, algorithms=['HS256'] ) except Exception as e: return str(e) return None
def jwt(): return PyJWT()
from jwt.api_jwt import PyJWT payload = {'id': 5, 'email': 'ASDASDA'} key = 'secret' jwt_Obj = PyJWT() jwt_token = jwt_Obj.encode(payload=payload, key=key) decode_token = jwt_Obj.decode(jwt_token, key=key) print(jwt_token) print(decode_token)