Ë
    �;ºiø  ã                   ó>  — d Z ddlZddlZddlZddlmZmZm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 dd
lmZ  eddg¬«      ZdZdZej5                  d«      defd„«       Zej5                  de
¬«       ee«      fdedefd„«       Zdedede
fd„Zy)u  
LinkedIn OAuth2 Router
Ports the full OAuth2 flow from linkdintest/app.py into the main FastAPI platform.
Flow:
  1. GET /linkedin/login  â†’ Redirect user to LinkedIn authorization URL
  2. GET /linkedin/callback â†’ Exchange auth code for access token, save to DB
é    N)Ú	APIRouterÚRequestÚDepends)ÚRedirectResponseÚHTMLResponse)ÚSession)Úsettings)Úget_db)ÚUserPlatformKey)Úget_optional_user_id)Úencrypt_valuez	/linkedinzLinkedIn OAuth)ÚprefixÚtagsz/https://www.linkedin.com/oauth/v2/authorizationz-https://www.linkedin.com/oauth/v2/accessTokenú/loginÚrequestc                 ó¦   — t        | «      }|st        dd¬«      S t        › dt        j                  › dt        j
                  › d�}t        |d¬«      S )z:Redirect the user to LinkedIn's OAuth2 authorization page.r   é.  ©Ústatus_codez?response_type=code&client_id=z&redirect_uri=z)&scope=openid%20profile%20w_member_social)r   r   ÚLINKEDIN_AUTH_URLr	   ÚLINKEDIN_CLIENT_IDÚLINKEDIN_REDIRECT_URI)r   Úuser_idÚauth_urls      úy/var/labsstorage/home/Thilakkumar/htdocs/soical-media-automation/automation_platform/app/routers/linkedin_oauth_router.pyÚlinkedin_loginr      sa   € ô # 7Ó+€GÙÜ °cÔ:Ð:ô Ð
ðÜ×1Ñ1Ð2Ø
œ×7Ñ7Ð8Ø
3ð		5ð ô ˜H°#Ô6Ð6ó    z	/callback)Úresponse_classÚdbc           
   ƒ   ó|  K  — t        | «      }|st        dd¬«      S | j                  j                  d«      }| j                  j                  d«      }|s|st	        dd|xs d› �¬	«      S 	 t        j                  d
