Post-Tyranny-Tech-Infrastru.../ansible/roles/authentik/files/create_recovery_flow.py
Pieter 301394df14 fix: Make recovery flow creation non-blocking
- Changed recovery flow task to not fail deployment if flow doesn't exist
- Simplified recovery flow script to just check for existing flows
- Email configuration (SMTP) is the critical part that makes recovery work
- Flows can be configured manually in Authentik UI if needed
2026-01-13 11:26:15 +01:00

70 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""
Create password recovery flow in Authentik
Allows users to reset their password via email
"""
import sys
import json
import urllib.request
import urllib.error
def api_request(base_url, token, path, method='GET', data=None):
"""Make API request to Authentik"""
url = f"{base_url}{path}"
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
request_data = json.dumps(data).encode() if data else None
req = urllib.request.Request(url, data=request_data, headers=headers, method=method)
try:
with urllib.request.urlopen(req, timeout=30) as resp:
return resp.status, json.loads(resp.read().decode())
except urllib.error.HTTPError as e:
error_body = e.read().decode()
try:
error_data = json.loads(error_body)
except:
error_data = {'error': error_body}
return e.code, error_data
def main():
if len(sys.argv) != 3:
print(json.dumps({'error': 'Usage: create_recovery_flow.py <base_url> <api_token>'}))
sys.exit(1)
base_url = sys.argv[1]
token = sys.argv[2]
# Check if recovery flow already exists with slug 'recovery-flow'
status, flows = api_request(base_url, token, '/api/v3/flows/instances/')
if status != 200:
print(json.dumps({'error': 'Failed to list flows', 'details': flows}), file=sys.stderr)
sys.exit(1)
# Check if we already have a recovery flow configured
existing_recovery = next((f for f in flows.get('results', [])
if f.get('slug') == 'recovery-flow' or f.get('designation') == 'recovery'), None)
if existing_recovery:
print(json.dumps({
'success': True,
'message': 'Recovery flow already exists',
'flow_id': existing_recovery['pk'],
'flow_slug': existing_recovery['slug']
}))
sys.exit(0)
# Create a simple recovery flow
# Note: In production Authentik, you would import flows via blueprints or UI
# For initial deployment, we just configure email settings and rely on manual flow setup
print(json.dumps({
'success': True,
'message': 'No recovery flow found - will use default Authentik flow after manual setup',
'note': 'Admin should configure recovery flow in Authentik UI: Flows & Stages'
}))
if __name__ == '__main__':
main()