def is_user_login_ok(request): print("starting login") client = FusionAuthClient(settings.FUSION_AUTH_API_KEY, settings.FUSION_AUTH_BASE_URL) code = request.GET.get("code") if not code: print("no code") return False try: redirect_url = request.build_absolute_uri(reverse("dashboard")) r = client.exchange_o_auth_code_for_access_token_using_pkce( code, redirect_url, request.session['pkce_verifier'], settings.CLIENT_ID, settings.FUSION_AUTH_CLIENT_SECRET, ) if r.was_successful(): print("all good") access_token = r.success_response["access_token"] user_id = r.success_response["userId"] get_or_create_user(user_id, request) return user_id else: print("could not exchange code for token") print(r.error_response) return False except Exception as e: print("Something went wrong") print(e)
def get(self, request): user_id = is_user_login_ok(request) if not user_id: login_url = get_login_url(request) return redirect(login_url) user = None birthday = None try: client = FusionAuthClient(settings.FUSION_AUTH_API_KEY, settings.FUSION_AUTH_BASE_URL) print(user_id) r = client.retrieve_user(user_id) print(request.user.username) if r.was_successful(): birthday = r.success_response["user"].get( "birthDate", 'unknown') print(birthday) else: print("coudln't get user") print(r.error_response) print("render dashboard with ", user_id) return render(request, "secretbirthdaysapp/dashboard.html", { "birthday": birthday, "user_id": user_id }) except Exception as e: print("couldn't get user") print(e) return redirect(login_url)
def setUp(self): fusionauth_url = os.getenv( 'FUSIONAUTH_URL' ) if 'FUSIONAUTH_URL' in os.environ else 'http://localhost:9011' fusionauth_api_key = os.getenv( 'FUSIONAUTH_API_KEY' ) if 'FUSIONAUTH_API_KEY' in os.environ else 'bf69486b-4733-4470-a592-f1bfce7af580' self.client = FusionAuthClient(fusionauth_api_key, fusionauth_url)
def do_delete(): if session.get('user') != None: user = session['user'] else: user = None if user is None: return redirect(app.config['JODAL_URL']) # first delete associated user data delete_user_data(user['sub']) # Delete User For A Given ID client = FusionAuthClient(app.config['API_KEY'], app.config['FA_INTERNAL_URL']) client_response = client.delete_user(user['sub']) if client_response.was_successful(): session.clear() return redirect(app.config['FA_URL'] + '/oauth2/logout?client_id=' + app.config['CLIENT_ID']) else: return redirect(app.config['JODAL_URL'])
class FusionAuthClientTest(unittest2.TestCase): def setUp(self): self.client = FusionAuthClient('bf69486b-4733-4470-a592-f1bfce7af580', 'http://*****:*****@vandaleyindustries.com') if get_user_response.status is 200: delete_user_response = self.client.delete_user( get_user_response.success_response['user']['id']) self.assertEqual(delete_user_response.status, 200, delete_user_response.error_response) else: self.assertEqual(get_user_response.status, 404, get_user_response.error_response) # Create a new registration for this user. user_request = { 'sendSetPasswordEmail': False, 'skipVerification': True, 'user': { 'email': '*****@*****.**', 'password': '******' } } create_user_response = self.client.create_user(None, user_request) self.assertEqual(create_user_response.status, 200, create_user_response.error_response) # Retrieve the user user_id = create_user_response.success_response['user']['id'] get_user_response = self.client.retrieve_user(user_id) self.assertEqual(get_user_response.status, 200) self.assertIsNotNone(get_user_response.success_response) self.assertIsNone(get_user_response.error_response) self.assertEquals(get_user_response.success_response['user']['email'], '*****@*****.**') self.assertFalse( 'password' in get_user_response.success_response['user']) self.assertFalse('salt' in get_user_response.success_response['user']) def test_retrieve_user_missing(self): user_id = uuid.uuid4() client_response = self.client.retrieve_user(user_id) self.assertEqual(client_response.status, 404) self.assertIsNone(client_response.success_response) self.assertIsNone(client_response.error_response)
class FusionAuthClientTest(unittest2.TestCase): def setUp(self): fusionauth_url = os.getenv( 'FUSIONAUTH_URL' ) if 'FUSIONAUTH_URL' in os.environ else 'http://*****:*****@vandaleyindustries.com') if get_user_response.status is 200: delete_user_response = self.client.delete_user( get_user_response.success_response['user']['id']) self.assertEqual(delete_user_response.status, 200, delete_user_response.error_response) else: self.assertEqual(get_user_response.status, 404, get_user_response.error_response) # Create a new registration for this user. user_request = { 'sendSetPasswordEmail': False, 'skipVerification': True, 'user': { 'email': '*****@*****.**', 'password': '******' } } create_user_response = self.client.create_user(user_request) self.assertEqual(create_user_response.status, 200, create_user_response.error_response) # Retrieve the user user_id = create_user_response.success_response['user']['id'] get_user_response = self.client.retrieve_user(user_id) self.assertEqual(get_user_response.status, 200) self.assertIsNotNone(get_user_response.success_response) self.assertIsNone(get_user_response.error_response) self.assertEquals(get_user_response.success_response['user']['email'], '*****@*****.**') self.assertFalse( 'password' in get_user_response.success_response['user']) self.assertFalse('salt' in get_user_response.success_response['user']) def test_retrieve_user_missing(self): user_id = uuid.uuid4() client_response = self.client.retrieve_user(user_id) self.assertEqual(client_response.status, 404) self.assertIsNone(client_response.success_response) self.assertIsNone(client_response.error_response)
from fusionauth.fusionauth_client import FusionAuthClient # You must supply your API key and URL here client = FusionAuthClient('api-key', 'http://*****:*****@vandaleyindustries.com', 'password': '******' } } client_response = client.create_user(user_request) if client_response.was_successful(): print(client_response.success_response) else: print(client_response.error_response)
import os from app import app from flask import Flask, request, render_template, send_from_directory, session from fusionauth.fusionauth_client import FusionAuthClient import pkce #UPDATE ME api_key = "" client_id = "85a03867-dccf-4882-adde-1a79aeec50df" client_secret = "7gh9U0O1wshsrVVvflccX-UL2zxxsYccjdw8_rOfsfE" host_ip = "localhost" #/UPDATE ME client = FusionAuthClient(api_key, "http://{}:9011".format(host_ip)) @app.route("/") def index(): code_verifier, code_challenge = pkce.generate_pkce_pair() login_uri = "http://{}:9011/oauth2/authorize?client_id={}&response_type=code&redirect_uri=http%3A%2F%2F{}%3A5000%2Foauth-callback&code_challenge={}&code_challenge_method=S256".format( host_ip, client_id, host_ip, code_challenge) register_uri = "http://{}:9011/oauth2/register?client_id={}&response_type=code&redirect_uri=http%3A%2F%2F{}%3A5000%2Foauth-callback&code_challenge={}&code_challenge_method=S256".format( host_ip, client_id, host_ip, code_challenge) # save the verifier in session to send it later to the token endpoint session['code_verifier'] = code_verifier return render_template("public/index.html", login_uri=login_uri, register_uri=register_uri) @app.route("/oauth-callback")
def setUp(self): self.client = FusionAuthClient('bf69486b-4733-4470-a592-f1bfce7af580', 'http://localhost:9011')
def post(self, request): birthday = request.POST.get("birthday") user_id = request.POST.get("user_id") normalised_birthday = None print(birthday) print(user_id) try: dt = dateparser.parse(birthday) normalised_birthday = dt.strftime("%Y-%m-%d") except Exception as e: print(e) print("Couldn't parse birthday") if not normalised_birthday: return render( request, "secretbirthdaysapp/dashboard.html", { "message": "Couldn't parse birthday. Please use YYYY-MM-DD", "user_id": user_id }, ) try: client = FusionAuthClient(settings.FUSION_AUTH_API_KEY, settings.FUSION_AUTH_BASE_URL) r = client.patch_user(user_id, {"user": { "birthDate": normalised_birthday }}) if r.was_successful(): print(r.success_response) return render( request, "secretbirthdaysapp/dashboard.html", { "message": "Updated your birthday", "birthday": normalised_birthday, "user_id": user_id, }, ) else: print(r.error_response) return render( request, "secretbirthdaysapp/dashboard.html", { "message": "Something went wrong", "user_id": user_id }, ) except Exception as e: print(e) return render( request, "secretbirthdaysapp/dashboard.html", {"message": "Something went wrong"}, )
from fusionauth.fusionauth_client import FusionAuthClient # You must supply your API key and URL here client = FusionAuthClient('api-key', 'http://*****:*****@vandaleyindustries.com', 'password': '******' } } client_response = client.create_user(user_request) if client_response.was_successful(): print(client_response.success_response) else: print(client_response.error_response) client_response = client.retrieve_user_by_email('*****@*****.**') if client_response.was_successful(): print(client_response.success_response) else: print(client_response.error_response)
# config related app.config["SESSION_TYPE"] = "filesystem" app.config["SESSION_PERMANENT"] = True app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(hours=5) app.config["MONGO_URI"] = SERVER_DB_URI app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER app.config["TRANSLATE_FOLDER"] = TRANSLATE_FOLDER # app wrappers mongo = PyMongo(app) Session(app) CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True) app.secret_key = "super secret key" # Auth server related fusionAuthServer = FusionAuthClient(api_key, fusionauth_address) redis_con = Redis() # Eager loading of translator module translator = TranslatorSrt() @app.route("/job_status/<job_id>") def get_job_status(job_id): job = rq.Queue(connection=redis_con).fetch_job(job_id) response = {"status": "unknown"} if job: response = { "status": job.get_status(), "result": job.result,
import sys from fusionauth.fusionauth_client import FusionAuthClient # You must supply your API key and URL here client = FusionAuthClient('api-key', 'http://*****:*****@doe.io', 'password': '******', 'firstName': 'John', 'lastName': 'Doe', 'twoFactorEnabled': False, 'username': '******' }, 'sendSetPasswordEmail': False, 'skipVerification': False }