pyx / sanic-auth Goto Github PK
View Code? Open in Web Editor NEWSanic-Auth - Simple Authentication for Sanic
License: Other
Sanic-Auth - Simple Authentication for Sanic
License: Other
Hello,
I'm organizing my project into many different Blueprints(the routes), and all the routes(ENDPOINTS) can be accessed only after auth is done. I'm trying to utilize sanic-auth to help but just found the example given in the doc seems only applied to a centralized route file, say main.py. How sanic-auth could be integrated to the projects as mine which is composed of different Blueprints? Thank you!
When i try to access route with @login_required and it takes me to the auth page but after login it takes me back to route but without tag, so it throws 404 error. When i try to access the route with tag via url it makes me enter password again and i get stuck
The json mechanism you use right now is failing when the objects are BSON encoded python objects from MongoDB.
Ultimately it seems like Pickle would be a better solution and remove any issues with use-cases ...
On top of that it would be great if the entire user object was available on the session, sanic-auth didn't look for specific field names, and went about this in a more generic way since we can't always assume people can or will code their DB/User models to match.
thanks!
-k. noah
Is this easy to do with sanic-auth? The only examples I'm seeing are in-memory dictionaries that get blasted on server restart.
Thanks!
Is this a desired behaviour?
Is the Flask-login like "Remember me" functionality supported in Sanic-auth?
[2022-04-21 22:44:38 +0300] [15964] [ERROR] Exception occurred while handling uri: 'http://127.0.0.1:5000/login' Traceback (most recent call last): File "handle_request", line 83, in handle_request ) File "C:\Users\Beliboba\PycharmProjects\beliboba\main.py", line 63, in login auth.login_user(request, user) File "C:\Users\Beliboba\PycharmProjects\beliboba\venv\lib\site-packages\sanic_auth\__init__.py", line 47, in login_user self.get_session(request)[self.auth_session_key] = self.serialize(user) File "C:\Users\Beliboba\PycharmProjects\beliboba\venv\lib\site-packages\sanic_auth\__init__.py", line 118, in serialize return {'uid': user.id, 'name': user.name} AttributeError: 'str' object has no attribute 'id' test test [2022-04-21 22:44:38 +0300] - (sanic.access)[INFO][127.0.0.1:53425]: POST http://127.0.0.1:5000/login 500 799
What do i need to do? User is stored in sqlite3 database
I haven't read the role of "user_keyword" for a long time.Can you help me with your answer?
Sanic lets us use Decorators in Class based Views, this way. So, if my views are in views.py
, how can I access the auth.decorator_name
in those Classes?
Am having trouble importing the auth
defined in my __init__.py
, or am I doing it wrong.
__init__.py
:
.
.
app = Sanic(__name__)
app.config.AUTH_LOGIN_ENDPOINT = 'login'
.
.
auth = Auth(app)
@auth.serializer
def serializer(user):
return {'id':str(user.id),'email':user.email,'name':user.username}
@auth.user_loader
def user_loader(user):
return user
app.add_route(Login.as_view(),'/login')
app.add_route(Register.as_view(),'/signup')
views.py
:
class Dashboard(HTTPMethodView):
# decorators = [auth.login_required]
async def dashboard(self, request):
pass
I use this examples:
one browser ( chrome ) logged in,
then visit '/', found all other clients (firefox or curl) was loged in.
$ curl -sv 'http://localhost:8004'
* Connected to localhost (127.0.0.1) port 8004 (#0)
> GET / HTTP/1.1
> Host: localhost:8004
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Length: 48
< Content-Type: text/html; charset=utf-8
< Connection: keep-alive
< Keep-Alive: 5
<
* Connection #0 to host localhost left intact
<a href="/logout">Logout</a><p>Welcome, demo</p>
Is this the expected effect?
Am I miss some thing?
tested on ubuntu 20.04
with
Not sure when this broke, but with sanic 20.3 and sanic_session 0.7 I'm getting
2020-05-30T17:40:47.800 ERROR handlers.py#140 Exception occurred while handling uri: 'http://192.168.20.55/'
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/sanic/app.py", line 973, in handle_request
response = await response
File "/usr/lib/python3.8/site-packages/sanic_auth/__init__.py", line 93, in privileged
user = self.current_user(request)
File "/usr/lib/python3.8/site-packages/sanic_auth/__init__.py", line 61, in current_user
token = self.get_session(request).get(self.auth_session_key, None)
File "/usr/lib/python3.8/site-packages/sanic_auth/__init__.py", line 143, in get_session
return request[self.session_name]
TypeError: 'Request' object is not subscriptable
How to use sanic-auth for user authentication of blueprints registered in the app
[2022-04-25 15:58:52 +0300] [8448] [ERROR] Exception occurred while handling uri: 'http://127.0.0.1:5000/login'
Traceback (most recent call last):
File "handle_request", line 83, in handle_request
)
File "C:\Users\Beliboba\PycharmProjects\beliboba\main.py", line 64, in login
auth.login_user(request, user=user)
File "C:\Users\Beliboba\PycharmProjects\beliboba\venv\lib\site-packages\sanic_auth_init_.py", line 47, in login_user
self.get_session(request)[self.auth_session_key] = self.serialize(user)
File "C:\Users\Beliboba\PycharmProjects\beliboba\venv\lib\site-packages\sanic_auth_init_.py", line 143, in get_session
return request.ctx.session
AttributeError: 'list' object has no attribute 'ctx'
Hi pyx,
I have another issue want your help. I'm trying to use multiple workers(app.run(host="0.0.0.0", port=8000,workers=2,debug=True)) for the my app and using sanic-auth for user authentication, but encountering some issue, it requires me multiple several times login. When I change it to a single worker(host="0.0.0.0", port=8000,debug=True)), the problem solves. Any suggestion on that? Thank you!
2017-06-01 16:56:42 - (sanic)[ERROR]: Exception occurred while handling uri: "http://127.0.0.1:8000/setup"
Traceback (most recent call last):
File "/home/test/sanic/lib/python3.6/site-packages/sanic/app.py", line 471, in handle_request
response = await response
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 109, in __next__
return self.gen.send(None)
File "/home/test/PycharmProjects/Sanic-MDL-Blog/app/views.py", line 104, in setup
auth.login_user(request, wform.username.data)
File "/home/test/sanic/lib/python3.6/site-packages/sanic_auth/__init__.py", line 47, in login_user
self.get_session(request)[self.auth_session_key] = self.serialize(user)
File "/home/test/sanic/lib/python3.6/site-packages/sanic_auth/__init__.py", line 102, in serialize
return {'uid': user.id, 'name': user.name}
AttributeError: 'str' object has no attribute 'id'
and
2017-06-01 17:10:17 - (sanic)[ERROR]: Traceback (most recent call last):
File "/home/test/sanic/lib/python3.6/site-packages/sanic/app.py", line 471, in handle_request
response = await response
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 109, in __next__
return self.gen.send(None)
File "/home/test/sanic/lib/python3.6/site-packages/sanic_auth/__init__.py", line 87, in privileged
u = self.login_url or request.app.url_for(self.login_endpoint)
File "/home/test/sanic/lib/python3.6/site-packages/sanic/app.py", line 357, in url_for
view_name))
sanic.exceptions.URLBuildError: Endpoint with name `auth.login` was not found
doesnt play well with sanic_session while assigning user to request.ctx.session in the middleware.
user is not saved and manually saving is not working since auth takes over the session and user data is limited by this module.
have to fetch user data from the db by using the uid from the _auth and this makes two calls to the db.
im using MongoDBSessionInterface of the sanic_session
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.