Skip to content

Commit 1239708

Browse files
committed
fixes #580
1 parent 80ffd13 commit 1239708

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

fasthtml/_modidx.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@
149149
'fasthtml/oauth.py'),
150150
'fasthtml.oauth.OAuth': ('api/oauth.html#oauth', 'fasthtml/oauth.py'),
151151
'fasthtml.oauth.OAuth.__init__': ('api/oauth.html#oauth.__init__', 'fasthtml/oauth.py'),
152-
'fasthtml.oauth.OAuth._chk_auth': ('api/oauth.html#oauth._chk_auth', 'fasthtml/oauth.py'),
153-
'fasthtml.oauth.OAuth.chk_auth': ('api/oauth.html#oauth.chk_auth', 'fasthtml/oauth.py'),
154-
'fasthtml.oauth.OAuth.login': ('api/oauth.html#oauth.login', 'fasthtml/oauth.py'),
152+
'fasthtml.oauth.OAuth.check_invalid': ('api/oauth.html#oauth.check_invalid', 'fasthtml/oauth.py'),
153+
'fasthtml.oauth.OAuth.get_auth': ('api/oauth.html#oauth.get_auth', 'fasthtml/oauth.py'),
155154
'fasthtml.oauth.OAuth.login_link': ('api/oauth.html#oauth.login_link', 'fasthtml/oauth.py'),
156155
'fasthtml.oauth.OAuth.logout': ('api/oauth.html#oauth.logout', 'fasthtml/oauth.py'),
156+
'fasthtml.oauth.OAuth.redir_login': ('api/oauth.html#oauth.redir_login', 'fasthtml/oauth.py'),
157157
'fasthtml.oauth.OAuth.redir_url': ('api/oauth.html#oauth.redir_url', 'fasthtml/oauth.py'),
158158
'fasthtml.oauth.WebApplicationClient.login_link': ( 'api/oauth.html#webapplicationclient.login_link',
159159
'fasthtml/oauth.py'),

fasthtml/oauth.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ def __init__(self, app, cli, skip=None, redir_path='/redirect', error_path='/err
142142
if not skip: skip = [redir_path,error_path,login_path]
143143
store_attr()
144144
def before(req, session):
145-
token = session.pop('token', None)
146145
auth = req.scope['auth'] = session.get('auth')
147-
if not auth: return RedirectResponse(self.login_path, status_code=303)
148-
146+
if not auth: return self.redir_login(session)
147+
res = self.check_invalid(req, session, auth)
148+
if res: return res
149149
app.before.append(Beforeware(before, skip=skip))
150150

151151
@app.get(redir_path)
@@ -154,25 +154,24 @@ def redirect(req, session, code:str=None, error:str=None, state:str=None):
154154
scheme = 'http' if url_match(req.url,self.http_patterns) or not self.https else 'https'
155155
base_url = f"{scheme}://{req.url.netloc}"
156156
info = AttrDictDefault(cli.retr_info(code, base_url+redir_path))
157-
if not self._chk_auth(req, info, session): return RedirectResponse(self.login_path, status_code=303)
158-
return self.login(info, state, session=session)
157+
ident = info.get(self.cli.id_key)
158+
if not ident: return self.redir_login(session)
159+
res = self.get_auth(info, ident, session, state)
160+
if not res: return self.redir_login(session)
161+
req.scope['auth'] = session['auth'] = ident
162+
return res
159163

160164
@app.get(logout_path)
161165
def logout(session):
162166
session.pop('auth', None)
163167
return self.logout(session)
164168

169+
def redir_login(self, session): return RedirectResponse(self.login_path, status_code=303)
165170
def redir_url(self, req):
166171
scheme = 'http' if url_match(req.url,self.http_patterns) or not self.https else 'https'
167172
return redir_url(req, self.redir_path, scheme)
168173

169174
def login_link(self, req, scope=None, state=None): return self.cli.login_link(self.redir_url(req), scope=scope, state=state)
170-
171-
def login(self, info, state, session): raise NotImplementedError()
172-
def logout(self, session): return RedirectResponse(self.login_path, status_code=303)
173-
def chk_auth(self, info, ident, session): raise NotImplementedError()
174-
def _chk_auth(self, req, info, session):
175-
ident = info.get(self.cli.id_key)
176-
if not ident and self.chk_auth(info, ident, session): return print('failed', info)
177-
req.scope['auth'] = session['auth'] = ident
178-
return True
175+
def check_invalid(self, req, session, auth): return False
176+
def logout(self, session): return self.redir_login(session)
177+
def get_auth(self, info, ident, session, state): raise NotImplementedError()

nbs/api/08_oauth.ipynb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@
421421
" if not skip: skip = [redir_path,error_path,login_path]\n",
422422
" store_attr()\n",
423423
" def before(req, session):\n",
424-
" token = session.pop('token', None)\n",
425424
" auth = req.scope['auth'] = session.get('auth')\n",
426-
" if not auth: return RedirectResponse(self.login_path, status_code=303)\n",
427-
"\n",
425+
" if not auth: return self.redir_login(session)\n",
426+
" res = self.check_invalid(req, session, auth)\n",
427+
" if res: return res\n",
428428
" app.before.append(Beforeware(before, skip=skip))\n",
429429
"\n",
430430
" @app.get(redir_path)\n",
@@ -433,28 +433,27 @@
433433
" scheme = 'http' if url_match(req.url,self.http_patterns) or not self.https else 'https'\n",
434434
" base_url = f\"{scheme}://{req.url.netloc}\"\n",
435435
" info = AttrDictDefault(cli.retr_info(code, base_url+redir_path))\n",
436-
" if not self._chk_auth(req, info, session): return RedirectResponse(self.login_path, status_code=303)\n",
437-
" return self.login(info, state, session=session)\n",
436+
" ident = info.get(self.cli.id_key)\n",
437+
" if not ident: return self.redir_login(session)\n",
438+
" res = self.get_auth(info, ident, session, state)\n",
439+
" if not res: return self.redir_login(session)\n",
440+
" req.scope['auth'] = session['auth'] = ident\n",
441+
" return res\n",
438442
"\n",
439443
" @app.get(logout_path)\n",
440444
" def logout(session):\n",
441445
" session.pop('auth', None)\n",
442446
" return self.logout(session)\n",
443447
"\n",
448+
" def redir_login(self, session): return RedirectResponse(self.login_path, status_code=303)\n",
444449
" def redir_url(self, req):\n",
445450
" scheme = 'http' if url_match(req.url,self.http_patterns) or not self.https else 'https'\n",
446451
" return redir_url(req, self.redir_path, scheme)\n",
447452
"\n",
448453
" def login_link(self, req, scope=None, state=None): return self.cli.login_link(self.redir_url(req), scope=scope, state=state)\n",
449-
"\n",
450-
" def login(self, info, state, session): raise NotImplementedError()\n",
451-
" def logout(self, session): return RedirectResponse(self.login_path, status_code=303)\n",
452-
" def chk_auth(self, info, ident, session): raise NotImplementedError()\n",
453-
" def _chk_auth(self, req, info, session):\n",
454-
" ident = info.get(self.cli.id_key)\n",
455-
" if not ident and self.chk_auth(info, ident, session): return print('failed', info)\n",
456-
" req.scope['auth'] = session['auth'] = ident\n",
457-
" return True"
454+
" def check_invalid(self, req, session, auth): return False\n",
455+
" def logout(self, session): return self.redir_login(session)\n",
456+
" def get_auth(self, info, ident, session, state): raise NotImplementedError()"
458457
]
459458
},
460459
{

0 commit comments

Comments
 (0)