-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthon.ps1
More file actions
660 lines (565 loc) · 22.6 KB
/
Copy pathAuthon.ps1
File metadata and controls
660 lines (565 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
<#
╔══════════════════════════════════════════════════════════════════════════════╗
║ Authon PowerShell SDK — Software Licensing & Authentication ║
║ Version: 1.0.0 ║
║ Dependencies: None (Invoke-RestMethod built-in) ║
║ ║
║ Website: https://authon.pro ║
║ Docs: https://authon.pro/docs ║
║ Discord: https://discord.gg/jMZCTKPsmE ║
║ Status: https://authon.pro/status ║
║ Health: https://api.authon.pro/health ║
║ GitHub: https://github.com/authonpro ║
║ ║
║ Requirements: PowerShell 5.1+ or PowerShell Core 7+ ║
║ ║
║ Usage: ║
║ . .\Authon.ps1 ║
║ Initialize-Authon -AppId "app-id" -ApiKey "api-key" ║
║ $result = Invoke-AuthonInit ║
║ $login = Invoke-AuthonLogin -Username "user" -Password "pass" ║
║ if ($login.success) { Write-Host "Welcome $($Script:AuthonUsername)!" } ║
╚══════════════════════════════════════════════════════════════════════════════╝
#>
# ═══════════════════════════════════════════════════════════════════════════════
# MODULE STATE
# ═══════════════════════════════════════════════════════════════════════════════
$Script:AuthonVersion = "1.0.0"
$Script:AuthonApiUrl = "https://api.authon.pro/v1"
$Script:AuthonAppId = $null
$Script:AuthonApiKey = $null
$Script:AuthonTimeout = 15
# Session state
$Script:AuthonSessionToken = $null
$Script:AuthonUsername = $null
$Script:AuthonLevel = 0
$Script:AuthonSubscription = $null
$Script:AuthonExpiresAt = $null
# App info
$Script:AuthonAppName = $null
$Script:AuthonAppVersion = $null
$Script:AuthonHwidLock = $false
$Script:AuthonHashCheck = $false
$Script:AuthonInitialized = $false
# ═══════════════════════════════════════════════════════════════════════════════
# INITIALIZATION
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Configures the Authon SDK with your application credentials.
.DESCRIPTION
Must be called before any other Authon function.
Sets the Application ID and API Key for all subsequent requests.
.PARAMETER AppId
Your Application ID from the Authon dashboard.
.PARAMETER ApiKey
Your API Key from the Authon dashboard.
.PARAMETER ApiUrl
Custom API URL (default: https://api.authon.pro/v1).
.EXAMPLE
Initialize-Authon -AppId "your-app-id" -ApiKey "your-api-key"
#>
function Initialize-Authon {
param(
[Parameter(Mandatory = $true)]
[string]$AppId,
[Parameter(Mandatory = $true)]
[string]$ApiKey,
[string]$ApiUrl = "https://api.authon.pro/v1"
)
if ([string]::IsNullOrWhiteSpace($AppId)) { throw "AppId is required" }
if ([string]::IsNullOrWhiteSpace($ApiKey)) { throw "ApiKey is required" }
$Script:AuthonAppId = $AppId.Trim()
$Script:AuthonApiKey = $ApiKey.Trim()
$Script:AuthonApiUrl = $ApiUrl.TrimEnd('/')
}
# ═══════════════════════════════════════════════════════════════════════════════
# HWID GENERATION
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Generates a hardware ID unique to the current machine.
.DESCRIPTION
Uses WMI disk serial number + computer name, hashed with MD5.
Falls back to computer name + username if WMI is unavailable.
.OUTPUTS
System.String — 32-character lowercase hex MD5 hash.
.EXAMPLE
$hwid = Get-AuthonHWID
Write-Host "HWID: $hwid"
#>
function Get-AuthonHWID {
$raw = ""
try {
# Get disk serial via WMI
$disk = Get-CimInstance -ClassName Win32_DiskDrive -ErrorAction SilentlyContinue | Select-Object -First 1
if ($disk -and $disk.SerialNumber) {
$raw = $disk.SerialNumber.Trim()
}
}
catch {
# WMI not available
}
$raw += $env:COMPUTERNAME
if ([string]::IsNullOrWhiteSpace($raw)) {
$raw = "$env:COMPUTERNAME$env:USERNAME"
}
# Compute MD5
$md5 = [System.Security.Cryptography.MD5]::Create()
$bytes = [System.Text.Encoding]::UTF8.GetBytes($raw)
$hash = $md5.ComputeHash($bytes)
$md5.Dispose()
return ($hash | ForEach-Object { $_.ToString("x2") }) -join ''
}
# ═══════════════════════════════════════════════════════════════════════════════
# INTERNAL HTTP
# ═══════════════════════════════════════════════════════════════════════════════
function Invoke-AuthonRequest {
param(
[Parameter(Mandatory = $true)]
[hashtable]$Payload
)
if (-not $Script:AuthonAppId) { throw "Call Initialize-Authon first" }
$Payload["appId"] = $Script:AuthonAppId
$Payload["apiKey"] = $Script:AuthonApiKey
$json = $Payload | ConvertTo-Json -Depth 10 -Compress
try {
$response = Invoke-RestMethod -Uri $Script:AuthonApiUrl `
-Method Post `
-Body $json `
-ContentType "application/json" `
-Headers @{ "User-Agent" = "Authon-PowerShell-SDK/$Script:AuthonVersion" } `
-TimeoutSec $Script:AuthonTimeout `
-ErrorAction Stop
return $response
}
catch [System.Net.WebException] {
return @{ success = $false; message = "Connection failed. Check https://authon.pro/status" }
}
catch {
return @{ success = $false; message = "Unexpected error: $($_.Exception.Message)" }
}
}
# ═══════════════════════════════════════════════════════════════════════════════
# API: INIT
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Initializes the Authon API connection.
.DESCRIPTION
Validates credentials and retrieves app info.
Must be called after Initialize-Authon and before login/license.
.OUTPUTS
PSObject with success, message, data properties.
.EXAMPLE
$result = Invoke-AuthonInit
if ($result.success) { Write-Host "Connected to $Script:AuthonAppName" }
#>
function Invoke-AuthonInit {
$result = Invoke-AuthonRequest -Payload @{ type = "init" }
if ($result.success) {
$Script:AuthonAppName = $result.data.name
$Script:AuthonAppVersion = $result.data.version
$Script:AuthonHwidLock = $result.data.hwidLock
$Script:AuthonHashCheck = $result.data.hashCheck
$Script:AuthonInitialized = $true
}
return $result
}
# ═══════════════════════════════════════════════════════════════════════════════
# API: AUTHENTICATION
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Authenticates with username and password.
.PARAMETER Username
User's username.
.PARAMETER Password
User's password.
.PARAMETER HWID
Hardware ID (auto-generated if not provided).
.OUTPUTS
PSObject with success, message, data (sessionToken, username, level, subscription, expiresAt).
.EXAMPLE
$login = Invoke-AuthonLogin -Username "john" -Password "pass123"
if ($login.success) { Write-Host "Level: $Script:AuthonLevel" }
#>
function Invoke-AuthonLogin {
param(
[Parameter(Mandatory = $true)]
[string]$Username,
[Parameter(Mandatory = $true)]
[string]$Password,
[string]$HWID
)
if ([string]::IsNullOrWhiteSpace($Username) -or [string]::IsNullOrWhiteSpace($Password)) {
return @{ success = $false; message = "Username and password are required" }
}
if ([string]::IsNullOrWhiteSpace($HWID)) { $HWID = Get-AuthonHWID }
$result = Invoke-AuthonRequest -Payload @{
type = "login"
username = $Username
password = $Password
hwid = $HWID
}
if ($result.success) {
$Script:AuthonSessionToken = $result.data.sessionToken
$Script:AuthonUsername = $result.data.username
$Script:AuthonLevel = $result.data.level
$Script:AuthonSubscription = $result.data.subscription
$Script:AuthonExpiresAt = $result.data.expiresAt
}
return $result
}
<#
.SYNOPSIS
Authenticates using a license key only.
.PARAMETER LicenseKey
The license key to validate/activate.
.PARAMETER HWID
Hardware ID (auto-generated if not provided).
.EXAMPLE
$result = Invoke-AuthonLicense -LicenseKey "XXXXX-XXXXX-XXXXX-XXXXX"
#>
function Invoke-AuthonLicense {
param(
[Parameter(Mandatory = $true)]
[string]$LicenseKey,
[string]$HWID
)
if ([string]::IsNullOrWhiteSpace($LicenseKey)) {
return @{ success = $false; message = "License key is required" }
}
if ([string]::IsNullOrWhiteSpace($HWID)) { $HWID = Get-AuthonHWID }
$result = Invoke-AuthonRequest -Payload @{
type = "license"
licenseKey = $LicenseKey
hwid = $HWID
}
if ($result.success) {
$Script:AuthonSessionToken = $result.data.sessionToken
$Script:AuthonUsername = $result.data.username
$Script:AuthonLevel = $result.data.level
$Script:AuthonSubscription = $result.data.subscription
$Script:AuthonExpiresAt = $result.data.expiresAt
}
return $result
}
<#
.SYNOPSIS
Registers a new user account with a license key.
.PARAMETER Username
Desired username.
.PARAMETER Password
Desired password.
.PARAMETER LicenseKey
A valid, unused license key.
.PARAMETER HWID
Hardware ID (auto-generated if not provided).
.EXAMPLE
$result = Invoke-AuthonRegister -Username "newuser" -Password "pass" -LicenseKey "XXXXX"
#>
function Invoke-AuthonRegister {
param(
[Parameter(Mandatory = $true)]
[string]$Username,
[Parameter(Mandatory = $true)]
[string]$Password,
[Parameter(Mandatory = $true)]
[string]$LicenseKey,
[string]$HWID
)
if ([string]::IsNullOrWhiteSpace($HWID)) { $HWID = Get-AuthonHWID }
return Invoke-AuthonRequest -Payload @{
type = "register"
username = $Username
password = $Password
licenseKey = $LicenseKey
hwid = $HWID
}
}
# ═══════════════════════════════════════════════════════════════════════════════
# API: SESSION MANAGEMENT
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Validates the current session (heartbeat).
.OUTPUTS
Boolean — True if session is valid.
#>
function Invoke-AuthonCheck {
if (-not $Script:AuthonSessionToken) { return $false }
$result = Invoke-AuthonRequest -Payload @{
type = "check"
sessionToken = $Script:AuthonSessionToken
}
return [bool]$result.success
}
<#
.SYNOPSIS
Ends the current session and clears local state.
.OUTPUTS
Boolean — True if logout was successful.
#>
function Invoke-AuthonLogout {
if (-not $Script:AuthonSessionToken) { return $false }
$result = Invoke-AuthonRequest -Payload @{
type = "logout"
sessionToken = $Script:AuthonSessionToken
}
if ($result.success) {
$Script:AuthonSessionToken = $null
$Script:AuthonUsername = $null
$Script:AuthonLevel = 0
$Script:AuthonSubscription = $null
$Script:AuthonExpiresAt = $null
}
return [bool]$result.success
}
# ═══════════════════════════════════════════════════════════════════════════════
# API: VARIABLES
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Gets an application-level variable.
.PARAMETER Key
Variable name.
.OUTPUTS
String — Variable value, or $null.
#>
function Get-AuthonVar {
param([Parameter(Mandatory = $true)] [string]$Key)
$result = Invoke-AuthonRequest -Payload @{
type = "var"
key = $Key
sessionToken = $Script:AuthonSessionToken
}
if ($result.success) { return $result.data.value }
return $null
}
<#
.SYNOPSIS
Sets a user-level variable.
.PARAMETER Key
Variable name.
.PARAMETER Value
Variable value.
.OUTPUTS
Boolean — True if saved.
#>
function Set-AuthonVar {
param(
[Parameter(Mandatory = $true)] [string]$Key,
[Parameter(Mandatory = $true)] [string]$Value
)
$result = Invoke-AuthonRequest -Payload @{
type = "setvar"
key = $Key
value = $Value
sessionToken = $Script:AuthonSessionToken
}
return [bool]$result.success
}
<#
.SYNOPSIS
Gets a user-level variable.
.PARAMETER Key
Variable name.
.OUTPUTS
String — Variable value, or $null.
#>
function Get-AuthonUserVar {
param([Parameter(Mandatory = $true)] [string]$Key)
$result = Invoke-AuthonRequest -Payload @{
type = "getvar"
key = $Key
sessionToken = $Script:AuthonSessionToken
}
if ($result.success) { return $result.data.value }
return $null
}
# ═══════════════════════════════════════════════════════════════════════════════
# API: FILES
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Lists files available to the authenticated user.
.OUTPUTS
Array of file objects with id, name, size, minLevel.
#>
function Get-AuthonFiles {
$result = Invoke-AuthonRequest -Payload @{
type = "list_files"
sessionToken = $Script:AuthonSessionToken
}
if ($result.success) { return $result.data }
return @()
}
<#
.SYNOPSIS
Downloads a file by its ID.
.PARAMETER FileId
File ID from Get-AuthonFiles.
.PARAMETER OutputPath
Path to save the downloaded file.
.OUTPUTS
Boolean — True if download was successful.
#>
function Save-AuthonFile {
param(
[Parameter(Mandatory = $true)] [string]$FileId,
[Parameter(Mandatory = $true)] [string]$OutputPath
)
if (-not $Script:AuthonSessionToken) { return $false }
$payload = @{
type = "file"
appId = $Script:AuthonAppId
apiKey = $Script:AuthonApiKey
fileId = $FileId
sessionToken = $Script:AuthonSessionToken
}
$json = $payload | ConvertTo-Json -Depth 10 -Compress
try {
Invoke-RestMethod -Uri $Script:AuthonApiUrl `
-Method Post `
-Body $json `
-ContentType "application/json" `
-OutFile $OutputPath `
-ErrorAction Stop
return $true
}
catch {
# Try GET fallback
try {
$url = "$Script:AuthonApiUrl/files/download/$FileId`?token=$Script:AuthonSessionToken"
Invoke-WebRequest -Uri $url -OutFile $OutputPath -ErrorAction Stop
return $true
}
catch {
return $false
}
}
}
# ═══════════════════════════════════════════════════════════════════════════════
# API: LOGGING & ANALYTICS
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Sends an activity log message to the dashboard.
.PARAMETER Message
Log message (max 500 chars).
.OUTPUTS
Boolean — True if logged.
#>
function Send-AuthonLog {
param([Parameter(Mandatory = $true)] [string]$Message)
if ($Message.Length -gt 500) { $Message = $Message.Substring(0, 500) }
$result = Invoke-AuthonRequest -Payload @{
type = "log"
message = $Message
sessionToken = $Script:AuthonSessionToken
}
return [bool]$result.success
}
<#
.SYNOPSIS
Gets the list of currently online users.
.OUTPUTS
PSObject with count and users.
#>
function Get-AuthonOnline {
$result = Invoke-AuthonRequest -Payload @{
type = "fetch_online"
sessionToken = $Script:AuthonSessionToken
}
if ($result.success) { return $result.data }
return @{ count = 0; users = @() }
}
<#
.SYNOPSIS
Gets application statistics.
.OUTPUTS
PSObject with totalUsers, onlineUsers, totalKeys, appVersion.
#>
function Get-AuthonStats {
$result = Invoke-AuthonRequest -Payload @{
type = "fetch_stats"
sessionToken = $Script:AuthonSessionToken
}
if ($result.success) { return $result.data }
return @{}
}
# ═══════════════════════════════════════════════════════════════════════════════
# API: SECURITY
# ═══════════════════════════════════════════════════════════════════════════════
<#
.SYNOPSIS
Checks if an IP or HWID is blacklisted.
.PARAMETER IP
IP address to check (optional).
.PARAMETER HWID
HWID to check (optional).
.OUTPUTS
PSObject with blacklisted (bool) and reason.
#>
function Test-AuthonBlacklist {
param(
[string]$IP,
[string]$HWID
)
$payload = @{ type = "check_blacklist" }
if (-not [string]::IsNullOrWhiteSpace($IP)) { $payload["ip"] = $IP }
if (-not [string]::IsNullOrWhiteSpace($HWID)) { $payload["hwid"] = $HWID }
$result = Invoke-AuthonRequest -Payload $payload
if ($result.success) { return $result.data }
return @{ blacklisted = $false; reason = $null }
}
<#
.SYNOPSIS
Redeems a referral code for bonus subscription days.
.PARAMETER Code
Referral code.
.OUTPUTS
PSObject with success, message, data (expiresAt, rewardDays).
#>
function Invoke-AuthonRedeemReferral {
param([Parameter(Mandatory = $true)] [string]$Code)
return Invoke-AuthonRequest -Payload @{
type = "redeem_referral"
code = $Code
sessionToken = $Script:AuthonSessionToken
}
}
# ═══════════════════════════════════════════════════════════════════════════════
# EXPORT
# ═══════════════════════════════════════════════════════════════════════════════
# Export all public functions
Export-ModuleMember -Function @(
'Initialize-Authon',
'Get-AuthonHWID',
'Invoke-AuthonInit',
'Invoke-AuthonLogin',
'Invoke-AuthonLicense',
'Invoke-AuthonRegister',
'Invoke-AuthonCheck',
'Invoke-AuthonLogout',
'Get-AuthonVar',
'Set-AuthonVar',
'Get-AuthonUserVar',
'Get-AuthonFiles',
'Save-AuthonFile',
'Send-AuthonLog',
'Get-AuthonOnline',
'Get-AuthonStats',
'Test-AuthonBlacklist',
'Invoke-AuthonRedeemReferral'
) -Variable @(
'AuthonSessionToken',
'AuthonUsername',
'AuthonLevel',
'AuthonSubscription',
'AuthonExpiresAt',
'AuthonAppName',
'AuthonAppVersion',
'AuthonInitialized'
)