
    )Jf1                        d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZmZ  ej        e          Zej        ej        z   d	z   Z ee          j        Zd
 Zd Zd Zd Z  G d de          Z!dS )zP
This module contains an C{L{OpenIDStore}} implementation backed by
flat files.
    N)EEXISTENOENT)mkstemp)Association)OpenIDStore)nonce)	cryptutiloidutil.c                    t          j        t          j        |                     }t	          |          }|                    dd          }|                    dd          }|                    dd          }t          |          S )N   +   _   /   .   =    )r
   toBase64r	   sha1	bytearrayreplacebytes)sh64s     Y/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/openid/store/filestore.py_safe64r      sl    

9>!,,
-
-C
C..C
++dD
!
!C
++dD
!
!C
++dC
 
 C::r   c                     g }| D ]L}t          |          r|                    |           '|                    dt          |          z             Md                    |          S )Nz_%02X )_isFilenameSafeappendordjoin)r   filename_chunkscs      r   _filenameEscaper$   %   sq    O 5 51 	5""1%%%%""7SVV#3444477?###r   c                     	 t          j        |            dS # t          $ r}|j        t          k    rY d}~dS  d}~ww xY w)zoAttempt to remove a file, returning whether the file existed at
    the time of the call.

    str -> bool
       Nr   )osunlinkOSErrorerrnor   )filenamewhys     r   _removeIfPresentr-   /   s\    

	( q    911111s    
>99>c                     	 t          j        |            dS # t          $ r;}|j        t          k    st           j                            |           s Y d}~dS d}~ww xY w)zCreate dir_name as a directory if it does not exist. If it
    exists, make sure that it is, in fact, a directory.

    Can raise OSError

    str -> NoneType
    N)r'   makedirsr)   r*   r   pathisdir)dir_namer,   s     r   
