Skip to content
Snippets Groups Projects

[fix] #278: changing regex for PubSec authfile authentication

Merged [fix] #278: changing regex for PubSec authfile authentication
All threads resolved!
Merged matograine requested to merge 273_pubsec into dev
All threads resolved!
Files
3
+ 24
10
@@ -15,14 +15,21 @@ You should have received a copy of the GNU Affero General Public License
along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
"""
from silkaj.tools import message_exit
import re
from click import command, option, pass_context, confirm
from getpass import getpass
from pathlib import Path
from re import compile, search
from duniterpy.key import SigningKey
from duniterpy.key.scrypt_params import ScryptParams
from silkaj.tools import message_exit
from silkaj.constants import PUBKEY_PATTERN
SEED_HEX_PATTERN = "^[0-9a-fA-F]{64}$"
PUBSEC_PUBKEY_PATTERN = "pub: ({0})".format(PUBKEY_PATTERN)
PUBSEC_SIGNKEY_PATTERN = "sec: ([1-9A-HJ-NP-Za-km-z]{87,90})"
@pass_context
def auth_method(ctx):
@@ -60,20 +67,27 @@ def generate_auth_file(file):
@pass_context
def auth_by_auth_file(ctx):
"""
Uses an authentication file to generate the key
Authfile can either be:
* A seed in hexadecimal encoding
* PubSec format with public and private key in base58 encoding
"""
file = ctx.obj["AUTH_FILE_PATH"]
authfile = Path(file)
if not authfile.is_file():
message_exit('Error: the file "' + file + '" does not exist')
filetxt = authfile.open("r").read()
regex_seed = compile("^[0-9a-fA-F]{64}$")
regex_gannonce = compile(
"^pub: [1-9A-HJ-NP-Za-km-z]{43,44}\nsec: [1-9A-HJ-NP-Za-km-z]{88,90}.*$"
)
# Seed Format
if search(regex_seed, filetxt):
# two regural expressions for the PubSec format
regex_pubkey = re.compile(PUBSEC_PUBKEY_PATTERN, re.MULTILINE)
regex_signkey = re.compile(PUBSEC_SIGNKEY_PATTERN, re.MULTILINE)
# Seed hexadecimal format
if re.search(re.compile(SEED_HEX_PATTERN), filetxt):
return SigningKey.from_seedhex_file(file)
# gannonce.duniter.org Format
elif search(regex_gannonce, filetxt):
# PubSec format
elif re.search(regex_pubkey, filetxt) and re.search(regex_signkey, filetxt):
return SigningKey.from_pubsec_file(file)
else:
message_exit("Error: the format of the file is invalid")
Loading