smbcloud-network 1.1.1

Command line tool for deploying NextJS app on the smbCloud platform.
Documentation
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
use {
    log::{debug, error},
    reqwest::{RequestBuilder, Response, StatusCode},
    serde::de::DeserializeOwned,
    smbcloud_model::{
        account::SmbAuthorization,
        error_codes::{ErrorCode, ErrorResponse},
        login::AccountStatus,
    },
};

/// The value of an `Authorization` header for a bearer token.
///
/// The login flow stores the whole header value (`Bearer <jwt>`), while deploy
/// tokens are stored bare. Strips one leading `Bearer ` (any case) and adds
/// exactly one, so callers never send `Bearer Bearer <jwt>`.
pub fn bearer_value(token: &str) -> String {
    let token = token.trim();
    let bare = match token.get(..7) {
        Some(prefix) if prefix.eq_ignore_ascii_case("bearer ") => token[7..].trim_start(),
        _ => token,
    };
    format!("Bearer {bare}")
}

//use std::time::Duration;
#[cfg(debug_assertions)]
const LOG_RESPONSE_BODY: bool = true; // You know what to do here.
#[cfg(not(debug_assertions))]
const LOG_RESPONSE_BODY: bool = false;

/// Check if there is an active internet connection.
///
/// Deprecated: this pings a third-party host, which fails behind an egress
/// allowlist even when the smbCloud API is reachable. A request that cannot
/// connect already returns `ErrorCode::NetworkError`.
#[cfg(not(target_arch = "wasm32"))]
#[deprecated(
    since = "1.1.0",
    note = "the request itself reports NetworkError; this pinged a third-party host"
)]
pub async fn check_internet_connection() -> bool {
    debug!("Checking internet connection");
    let client = reqwest::Client::builder()
        //.timeout(Duration::from_secs(5)) Does not work on wasm32
        .build();

    if let Err(e) = client {
        error!("Failed to create client for connectivity check: {:?}", e);
        return false;
    }

    if let Ok(client) = client {
        match client.get("https://dns.google").send().await {
            Ok(response) => {
                debug!(
                    "Internet connection check successful: {}",
                    response.status()
                );
                response.status().is_success()
            }
            Err(e) => {
                error!("Internet connection check failed: {:?}", e);
                false
            }
        }
    } else {
        false
    }
}

#[cfg(target_arch = "wasm32")]
#[deprecated(
    since = "1.1.0",
    note = "the request itself reports NetworkError; this pinged a third-party host"
)]
pub async fn check_internet_connection() -> bool {
    true
}

/// Log why a request failed to send (connect, timeout, proxy or other), so
/// `RUST_LOG=debug` explains a `NetworkError` without changing its message.
pub fn log_send_error(e: &reqwest::Error) {
    #[cfg(not(target_arch = "wasm32"))]
    let connect = e.is_connect();
    #[cfg(target_arch = "wasm32")]
    let connect = false;
    let kind = if e.is_timeout() {
        "timeout"
    } else if connect {
        "connect"
    } else if e.is_builder() {
        "builder"
    } else {
        "other"
    };
    debug!("Request failed to send ({kind}): {e:?}");
}

pub async fn parse_error_response<T: DeserializeOwned>(
    response: Response,
) -> Result<T, ErrorResponse> {
    let error_response_body = match response.text().await {
        Ok(body) => body,
        Err(e) => {
            error!("Failed to get response body: {:?}", e);
            return Err(ErrorResponse::Error {
                error_code: ErrorCode::NetworkError,
                message: ErrorCode::NetworkError.message(None).to_string(),
            });
        }
    };

    if LOG_RESPONSE_BODY {
        debug!(
            "Parse Error >>>> {:?}",
            serde_json::to_string_pretty(&error_response_body)
        );
    }

    let error_response = match serde_json::from_str(&error_response_body) {
        Ok(error_response) => error_response,
        Err(e) => {
            error!("Failed to parse error response: {:?}", e);
            return Err(ErrorResponse::Error {
                error_code: ErrorCode::ParseError,
                message: ErrorCode::ParseError.message(None).to_string(),
            });
        }
    };
    // The parsing itself is succeed.
    // Why is this an ErrorResponse.
    Err(error_response)
}