¬«      4 ƒd{  –—† }|j                  t        d|t        j                  t        j                  t        j                  dœddi¬«      ƒ d{  –—† }ddd«      ƒd{  –—†  j                  «       }|j                  d«      }	|j                  d«      }
|	st	        dd|› �¬	«      S d}|
rm	 |
j!                  d«      d   }|dt#        |«       dz  z  z   }t%        j&                  |«      }t        j(                  |«      }|j                  dd«      }|rd|› �}|j+                  t,        «      j/                  t,        j0                  |k(  t,        j2                  dk(  «      j5                  «       }t7        |	«      }|rt7        |«      nd}|r||_        ||_        n|j=                  t-        |d|d|¬«      «       |j?                  «        t	        dd ¬	«      S 7 �ŒØ7 �Œ‰7 �Œ|# 1 ƒd{  –—†7  sw Y   �Œ�xY w# t        $ r#}t	        ddt        |«      › �¬	«      cY d}~S d}~ww xY w# t        $ r Y �Œw xY w­w)!zAHandle LinkedIn callback: exchange code for token and save to DB.r   r   r   ÚcodeÚerrorFzLinkedIn authorization failed: zNo code received)ÚsuccessÚmessageé   )ÚtimeoutNÚauthorization_code)Ú
grant_typer!   Ú	client_idÚclient_secretÚredirect_urizContent-Typez!application/x-www-form-urlencoded)ÚdataÚheaderszToken exchange failed: Úaccess_tokenÚid_tokenzNo access token received: Ú ú.é   ú=é   Úsubzurn:li:person:Úlinkedin)r   Úplatform_nameÚencrypted_api_keyÚencrypted_secret_keyÚencrypted_extraTuF   âœ… LinkedIn connected successfully! Your access token has been saved.) r   r   Úquery_paramsÚgetÚ_result_pageÚhttpxÚAsyncClientÚpostÚLINKEDIN_TOKEN_URLr	   r   ÚLINKEDIN_CLIENT_SECRETr   Ú	ExceptionÚstrÚjsonÚsplitÚlenÚbase64Úurlsafe_b64decodeÚloadsÚqueryr   Úfilterr   r7   Úfirstr   r8   r:   ÚaddÚcommit)r   r   r   r!   r"   ÚclientÚ
token_respÚeÚ
token_jsonr.   r/   Ú
person_urnÚpayloadÚpaddedÚdecodedÚ	user_infor5   ÚexistingÚ	enc_tokenÚenc_urns                       r   Úlinkedin_callbackr\   ,   s¸  è ø€ ô # 7Ó+€GÙÜ °cÔ:Ð:à×Ñ×#Ñ# FÓ+€DØ× Ñ ×$Ñ$ WÓ-€Eá‘DÜØØ5°eÒ6QÐ?QÐ5RÐSô
ð 	
ðWÜ×$Ñ$¨RÔ0÷ 	ð 	°FØ%Ÿ{™{Ü"à"6Ø Ü!)×!<Ñ!<Ü%-×%DÑ%DÜ$,×$BÑ$Bñð (Ð)LÐMð  +ó 
 ÷ 
ˆJ÷	÷ 	ð —‘Ó"€JØ—>‘> .Ó1€LØ�~‰~˜jÓ)€HáÜ EÐ5OÐPZÈ|Ð3\Ô]Ð]ð €JÙð		Ø—n‘n SÓ)¨!Ñ,ˆGØ˜s¤s¨7£| m°aÑ&7Ñ8Ñ8ˆFÜ×.Ñ.¨vÓ6ˆGÜŸ
™
 7Ó+ˆIØ—-‘-  rÓ*ˆCÙØ-¨c¨UÐ3�
ð
 �x‰xœÓ(×/Ñ/Ü×Ñ 7Ñ*Ü×%Ñ%¨Ñ3ó÷ �eƒgð ô
 ˜lÓ+€IÙ+5Œm˜JÔ'¸2€GáØ%.ˆÔ"Ø#*ˆÕ à
�‰ŒØØ$Ø'Ø!#Ø#ô
ô 	ð ‡I�I„KäØØXôð ðw	ùð
ùð	ù÷ 	÷ 	ò 	ûô ò WÜ EÐ5LÌSÐQRËVÈHÐ3UÔVÕVûðWûô* ò 	Úð	üs¸   ‚A)J<Á,I= ÂIÂI= Â
AI'ÃI!ÃI'ÃI= Ã(I$Ã)I= Ã-AJ<Ä6A,J, Æ"B<J<ÉI= É!I'É$I= É'I:É-I0É.I:É5I= É=	J)ÊJ$ÊJ)ÊJ<Ê$J)Ê)J<Ê,	J9Ê5J<Ê8J9Ê9J<r#   r$   Úreturnc                 óR   — | rdnd}| rdnd}t        d|› d|› d| rdnd	› d
|› d�	«      S )z>Returns a simple inline HTML result page after OAuth callback.u   âœ…u   â�Œz#28a745z#dc3545a>  
    <!DOCTYPE html>
    <html>
    <head>
      <title>LinkedIn Connection</title>
      <meta charset="utf-8">
      <style>
        body { font-family: 'Segoe UI', sans-serif; background: #0d1117;
               color: #c9d1d9; display: flex; align-items: center;
               justify-content: center; height: 100vh; margin: 0; }
        .card { background: #161b22; border: 1px solid #30363d;
                 border-radius: 16px; padding: 40px 50px; text-align: center;
                 max-width: 500px; box-shadow: 0 8px 32px rgba(0,0,0,0.5); }
        h2 { color: af  ; font-size: 1.8rem; margin-bottom: 10px; }
        p  { color: #8b949e; margin-bottom: 24px; }
        a  { background: #0077b5; color: white; text-decoration: none;
               padding: 12px 28px; border-radius: 8px; font-weight: 600; }
        a:hover { background: #005582; }
      </style>
    </head>
    <body>
      <div class="card">
        <h2>ú ÚSuccessÚFailedz</h2>
        <p>ue   </p>
        <a href="/dashboard">â†� Back to Dashboard</a>
      </div>
    </body>
    </html>
    )r   )r#   r$   ÚiconÚcolors       r   r=   r=      sX   € á‰5 €DÙ ‰I i€EÜð ð �Wð 	ð ˆF�!¡‘I¨hÐ7ð 8Øˆ9ð ð/ó 	ð 	r   ) Ú__doc__rH   rE   r>   Úfastapir   r   r   Úfastapi.responsesr   r   Úsqlalchemy.ormr   Ú
app.configr	   Úapp.databaser
   Úapp.models.api_keysr   Úapp.auth.jwt_handlerr   Ú
app.cryptor   Úrouterr   rA   r<   r   r\   ÚboolrD   r=   © r   r   ú<module>rp      sÅ   ðñó Û Û ß /Ñ /ß <Ý "å Ý Ý /Ý 5Ý $á	˜+Ð-=Ð,>Ô	?€àEÐ ØDÐ ð ‡�ˆHÓð7˜Gò 7ó ð7ð  ‡�ˆK¨€Ó5Ù<CÀF»Oñ O Wð O°'ò Oó 6ðOðd 	˜$ð  	¨ð  	°ô  	r   