api.auth   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A unauthorized_error() 0 3 1
A verify_password() 0 10 2
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