| 1 |
# |
| 2 |
# Bacula(R) - The Network Backup Solution |
| 3 |
# |
| 4 |
# Copyright (C) 2000-2022 Kern Sibbald |
| 5 |
# |
| 6 |
# The original author of Bacula is Kern Sibbald, with contributions |
| 7 |
# from many others, a complete list can be found in the file AUTHORS. |
| 8 |
# |
| 9 |
# You may use this file and others of this release according to the |
| 10 |
# license defined in the LICENSE file, which includes the Affero General |
| 11 |
# Public License, v3.0 ("AGPLv3") and some additional permissions and |
| 12 |
# terms pursuant to its AGPLv3 Section 7. |
| 13 |
# |
| 14 |
# This notice must be preserved when any source code is |
| 15 |
# conveyed and/or propagated. |
| 16 |
# |
| 17 |
# Bacula(R) is a registered trademark of Kern Sibbald. |
| 18 |
# |
| 19 |
# Copyright (c) 2019 by Inteos sp. z o.o. |
| 20 |
# All rights reserved. IP transfered to Bacula Systems according to agreement. |
| 21 |
# Author: Radosław Korzeniewski, [email protected], Inteos Sp. z o.o. |
| 22 |
# |
| 23 |
import logging |
| 24 |
import random |
| 25 |
import string |
| 26 |
|
| 27 |
TOKENSIZE = 64 |
| 28 |
|
| 29 |
|
| 30 |
def generate_token(size=TOKENSIZE, chars=tuple(string.ascii_letters) + tuple(string.digits)): |
| 31 |
""" |
| 32 |
Generates a random string of characters composed on letters and digits |
| 33 |
:param size: the number of characters to generate - the length of the token string, default is 64 |
| 34 |
:param chars: the allowed characters set, the default is ascii letters and digits |
| 35 |
:return: the token used for authorization |
| 36 |
""" |
| 37 |
return ''.join(random.choice(chars) for _ in range(size)) |
| 38 |
|
| 39 |
|
| 40 |
def create_auth_data(token): |
| 41 |
auth_data = 'Token: {token:{size}}\n'.format(token=token, size=TOKENSIZE) |
| 42 |
return auth_data |
| 43 |
|
| 44 |
|
| 45 |
def auth_data_length(): |
| 46 |
return len(create_auth_data('')) |
| 47 |
|
| 48 |
|
| 49 |
def check_auth_data(token, data): |
| 50 |
""" |
| 51 |
Verifies the authorization data received from peer |
| 52 |
:param token: the token at size |
| 53 |
:param data: |
| 54 |
:return: |
| 55 |
""" |
| 56 |
authdata = create_auth_data(token) |
| 57 |
logging.debug('AUTH_DATA:' + authdata.rstrip('\n')) |
| 58 |
ddata = data.decode() |
| 59 |
logging.debug('RECV_TOKEN_DATA:'+ddata.rstrip('\n')) |
| 60 |
return authdata == ddata |