_ensureDirr3   B   ss    
H   9bgmmH&=&= s    
A0AAc                   b    e Zd ZdZd Zd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd Zd Zd Zd ZdS )FileOpenIDStorea  
    This is a filesystem-based store for OpenID associations and
    nonces.  This store should be safe for use in concurrent systems
    on both windows and unix (excluding NFS filesystems).  There are a
    couple race conditions in the system, but those failure cases have
    been set up in such a way that the worst-case behavior is someone
    having to try to log in a second time.

    Most of the methods of this class are implementation details.
    People wishing to just use this store need only pay attention to
    the C{L{__init__}} method.

    Methods of this object can raise OSError if unexpected filesystem
    conditions, such as bad permissions or missing directories, occur.
    c                    t           j                            t           j                            |                    }t           j                            |d          | _        t           j                            |d          | _        t           j                            |d          | _        d| _        | 	                                 dS )a1  
        Initializes a new FileOpenIDStore.  This initializes the
        nonce and association directories, which are subdirectories of
        the directory passed in.

        @param directory: This is the directory to put the store
            directories in.

        @type directory: C{str}
        noncesassociationstempi`T  N)
r'   r0   normpathabspathr!   	nonce_dirassociation_dirtemp_dirmax_nonce_age_setup)self	directorys     r   __init__zFileOpenIDStore.__init__b   s     G$$RW__Y%?%?@@	i::!w||I~FF Y77(r   c                 ~    t          | j                   t          | j                   t          | j                   dS )ziMake sure that the directories in which we store our data
        exist.

        () -> NoneType
        N)r3   r<   r=   r>   rA   s    r   r@   zFileOpenIDStore._setup|   s;     	4>"""4'(((4=!!!!!r   c                     t          | j                  \  }}	 t          j        |d          }||fS #  t	          |            xY w)a_  Create a temporary file on the same filesystem as
        self.association_dir.

        The temporary directory should not be cleaned if there are any
        processes using the store. If there is no active process using
        the store, it is safe to remove all of the files in the
        temporary directory.

        () -> (file, str)
        )dirwb)r   r>   r'   fdopenr-   )rA   fdnamefile_objs       r   _mktempzFileOpenIDStore._mktemp   sT     t}---D	yT**HT>!	T"""s	   3 Ac                    |                     d          dk    rt          d|z            |                    dd          \  }}t          |                    dd          d                   }t	          |          }|rt	          |          }nd}|d|d|d|}t
          j                            | j        |          S )	aO  Create a unique filename for a given server url and
        handle. This implementation does not assume anything about the
        format of the handle. The filename that is returned will
        contain the domain name from the server URL for ease of human
        inspection of the data directory.

        (str, str) -> str
        ://zBad server URL: %rr&   /r   r   -)	find
ValueErrorsplitr$   r   r'   r0   r!   r=   )	rA   
server_urlhandleprotorestdomainurl_hashhandle_hashr+   s	            r   getAssociationFilenamez&FileOpenIDStore.getAssociationFilename   s     ??5!!R''1J>??? &&ua00t C!3!3A!677:&& 	!&//KKK$)EE666888[[Iw||D0(;;;r   c                    |                                 }|                     ||j                  }|                                 \  }}	 	 |                    |           t          j        |                                           |                                 n# |                                 w xY w	 t          j	        ||           dS # t          $ rp}|j        t          k    r 	 t          j        |           n)# t          $ r}|j        t          k    rn Y d}~nd}~ww xY wt          j	        ||           Y d}~dS d}~ww xY w#  t          |            xY w)zcStore an association in the association directory.

        (str, Association) -> NoneType
        N)	serializer]   rW   rM   writer'   fsyncfilenocloserenamer)   r*   r   r(   r   r-   )rA   rV   associationassociation_sr+   tmp_filetmpr,   s           r   storeAssociationz FileOpenIDStore.storeAssociation   s   
 $--//..z;;MNN#	!}---**+++        )	#x((((( ) ) )9&&
Ih''''   yF**  	#x(((((((((#)$	 S!!!sl   	;B E B//E 3C
 

ED?&C;:D?;
D!DD?D!!D?9E ?EE ENc                 >   |d}|                      ||          }|r|                     |          S t          j        | j                  }g }t          j                            |          }|D ],}|                    |          r|                    |           -g }|D ]Z}t          j        	                    | j        |          }	|                     |	          }
|
|                    |
j
        |
f           [|                                 |r|d         \  }}|S dS )zRetrieve an association. If no handle is specified, return
        the association with the latest expiration.

        (str, str or NoneType) -> Association or NoneType
        Nr   rP   )r]   _getAssociationr'   listdirr=   r0   basename
startswithr   r!   issuedsort)rA   rV   rW   r+   association_filesmatching_filesrK   association_filematching_associations	full_namere   _assocs                r   getAssociationzFileOpenIDStore.getAssociation   sJ    >F ..z6BB 	''111 "
4+? @ @N7##H--D$5 < < #..t44 <"))*:;;;$&!& ; ;GLL)=tDD	"229==*)00$+[9; ; ; "&&((( % 226
Etr   c                    	 t          |d          }n)# t          $ r}|j        t          k    rY d }~d S  d }~ww xY w	 |                                }|                                 n# |                                 w xY w	 t          j        |          }n # t          $ r t          |           Y d S w xY w|j
        dk    rt          |           d S |S )Nrbr   )openIOErrorr*   r   readrc   r   deserializerT   r-   	expiresIn)rA   r+   
assoc_filer,   assoc_sre   s         r   rk   zFileOpenIDStore._getAssociation  s   	h--JJ 	 	 	yF""ttttt		 oo''GJ	%1'::KK 	 	 	X&&&44	
  A%%X&&&4s1    
9449A& &A< B B21B2c                     |                      ||          }|dS |                     ||          }t          |          S )zcRemove an association if it exists. Do nothing if it does not.

        (str, str) -> bool
        Nr   )rx   r]   r-   )rA   rV   rW   rw   r+   s        r   removeAssociationz!FileOpenIDStore.removeAssociation!  sF    
 ##J77=122:vFFH#H---r   c                    t          |t          j                    z
            t          j        k    rdS |r|                    dd          \  }}nd\  }}t          |                    dd          d                   }t          |          }t          |          }d|||||fz  }	t          j        	                    | j
        |	          }		 t          j        |	t          j        t          j        z  t          j        z  d          }
t          j        |
           d	S # t           $ r}|j        t$          k    rY d
}~dS  d
}~ww xY w)zAReturn whether this nonce is valid.

        str -> bool
        FrO   r&   )r   r   rQ   r   z%08x-%s-%s-%s-%s   TN)abstimer   SKEWrU   r$   r   r'   r0   r!   r<   r{   O_CREATO_EXCLO_WRONLYrc   r)   r*   r   )rA   rV   	timestampsaltrX   rY   rZ   r[   	salt_hashr+   rJ   r,   s               r   useNoncezFileOpenIDStore.useNonce-  s<   
 y49;;&''%*445 	!$**5!44KE44 !KE4 C!3!3A!677:&&DMM	%E68)2)4 4 7<<99		2:	#9BK#GOOB HRLLL4  	 	 	yF""uuuuu		s   :D 
E&D=<D==Ec                 D    g } fdt          j         j                  D             }|D ]}	 t          |d          }	 |                                }|                                 n# |                                 w xY w	 t          j        |          }|                    ||f           # t          $ r t          |           Y w xY w# t          $ rF}|j        t          k    r+t                              |d j        j        d           n Y d }~d }~ww xY w|S )Nc                 Z    g | ]'}t           j                            j        |          (S  )r'   r0   r!   r=   ).0r+   rA   s     r   
<listcomp>z.FileOpenIDStore._allAssocs.<locals>.<listcomp>R  s<     !
 !
 !
 GLL-x88!
 !
 !
r   rz   z disappeared during z._allAssocs)r'   rl   r=   r{   r}   rc   r   r~   r   rT   r-   r|   r*   r   logger	exception	__class____name__)rA   all_associationsassociation_filenamesassociation_filenamers   r   re   r,   s   `       r   
_allAssocszFileOpenIDStore._allAssocsO  s   !
 !
 !
 !
