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!
3 files
+ 34
17
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 24
10
@@ -15,14 +15,21 @@ You should have received a copy of the GNU Affero General Public License
@@ -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/>.
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 click import command, option, pass_context, confirm
from getpass import getpass
from getpass import getpass
from pathlib import Path
from pathlib import Path
from re import compile, search
from duniterpy.key import SigningKey
from duniterpy.key import SigningKey
from duniterpy.key.scrypt_params import ScryptParams
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
@pass_context
def auth_method(ctx):
def auth_method(ctx):
@@ -60,20 +67,27 @@ def generate_auth_file(file):
@@ -60,20 +67,27 @@ def generate_auth_file(file):
@pass_context
@pass_context
def auth_by_auth_file(ctx):
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"]
file = ctx.obj["AUTH_FILE_PATH"]
authfile = Path(file)
authfile = Path(file)
if not authfile.is_file():
if not authfile.is_file():
message_exit('Error: the file "' + file + '" does not exist')
message_exit('Error: the file "' + file + '" does not exist')
filetxt = authfile.open("r").read()
filetxt = authfile.open("r").read()
regex_seed = compile("^[0-9a-fA-F]{64}$")
regex_gannonce = compile(
# two regural expressions for the PubSec format
"^pub: [1-9A-HJ-NP-Za-km-z]{43,44}\nsec: [1-9A-HJ-NP-Za-km-z]{88,90}.*$"
regex_pubkey = re.compile(PUBSEC_PUBKEY_PATTERN, re.MULTILINE)
)
regex_signkey = re.compile(PUBSEC_SIGNKEY_PATTERN, re.MULTILINE)
# Seed Format
if search(regex_seed, filetxt):
# Seed hexadecimal format
 
if re.search(re.compile(SEED_HEX_PATTERN), filetxt):
return SigningKey.from_seedhex_file(file)
return SigningKey.from_seedhex_file(file)
# gannonce.duniter.org Format
# PubSec format
elif search(regex_gannonce, filetxt):
elif re.search(regex_pubkey, filetxt) and re.search(regex_signkey, filetxt):
return SigningKey.from_pubsec_file(file)
return SigningKey.from_pubsec_file(file)
else:
else:
message_exit("Error: the format of the file is invalid")
message_exit("Error: the format of the file is invalid")
Loading