o
    54g-                     @   s   d dl mZ ddlmZ ddlmZmZ dZdZ	dZ
d	Zg d
ZdgZddgZdZg dZg dZg dZg ZddgZddgZdgZddgZddgZG dd deZdS )    )ImproperlyConfigured   )PUSH_NOTIFICATIONS_SETTINGS   )
BaseConfigcheck_apns_certificatezUApplication '{application_id}' ({platform}) does not support the setting '{setting}'.zpPUSH_NOTIFICATIONS_SETTINGS.APPLICATIONS["{application_id}"]["PLATFORM"] is {code}. Must be one of: {platforms}.z:Unknown Platform: {platform}. Must be one of: {platforms}.zUPUSH_NOTIFICATIONS_SETTINGS.APPLICATIONS["{application_id}"]["{setting}"] is missing.)APNSFCMWNSWPPLATFORMAPPLICATION_GROUPAPPLICATION_SECRETCERTIFICATE)AUTH_KEY_PATHAUTH_KEY_IDTEAM_ID)r   ENCRYPTION_ALGORITHMTOKEN_LIFETIME)USE_SANDBOXUSE_ALTERNATIVE_PORTTOPICMAX_RECIPIENTSFIREBASE_APPPACKAGE_SECURITY_ID
SECRET_KEYWNS_ACCESS_URLPRIVATE_KEYCLAIMSERROR_TIMEOUTPOST_URLc                   @   s  e Zd ZdZd:ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Z	d;ddZdd Zd:ddZd:ddZd:ddZd:d d!Zd:d"d#Zd:d$d%Zd:d&d'Zd:d(d)Zd:d*d+Zd:d,d-Zd:d.d/Zd:d0d1Zd:d2d3Zd4d5 Zd:d6d7Zd:d8d9ZdS )<	AppConfigzB
	Supports any number of push notification enabled applications.
	Nc                 C   s,   |pt | _| jdi  | | jd  d S )NAPPLICATIONS)SETTINGS	_settings
setdefault_validate_applications)selfsettings r)   a/var/www/html/notificationserver/venv/lib/python3.10/site-packages/push_notifications/conf/app.py__init__J   s   
zAppConfig.__init__c                 C   s*   |  D ]\}}| || ||d< qdS )z#Validate the application collectionAPPLICATION_IDN)items_validate_config)r'   appsapplication_idapplication_configr)   r)   r*   r&   T   s   
z AppConfig._validate_applicationsc                 C   s   | dd }|d u rttj|ddtd|tvr(ttj|ddtddj|d }t| |r?t| ||| d S tt	j|dtd)	Nr   requiredz, )r0   code	platformsinvalidz_validate_{platform}_config)platform)r6   r4   )
getr   BAD_PLATFORMformatjoin	PLATFORMSlowerhasattrgetattrUNKNOWN_PLATFORM)r'   r0   r1   r6   validate_fnr)   r)   r*   r.   [   s4   	
zAppConfig._validate_configc                 C   s   t t t t t }| ||| t| v }d| _tD ]}|| vr)d| _ nq|s:| js:t	t
j|ttfdd }|rAd}n| jr\d}tt t t  }| ||| | ||t | ||  |dd |dd |dd  d S )	NTFr0   settingr   r   r   r   r   )REQUIRED_SETTINGSOPTIONAL_SETTINGSAPNS_AUTH_CREDS_REQUIREDAPNS_AUTH_CREDS_OPTIONALAPNS_OPTIONAL_SETTINGS_validate_allowed_settingsAPNS_SETTINGS_CERT_CREDSkeyshas_token_credsr   MISSING_SETTINGr9   _validate_required_settings_validate_apns_certificater%   )r'   r0   r1   allowedhas_cert_credstoken_setting	cert_pathallowed_tokensr)   r)   r*   _validate_apns_config~   sX   
zAppConfig._validate_apns_configc              
   C   sl   z!t |}| }t| W d   W dS 1 sw   Y  W dS  ty5 } ztd||d}~ww )z)Validate the APNS certificate at startup.Nz5The APNS certificate file at {!r} is not readable: {})openreadr   	Exceptionr   r9   )r'   certfilefcontenter)   r)   r*   rN      s   

