Skip to content

Commit ad3086f

Browse files
committed
add login token func
1 parent fb82ff1 commit ad3086f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="meshctrl",
8-
version="0.1.13",
8+
version="0.1.14",
99
description="Python port of MeshCentral's Meshctrl.js program",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",

src/meshctrl/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import time
22
import uuid
3+
import base64
34
from base64 import b64encode
45
import random
56
import string
67
import re
78
from typing import Dict, List, Optional
89

910
from Crypto.Cipher import AES
11+
from Crypto.Hash import SHA3_384
1012
from Crypto.Random import get_random_bytes
13+
from Crypto.Util.Padding import pad
1114

1215

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

3134

35+
def get_login_token(self, key, user, action=3):
36+
try:
37+
key = bytes.fromhex(key)
38+
key1 = key[0:48]
39+
key2 = key[48:]
40+
msg = '{{"a":{}, "u":"{}","time":{}}}'.format(
41+
action, user.lower(), int(time.time())
42+
)
43+
iv = get_random_bytes(16)
44+
45+
# sha
46+
h = SHA3_384.new()
47+
h.update(key1)
48+
hashed_msg = h.digest() + msg.encode()
49+
50+
# aes
51+
cipher = AES.new(key2, AES.MODE_CBC, iv)
52+
msg = cipher.encrypt(pad(hashed_msg, 16))
53+
54+
return base64.b64encode(iv + msg, altchars=b"@$").decode("utf-8")
55+
except Exception:
56+
return "err"
57+
58+
3259
def b64_to_hex(hex: str) -> str:
3360
return b64encode(bytes.fromhex(hex)).decode().replace(r"/", "$").replace(r"+", "@")
3461

0 commit comments

Comments
 (0)