Jt';<<!
 !
 !
 %: 	= 	= =#'(<d#C#C -.3355G$**,,,,$**,,,,="-"9'"B"BK %++-{;= = = = " ; ; ;$%9:::::;    9&&$$,,,dn.E.E.E&G H H H H H H H H H*  s4   CA((A>B..C
	C

D<DDc                 V    |                                   |                                  dS )zRemove expired entries from the database. This is
        potentially expensive, so only run when it is acceptable to
        take time.

        () -> NoneType
        N)cleanupAssociationscleanupNoncesrE   s    r   cleanupzFileOpenIDStore.cleanupp  s.     	  """r   c                 |    d}|                                  D ]$\  }}|j        dk    rt          |           |dz  }%|S )Nr   r&   )r   r   r-   )rA   removedassoc_filenamerw   s       r   r   z#FileOpenIDStore.cleanupAssociationsz  sO    %)__%6%6 	 	!NE!## 0001r   c                 v   t          j        | j                  }t          j                    }d}|D ]}|                    dd          d         }t          |d          }t          ||z
            t          j        k    r9t           j	        
                    | j        |          }t          |           |dz  }|S )Nr   rR   r&      )r'   rl   r<   r   rU   intr   r   r   r0   r!   r-   )rA   r7   nowr   nonce_fnamer   r+   s          r   r   zFileOpenIDStore.cleanupNonces  s    DN++ikk! 	 	K#))#q11!4IIr**I9s?##ej007<<DD ***1r   )N)r   
__module____qualname____doc__rC   r@   rM   r]   ri   rx   rk   r   r   r   r   r   r   r   r   r   r5   r5   Q   s            4" " "  &< < <0( ( (T( ( ( (T  8
. 
. 
.     D     B        r   r5   )"r   stringr'   os.pathr   loggingr*   r   r   tempfiler   openid.associationr   openid.store.interfacer   openid.storer   openidr	   r
   	getLoggerr   r   ascii_lettersdigits_filename_allowedset__contains__r   r   r$   r-   r3   r5   r   r   r   <module>r      sj   
  				                          * * * * * * . . . . . .       % % % % % % % %		8	$	$(6=83> #'((5  $ $ $  &  ~ ~ ~ ~ ~k ~ ~ ~ ~ ~r   