pub async fn request_login(builder: RequestBuilder) -> Result<AccountStatus, ErrorResponse> {
    let response = builder.send().await;
    let response = match response {
        Ok(response) => response,
        Err(e) => {
            error!("request_login: Failed to get response: {:?}", e);
            return Err(ErrorResponse::Error {
                error_code: ErrorCode::NetworkError,
                message: ErrorCode::NetworkError.message(None).to_string(),
            });
        }
    };

    if LOG_RESPONSE_BODY {
        debug!("request_login: Parse >>>> {:?}", response.status());
    }

    match (response.status(), response.headers().get("Authorization")) {
        (StatusCode::OK, Some(token)) => {
            // Login successful, let's get the access token for real.
            let access_token = match token.to_str() {
                Ok(token) => token.to_string(),
                Err(_) => {
                    return Err(ErrorResponse::Error {
                        error_code: ErrorCode::NetworkError,
                        message: ErrorCode::NetworkError.message(None).to_string(),
                    });
                }
            };
            Ok(AccountStatus::Ready { access_token })
        }
        (StatusCode::OK, None) => {
            // Silent login from oauth. Need improvement.
            let error_response = match parse_error_response::<ErrorResponse>(response).await {
                Ok(error) => error,
                Err(_) => return Ok(AccountStatus::NotFound),
            };
            match error_response {
                ErrorResponse::Error {
                    error_code,
                    message: _,
                } => match error_code {
                    ErrorCode::EmailNotVerified => Ok(AccountStatus::Incomplete {
                        status: smbcloud_model::account::ErrorCode::EmailUnverified,
                    }),
                    ErrorCode::PasswordNotSet => Ok(AccountStatus::Incomplete {
                        status: smbcloud_model::account::ErrorCode::PasswordNotSet,
                    }),
                    ErrorCode::Unknown => Ok(AccountStatus::Ready {
                        access_token: "tokenization".to_string(),
                    }),
                    _ => Ok(AccountStatus::NotFound),
                },
            }
        }
        (StatusCode::NOT_FOUND, _) => {
            // Account not found
            Ok(AccountStatus::NotFound)
        }
        (StatusCode::UNPROCESSABLE_ENTITY, _) => {
            let body_text = match response.text().await {
                Ok(text) => text,
                Err(_) => {
                    return Err(ErrorResponse::Error {
                        error_code: ErrorCode::NetworkError,
                        message: ErrorCode::NetworkError.message(None).to_string(),
                    });
                }
            };

            let result: SmbAuthorization = match serde_json::from_str(&body_text) {
                Ok(res) => res,
                Err(_) => {
                    // Try generic JSON for a useful message.
                    let message = serde_json::from_str::<serde_json::Value>(&body_text)
                        .ok()
                        .and_then(|v| {
                            v.get("message")
                                .and_then(serde_json::Value::as_str)
                                .map(ToOwned::to_owned)
                        })
                        .unwrap_or_else(|| ErrorCode::NetworkError.message(None).to_string());
                    return Err(ErrorResponse::Error {
                        error_code: ErrorCode::NetworkError,
                        message,
                    });
                }
            };

            let error_code = match result.error_code {
                Some(code) => code,
                None => {
                    // No error_code but we have a message — return it as Incomplete with a
                    // generic unverified status so the client can display the message.
                    return Err(ErrorResponse::Error {
                        error_code: ErrorCode::NetworkError,
                        message: result.message,
                    });
                }
            };
            Ok(AccountStatus::Incomplete { status: error_code })
        }
        (StatusCode::UNAUTHORIZED, _) => {
            let body_text = match response.text().await {
                Ok(text) => text,
                Err(_) => {
                    return Err(ErrorResponse::Error {
                        error_code: ErrorCode::NetworkError,
                        message: ErrorCode::NetworkError.message(None).to_string(),
                    });
                }
            };

            // Try parsing as SmbAuthorization first.
            if let Ok(result) = serde_json::from_str::<SmbAuthorization>(&body_text) {
                let error_code = match result.error_code {
                    Some(code) => code,
                    None => {
                        // Response parsed but no error_code — surface the server's message.
                        return Err(ErrorResponse::Error {
                            error_code: ErrorCode::Unauthorized,
                            message: result.message,
                        });
                    }
                };
                return Err(ErrorResponse::Error {
                    error_code: ErrorCode::Unauthorized,
                    message: error_code.to_string(),
                });
            }

            // Fallback: try to extract message or error from a generic JSON object.
            if let Ok(generic) = serde_json::from_str::<serde_json::Value>(&body_text) {
                let extracted = generic
                    .get("message")
                    .or_else(|| generic.get("error"))
                    .and_then(serde_json::Value::as_str)
                    .unwrap_or("Sign in failed.");
                return Err(ErrorResponse::Error {
                    error_code: ErrorCode::Unauthorized,
                    message: extracted.to_string(),
                });
            }

            Err(ErrorResponse::Error {
                error_code: ErrorCode::Unauthorized,
                message: "Sign in failed.".to_string(),
            })
        }
        (status, _) => parse_error_response(response)
            .await
            .map_err(|_| ErrorResponse::Error {
                error_code: ErrorCode::NetworkError,
                message: format!("Unexpected login response status: {}", status),
            }),
    }
}