&
z$AppConfig._validate_apns_certificatec                 C   sH   t t t t }| ||| | ||t |dd  |dd d S )Nr   r   i  )rC   rD   FCM_REQUIRED_SETTINGSFCM_OPTIONAL_SETTINGSrH   rM   r%   r'   r0   r1   rO   r)   r)   r*   _validate_fcm_config   s   zAppConfig._validate_fcm_configc                 C   s<   t t t t }| ||| | ||t |dd d S )Nr   z&https://login.live.com/accesstoken.srf)rC   rD   WNS_REQUIRED_SETTINGSWNS_OPTIONAL_SETTINGSrH   rM   r%   r^   r)   r)   r*   _validate_wns_config   s   zAppConfig._validate_wns_configc                 C   sF   t t t t }| ||| | ||t |dddddd d S )Nr    z#https://fcm.googleapis.com/fcm/sendz(https://wns2-par02p.notify.windows.com/wz2https://updates.push.services.mozilla.com/wpush/v2)CHROMEOPERAEDGEFIREFOX)rC   rD   WP_REQUIRED_SETTINGSWP_OPTIONAL_SETTINGSrH   rM   r%   r^   r)   r)   r*   _validate_wp_config   s   zAppConfig._validate_wp_configc                 C   s0   |  D ]}||vrtd|d ||qdS )z*Confirm only allowed settings are present.z5Platform {}, app {} does not support the setting: {}.r   N)rJ   r   r9   )r'   r0   r1   allowed_settingssetting_keyr)   r)   r*   rH      s   
z$AppConfig._validate_allowed_settingsTc                 C   s6   |D ]}||  vr|rttj||d dS qdS )z!All required keys must be presentrA   FT)rJ   r   rL   r9   )r'   r0   r1   required_settingsshould_throwrk   r)   r)   r*   rM      s   	z%AppConfig._validate_required_settingsc                 C   s   |sd}t d|| jd|d}|du r t d||d|kr4t tj||d|d||vrAt tj||d||S )	zs
		Walks through PUSH_NOTIFICATIONS_SETTINGS to find the correct setting value
		or raises ImproperlyConfigured.
		z!push_notifications.conf.AppConfigz9{} requires the application_id be specified at all times.r"   Nz2No application configured with application_id: {}.r   )r0   r6   rB   rA   )r   r9   r$   r7   SETTING_MISMATCHrL   )r'   r0   r6   settings_keyconf_cls
app_configr)   r)   r*   _get_application_settings   s2   	
z#AppConfig._get_application_settingsc                 C      |  |ddS )Nr	   r   rr   r'   r0   r)   r)   r*   get_firebase_app"     zAppConfig.get_firebase_appc                 C   s   | j S N)rK   ru   r)   r)   r*   has_auth_token_creds%  s   zAppConfig.has_auth_token_credsc                 C   rs   )Nr	   r   rt   ru   r)   r)   r*   get_max_recipients(  rw   zAppConfig.get_max_recipientsc                 C   sX   |  |dd}t|ts*t|dr|jS t|dst|dr&d|v r&|d S td|S )Nr   r   pathhas_key__contains__z`The APNS certificate settings value should be a string, or should have a 'path' attribute or key)rr   
isinstancestrr=   r{   r   )r'   r0   rr)   r)   r*   get_apns_certificate+  s   

zAppConfig.get_apns_certificatec                 C   s   |  || || |fS rx   )_get_apns_auth_key_path_get_apns_auth_key_id_get_apns_team_idru   r)   r)   r*   get_apns_auth_creds:  s
   zAppConfig.get_apns_auth_credsc                 C   rs   )Nr   r   rt   ru   r)   r)   r*   r   @  rw   z!AppConfig._get_apns_auth_key_pathc                 C   rs   )Nr   r   rt   ru   r)   r)   r*   r   C  rw   zAppConfig._get_apns_auth_key_idc                 C   rs   )Nr   r   rt   ru   r)   r)   r*   r   F  rw   zAppConfig._get_apns_team_idc                 C   rs   )Nr   r   rt   ru   r)   r)   r*   get_apns_use_sandboxI  rw   zAppConfig.get_apns_use_sandboxc                 C   rs   )Nr   r   rt   ru   r)   r)   r*   get_apns_use_alternative_portL  rw   z'AppConfig.get_apns_use_alternative_portc                 C   rs   )Nr   r   rt   ru   r)   r)   r*   get_apns_topicO  rw   zAppConfig.get_apns_topicc                 C   rs   )Nr
   r   rt   ru   r)   r)   r*   get_wns_package_security_idR  rw   z%AppConfig.get_wns_package_security_idc                 C   rs   )Nr
   r   rt   ru   r)   r)   r*   get_wns_secret_keyU  rw   zAppConfig.get_wns_secret_keyc                 C   s   |  |dd| S )Nr   r    rt   )r'   r0   browserr)   r)   r*   get_wp_post_urlX  s   zAppConfig.get_wp_post_urlc                 C   rs   )Nr   r   rt   ru   r)   r)   r*   get_wp_private_key[  rw   zAppConfig.get_wp_private_keyc                 C   rs   )Nr   r   rt   ru   r)   r)   r*   get_wp_claims^  rw   zAppConfig.get_wp_claimsrx   )T)__name__
__module____qualname____doc__r+   r&   r.   rT   rN   r_   rb   ri   rH   rM   rr   rv   ry   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   r!   E   s<    

#+

'












r!   N)django.core.exceptionsr   r(   r   r#   baser   r   rn   r8   r?   rL   r;   rC   rD   rI   rE   rF   rG   r\   r]   r`   ra   rg   rh   r!   r)   r)   r)   r*   <module>   s8    	