def github_logout(request): """ Endpoint to get the logout of the authorized github account( deletes the stored token from the database) Inputs: ------- request: Requires: username in request data Returns: -------- response: JsonResponse describing success or failure """ username = request.data.get("username") try: user = User(username=username, password=None) this_user = user.find() if this_user.get("GitAccessToken"): user.update("GitAccessToken", None) status, success, message = 200, True, "Logged out" except Exception as e: print(e) status, success, message = 500, False, "Something went wrong" return JsonResponse({"success": success, "message": message}, status=status)
def authorize_github(request): """ Endpoint to authorize the github account of the user Generates a new access token, encrypts it and stores it in the database Inputs: ------- request: Requires: username, code(to generate new access token) in request data Returns: -------- response: JsonResponse describing success or failure """ code = request.data.get("code") username = request.data.get("username") user = User(username=username, password=None) try: git_access_token = generate_git_access_token(code) assert git_access_token is not None encrypted_git_access_token = encrypt(git_access_token) assert encrypted_git_access_token is not None user.update("GitAccessToken", encrypted_git_access_token) status, success, message = ( 200, True, "Successfully authorized", ) except Exception as e: print(e) status, success, message = ( 500, False, "Something went wrong", ) return JsonResponse( {"success": success, "message": message}, status=status, )