Total Complexity | 3 |
Total Lines | 24 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from flask import current_app, g |
||
2 | from flask_httpauth import HTTPBasicAuth |
||
3 | from .models import User |
||
4 | from .errors import unauthorized |
||
5 | |||
6 | auth = HTTPBasicAuth() |
||
7 | |||
8 | |||
9 | @auth.verify_password |
||
10 | def verify_password(username_or_token, password): |
||
11 | if current_app.config['USE_TOKEN_AUTH']: |
||
12 | # token authentication |
||
13 | g.user = User.verify_auth_token(username_or_token) |
||
14 | return g.user is not None |
||
15 | else: |
||
16 | # username/password authentication |
||
17 | g.user = User.query.filter_by(username=username_or_token).first() |
||
18 | return g.user is not None and g.user.verify_password(password) |
||
19 | |||
20 | |||
21 | @auth.error_handler |
||
22 | def unauthorized_error(): |
||
23 | return unauthorized('Please authenticate to access this API') |
||
24 |