Skip to content

Add sample workflow for reauthentication after risk event #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
19 changes: 19 additions & 0 deletions reauth_after_risk_event/starter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import asyncio
from datetime import timedelta
from temporalio.client import Client

async def main():
client = await Client.connect("localhost:7233")

result = await client.start_workflow(
"ReauthenticationAfterRiskEventWorkflow",
"user-123", # user_id
id="reauth-workflow-user-123",
task_queue="reauth-task-queue",
run_timeout=timedelta(minutes=1), # Correct timeout usage
)

print(f"Started workflow. Run ID: {result.id}")

if __name__ == "__main__":
asyncio.run(main())
17 changes: 17 additions & 0 deletions reauth_after_risk_event/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import asyncio
from temporalio.worker import Worker
from temporalio.client import Client
from workflow import ReauthenticationAfterRiskEventWorkflow

async def main():
client = await Client.connect("localhost:7233")
worker = Worker(
client,
task_queue="reauth-task-queue",
workflows=[ReauthenticationAfterRiskEventWorkflow],
)
print("Worker started.")
await worker.run()

if __name__ == "__main__":
asyncio.run(main())
29 changes: 29 additions & 0 deletions reauth_after_risk_event/workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from datetime import timedelta
from temporalio import workflow

# Signal to be sent when user completes reauthentication
@workflow.defn
class ReauthenticationAfterRiskEventWorkflow:
def __init__(self):
self.reauthenticated = False

@workflow.signal
async def complete_reauthentication(self):
self.reauthenticated = True

@workflow.run
async def run(self, user_id: str, timeout_minutes: int = 10) -> str:
workflow.logger.info(f"Started reauth workflow for user: {user_id}")

# Wait for signal or timeout
try:
await workflow.wait_condition(
lambda: self.reauthenticated,
timeout=timedelta(minutes=timeout_minutes)
)
except TimeoutError:
workflow.logger.warn("Timeout waiting for reauthentication")
return "failed_timeout"

workflow.logger.info("User successfully reauthenticated")
return "success"