
    gi                        d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZ ddlmZ ddlmZmZmZ d	Zd
Zd Z ej,                          ej.                  e      z
  Z eej4                  j7                  eej8                  j:                  de      j=                  dd      j?                  dddd            Z  ee      Z!e D ]  Z"e! ee"         jG                  e"         e$d e%e        d e%e!       dejM                          d       dddddddZ'e!jQ                         D ]9  \  \  Z)Z*Z+ ej,                         Z,e+d   Z-e+D  cg c]  }  e.| j^                         c} Z0e+D  cg c]  }  e.| jb                         c} Z2 e$de) d e%e+       de2 de0        	  ee-e+e)      Z3 ee3e)e-jh                  e)ejj                  jl                  k7        Z7 ee)e7      \  Z<Z=e<D ch c]  }|r	 ee)|       c}Z>g Z?g Z@e+D ]Z  Z" ee)e"j                  j                        ZCeCe>v re?jG                  e"jb                         @e@jG                  e"jb                         \  ejr                         5  e?rAej4                  j7                  e?      ju                  ej8                  j                  e,        e@rAej4                  j7                  e@      ju                  ej8                  j:                  e,        d!d!d!       e'd"xx   d#z  cc<   e'd$xx    e%e+      z  cc<   e'd*xx    e%e?      z  cc<   e'd&xx    e%e@      z  cc<    e$d+ e%e?       d, e%e@              <  e$d-e'        y!c c} w c c} w # ee	f$ rZ8 ejr                         5  ej4                  j7                  e2      ju                  ej8                  j:                  e,        d!d!d!       n# 1 sw Y   nxY we'd"xx   d#z  cc<   e'd$xx    e%e+      z  cc<   e'd%xx   d#z  cc<   e'd&xx    e%e+      z  cc<    e$d'e8        Y d!Z8[8d!Z8[8we;$ r#Z8e'd(xx   d#z  cc<    e$d)e8        Y d!Z8[89d!Z8[8ww xY wc c}w # 1 sw Y   vxY w).a  
Rerun token checks for recently EXPIRED owner/editor UserAccount refresh tokens.

Run from the repo root:
    uv run python manage.py shell < /tmp/rerun_recent_expired_token_checks.py

Scope:
    - role in ("editor", "owner")
    - status = "EXPIRED"
    - last_token_check_time in the last 5 rolling days

This intentionally does not call the shared validator because that path can also
update Account names. This script only updates status and last_token_check_time
for the selected UserAccount rows.
    )defaultdict)transaction)timezone)TokenExpiredExceptionTokenInvalidException)AccountUserAccount)get_ad_accounts_list)#_build_token_validation_credentials_extract_accessible_accounts_normalize_account_id   )editorownerc                     t        | j                  d      r| j                  j                         nt        | j                        }| j                  j
                  |j                         fS )Ntobytes)hasattrrefresh_tokenr   bytesaccountplatformhex)user_accounttoken_bytess     )/tmp/rerun_recent_expired_token_checks.py	token_keyr   $   s[     <--y9 	""**,<--. 
 ((+//*;;;    )daysF)role__instatusrefresh_token__isnulllast_token_check_time__gter   useraccount__platformuser_id
account_ididz	Selected z UserAccount rows across z$ unique platform/token groups since .)groups_checkedrows_checkedtokens_invalidrows_set_activerows_kept_expirederrorszChecking platform=z rows=z user_account_ids=z account_ids=)	list_only)id__in)r    last_token_check_timeNr)      r*   r+   r-   z'  token invalid/expired; kept EXPIRED: r.   z4  ERROR: token check failed without status changes: r,   z  token valid; set ACTIVE=z kept EXPIRED=zDone: )E__doc__collectionsr   	django.dbr   django.utilsr   organization_auth.exceptionsr   r   organization_auth.modelsr   r	   organization_auth.servicesr
   "organization_auth.tasks.auth_tasksr   r   r   LOOKBACK_DAYSROLESr   now	timedeltacutofflistobjectsfilterUserAccountStatusEXPIREDselect_relatedorder_byuser_accountsgroupsr   appendprintlen	isoformatsummaryitemsr   
_token_hexgroup
checked_atfirst_user_accountstrr&   account_idsr'   user_account_idscredentials_dictr#   PlatformLINKEDINaccount_listexcatomicupdate	Exceptionaccessible_account_ids_account_name_map!normalized_accessible_account_ids
active_idsexpired_idsr   ad_account_idnormalized_account_idACTIVE)uar&   s   00r   <module>rg      s    $ ! ! U 9 ;  < 
,(,,-@	@,,44##)	   ^Iv&X!9lDA	 
T	!L
9\"#**<8 " M"##<
6{m78H8H8J7K1N  &,\\^!XzEJq056"3r}}%6K-23UrBEE
U3		:VCJ< 0,-];-	I>

 ,##'"2"2";";;	
0 1M1-- 1)0J 	h
30)% JK 5  ..!
 !$EEloo.|/  
			&&j&9@@"44;;&0 A  &&k&:AA"44<<&0 B  
 "Ns5z)#j/1 C$44 	&s:&7~c+FVEW
XYa &4d wi _ 73( "#89 [!&&.>&?FF"44<<&0 G  "!! 	 !Q&!3u:- !Q&!#$E
2$7u=> QDSEJK)$ 
	s]   M3.M8%7M=,Q+5BQ0=Q(P=AO 	P= O)%AP==Q(Q##Q(0Q:	