项目作者: enodari

项目描述 :
HMAC-SHA256 expiring tokens
高级语言: Python
项目地址: git://github.com/enodari/dwt.git
创建时间: 2019-02-27T15:16:17Z
项目社区:https://github.com/enodari/dwt

开源协议:MIT License

下载


dwt

Generate and check HMAC-SHA256 expiring tokens.

It’s similar to JWT, but:

  • HS256 only.
  • no header.
  • no payload, only a short expiration time.

So why I made it? It was fun. It’s not meant to be used in a production environment.

Installation

  1. $ git clone https://github.com/enodari/dwt.git && cd dwt/
  2. $ pip install -e .

Usage Example

  1. import dwt
  2. KEY = 'you-should-use-a-long-key'
  3. token = dwt.issue(KEY, ttl=5)
  4. dwt.check(KEY, token)

Remember to always use a strong key (256-bit minimum).

SSO demo

You can use dwt to issue and check tokens for a simple, single-user (clearly not production ready) SSO client/server.

  1. import dwt
  2. from flask import Flask, make_response, redirect, request
  3. KEY = 'you-should-use-a-long-key'
  4. app = Flask('app') # run this app on port 8000
  5. sso = Flask('sso') # run this app on port 5000
  6. @app.route('/')
  7. def hello_world():
  8. token = request.cookies.get('token')
  9. if not dwt.check(KEY, token):
  10. return redirect('http://127.0.0.1:5000/?next=http://127.0.0.1:8000')
  11. return 'Hello, World!'
  12. @sso.route('/', methods=['GET', 'POST'])
  13. def login_page():
  14. if request.method == 'POST':
  15. if request.form.get('pass', '') == 'TEST':
  16. response = make_response(redirect(request.form.get('next', '')))
  17. response.set_cookie('token', dwt.issue(KEY, ttl=60)) # expires in a minute
  18. return response
  19. return '''<form method=POST>
  20. <input type=password name=pass>
  21. <input type=hidden name=next value={}>
  22. <input type=submit value=go>
  23. </form>'''.format(request.args.get('next'))

License

MIT