o
    xwi)                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlmZ ej	
dZej	er3ee ej	eZeej	vrEej	d e d dlmZmZmZ d dlmZ d dlmZmZmZmZmZ d d	lmZ d d
l m!Z! d dl"m#Z#m$Z$ ee%ej	&edddZ'de'_(ej	&edZ)d4ddZ*d5ddZ+e',ddd Z-e'j,dddgd d!d" Z.e'j,d#ddgd d$d% Z/e'j,d&ddgd d'd( Z0e'j,d)ddgd d6d+d,Z1e'j,d-ddgd d6d.d/Z2e'j,d0ddgd d6d1d2Z3e%d3kre 4e' dS dS )7    )annotationsN)datetime)
CGIHandlerz%~/.local/lib/python3.10/site-packages)Flaskrender_templaterequest)generate_password_hash)get_setup_token_rowget_password_token_rowget_admin_account_by_usernameis_password_token_usableis_setup_token_usable)create_admin_password_update)$create_recipient_preferences_request)get_recipient_prefs_token_rowis_recipient_prefs_token_usableauth_app	templates)template_folderz"replace-this-later-with-env-secretzauth_runtime_error.logcontextstrreturnNonec                 C  sl   t tddd%}|dt   d|  d |t  |d W d    d S 1 s/w   Y  d S )Nazutf-8)encodingz
===== zZ z =====

)open	ERROR_LOGwriter   utcnow	isoformat	traceback
format_exc)r   fh r$   auth.pylog_runtime_error*   s
    "r&   titlec                 C  s   d|  d|  dS )NzJ<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>a{   - The Weather Monitor</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      background: #f5f7fa;
      color: #1f2937;
    }
    .wrap {
      max-width: 720px;
      margin: 40px auto;
      padding: 0 16px;
    }
    .card {
      background: #fff;
      border: 1px solid #d1d5db;
      border-radius: 12px;
      padding: 24px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    }
    .muted {
      color: #6b7280;
    }
  </style>
</head>
<body>
  <div class="wrap">
    <section class="card">
      <h1>z</h1>
      <div class="muted">Your password has been submitted. Allow 5 to 10 minutes for changes to take effect. You may now close this page.</div>
    </section>
  </div>
</body>
</html>
r$   )r'   r$   r$   r%    _submitted_password_success_html1   s
   "r(   /c                   C  s   dS )Nz*theweathermonitor.com auth CGI app is liver$   r$   r$   r$   r%   index]   s   r*   z/change-notification-settingsGETPOST)methodsc                  C  s   d } d }d}d}t jdkr@z&t jdpd }t jdpd }|r&|s)d} n	t||d W dS W n ty?   td  w t jd	pGd d
krNd}t	d| |||dS )N r,   emailphonez,Email address and phone number are required.)r/   r0   zKStatus: 303 See Other
Location: /change-notification-settings?submitted=1

z)change_notification_settings_request POST	submitted1z=If we found a matching recipient, a secure link will be sent.z)change_notification_settings_request.htmlerrormessager/   r0   )
r   methodformgetstripr   	Exceptionr&   argsr   r3   r$   r$   r%   $change_notification_settings_requestb   s4   
r<   z/auth/loginc                  C  s8   d} d }t jdkrt jdpd } d}td| |dS )Nr.   r,   usernamezLogin is not enabled yet.z
login.htmlr=   r4   )r   r6   r7   r8   r9   r   r>   r$   r$   r%   
auth_login   s   
r?   z/auth/forgot-passwordc                  C  sZ   d} d }d }t jdkr%t jdpd } | sd}nt| }|r#d}nd}td| ||dS )Nr.   r,   r=   zUsername is required.zBIf that account exists and is eligible, a reset link will be sent.zforgot_password.html)r=   r4   r5   )r   r6   r7   r8   r9   r   r   )r=   r4   r5   acctr$   r$   r%   auth_forgot_password   s"   
rA   z/auth/setup-password/<token>tokenc              	   C  s`  t | }t|\}}|std| |ddddS tjdkrzitjdp#d}tjdp+d}t|dk rOtd| dt|d	 p<dt|d
 pCdt|d pJddW S ||krptd| dt|d	 p]dt|d
 pddt|d pkddW S t	|}t
t|d | d|d tdW S  ty   td|    w td| d t|d	 pdt|d
 pdt|d pddS )Nzsetup_password.htmlr.   rB   r4   	full_namer=   	home_siter,   passwordpassword_confirm   'Password must be at least 8 characters.rD   r=   rE   Passwords do not match.admin_account_idsetuprK   rB   purposepassword_hashzPassword Submittedzauth_setup_password POST token=)r	   r   r   r   r6   r7   r8   lenr   r   r   intr(   r:   r&   rB   	token_rowokreasonrF   rG   rO   r$   r$   r%   auth_setup_password   sj   
			

rV   z/auth/reset-password/<token>c              	   C  sb  t | }t|d\}}|std| |ddddS tjdkrzitjdp$d}tjdp,d}t|dk rPtd| d	t|d
 p=dt|d pDdt|d pKddW S ||krqtd| dt|d
 p^dt|d pedt|d plddW S t	|}t
t|d | d|d tdW S  ty   td|    w td| d t|d
 pdt|d pdt|d pddS )Nresetzreset_password.htmlr.   rC   r,   rF   rG   rH   rI   rD   r=   rE   rJ   rK   rM   zPassword Reset Submittedzauth_reset_password POST token=)r
   r   r   r   r6   r7   r8   rP   r   r   r   rQ   r(   r:   r&   rR   r$   r$   r%   auth_reset_password   sj   
			

rX   z/prefs/<token>c                 C  s   t | }t|\}}|sd}td| ||d dddddddS d }tjdkr&d}td| dd |t|d	 p2dt|d
 p9dt|d p@dt|d pGdt|d pNdt|d pUddS )Nz:This notification settings link is invalid or has expired.zrecipient_preferences.htmlr.   r   )
rB   token_statusr4   r5   rD   rE   r/   smsemail_enabledsms_enabledr,   z0Saving notification settings is not enabled yet.readyrD   rE   r/   rZ   r[   r\   )r   r   r   r   r6   r   rQ   )rB   rS   rT   rY   r4   r5   r$   r$   r%   recipient_preferences2  sB   
r^   __main__)r   r   r   r   )r'   r   r   r   )rB   r   )5
__future__r   ossyssiter!   r   wsgiref.handlersr   path
expanduser	USER_SITEisdir
addsitedirdirname__file__APP_ROOTinsertflaskr   r   r   werkzeug.securityr   auth_app.auth_repor	   r
   r   r   r   #auth_app.admin_password_update_repor   auth_app.recipient_request_repor   auth_app.recipient_prefs_repor   r   __name__joinapp
secret_keyr   r&   r(   router*   r<   r?   rA   rV   rX   r^   runr$   r$   r$   r%   <module>   sR   



,

#

??'