项目作者: darkanthey

项目描述 :
OAuth 2.0 provider written in python. Can work without database.
高级语言: Python
项目地址: git://github.com/darkanthey/oauth2-stateless.git
创建时间: 2018-01-18T17:40:32Z
项目社区:https://github.com/darkanthey/oauth2-stateless

开源协议:MIT License

下载


Oauth2-stateless

Oauth2-stateless is a framework that aims at making it easy to provide authentication
via OAuth 2.0 within an application stack.
Main difference of this library is the simplicity
and the ability to work without any database just with ‘stateless’
tokens based on JWT JSON Web Tokens.

Documentation

Status

Travis Build Status
License

Oauth2-stateless has reached its beta phase. All main parts of the OAuth 2.0 RFC such as the various types of Grants, Refresh Token and Scopes have been implemented.

Installation

oauth2-stateless is available on PyPI

  1. pip install oauth2-stateless

Usage

Example Authorization server

  1. from wsgiref.simple_server import make_server
  2. import oauth2
  3. import oauth2.grant
  4. import oauth2.error
  5. from oauth2.store.memory import ClientStore
  6. from oauth2.store.stateless import Token Store
  7. import oauth2.tokengenerator
  8. import oauth2.web.wsgi
  9. # Create a SiteAdapter to interact with the user.
  10. # This can be used to display confirmation dialogs and the like.
  11. class ExampleSiteAdapter(oauth2.web.AuthorizationCodeGrantSiteAdapter, oauth2.web.ImplicitGrantSiteAdapter):
  12. TEMPLATE = '''
  13. <html>
  14. <body>
  15. <p>
  16. <a href="{url}&confirm=confirm">confirm</a>
  17. </p>
  18. <p>
  19. <a href="{url}&deny=deny">deny</a>
  20. </p>
  21. </body>
  22. </html>'''
  23. def authenticate(self, request, environ, scopes, client):
  24. # Check if the user has granted access
  25. example_user_id = 123
  26. example_ext_data = {}
  27. if request.post_param("confirm") == "confirm":
  28. return example_ext_data, example_user_id
  29. raise oauth2.error.UserNotAuthenticated
  30. def render_auth_page(self, request, response, environ, scopes, client):
  31. url = request.path + "?" + request.query_string
  32. response.body = self.TEMPLATE.format(url=url)
  33. return response
  34. def user_has_denied_access(self, request):
  35. # Check if the user has denied access
  36. if request.post_param("deny") == "deny":
  37. return True
  38. return False
  39. # Create an in-memory storage to store your client apps.
  40. client_store = ClientStore()
  41. # Add a client
  42. client_store.add_client(client_id="abc", client_secret="xyz", redirect_uris=["http://localhost/callback"])
  43. site_adapter = ExampleSiteAdapter()
  44. # Create an in-memory storage to store issued tokens.
  45. # LocalTokenStore can store access and auth tokens
  46. stateless_token = oauth2.tokengenerator.StatelessTokenGenerator(secret_key='xxx')
  47. token_store = TokenStore(stateless)
  48. # Create the controller.
  49. provider = oauth2.Provider(
  50. access_token_store=token_store,
  51. auth_code_store=token_store,
  52. client_store=client_store,
  53. token_generator=stateless_token)
  54. )
  55. # Add Grants you want to support
  56. provider.add_grant(oauth2.grant.AuthorizationCodeGrant(site_adapter=site_adapter))
  57. provider.add_grant(oauth2.grant.ImplicitGrant(site_adapter=site_adapter))
  58. # Add refresh token capability and set expiration time of access tokens to 30 days
  59. provider.add_grant(oauth2.grant.RefreshToken(expires_in=2592000))
  60. # Wrap the controller with the Wsgi adapter
  61. app = oauth2.web.wsgi.Application(provider=provider)
  62. if __name__ == "__main__":
  63. httpd = make_server('', 8080, app)
  64. httpd.serve_forever()

This example only shows how to instantiate the server.
It is not a working example as a client app is missing.
Take a look at the examples directory.

Or just run this example:

  1. python docs/examples/stateless_client_server.py

This is already a workable example. They can work without database
because oauth token already contain all the necessary information like
a user_id, grant_type, data, scopes and client_id.
If you want to check user state like a ban, disable, etc.
You can check this param on server site from database. By adding this check to
/api/me or redefine oauth2.tokengenerator and add specific logic.

Supported storage backends

Oauth2-stateless does not force you to use a specific database or you
can work without database with stateless token.

It currently supports these storage backends out-of-the-box:

  • MongoDB
  • MySQL
  • Redis
  • Memcached
  • Dynamodb

However, you are not not bound to these implementations.
By adhering to the interface defined by the base classes in oauth2.store,
you can easily add an implementation of your backend.
It also is possible to mix different backends and e.g. read data of a client
from MongoDB while saving all tokens in memcached for fast access.

Take a look at the examples in the examples directory of the project.

Site adapter

  • aiohttp
  • flask
  • tornado
  • uwsgi

Like for storage, oauth2-stateless does not define how you identify a
user or show a confirmation dialogue.
Instead your application should use the API defined by oauth2.web.SiteAdapter.

Contributors

DarkAnthey |
:—-:
|DarkAnthey|