| 691 | |
| 692 | |
| 693 | def resolve_device_request(request_token: str, approved: bool) -> bool: |
| 694 | if not AUTH_DB_PATH.exists(): |
| 695 | return False |
| 696 | |
| 697 | try: |
| 698 | with open_auth_db() as connection: |
| 699 | row = connection.execute( |
| 700 | """ |
| 701 | SELECT * |
| 702 | FROM device_approval_requests |
| 703 | WHERE request_token = ? AND status = 'pending' |
| 704 | LIMIT 1 |
| 705 | """, |
| 706 | (request_token,), |
| 707 | ).fetchone() |
| 708 | if row is None: |
| 709 | return False |
| 710 | |
| 711 | if approved: |
| 712 | connection.execute( |
| 713 | """ |
| 714 | INSERT INTO trusted_devices ( |
| 715 | user_id, |
| 716 | device_id, |
| 717 | device_name, |
| 718 | platform, |
| 719 | app_type, |
| 720 | first_approved_at, |
| 721 | last_seen, |
| 722 | last_login, |
| 723 | last_ip, |
| 724 | last_user_agent, |
| 725 | is_active |
| 726 | ) |
| 727 | VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?, ?, 1) |
| 728 | ON CONFLICT(user_id, device_id) |
| 729 | DO UPDATE SET |
| 730 | device_name = excluded.device_name, |
| 731 | platform = excluded.platform, |
| 732 | app_type = excluded.app_type, |
| 733 | last_seen = CURRENT_TIMESTAMP, |
| 734 | last_login = CURRENT_TIMESTAMP, |
| 735 | last_ip = excluded.last_ip, |
| 736 | last_user_agent = excluded.last_user_agent, |
| 737 | is_active = 1 |
| 738 | """, |
| 739 | ( |
| 740 | row["user_id"], |
| 741 | row["device_id"], |
| 742 | row["device_name"], |
| 743 | row["platform"], |
| 744 | row["app_type"], |
| 745 | row["requested_ip"], |
| 746 | row["requested_user_agent"], |
| 747 | ), |
| 748 | ) |
| 749 | |
| 750 | connection.execute( |