pub async fn request<R: DeserializeOwned>(builder: RequestBuilder) -> Result<R, ErrorResponse> {
    let response = builder.send().await;
    let response = match response {
        Ok(response) => response,
        Err(e) => {
            error!("Failed to get response: {:?}", e);
            log_send_error(&e);
            return Err(ErrorResponse::Error {
                error_code: ErrorCode::NetworkError,
                message: ErrorCode::NetworkError.message(None).to_string(),
            });
        }
    };
    let response = match response.status() {
        reqwest::StatusCode::OK | reqwest::StatusCode::CREATED => response,
        status => {
            error!("Failed to get response: {:?}", status);
            // This should handle parsing the error response.
            return parse_error_response(response).await;
        }
    };

    let response_body = match response.text().await {
        Ok(body) => body,
        Err(e) => {
            error!("Failed to get response body: {:?}", e);
            return Err(ErrorResponse::Error {
                error_code: ErrorCode::NetworkError,
                message: ErrorCode::NetworkError.message(None).to_string(),
            });
        }
    };

    if LOG_RESPONSE_BODY {
        debug!(
            "Parse >>>> {:?}",
            serde_json::to_string_pretty(&response_body)
        );
    }

    let response = match serde_json::from_str::<R>(&response_body) {
        Ok(response) => response,
        Err(e) => {
            error!("Failed to parse response: {:?}", e);
            return Err(ErrorResponse::Error {
                error_code: ErrorCode::ParseError,
                message: e.to_string(),
            });
        }
    };

    Ok(response)
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;
    use serde_json::Value;
    use std::time::{Duration, Instant};

    #[tokio::test]
    async fn unreachable_host_reports_network_error_quickly() {
        let started = Instant::now();
        let builder = reqwest::Client::new().get("http://127.0.0.1:1");
        let result = request::<Value>(builder).await;
        match result {
            Err(ErrorResponse::Error { error_code, .. }) => {
                assert!(matches!(error_code, ErrorCode::NetworkError));
            }
            other => panic!("expected NetworkError, got {other:?}"),
        }
        assert!(started.elapsed() < Duration::from_secs(5));
    }

    #[test]
    fn workspace_does_not_call_the_connectivity_preflight() {
        let crates = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
        let mut callers = Vec::new();
        let mut stack = vec![crates];
        while let Some(dir) = stack.pop() {
            for entry in std::fs::read_dir(&dir).unwrap().flatten() {
                let path = entry.path();
                if path.is_dir() {
                    stack.push(path);
                } else if path.extension().is_some_and(|e| e == "rs")
                    && !path.ends_with("smbcloud-network/src/network.rs")
                    && std::fs::read_to_string(&path)
                        .unwrap()
                        .contains("check_internet_connection")
                {
                    callers.push(path);
                }
            }
        }
        assert!(
            callers.is_empty(),
            "preflight still called from {callers:?}"
        );
    }
}

#[cfg(test)]
mod bearer_tests {
    use super::bearer_value;

    #[test]
    fn adds_one_prefix() {
        assert_eq!(bearer_value("Bearer x"), "Bearer x");
        assert_eq!(bearer_value("bearer x"), "Bearer x");
        assert_eq!(bearer_value("x"), "Bearer x");
        assert_eq!(bearer_value("smb_deploy_abc"), "Bearer smb_deploy_abc");
    }
}