-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexample.py
63 lines (45 loc) · 1.47 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""Example usage.
"""
from __future__ import unicode_literals
import bottle
from bottle_jwt import (JWTProviderPlugin, jwt_auth_required)
app = bottle.Bottle()
server_secret = '*Y*^%JHg7623'
class AuthBackend(object):
"""Implementing an auth backend class with at least two methods.
"""
user = {'id': 1237832, 'username': 'pav', 'password': '123', 'data': {'sex': 'male', 'active': True}}
def authenticate_user(self, username, password):
"""Authenticate User by username and password.
Returns:
A dict representing User Record or None.
"""
if username == self.user['username'] and password == self.user['password']:
return self.user
return None
def get_user(self, user_id):
"""Retrieve User By ID.
Returns:
A dict representing User Record or None.
"""
if user_id == self.user['id']:
return {k: self.user[k] for k in self.user if k != 'password'}
return None
provider_plugin = JWTProviderPlugin(
keyword='jwt',
auth_endpoint='/auth',
backend=AuthBackend(),
fields=('username', 'password'),
secret=server_secret,
ttl=30
)
app.install(provider_plugin)
@app.get('/')
@jwt_auth_required
def private_resource(jwt):
print(jwt)
return {"scope": "For your eyes only!", "user": bottle.request.get_user()}
bottle.debug(True)
bottle.run(
app=app, port=9092, host='0.0.0.0', reloader=True)