-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_keygen.rs
More file actions
190 lines (159 loc) · 5.51 KB
/
test_keygen.rs
File metadata and controls
190 lines (159 loc) · 5.51 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
use reqwest;
use serde::Serialize;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// TODO: Replace with a separate TEST Keygen account, not production!
// Get these values from environment variables for security
let account_id = std::env::var("KEYGEN_TEST_ACCOUNT_ID")
.expect("Set KEYGEN_TEST_ACCOUNT_ID environment variable");
let license_key = std::env::var("KEYGEN_TEST_LICENSE_KEY")
.expect("Set KEYGEN_TEST_LICENSE_KEY environment variable");
let fingerprint = "test-machine-123";
// Test 1: Validate license
println!("=== TESTING LICENSE VALIDATION ===");
#[derive(Serialize)]
struct ValidateRequest<'a> {
meta: ValidateMeta<'a>,
}
#[derive(Serialize)]
struct ValidateMeta<'a> {
key: &'a str,
scope: ValidateScope<'a>,
}
#[derive(Serialize)]
struct ValidateScope<'a> {
fingerprint: &'a str,
}
let body = ValidateRequest {
meta: ValidateMeta {
key: license_key,
scope: ValidateScope { fingerprint },
},
};
let client = reqwest::Client::new();
let url = format!(
"https://api.keygen.sh/v1/accounts/{}/licenses/actions/validate-key",
account_id
);
let response = client
.post(&url)
.header("Content-Type", "application/vnd.api+json")
.header("Accept", "application/vnd.api+json")
.json(&body)
.send()
.await?;
let status = response.status();
let body_text = response.text().await?;
println!("Validation status: {}", status);
println!("Validation response: {}", body_text);
if !status.is_success() {
println!("\n❌ Validation failed!");
return Ok(());
}
let json: serde_json::Value = serde_json::from_str(&body_text)?;
let license_id = json["data"]["id"].as_str().unwrap_or("");
println!("✓ License ID: {}", license_id);
// Test 2: Machine activation with license KEY as bearer
println!("\n=== TESTING MACHINE ACTIVATION (LICENSE KEY AS BEARER) ===");
#[derive(Serialize)]
struct MachineRequest<'a> {
data: MachineData<'a>,
}
#[derive(Serialize)]
struct MachineData<'a> {
#[serde(rename = "type")]
type_field: &'a str,
attributes: MachineAttributes<'a>,
relationships: MachineRelationships<'a>,
}
#[derive(Serialize)]
struct MachineAttributes<'a> {
fingerprint: &'a str,
name: &'a str,
}
#[derive(Serialize)]
struct MachineRelationships<'a> {
license: LicenseRelationship<'a>,
}
#[derive(Serialize)]
struct LicenseRelationship<'a> {
data: LicenseData<'a>,
}
#[derive(Serialize)]
struct LicenseData<'a> {
#[serde(rename = "type")]
type_field: &'a str,
id: &'a str,
}
let machine_body = MachineRequest {
data: MachineData {
type_field: "machines",
attributes: MachineAttributes {
fingerprint,
name: "Test Machine",
},
relationships: MachineRelationships {
license: LicenseRelationship {
data: LicenseData {
type_field: "licenses",
id: license_key,
},
},
},
},
};
let machine_url = format!("https://api.keygen.sh/v1/accounts/{}/machines", account_id);
let machine_response = client
.post(&machine_url)
.header("Content-Type", "application/vnd.api+json")
.header("Accept", "application/vnd.api+json")
.bearer_auth(license_key)
.json(&machine_body)
.send()
.await?;
let machine_status = machine_response.status();
let machine_body_text = machine_response.text().await?;
println!("Machine activation status: {}", machine_status);
println!("Machine activation response: {}", machine_body_text);
if machine_status.is_success() {
println!("\n✅ SUCCESS! Machine activated with license key as bearer auth.");
} else {
println!("\n❌ Failed with license key as bearer. Trying license ID...");
// Test 3: Try with license ID as bearer
let machine_body2 = MachineRequest {
data: MachineData {
type_field: "machines",
attributes: MachineAttributes {
fingerprint: "test-machine-456",
name: "Test Machine 2",
},
relationships: MachineRelationships {
license: LicenseRelationship {
data: LicenseData {
type_field: "licenses",
id: license_id,
},
},
},
},
};
let machine_response2 = client
.post(&machine_url)
.header("Content-Type", "application/vnd.api+json")
.header("Accept", "application/vnd.api+json")
.bearer_auth(license_key)
.json(&machine_body2)
.send()
.await?;
let status2 = machine_response2.status();
let body2 = machine_response2.text().await?;
println!("\nWith license ID in body:");
println!("Status: {}", status2);
println!("Response: {}", body2);
if status2.is_success() {
println!("\n✅ SUCCESS! Use license ID in body, license KEY as bearer!");
}
}
Ok(())
}