Codebase list python-flask-httpauth / fresh-releases/main
New upstream release. Debian Janitor 11 months ago
11 changed file(s) with 64 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
2121 strategy:
2222 matrix:
2323 os: [ubuntu-latest, macos-latest, windows-latest]
24 python: ['3.6', '3.7', '3.8', '3.9', '3.10', 'pypy-3.8']
24 python: ['3.8', '3.9', '3.10', '3.11', 'pypy-3.8']
25 flask: ['flask<2.3', 'flask>=2.3']
2526 fail-fast: false
2627 runs-on: ${{ matrix.os }}
2728 steps:
3233 - run: python -m pip install --upgrade pip wheel
3334 - run: pip install tox tox-gh-actions
3435 - run: tox
36 env:
37 FLASK_VERSION: ${{ matrix.flask }}
3538 coverage:
3639 name: coverage
3740 runs-on: ubuntu-latest
3841 steps:
39 - uses: actions/checkout@v2
40 - uses: actions/setup-python@v2
42 - uses: actions/checkout@v3
43 - uses: actions/setup-python@v3
4144 - run: python -m pip install --upgrade pip wheel
42 - run: pip install tox tox-gh-actions codecov
45 - run: pip install tox tox-gh-actions
4346 - run: tox
44 - run: codecov
47 - uses: codecov/codecov-action@v3
48 with:
49 files: ./coverage.xml
50 fail_ci_if_error: true
00 # Flask-HTTPAuth change log
1
2 **Release 4.8.0** - 2023-04-27
3
4 - Changes to handle breaking changes in Flask/Werkzeug 2.3 [#160](https://github.com/miguelgrinberg/flask-httpauth/issues/160) ([commit](https://github.com/miguelgrinberg/flask-httpauth/commit/76548398847fcedc8421a8f4b4c2d12ee16270fe))
5 - Remove Python 3.6 and 3.7 from builds, add Python 3.11 ([commit](https://github.com/miguelgrinberg/flask-httpauth/commit/04399bd2e121f8f0bff34360af74978920ba9096))
6 - Replace itsdangerous with pyjwt in examples [#157](https://github.com/miguelgrinberg/flask-httpauth/issues/157) ([commit](https://github.com/miguelgrinberg/flask-httpauth/commit/6f708b09f4d54e48ef1a3501c63867ffb1789077))
7 - Better documentation for the `get_user_roles` callback argument [#152](https://github.com/miguelgrinberg/flask-httpauth/issues/152) ([commit](https://github.com/miguelgrinberg/flask-httpauth/commit/03ff9443114ac57cae34dcf0aae39e89985529b0)) (thanks **Taranjeet Singh**!)
18
29 **Release 4.7.0** - 2022-05-29
310
0 python-flask-httpauth (4.7.0-1) UNRELEASED; urgency=low
0 python-flask-httpauth (4.8.0-1) UNRELEASED; urgency=low
11
22 * New upstream release.
3 * New upstream release.
34
4 -- Debian Janitor <janitor@jelmer.uk> Sat, 31 Dec 2022 00:20:44 -0000
5 -- Debian Janitor <janitor@jelmer.uk> Fri, 19 May 2023 23:57:37 -0000
56
67 python-flask-httpauth (4.5.0-4) unstable; urgency=medium
78
212212
213213 .. method:: get_user_roles(roles_callback)
214214
215 If defined, this callback function will be called by the framework to obtain the roles assigned to a given user. The callback function takes a single argument, the user for which roles are requested. The user object passed to this function will be the one returned by the ``verify_callback`` function. The function should return the role or list of roles that belong to the user. Example::
215 If defined, this callback function will be called by the framework to obtain the roles assigned to a given user. The callback function takes a single argument, the user for which roles are requested. The user object passed to this function will be the one returned by the "verify" callback. If the verify callback returned ``True`` instead of a user object, then the ``Authorization`` object provided by Flask will be passed to this function. The function should return the role or list of roles that belong to the user. Example::
216216
217217 @auth.get_user_roles
218218 def get_user_roles(user):
55
66 The root URL for this application can be accessed via basic auth, providing
77 username and password, or via token auth, providing a bearer JWS token.
8
9 This example requires the PyJWT package to be installed.
810 """
11 from time import time
912 from flask import Flask
1013 from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth, MultiAuth
1114 from werkzeug.security import generate_password_hash, check_password_hash
12 from itsdangerous import TimedJSONWebSignatureSerializer as JWS
15 import jwt
1316
1417
1518 app = Flask(__name__)
1619 app.config['SECRET_KEY'] = 'top secret!'
17 jws = JWS(app.config['SECRET_KEY'], expires_in=3600)
1820
1921 basic_auth = HTTPBasicAuth()
2022 token_auth = HTTPTokenAuth('Bearer')
2729 }
2830
2931 for user in users.keys():
30 token = jws.dumps({'username': user})
32 token = jwt.encode({'username': user, 'exp': int(time()) + 3600},
33 app.config['SECRET_KEY'], algorithm='HS256')
3134 print('*** token for {}: {}\n'.format(user, token))
3235
3336
4144 @token_auth.verify_token
4245 def verify_token(token):
4346 try:
44 data = jws.loads(token)
47 data = jwt.decode(token, app.config['SECRET_KEY'],
48 algorithms=['HS256'])
4549 except: # noqa: E722
4650 return False
4751 if 'username' in data:
11 """Token authentication example
22
33 This example demonstrates how to protect Flask endpoints with token
4 authentication, using tokens.
4 authentication, using JWT tokens. To use this example you need to install the
5 PyJWT library:
6
7 pip install pyjwt
58
69 When this application starts, a token is generated for each of the two users.
710 To gain access, you can use a command line HTTP client such as curl, passing
912
1013 curl -X GET -H "Authorization: Bearer <jws-token>" http://localhost:5000/
1114
12 The response should include the username, which is obtained from the token.
15 The response should include the username, which is obtained from the token. The
16 tokens have a validity time of one hour, after which they will be rejected.
1317 """
18 from time import time
1419 from flask import Flask
1520 from flask_httpauth import HTTPTokenAuth
16 from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
21 import jwt
1722
1823
1924 app = Flask(__name__)
2025 app.config['SECRET_KEY'] = 'top secret!'
21 token_serializer = Serializer(app.config['SECRET_KEY'], expires_in=3600)
2226
2327 auth = HTTPTokenAuth('Bearer')
2428
2529
2630 users = ['john', 'susan']
2731 for user in users:
28 token = token_serializer.dumps({'username': user}).decode('utf-8')
32 token = jwt.encode({'username': user, 'exp': int(time()) + 3600},
33 app.config['SECRET_KEY'], algorithm='HS256')
2934 print('*** token for {}: {}\n'.format(user, token))
3035
3136
3237 @auth.verify_token
3338 def verify_token(token):
3439 try:
35 data = token_serializer.loads(token)
40 data = jwt.decode(token, app.config['SECRET_KEY'],
41 algorithms=['HS256'])
3642 except: # noqa: E722
3743 return False
3844 if 'username' in data:
00 [metadata]
11 name = Flask-HTTPAuth
2 version = 4.7.0
2 version = 4.8.0
33 author = Miguel Grinberg
44 author_email = miguel.grinberg@gmail.com
55 description = HTTP authentication for Flask routes
7575 auth = None
7676 if self.header is None or self.header == 'Authorization':
7777 auth = request.authorization
78 if auth is None and 'Authorization' in request.headers:
79 # Flask/Werkzeug do not recognize any authentication types
80 # other than Basic or Digest, so here we parse the header by
81 # hand
78 if auth is None and \
79 'Authorization' in request.headers: # pragma: no cover
80 # Flask/Werkzeug versions before 2.3 do not recognize any
81 # authentication types other than Basic or Digest, so here we
82 # parse the header by hand
8283 try:
8384 auth_type, token = request.headers['Authorization'].split(
8485 None, 1)
85 auth = Authorization(auth_type, {'token': token})
86 auth = Authorization(auth_type)
87 auth.token = token
8688 except (ValueError, KeyError):
8789 # The Authorization header is either empty or has no token
8890 pass
8991 elif self.header in request.headers:
9092 # using a custom header, so the entire value of the header is
9193 # assumed to be a token
92 auth = Authorization(self.scheme,
93 {'token': request.headers[self.header]})
94 auth = Authorization(self.scheme)
95 auth.token = request.headers[self.header]
9496
9597 # if the auth type does not match, we act as if there is no auth
9698 # this is better than failing directly, as it allows the callback
354356 or not auth.nonce or not auth.response \
355357 or not stored_password_or_ha1:
356358 return False
357 if not(self.verify_nonce_callback(auth.nonce)) or \
358 not(self.verify_opaque_callback(auth.opaque)):
359 if not self.verify_nonce_callback(auth.nonce) or \
360 not self.verify_opaque_callback(auth.opaque):
359361 return False
360362 if auth.qop and auth.qop not in self.qop: # pragma: no cover
361363 return False
390392 return f
391393
392394 def authenticate(self, auth, stored_password):
393 if auth:
394 token = auth['token']
395 else:
396 token = ""
395 token = getattr(auth, 'token', '')
397396 if self.verify_token_callback:
398397 return self.ensure_sync(self.verify_token_callback)(token)
399398
2929
3030 @token_auth.get_user_roles
3131 def get_token_role(auth):
32 if auth['token'] == 'this-is-the-token!':
32 if auth.token == 'this-is-the-token!':
3333 return 'foo'
3434 return
3535
4343
4444 @custom_token_auth.get_user_roles
4545 def get_custom_token_role(auth):
46 if auth['token'] == 'this-is-the-custom-token!':
46 if auth.token == 'this-is-the-custom-token!':
4747 return 'foo'
4848 return
4949
3232
3333 @token_auth.get_user_roles
3434 async def get_token_role(auth):
35 if auth['token'] == 'this-is-the-token!':
35 if auth.token == 'this-is-the-token!':
3636 return 'foo'
3737 return
3838
4646
4747 @custom_token_auth.get_user_roles
4848 async def get_custom_token_role(auth):
49 if auth['token'] == 'this-is-the-custom-token!':
49 if auth.token == 'this-is-the-custom-token!':
5050 return 'foo'
5151 return
5252
00 [tox]
1 envlist=flake8,py36,py37,py38,py39,py310pypy3,docs
1 envlist=flake8,py37,py38,py39,py310,py311,pypy3,docs
22 skip_missing_interpreters=True
33
44 [gh-actions]
55 python =
6 3.6: py36
76 3.7: py37
87 3.8: py38
98 3.9: py39
109 3.10: py310
10 3.11: py311
1111 pypy-3: pypy3
1212
1313 [testenv]
1414 commands=
1515 pip install -e .
16 pytest -p no:logging --cov=src --cov-branch --cov-report=term-missing
16 pip install {env:FLASK_VERSION:flask>=2.3}
17 pytest -p no:logging --cov=src --cov-branch --cov-report=term-missing --cov-report=xml
1718 deps=
1819 asgiref
1920 pytest
2930 changedir=docs
3031 deps=
3132 sphinx
32 whitelist_externals=
33 allowlist_externals=
3334 make
3435 commands=
3536 make html