@@ -43,15 +43,14 @@ class AzureAuthState(rx.State):
4343
4444 access_token : str = rx .Cookie ()
4545 id_token : str = rx .Cookie ()
46-
47- app_state : str
48- code_verifier : str
49- nonce : str
50- redirect_to_url : str
5146 error_message : str
52-
5347 is_iframed : bool = False
48+
49+ _redirect_to_url : str
50+ _app_state : str
51+ _code_verifier : str
5452 _requested_scopes : str = "openid email profile"
53+ _nonce : str
5554 _expected_at_hash : str | None = None
5655
5756 async def _validate_tokens (self , expiration_only : bool = False ) -> bool :
@@ -83,9 +82,9 @@ async def _validate_tokens(self, expiration_only: bool = False) -> bool:
8382 # validate nonce
8483 try :
8584 if (
86- hasattr (self , "nonce " )
85+ hasattr (self , "_nonce " )
8786 and id_claims .get ("nonce" )
88- and id_claims .get ("nonce" ) != self .nonce
87+ and id_claims .get ("nonce" ) != self ._nonce
8988 ):
9089 print ("Nonce mismatch" ) # noqa: T201
9190 return False
@@ -182,25 +181,25 @@ async def redirect_to_login(self):
182181 ]
183182
184183 # store app state and code verifier in session
185- self .app_state = secrets .token_urlsafe (64 )
186- self .code_verifier = secrets .token_urlsafe (64 )
187- self .redirect_to_url = self .router .url
184+ self ._app_state = secrets .token_urlsafe (64 )
185+ self ._code_verifier = secrets .token_urlsafe (64 )
186+ self ._redirect_to_url = self .router .url
188187
189188 # calculate code challenge
190- hashed = hashlib .sha256 (self .code_verifier .encode ("ascii" )).digest ()
189+ hashed = hashlib .sha256 (self ._code_verifier .encode ("ascii" )).digest ()
191190 encoded = base64 .urlsafe_b64encode (hashed )
192191 code_challenge = encoded .decode ("ascii" ).strip ("=" )
193192
194193 # store nonce for ID token validation
195- self .nonce = secrets .token_urlsafe (48 )
194+ self ._nonce = secrets .token_urlsafe (48 )
196195
197196 # get request params
198197 query_params = {
199198 "client_id" : client_id (),
200199 "redirect_uri" : self ._redirect_uri (),
201200 "scope" : self ._requested_scopes ,
202- "state" : self .app_state ,
203- "nonce" : self .nonce ,
201+ "state" : self ._app_state ,
202+ "nonce" : self ._nonce ,
204203 "code_challenge" : code_challenge ,
205204 "code_challenge_method" : "S256" ,
206205 "response_type" : "code" ,
@@ -221,13 +220,13 @@ async def redirect_to_logout(self):
221220 return type (self ).redirect_to_logout_popup ()
222221
223222 # store app state in session
224- self .app_state = secrets .token_urlsafe (64 )
223+ self ._app_state = secrets .token_urlsafe (64 )
225224
226225 # get request params
227226 query_params = {
228227 "id_token_hint" : self .id_token ,
229228 "post_logout_redirect_uri" : self ._index_uri (),
230- "state" : self .app_state ,
229+ "state" : self ._app_state ,
231230 }
232231
233232 # build request_uri
@@ -245,7 +244,7 @@ async def auth_callback(self):
245244 headers = {"Content-Type" : "application/x-www-form-urlencoded" }
246245 code = self .router .page .params .get ("code" )
247246 app_state = self .router .page .params .get ("state" )
248- if app_state != self .app_state :
247+ if app_state != self ._app_state :
249248 self .error_message = "App state mismatch. Possible CSRF attack."
250249 return rx .toast .error ("Authentication error" )
251250 if not code :
@@ -255,7 +254,7 @@ async def auth_callback(self):
255254 "grant_type" : "authorization_code" ,
256255 "code" : code ,
257256 "redirect_uri" : self ._redirect_uri (),
258- "code_verifier" : self .code_verifier ,
257+ "code_verifier" : self ._code_verifier ,
259258 }
260259 async with httpx .AsyncClient () as client :
261260 resp = await client .post (
@@ -275,7 +274,7 @@ async def auth_callback(self):
275274 id_token = exchange ["id_token" ],
276275 )
277276
278- return rx .redirect (self .redirect_to_url )
277+ return rx .redirect (self ._redirect_to_url )
279278
280279 async def _set_tokens (self , access_token : str , id_token : str ):
281280 self .access_token = access_token
@@ -337,7 +336,7 @@ async def on_iframe_auth_success(self, event: WindowMessage):
337336 """
338337 if event ["data" ].get ("type" ) != "auth" :
339338 return
340- self .nonce = event ["data" ].get ("nonce" , self .nonce )
339+ self ._nonce = event ["data" ].get ("nonce" , self ._nonce )
341340 await self ._set_tokens (
342341 access_token = event ["data" ].get ("access_token" ),
343342 id_token = event ["data" ].get ("id_token" ),
@@ -354,6 +353,6 @@ def post_auth_message(self):
354353 "type" : "auth" ,
355354 "access_token" : self .access_token ,
356355 "id_token" : self .id_token ,
357- "nonce" : self .nonce ,
356+ "nonce" : self ._nonce ,
358357 }
359358 return rx .call_script (POST_MESSAGE_AND_CLOSE_POPUP (payload , self .origin , 500 ))
0 commit comments