a
    0s/h                     @   s\   G d d dZ G dd deZG dd deZG dd dZG dd	 d	ZG d
d dZdS )c                   @   s   e Zd ZdddZdd ZdS )
BasePluginNc                 C   s
   || _ d S N)sourceselfsrc r   5/opt/jumpscale7/apps/vncproxy/utils/./auth_plugins.py__init__   s    zBasePlugin.__init__c                 C   s   d S r   r   r   headerstarget_hosttarget_portr   r   r   authenticate   s    zBasePlugin.authenticate)N__name__
__module____qualname__r	   r   r   r   r   r   r      s   
r   c                       s&   e Zd Zddi df fdd	Z  ZS )AuthenticationErrorN  c                    s8   || _ || _|| _|d u r|}t d| j |f  d S )Nz%s %s)coder   msgsuperr	   )r   log_msgresponse_coderesponse_headersresponse_msg	__class__r   r   r	   
   s    zAuthenticationError.__init__r   r   r   r	   __classcell__r   r   r   r   r   	   s   r   c                       s   e Zd Z fddZ  ZS )InvalidOriginErrorc                    s(   || _ || _t jdd||f d d S )NzInvalid Originz3Invalid Origin Header: Expected one of %s, got '%s')r   r   )Zexpected_originZactual_originr   r	   )r   expectedactualr   r   r   r	      s    zInvalidOriginError.__init__r   r   r   r   r   r       s   r    c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )BasicHTTPAuthz=Verifies Basic Auth headers. Specify src as username:passwordNc                 C   s
   || _ d S r   r   r   r   r   r   r	   #   s    zBasicHTTPAuth.__init__c           	      C   s   dd l }|d}|r|ds(|   z||dd  }W n tyX   |   Y n0 z|d}W n ty   |   Y n0 |dd}t	|dkr|   | j
| s|   n|   d S )	N    ZAuthorizationzBasic    z
ISO-8859-1:      )base64get
startswith
auth_error	b64decode	TypeErrordecodeUnicodeDecodeErrorsplitlenvalidate_credsdemand_auth)	r   r   r   r   r*   Zauth_headerZuser_pass_rawZuser_pass_as_textZ	user_passr   r   r   r   &   s&    



zBasicHTTPAuth.authenticatec                 C   s   d||f | j krdS dS d S )Nz%s:%sTFr$   )r   usernamepasswordr   r   r   r4   B   s    zBasicHTTPAuth.validate_credsc                 C   s   t ddd S )Nr   r   r   r   r   r   r   r-   H   s    zBasicHTTPAuth.auth_errorc                 C   s   t dddidd S )Ni  zWWW-AuthenticatezBasic realm="Websockify")r   r   r9   r:   r   r   r   r5   K   s    zBasicHTTPAuth.demand_auth)N)	r   r   r   __doc__r	   r   r4   r-   r5   r   r   r   r   r#       s   
r#   c                   @   s   e Zd ZdddZdd ZdS )ExpectOriginNc                 C   s   |d u rg | _ n
| | _ d S r   r   r2   r   r   r   r   r	   P   s    zExpectOrigin.__init__c                 C   s0   | dd }|d u s|| jvr,t| j|dd S )NOrigin)r!   r"   )r+   r   r    )r   r   r   r   originr   r   r   r   V   s    zExpectOrigin.authenticate)Nr   r   r   r   r   r<   O   s   
r<   c                   @   s"   e Zd ZdZdddZdd ZdS )ClientCertCNAuthz]Verifies client by SSL certificate. Specify src as whitespace separated list of common names.Nc                 C   s   |d u rg | _ n
| | _ d S r   r=   r   r   r   r   r	   ^   s    zClientCertCNAuth.__init__c                 C   s    | dd | jvrtddd S )NSSL_CLIENT_S_DN_CNr   r8   )r+   r   r   r
   r   r   r   r   d   s    zClientCertCNAuth.authenticate)N)r   r   r   r;   r	   r   r   r   r   r   r@   [   s   
r@   N)r   	Exceptionr   r    r#   r<   r@   r   r   r   r   <module>   s
   /