Skip to content

Commit fe82740

Browse files
committed
add registration testing
1 parent 8b41fe6 commit fe82740

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

main.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,54 @@ func registerPartnerAccount(token string) (*RegistrationResponse, error) {
124124
return teslaResp.Response, nil
125125
}
126126

127+
func verifyPublicKey(token, domain string) (*RegistrationResponse, error) {
128+
// Clean the domain
129+
domain = strings.TrimSpace(domain)
130+
domain = strings.ToLower(domain)
131+
domain = strings.TrimPrefix(domain, "https://")
132+
domain = strings.TrimPrefix(domain, "http://")
133+
domain = strings.TrimSuffix(domain, "/")
134+
135+
url := fmt.Sprintf("https://fleet-api.prd.na.vn.cloud.tesla.com/api/1/partner_accounts/public_key?domain=%s", domain)
136+
137+
req, err := http.NewRequest("GET", url, nil)
138+
if err != nil {
139+
return nil, fmt.Errorf("error creating verification request: %v", err)
140+
}
141+
142+
req.Header.Set("Authorization", "Bearer "+token)
143+
144+
client := &http.Client{}
145+
resp, err := client.Do(req)
146+
if err != nil {
147+
return nil, fmt.Errorf("error making verification request: %v", err)
148+
}
149+
defer resp.Body.Close()
150+
151+
body, err := io.ReadAll(resp.Body)
152+
if err != nil {
153+
return nil, fmt.Errorf("error reading verification response: %v", err)
154+
}
155+
156+
log.Printf("Public key verification response status: %d", resp.StatusCode)
157+
log.Printf("Public key verification response body: %s", string(body))
158+
159+
var teslaResp TeslaAPIResponse
160+
if err := json.Unmarshal(body, &teslaResp); err != nil {
161+
return nil, fmt.Errorf("error parsing verification response: %v", err)
162+
}
163+
164+
if resp.StatusCode != http.StatusOK {
165+
return nil, fmt.Errorf("verification failed with status %d: %s", resp.StatusCode, teslaResp.Error)
166+
}
167+
168+
if teslaResp.Response == nil {
169+
return nil, fmt.Errorf("empty verification response from Tesla API")
170+
}
171+
172+
return teslaResp.Response, nil
173+
}
174+
127175
var (
128176
clientID []byte
129177
clientSecret []byte
@@ -180,9 +228,43 @@ func main() {
180228
})
181229
}
182230

231+
// Verify the public key registration
232+
verifyResp, err := verifyPublicKey(token.AccessToken, string(domainName))
233+
if err != nil {
234+
return c.Status(500).JSON(fiber.Map{
235+
"error": "Public key verification failed: " + err.Error(),
236+
"token": token,
237+
"registration": regResp,
238+
})
239+
}
240+
183241
return c.JSON(fiber.Map{
184242
"token": token,
185243
"registration": regResp,
244+
"verification": verifyResp,
245+
})
246+
})
247+
248+
// Add a separate endpoint for verification only
249+
app.Post("/verify-key", func(c *fiber.Ctx) error {
250+
token, err := generateToken()
251+
if err != nil {
252+
return c.Status(500).JSON(fiber.Map{
253+
"error": "Token generation failed: " + err.Error(),
254+
})
255+
}
256+
257+
verifyResp, err := verifyPublicKey(token.AccessToken, string(domainName))
258+
if err != nil {
259+
return c.Status(500).JSON(fiber.Map{
260+
"error": "Public key verification failed: " + err.Error(),
261+
"token": token,
262+
})
263+
}
264+
265+
return c.JSON(fiber.Map{
266+
"token": token,
267+
"verification": verifyResp,
186268
})
187269
})
188270

