Total Complexity | 4 |
Total Lines | 30 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from flask import Blueprint, g |
||
2 | from flask_httpauth import HTTPBasicAuth |
||
3 | from .models import User |
||
4 | from .errors import unauthorized |
||
5 | from .decorators import no_cache, json |
||
6 | |||
7 | token = Blueprint('token', __name__) |
||
8 | token_auth = HTTPBasicAuth() |
||
9 | |||
10 | |||
11 | @token_auth.verify_password |
||
12 | def verify_password(username_or_token, password): |
||
13 | g.user = User.query.filter_by(username=username_or_token).first() |
||
14 | if not g.user: |
||
15 | return False |
||
16 | return g.user.verify_password(password) |
||
17 | |||
18 | |||
19 | @token_auth.error_handler |
||
20 | def unauthorized_error(): |
||
21 | return unauthorized('Please authenticate to access this API') |
||
22 | |||
23 | |||
24 | @token.route('/request-token') |
||
25 | @no_cache |
||
26 | @token_auth.login_required |
||
27 | @json |
||
28 | def request_token(): |
||
29 | return {'token': g.user.generate_auth_token()} |
||
30 |