Skip to content

Commit fbe32ce

Browse files
committed
Update for new client
1 parent 2c793de commit fbe32ce

File tree

1 file changed

+18
-14
lines changed
  • python-flask-mfa-example

1 file changed

+18
-14
lines changed

python-flask-mfa-example/app.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
2-
from typing import Any, cast
2+
from typing import Any, assert_never, cast
33
from flask import Flask, session, redirect, render_template, request, url_for, jsonify
4-
import json
54
import workos
65
from flask_lucide import Lucide
76

@@ -12,10 +11,12 @@
1211
lucide = Lucide(app)
1312

1413
# WorkOS Setup
15-
16-
workos.api_key = os.getenv("WORKOS_API_KEY")
17-
workos.client_id = os.getenv("WORKOS_CLIENT_ID")
18-
workos.base_api_url = "http://localhost:7000/" if DEBUG else workos.base_api_url
14+
base_api_url = "http://localhost:7000/" if DEBUG else None
15+
workos_client = workos.WorkOSClient(
16+
api_key=os.getenv("WORKOS_API_KEY"),
17+
client_id=os.getenv("WORKOS_CLIENT_ID"),
18+
base_url=base_api_url,
19+
)
1920

2021

2122
@app.route("/")
@@ -45,7 +46,7 @@ def enroll_sms_factor():
4546
if not factor_type in ("sms", "totp"):
4647
return "Invalid factor type"
4748

48-
new_factor = workos.client.mfa.enroll_factor(
49+
new_factor = workos_client.mfa.enroll_factor(
4950
type=factor_type, phone_number=phone_number
5051
)
5152

@@ -61,7 +62,7 @@ def enroll_totp_factor():
6162
issuer = data["issuer"]
6263
user = data["user"]
6364

64-
new_factor = workos.client.mfa.enroll_factor(
65+
new_factor = workos_client.mfa.enroll_factor(
6566
type=type, totp_issuer=issuer, totp_user=user
6667
)
6768

@@ -95,20 +96,23 @@ def factor_detail():
9596

9697
@app.route("/challenge_factor", methods=["POST"])
9798
def challenge_factor():
98-
if session["current_factor_type"] == "sms":
99+
factor_type = session["current_factor_type"]
100+
101+
if factor_type == "sms":
99102
message = request.form["sms_message"]
100103
session["sms_message"] = message
101104

102-
challenge = workos.client.mfa.challenge_factor(
105+
challenge = workos_client.mfa.challenge_factor(
103106
authentication_factor_id=session["current_factor"],
104107
sms_template=message,
105108
)
106-
107-
if session["current_factor_type"] == "totp":
109+
elif factor_type == "totp":
108110
authentication_factor_id = session["current_factor"]
109-
challenge = workos.client.mfa.challenge_factor(
111+
challenge = workos_client.mfa.challenge_factor(
110112
authentication_factor_id=authentication_factor_id,
111113
)
114+
else:
115+
assert_never(factor_type)
112116

113117
session["challenge_id"] = challenge.id
114118
session.modified = True
@@ -125,7 +129,7 @@ def buildCode(code_values):
125129

126130
code = buildCode(request.form)
127131
challenge_id = session["challenge_id"]
128-
verify_factor = workos.client.mfa.verify_challenge(
132+
verify_factor = workos_client.mfa.verify_challenge(
129133
authentication_challenge_id=challenge_id,
130134
code=code,
131135
)

0 commit comments

Comments
 (0)