
    qhA                         S r SSKrSSKrSSKJr  SSKJr  SSKJr  S r	 " S S\5      r
 " S	 S
\
5      r " S S\
5      r " S S\
5      rg)a.  
This module contains C{L{OpenIDStore}} implementations that use
various SQL databases to back them.

Example of how to initialize a store database::

python -c 'from openid.store import sqlstore; import pysqlite2.dbapi2;'
  'sqlstore.SQLiteStore(pysqlite2.dbapi2.connect("cstore.db")).createTables()'
    N)Association)OpenIDStore)noncec                    ^  U 4S jn[        T S5      (       a   T R                  SS  Ul        [        T S5      (       a  T R                  Ul        U$ ! [         a     N0f = f)Nc                 2   > U R                   " TU /UQ70 UD6$ N)_callInTransaction)selfargskwargsfuncs      G/var/www/html/env/lib/python3.13/site-packages/openid/store/sqlstore.pywrapped_inTxn.<locals>.wrapped   s     &&tTCDCFCC    __name__   __doc__)hasattrr   	TypeErrorr   )r   r   s   ` r   _inTxnr      sc    D tZ  	#}}QR0G tY,,N  		s   A 
AAc                       \ rS rSrSrSrSrSS jrS rS r	S	 r
S
 rS rS rS r\" \5      rS r\" \5      rSS jr\" \5      rS r\" \5      rS r\" \5      rS r\" \5      rS r\" \5      rSrg)SQLStore"   a@  
This is the parent class for the SQL stores, which contains the
logic common to all of the SQL stores.

The table names used are determined by the class variables
C{L{associations_table}} and
C{L{nonces_table}}.  To change the name of the tables used, pass
new table names into the constructor.

To create the tables with the proper schema, see the
C{L{createTables}} method.

This class shouldn't be used directly.  Use one of its subclasses
instead, as those contain the code necessary to use a specific
database.

All methods other than C{L{__init__}} and C{L{createTables}}
should be considered implementation details.


@cvar associations_table: This is the default name of the table to
    keep associations in

@cvar nonces_table: This is the default name of the table to keep
    nonces in.


@sort: __init__, createTables
oid_associations
oid_noncesNc                    Xl         SU l        0 U l        U=(       d    U R                  U=(       d    U R                  S.U l        SU l        [        U R                   S5      (       a,  [        U R                   S5      (       a  U R                   U l        [        U R                  S5      (       a  [        U R                  S5      (       d  [        S5      eg)a(  
This creates a new SQLStore instance.  It requires an
established database connection be given to it, and it allows
overriding the default table names.


@param conn: This must be an established connection to a
    database of the correct type for the SQLStore subclass
    you're using.

@type conn: A python database API compatible connection
    object.


@param associations_table: This is an optional parameter to
    specify the name of the table used for storing
    associations.  The default value is specified in
    C{L{SQLStore.associations_table}}.

@type associations_table: C{str}


@param nonces_table: This is an optional parameter to specify
    the name of the table used for storing nonces.  The
    default value is specified in C{L{SQLStore.nonces_table}}.

@type nonces_table: C{str}
N)associationsnoncesi`T  IntegrityErrorOperationalErrorzDError using database connection module (Maybe it can't be imported?))
conncur_statement_cacheassociations_tablenonces_table_table_namesmax_nonce_ager   
exceptionsRuntimeError)r
   r"   r%   r&   s       r   __init__SQLStore.__init__D   s    : 	 ".I$2I2I"7d&7&7
 )
 DII/00		#566"iiDO)9::);<<  ? @ @ =r   c                     U$ )zKConvert a blob as returned by the SQL engine into a str object.

str -> str r
   blobs     r   
blobDecodeSQLStore.blobDecodev   s	     r   c                     U$ )zUConvert a str object into the necessary object for storing
in the database as a blob.r.   r
   ss     r   
blobEncodeSQLStore.blobEncode|   s	     r   c                      U R                   U   $ ! [         a,    [        X5      nX R                  -  nX R                   U'   Us $ f = fr   )r$   KeyErrorgetattrr'   )r
   sql_namesqls      r   _getSQLSQLStore._getSQL   sR    	((22 	$)C$$$C.1!!(+J		s    3AAc                     U R                  U5      nS n[        [        XB5      5      nU R                  R	                  X55        g )Nc                 F    [        U [        5      (       a  [        U 5      $ U $ r   )
isinstancestr)args    r   unicode_to_str)SQLStore._execSQL.<locals>.unicode_to_str   s    #s##3x
r   )r=   listmapr#   execute)r
   r;   r   r<   rD   str_argss         r   _execSQLSQLStore._execSQL   s8    ll8$	 N12'r   c                 r   ^ ^ US S S:X  a  USS  S-   mU U4S jn[        T X5        U$ [        SU< S35      e)N   db__sqlc                  *   > TR                   " T/U Q76 $ r   )rJ   )r   r
   r;   s    r   r   "SQLStore.__getattr__.<locals>.func   s    }}X555r   z
Attribute z
 not found)setattrAttributeError)r
   attrr   r;   s   `  @r   __getattr__SQLStore.__getattr__   sG     8uABx&(H6 D$%K T!DEEr   c                    U R                   R                  5          U R                   R                  5       U l         U" U0 UD6nU R                  R	                  5         SU l         U R                   R                  5         U$ ! U R                  R	                  5         SU l        f = f!   U R                   R                  5         e = f)zExecute the given function inside of a transaction, with an
open cursor. If no exception is raised, the transaction is
comitted, otherwise it is rolled back.N)r"   rollbackcursorr#   closecommit)r
   r   r   r   rets        r   r	   SQLStore._callInTransaction   s    
 				yy'')DH D+F+ 
 II
  	II s"   B( B !B( #B%%B( (Cc                 D    U R                  5         U R                  5         g)z
This method creates the database tables necessary for this
store to work.  It should not be called if the tables already
exist.
N)db_create_noncedb_create_assocr
   s    r   txn_createTablesSQLStore.txn_createTables   s     	r   c                     UnU R                  XR                  U R                  UR                  5      UR                  UR
                  UR                  5        g)zASet the association for the server URL.

Association -> NoneType
N)db_set_assochandler6   secretissuedlifetime
assoc_type)r
   
server_urlassociationas       r   txn_storeAssociationSQLStore.txn_storeAssociation   s@    
 *hh//!((3QXXqzz,,	(r   c                    Ub  U R                  X5        OU R                  U5        U R                  R                  5       n[	        U5      S:X  a  g/ nU Hw  n[        U5      nU R                  US   5      US'   [        U6 nUR                  S:X  a  U R                  XR                  5        MZ  UR                  UR                  U45        My     U(       a  UR                  5         US   S   $ g)zrGet the most recent association that has been set for this
server URL and handle.

str -> NoneType or Association
Nr      )db_get_assocdb_get_assocsr#   fetchalllenrF   r1   r   	expiresIntxn_removeAssociationrf   appendrh   sort)r
   rk   rf   rowsr   valuesassocs          r   txn_getAssociationSQLStore.txn_getAssociation   s     j1z*xx  "t9>Lf OOF1I6q	#V,??a'..z<<H ''u(=>  !!##B'**r   c                 V    U R                  X5        U R                  R                  S:  $ )zRemove the association for the given server URL and handle,
returning whether the association existed at all.

(str, str) -> bool
r   )db_remove_assocr#   rowcount)r
   rk   rf   s      r   rx   SQLStore.txn_removeAssociation   s'     	Z0xx  1$$r   c                     [        U[        R                  " 5       -
  5      [        R                  :  a  g U R	                  XU5        g! U R
                  R                   a     gf = f)z]Return whether this nonce is present, and if it is, then
remove it from the set.

str -> boolFT)abstimer   SKEWdb_add_noncer)   r    )r
   rk   	timestampsalts       r   txn_useNonceSQLStore.txn_useNonce  s[    
 y499;&'%**4	jT:  -- 		s   A	 	A&%A&c                     U R                  [        [        R                  " 5       5      [        R                  -
  5        U R
                  R                  $ r   )db_clean_nonceintr   r   r   r#   r   ra   s    r   txn_cleanupNoncesSQLStore.txn_cleanupNonces  s4    C		,uzz9:xx   r   c                     U R                  [        [        R                  " 5       5      5        U R                  R                  $ r   )db_clean_assocr   r   r#   r   ra   s    r   txn_cleanupAssociations SQLStore.txn_cleanupAssociations  s+    C		,-xx   r   )r$   r'   r"   r#   r)   r(   )NNr   )r   
__module____qualname____firstlineno__r   r%   r&   r+   r1   r6   r=   rJ   rU   r	   rb   r   createTablesrn   storeAssociationr~   getAssociationrx   removeAssociationr   useNoncer   cleanupNoncesr   cleanupAssociations__static_attributes__r.   r   r   r   r   "   s    < ,L0@d
( F, *+L( 23> ./N% 45" l#H! ,-M! !!89r   r   c                   Z   ^  \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrSrS rU 4S jrSrU =r$ )SQLiteStorei"  z
This is an SQLite-based specialization of C{L{SQLStore}}.

To create an instance, see C{L{SQLStore.__init__}}.  To create the
tables it will use, see C{L{SQLStore.createTables}}.

All other methods are implementation details.
z
    CREATE TABLE %(nonces)s (
        server_url VARCHAR,
        timestamp INTEGER,
        salt CHAR(40),
        UNIQUE(server_url, timestamp, salt)
    );
    a  
    CREATE TABLE %(associations)s
    (
        server_url VARCHAR(2047),
        handle VARCHAR(255),
        secret BLOB(128),
        issued INTEGER,
        lifetime INTEGER,
        assoc_type VARCHAR(64),
        PRIMARY KEY (server_url, handle)
    );
    z}INSERT OR REPLACE INTO %(associations)s (server_url, handle, secret, issued, lifetime, assoc_type) VALUES (?, ?, ?, ?, ?, ?);z_SELECT handle, secret, issued, lifetime, assoc_type FROM %(associations)s WHERE server_url = ?;znSELECT handle, secret, issued, lifetime, assoc_type FROM %(associations)s WHERE server_url = ? AND handle = ?;zDSELECT server_url FROM %(associations)s WHERE issued + lifetime < ?;zADELETE FROM %(associations)s WHERE server_url = ? AND handle = ?;z9DELETE FROM %(associations)s WHERE issued + lifetime < ?;z(INSERT INTO %(nonces)s VALUES (?, ?, ?);z+DELETE FROM %(nonces)s WHERE timestamp < ?;c                     [        U5      $ r   )
memoryviewr4   s     r   r6   SQLiteStore.blobEncodeX  s    !}r   c                    >  [         [        U ]
  " U0 UD6$ ! U R                  R                   a0  n[
        R                  " S[        U5      5      (       a   S nAge S nAff = f)Nz^columns .* are not unique$F)superr   r   r)   r!   rematchrB   )r
   r   r   why	__class__s       r   r   SQLiteStore.useNonce[  sU    	d4dEfEE// 	xx5s3x@@		s    A!%AAA!r.   )r   r   r   r   r   create_nonce_sqlcreate_assoc_sqlset_assoc_sqlget_assocs_sqlget_assoc_sqlget_expired_sqlremove_assoc_sqlclean_assoc_sqladd_nonce_sqlclean_nonce_sqlr6   r   r   __classcell__)r   s   @r   r   r   "  sc    2MDN	E LO? RO>MCO
 
r   r   c                   f    \ rS rSrSr SSKrSrSr	Sr
SrS	rS
rSrSrSrSrSrg! \ a    Sr N!f = f)
MySQLStoreih  a  
This is a MySQL-based specialization of C{L{SQLStore}}.

Uses InnoDB tables for transaction support.

To create an instance, see C{L{SQLStore.__init__}}.  To create the
tables it will use, see C{L{SQLStore.createTables}}.

All other methods are implementation details.
r   Nz
    CREATE TABLE %(nonces)s (
        server_url BLOB NOT NULL,
        timestamp INTEGER NOT NULL,
        salt CHAR(40) NOT NULL,
        PRIMARY KEY (server_url(255), timestamp, salt)
    )
    ENGINE=InnoDB;
    aG  
    CREATE TABLE %(associations)s
    (
        server_url BLOB NOT NULL,
        handle VARCHAR(255) NOT NULL,
        secret BLOB NOT NULL,
        issued INTEGER NOT NULL,
        lifetime INTEGER NOT NULL,
        assoc_type VARCHAR(64) NOT NULL,
        PRIMARY KEY (server_url(255), handle)
    )
    ENGINE=InnoDB;
    zDREPLACE INTO %(associations)s VALUES (%%s, %%s, %%s, %%s, %%s, %%s);aSELECT handle, secret, issued, lifetime, assoc_type FROM %(associations)s WHERE server_url = %%s;FSELECT server_url FROM %(associations)s WHERE issued + lifetime < %%s;rSELECT handle, secret, issued, lifetime, assoc_type FROM %(associations)s WHERE server_url = %%s AND handle = %%s;EDELETE FROM %(associations)s WHERE server_url = %%s AND handle = %%s;;DELETE FROM %(associations)s WHERE issued + lifetime < %%s;.INSERT INTO %(nonces)s VALUES (%%s, %%s, %%s);-DELETE FROM %(nonces)s WHERE timestamp < %%s;r.   )r   r   r   r   r   MySQLdbr)   ImportErrorr   r   r   r   r   r   r   r   r   r   r   r.   r   r   r   r   h  ss    	$>MGNNO	J C TODMEOY  
s   % 00r   c                       \ rS rSrSr SSKrSr
SrSrS rS	rS
rSrSrSrSrSrSrSrS rS rSrg! \ a    SSKJr  \R                  5          NBf = f)PostgreSQLStorei  z
This is a PostgreSQL-based specialization of C{L{SQLStore}}.

To create an instance, see C{L{SQLStore.__init__}}.  To create the
tables it will use, see C{L{SQLStore.createTables}}.

All other methods are implementation details.
r   N)compatz
    CREATE TABLE %(nonces)s (
        server_url VARCHAR(2047) NOT NULL,
        timestamp INTEGER NOT NULL,
        salt CHAR(40) NOT NULL,
        PRIMARY KEY (server_url, timestamp, salt)
    );
    a  
    CREATE TABLE %(associations)s
    (
        server_url VARCHAR(2047) NOT NULL,
        handle VARCHAR(255) NOT NULL,
        secret BYTEA NOT NULL,
        issued INTEGER NOT NULL,
        lifetime INTEGER NOT NULL,
        assoc_type VARCHAR(64) NOT NULL,
        PRIMARY KEY (server_url, handle),
        CONSTRAINT secret_length_constraint CHECK (LENGTH(secret) <= 128)
    );
    c                     U R                  X5      nU R                  R                  5       n[        U5      (       a  U R	                  X4XVX5      $ U R                  XX4XV5      $ )z
Set an association.  This is implemented as a method because
REPLACE INTO is not supported by PostgreSQL (and is not
standard SQL).
)rs   r#   ru   rv   db_update_assocdb_new_assoc)	r
   rk   rf   rg   rh   ri   rj   resultr{   s	            r   re   PostgreSQLStore.db_set_assoc  se     "":6xx  "t99''(2< <
 $$Z%-; ;r   zCINSERT INTO %(associations)s VALUES (%%s, %%s, %%s, %%s, %%s, %%s);zUPDATE %(associations)s SET secret = %%s, issued = %%s, lifetime = %%s, assoc_type = %%s WHERE server_url = %%s AND handle = %%s;r   r   r   r   r   r   r   c                     SSK Jn  U" U5      $ )Nr   )Binary)psycopg2r   )r
   r0   r   s      r   r6   PostgreSQLStore.blobEncode  s    #d|r   c                 "    UR                  5       $ r   )tobytesr/   s     r   r1   PostgreSQLStore.blobDecode  s    ||~r   r.   )r   r   r   r   r   r   r   psycopg2cffir   registerr)   r   r   re   new_assoc_sqlupdate_assoc_sqlr   r   r   r   r   r   r   r6   r1   r   r.   r   r   r   r     s    
 J;&>MCGNNO	J C TODMEO
Y  's   2 AAr   )r   r   r   openid.associationr   openid.store.interfacer   openid.storer   r   r   r   r   r   r.   r   r   <module>r      s[    
  * .  }:{ }:@C( CL:F :FzYh Yr   