LeetAuth API
Search…
Code examples
Stuck at implementing our authentication system? Look here to find some code snippets explaining how to do so in 3 different languages. Check this page regularly as we may add your language here too.
All examples are raw implementations of our API. For the time being LeetAuth does not provide any official packages/modules for any languages. This is a simple guide that can be edited, distributed and shared by and to anyone using LeetAuth
The LeetAuth API supports every programming language that has any type of HTTP client and can parse JSON data. Here we only have snippets for most commonly cases of usage. Soon we will add more languages like: Golang, PHP, C++, Rust and Javascript

Python 3

This example can also be found at: https://github.com/LeetAuth/PythonExample

1
# Importing requests module for talking to the API
2
import requests
3
# Importing JSON for reading the response body
4
import json
5
# Importing simplejson for error handling when downloading a file
6
import simplejson
7
8
from urllib.parse import urlencode
9
10
11
class LeetAuth:
12
def __init__(self, identifier: str):
13
self.base_url = "https://api.leet-auth.dev/public"
14
self.app_id = identifier
15
self.auth_token = ""
16
17
def url(self, path: str) -> str:
18
return self.base_url + path
19
20
@staticmethod
21
def format_json(json_body: dict) -> str:
22
return json.dumps(json_body, indent=4, ensure_ascii=True)
23
24
@property
25
def headers(self) -> dict:
26
return {
27
"Authorization": self.auth_token,
28
"User-Agent": "LeetAuth"
29
}
30
31
# Pre-Auth Functions
32
def login(self, username: str, password: str, **kwargs) -> str:
33
response = requests.post(self.url("/authenticate"), params={
34
"app": self.app_id,
35
"username": username,
36
"password": password,
37
"hwid": kwargs.get("hwid", ""),
38
"checksum": kwargs.get("checksum", "")
39
}, headers=self.headers)
40
# Getting the JSON response from our API
41
json_body = response.json()
42
# Assigning the JWT to our session
43
if json_body["status"]:
44
self.auth_token = json_body["token"]
45
return self.format_json(json_body)
46
47
def register(self, username: str, password: str, license_key: str, **kwargs) -> str:
48
response = requests.post(self.url("/register"), params={
49
"app": self.app_id,
50
"username": username,
51
"password": password,
52
"license": license_key,
53
"hwid": kwargs.get("hwid", ""),
54
"checksum": kwargs.get("checksum", "")
55
}, headers=self.headers)
56
return self.format_json(response.json())
57
58
def renew_plan(self, username: str, license: str) -> str;
59
response = requests.post(self.url("/renew"), params={
60
"app": self.app_id,
61
"username": username,
62
"license": license_key,
63
}, headers=self.headers)
64
return self.format_json(response.json())
65
66
67
# Post-Auth functions
68
def get_properties(self) -> str:
69
response = requests.get(self.url("/properties"), params={
70
"app": self.app_id,
71
}, headers=self.headers)
72
return self.format_json(response.json())
73
74
def change_password(self, new_password: str, confirm_new_password: str) -> str:
75
response = requests.post(self.url("/change_password"), params={
76
"app": self.app_id,
77
"new_password": new_password,
78
"cnf_password": confirm_new_password
79
}, headers=self.headers)
80
return self.format_json(response.json())
81
82
def get_login_logs(self) -> str:
83
response = requests.get(self.url("/logins"), params={
84
"app": self.app_id,
85
}, headers=self.headers)
86
return self.format_json(response.json())
87
88
def download_file(self, file_name: str, path_to_save: str) -> str:
89
response = requests.get(self.url("/files/") + file_name, params={
90
"app": self.app_id,
91
}, headers=self.headers)
92
try:
93
json_body = response.json()
94
except simplejson.errors.JSONDecodeError:
95
# Writing the file in chunks, its a better way of writing if the file is big
96
with open(path_to_save, 'wb') as downloaded_file:
97
for chunk in response.iter_content(chunk_size=128):
98
downloaded_file.write(chunk)
99
# Returning a hardocded sucess response
100
return self.format_json({
101
"status": True,
102
"note": "Successfully downloaded the file"
103
})
104
return self.format_json(json_body)
105
106
def get_variable(self, variable_name: str) -> str:
107
response = requests.get(self.url("/variables/") + variable_name, params={
108
"app": self.app_id,
109
}, headers=self.headers)
110
return self.format_json(response.json())
Copied!

Usage example

1
# All methods besides the LeetAuth() return a string which can be printed out
2
# the string is just a nicely formatted JSON response, it can be loaded using
3
# json.loads(function_return) to have a dictionary object of it
4
5
# Creating a class instance using the class above
6
auth = LeetAuth("app_id")
7
# the hwid & checksum are optional arguments, if not provided they will be sent as empty strings
8
auth.register("username", "password", "license", hwid="hardware identifier", checksum="app checksum")
9
auth.login("username", "password", hwid="hardware identifier", checksum="app checksum")
10
if auth.auth_token == "":
11
print("Something went wrong during the authentication")
12
# Exitting the app and returning a non 0 response
13
exit(1)
14
15
auth.get_properties()
16
auth.change_password("newPassword123!", "newPassword123!")
17
auth.get_login_logs()
18
auth.download_file("hey.txt", "./myfile.txt")
19
auth.get_variable("myVariable")
20
print(auth.renew_plan("username", "license-key"))
Copied!

C#

We have created 2 examples for you guys to look at in order to integrate LeetAuth into your own service.

Copy link