views/index.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@
101101
gap: 10px;
102102
margin: 20px 0;
103103
}
104+
.success-message {
105+
color: #2e7d32;
106+
margin-top: 10px;
107+
display: none;
108+
padding: 15px;
109+
background-color: rgba(232, 245, 233, 0.9);
110+
border-radius: 20px;
111+
backdrop-filter: blur(8px);
112+
}
104113
</style>
105114
</head>
106115
<body>
@@ -113,6 +122,9 @@ <h1>Tesla Fleet Telemetry Operator</h1>
113122
<button class="tesla-button" onclick="generateToken()">
114123
Generate Partner Token & Register
115124
</button>
125+
<button class="tesla-button secondary" onclick="verifyKey()">
126+
Verify Public Key
127+
</button>
116128
<button class="tesla-button secondary" onclick="window.location.href='/.well-known/appspecific/com.tesla.3p.public-key.pem'">
117129
Download Public Key
118130
</button>
@@ -126,16 +138,23 @@ <h3>Partner Authentication Token:</h3>
126138
<h3>Registration Response:</h3>
127139
<pre id="registrationContent"></pre>
128140
</div>
141+
<div class="response-section">
142+
<h3>Verification Response:</h3>
143+
<pre id="verificationContent"></pre>
144+
</div>
129145
</div>
130146
<div id="errorMessage" class="error-message"></div>
147+
<div id="successMessage" class="success-message"></div>
131148
</div>
132149

133150
<script>
134151
async function generateToken() {
135152
const responseDisplay = document.getElementById('responseDisplay');
136153
const tokenContent = document.getElementById('tokenContent');
137154
const registrationContent = document.getElementById('registrationContent');
155+
const verificationContent = document.getElementById('verificationContent');
138156
const errorMessage = document.getElementById('errorMessage');
157+
const successMessage = document.getElementById('successMessage');
139158

140159
try {
141160
const response = await fetch('/generate-token', {
@@ -150,11 +169,48 @@ <h3>Registration Response:</h3>
150169
const data = await response.json();
151170
tokenContent.textContent = JSON.stringify(data.token, null, 2);
152171
registrationContent.textContent = JSON.stringify(data.registration, null, 2);
172+
verificationContent.textContent = JSON.stringify(data.verification, null, 2);
173+
responseDisplay.style.display = 'block';
174+
errorMessage.style.display = 'none';
175+
successMessage.textContent = 'Registration and verification successful!';
176+
successMessage.style.display = 'block';
177+
} catch (error) {
178+
errorMessage.textContent = error.message;
179+
errorMessage.style.display = 'block';
180+
successMessage.style.display = 'none';
181+
responseDisplay.style.display = 'none';
182+
}
183+
}
184+
185+
async function verifyKey() {
186+
const responseDisplay = document.getElementById('responseDisplay');
187+
const tokenContent = document.getElementById('tokenContent');
188+
const verificationContent = document.getElementById('verificationContent');
189+
const errorMessage = document.getElementById('errorMessage');
190+
const successMessage = document.getElementById('successMessage');
191+
192+
try {
193+
const response = await fetch('/verify-key', {
194+
method: 'POST',
195+
});
196+
197+
if (!response.ok) {
198+
const errorData = await response.json();
199+
throw new Error(errorData.error || 'Failed to verify public key');
200+
}
201+
202+
const data = await response.json();
203+
tokenContent.textContent = JSON.stringify(data.token, null, 2);
204+
verificationContent.textContent = JSON.stringify(data.verification, null, 2);
205+
document.querySelector('.response-section:nth-child(2)').style.display = 'none';
153206
responseDisplay.style.display = 'block';
154207
errorMessage.style.display = 'none';
208+
successMessage.textContent = 'Public key verification successful!';
209+
successMessage.style.display = 'block';
155210
} catch (error) {
156211
errorMessage.textContent = error.message;
157212
errorMessage.style.display = 'block';
213+
successMessage.style.display = 'none';
158214
responseDisplay.style.display = 'none';
159215
}
160216
}

0 commit comments

Comments
 (0)