File tree Expand file tree Collapse file tree 3 files changed +33
-24
lines changed
jupyterlab_leetcode/handlers Expand file tree Collapse file tree 3 files changed +33
-24
lines changed Original file line number Diff line number Diff line change 11import json
2+ import time
3+ from http .cookiejar import Cookie
4+ from typing import cast
25
36import browser_cookie3
47import tornado
2124
2225
2326class GetCookieHandler (BaseHandler ):
24- route = r"cookies/[a-zA-Z0-9_-]+ "
27+ route = r"cookies"
2528
2629 @tornado .web .authenticated
2730 def get (self ):
@@ -38,17 +41,26 @@ def get(self):
3841 return
3942
4043 cj = BROWSER_COOKIE_METHOD_MAP [browser ](domain_name = "leetcode.com" )
44+ cookie_session = next ((c for c in cj if c .name == "LEETCODE_SESSION" ), None )
45+ cookie_csrf = next ((c for c in cj if c .name == "csrftoken" ), None )
46+ exist = bool (cookie_session and cookie_csrf )
47+ expired = exist and (
48+ cast (Cookie , cookie_session ).is_expired ()
49+ or cast (Cookie , cookie_csrf ).is_expired ()
50+ )
51+
52+ resp = {"exist" : exist , "expired" : expired }
4153
42- self .finish (
43- json .dumps (
44- dict (
45- map (
46- lambda c : (
47- c .name ,
48- {"name" : c .name , "domain" : c .domain , "value" : c .value },
49- ),
50- cj ,
51- )
52- )
54+ if exist and not expired :
55+ cookie_session_expires = cast (Cookie , cookie_session ).expires
56+ max_age = (
57+ cookie_session_expires - int (time .time ())
58+ if cookie_session_expires is not None
59+ else 3600 * 24 * 14
5360 )
54- )
61+ self .add_header (
62+ "Set-Cookie" ,
63+ f"leetcode_browser={ browser } ; Path=/; HttpOnly; Max-Age={ max_age } " ,
64+ )
65+
66+ self .finish (json .dumps (resp ))
Original file line number Diff line number Diff line change @@ -44,23 +44,21 @@ const BrowserCookie = ({ onSuccess }: { onSuccess: () => void }) => {
4444 }
4545 } , [ checked , onSuccess ] ) ;
4646
47- const loadCookies = ( ) => {
47+ const checkCookie = ( ) => {
4848 // TODO: change alert
4949 if ( ! browser ) {
5050 alert ( 'Please select a browser.' ) ;
5151 return ;
5252 }
5353 if ( browser === 'safari' ) {
5454 alert (
55- 'Safari does not support loading cookies from the browser. Please use another browser.'
55+ 'Safari does not support getting cookies from the browser. Please use another browser.'
5656 ) ;
5757 return ;
5858 }
5959
60- getCookie ( 'all' , browser ) . then ( cookies => {
61- if ( cookies && cookies [ 'LEETCODE_SESSION' ] && cookies [ 'csrftoken' ] ) {
62- setChecked ( true ) ;
63- }
60+ getCookie ( browser ) . then ( cookies => {
61+ setChecked ( ! ! cookies [ 'exist' ] ) ;
6462 } ) ;
6563 } ;
6664
@@ -87,7 +85,7 @@ const BrowserCookie = ({ onSuccess }: { onSuccess: () => void }) => {
8785 </ option >
8886 ) ) }
8987 </ select >
90- < button onClick = { loadCookies } > Load </ button >
88+ < button onClick = { checkCookie } > Check </ button >
9189 < p > Checked: { checked ? 'Yes' : 'No' } </ p >
9290 </ div >
9391 ) ;
Original file line number Diff line number Diff line change 11import { requestAPI } from './handler' ;
22
33export async function getCookie (
4- name : string ,
54 browser : string
6- ) : Promise < { [ key : string ] : any } > {
7- return requestAPI < { [ key : string ] : any } > (
8- `/cookies/ ${ name } ?browser=${ browser } `
5+ ) : Promise < { [ key : string ] : boolean } > {
6+ return requestAPI < { [ key : string ] : boolean } > (
7+ `/cookies?browser=${ browser } `
98 ) . catch ( ( ) => ( { } ) ) ;
109}
You can’t perform that action at this time.
0 commit comments