Skip to content

Commit

Permalink
add login token func
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Apr 12, 2022
1 parent fb82ff1 commit ad3086f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="meshctrl",
version="0.1.13",
version="0.1.14",
description="Python port of MeshCentral's Meshctrl.js program",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
27 changes: 27 additions & 0 deletions src/meshctrl/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import time
import uuid
import base64
from base64 import b64encode
import random
import string
import re
from typing import Dict, List, Optional

from Crypto.Cipher import AES
from Crypto.Hash import SHA3_384
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad


def get_pwd_auth(username: str, password: str, token: str = None) -> str:
Expand All @@ -29,6 +32,30 @@ def get_auth_token(user: str, key: str, domain: str = "") -> str:
return b64encode(iv + tag + msg, altchars=b"@$").decode("utf-8")


def get_login_token(self, key, user, action=3):
try:
key = bytes.fromhex(key)
key1 = key[0:48]
key2 = key[48:]
msg = '{{"a":{}, "u":"{}","time":{}}}'.format(
action, user.lower(), int(time.time())
)
iv = get_random_bytes(16)

# sha
h = SHA3_384.new()
h.update(key1)
hashed_msg = h.digest() + msg.encode()

# aes
cipher = AES.new(key2, AES.MODE_CBC, iv)
msg = cipher.encrypt(pad(hashed_msg, 16))

return base64.b64encode(iv + msg, altchars=b"@$").decode("utf-8")
except Exception:
return "err"


def b64_to_hex(hex: str) -> str:
return b64encode(bytes.fromhex(hex)).decode().replace(r"/", "$").replace(r"+", "@")

Expand Down

0 comments on commit ad3086f

Please sign in to comment.