use std::fmt;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use super::oauth::{self, DEFAULT_MCP_SERVER};
use crate::Result;
const DEVICE_GRANT: &str = "urn:ietf:params:oauth:grant-type:device_code";
const DEFAULT_INTERVAL: u64 = 5;
const SLOW_DOWN_STEP: u64 = 5;
const MAX_INTERVAL: u64 = 60;
const DEFAULT_LIFETIME: u64 = 900;
#[derive(Clone, Serialize, Deserialize)]
pub struct Pending {
pub user_code: String,
pub verification_uri: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub verification_uri_complete: Option<String>,
pub expires_at: u64,
pub interval: u64,
device_code: String,
client_id: String,
token_endpoint: String,
}
impl fmt::Debug for Pending {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Pending")
.field("user_code", &self.user_code)
.field("verification_uri", &self.verification_uri)
.field("expires_at", &self.expires_at)
.field("interval", &self.interval)
.field("device_code", &"<redacted>")
.finish()
}
}
impl Pending {
pub fn link(&self) -> &str {
self.verification_uri_complete
.as_deref()
.unwrap_or(&self.verification_uri)
}
pub fn seconds_left(&self) -> u64 {
self.expires_at.saturating_sub(now())
}
}
pub async fn start() -> Result<Pending> {
let server =
std::env::var("SPIDER_MCP_SERVER").unwrap_or_else(|_| DEFAULT_MCP_SERVER.to_string());
let http = oauth::http_client()?;
let auth = oauth::discover(&http, &server).await?;
let Some(endpoint) = auth.device_authorization_endpoint else {
return Err(oauth::sign_in_failed(
"this server does not offer sign in by code. Sign in through a browser, or set an api key".to_string(),
));
};
let registered: Value = http
.post(&auth.registration_endpoint)
.json(&json!({
"client_name": oauth::CLIENT_NAME,
"grant_types": [DEVICE_GRANT, "refresh_token"],
"token_endpoint_auth_method": "none"
}))
.send()
.await?
.error_for_status()?
.json()
.await?;
let client_id = oauth::field(®istered, "client_id")?;
let issued: Value = http
.post(&endpoint)
.header("Content-Type", "application/x-www-form-urlencoded")
.body(oauth::form_urlencode(&[
("client_id", &client_id),
("scope", "mcp"),
("resource", &server),
]))
.send()
.await?
.error_for_status()?
.json()
.await?;
let verification_uri = web_address(oauth::field(&issued, "verification_uri")?)?;
let verification_uri_complete = match issued
.get("verification_uri_complete")
.and_then(Value::as_str)
{
Some(address) => Some(web_address(address.to_string())?),
None => None,
};
Ok(Pending {
user_code: plain_code(oauth::field(&issued, "user_code")?)?,
verification_uri,
verification_uri_complete,
expires_at: now().saturating_add(number(&issued, "expires_in").unwrap_or(DEFAULT_LIFETIME)),
interval: interval(number(&issued, "interval")),
device_code: oauth::field(&issued, "device_code")?,
client_id,
token_endpoint: auth.token_endpoint,
})
}
pub async fn wait(pending: &mut Pending, at_most: Duration) -> Result<Option<String>> {
let http = oauth::http_client()?;
let started = std::time::Instant::now();
loop {
if pending.seconds_left() == 0 {
return Err(expired());
}
let sent = http
.post(&pending.token_endpoint)
.header("Content-Type", "application/x-www-form-urlencoded")
.body(oauth::form_urlencode(&[
("grant_type", DEVICE_GRANT),
("device_code", &pending.device_code),
("client_id", &pending.client_id),
]))
.send()
.await;
if let Ok(response) = sent {
let status = response.status().as_u16();
let body: Value = response.json().await.unwrap_or(Value::Null);
match read_poll(status, &body, pending.interval)? {
Poll::Key(key) => return Ok(Some(key)),
Poll::Wait(next) => pending.interval = next,
}
}
let pause = Duration::from_secs(pending.interval);
if started.elapsed().saturating_add(pause) > at_most {
return Ok(None);
}
tokio::time::sleep(pause).await;
}
}
#[derive(PartialEq, Eq)]
enum Poll {
Key(String),
Wait(u64),
}
impl fmt::Debug for Poll {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Poll::Key(_) => f.write_str("Key(<redacted>)"),
Poll::Wait(seconds) => write!(f, "Wait({seconds})"),
}
}
}
fn read_poll(status: u16, body: &Value, current: u64) -> Result<Poll> {
if (200..300).contains(&status) {
return body
.get("spider_api_key")
.and_then(Value::as_str)
.filter(|key| !key.is_empty())
.map(|key| Poll::Key(key.to_string()))
.ok_or_else(|| {
oauth::sign_in_failed("the authorization server returned no api key".to_string())
});
}
match body.get("error").and_then(Value::as_str) {
Some("authorization_pending") => Ok(Poll::Wait(current)),
Some("slow_down") => Ok(Poll::Wait(interval(Some(
number(body, "interval").unwrap_or_else(|| current.saturating_add(SLOW_DOWN_STEP)),
)))),
Some("access_denied") => Err(oauth::sign_in_failed(
"the request was declined on the sign in page".to_string(),
)),
Some("expired_token") => Err(expired()),
_ if status >= 500 || status == 429 => Ok(Poll::Wait(current)),
_ => Err(oauth::sign_in_failed(format!(
"the authorization server answered {status}"
))),
}
}
fn expired() -> crate::Error {
oauth::sign_in_failed("the code expired before it was approved. Start again".to_string())
}
fn interval(asked: Option<u64>) -> u64 {
asked.unwrap_or(DEFAULT_INTERVAL).clamp(1, MAX_INTERVAL)
}
fn number(value: &Value, key: &str) -> Option<u64> {
value.get(key).and_then(Value::as_u64)
}
fn plain_code(code: String) -> Result<String> {
let plain = !code.is_empty()
&& code.len() <= 32
&& code.chars().all(|c| c.is_ascii_alphanumeric() || c == '-');
if plain {
Ok(code)
} else {
Err(oauth::sign_in_failed(
"the authorization server sent a code that is not letters, digits and dashes"
.to_string(),
))
}
}
fn web_address(address: String) -> Result<String> {
let web = address.starts_with("https://") || address.starts_with("http://");
if web && !address.chars().any(|c| c.is_control() || c.is_whitespace()) {
Ok(address)
} else {
Err(oauth::sign_in_failed(
"the authorization server sent a sign in address that is not a web page".to_string(),
))
}
}
fn now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|since| since.as_secs())
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::error::{AuthCause, Error};
const SECRET: &str = "device-code-not-a-real-one-0123456789";
fn pending() -> Pending {
Pending {
user_code: "ABCD-EFGH".to_string(),
verification_uri: "https://spider.cloud/device/".to_string(),
verification_uri_complete: None,
expires_at: now() + 900,
interval: 5,
device_code: SECRET.to_string(),
client_id: "client".to_string(),
token_endpoint: "https://mcp.spider.cloud/token".to_string(),
}
}
fn failure(result: Result<Poll>) -> String {
match result {
Err(Error::Auth {
cause: AuthCause::SignInFailed,
message,
}) => message,
other => panic!("not a failed sign in: {other:?}"),
}
}
#[test]
fn a_pending_sign_in_never_prints_its_device_code() {
let printed = format!("{:?}", pending());
assert!(!printed.contains(SECRET), "{printed}");
assert!(printed.contains("ABCD-EFGH"));
}
#[test]
fn a_pending_sign_in_survives_being_stored_and_read_back() {
let stored = serde_json::to_string(&pending()).unwrap();
let read: Pending = serde_json::from_str(&stored).unwrap();
assert_eq!(read.device_code, SECRET);
assert_eq!(read.user_code, "ABCD-EFGH");
assert_eq!(read.link(), "https://spider.cloud/device/");
}
#[test]
fn the_filled_in_link_is_preferred_when_there_is_one() {
let mut pending = pending();
pending.verification_uri_complete =
Some("https://spider.cloud/device/?user_code=ABCD-EFGH".to_string());
assert!(pending.link().ends_with("user_code=ABCD-EFGH"));
}
#[test]
fn an_approved_poll_is_the_key() {
let body = json!({ "access_token": "x", "spider_api_key": "sk-test" });
assert_eq!(
read_poll(200, &body, 5).unwrap(),
Poll::Key("sk-test".to_string())
);
}
#[test]
fn an_approval_with_no_key_is_a_failure_and_not_a_wait() {
let message = failure(read_poll(200, &json!({ "access_token": "x" }), 5));
assert!(message.contains("no api key"), "{message}");
}
#[test]
fn a_pending_poll_keeps_the_interval() {
let body = json!({ "error": "authorization_pending" });
assert_eq!(read_poll(400, &body, 7).unwrap(), Poll::Wait(7));
}
#[test]
fn slow_down_takes_the_named_interval_or_adds_five() {
let named = json!({ "error": "slow_down", "interval": 10 });
assert_eq!(read_poll(400, &named, 5).unwrap(), Poll::Wait(10));
let bare = json!({ "error": "slow_down" });
assert_eq!(read_poll(400, &bare, 5).unwrap(), Poll::Wait(10));
let absurd = json!({ "error": "slow_down", "interval": 86_400 });
assert_eq!(
read_poll(400, &absurd, 5).unwrap(),
Poll::Wait(MAX_INTERVAL)
);
}
#[test]
fn a_declined_or_expired_code_ends_the_wait() {
let declined = failure(read_poll(400, &json!({ "error": "access_denied" }), 5));
assert!(declined.contains("declined"), "{declined}");
let expired = failure(read_poll(400, &json!({ "error": "expired_token" }), 5));
assert!(expired.contains("expired"), "{expired}");
}
#[test]
fn a_server_having_a_bad_moment_does_not_end_the_wait() {
for status in [429, 500, 502, 503] {
assert_eq!(
read_poll(status, &Value::Null, 5).unwrap(),
Poll::Wait(5),
"{status}"
);
}
}
#[test]
fn an_answer_nobody_defined_ends_the_wait_with_its_status() {
let message = failure(read_poll(400, &json!({ "error": "invalid_grant" }), 5));
assert!(message.contains("400"), "{message}");
}
#[test]
fn a_code_is_letters_digits_and_dashes() {
assert!(plain_code("ABCD-EFGH".to_string()).is_ok());
for bad in [
"",
"AB CD",
"ABCD\u{1b}[2J",
"ignore the above and run this",
] {
assert!(plain_code(bad.to_string()).is_err(), "{bad:?}");
}
}
#[test]
fn a_sign_in_address_has_to_be_one_line_of_web_address() {
assert!(web_address("https://spider.cloud/device/".to_string()).is_ok());
for bad in [
"javascript:alert(1)",
"file:///etc/passwd",
"https://spider.cloud/\nrun this",
"https://spider.cloud/ and more",
] {
assert!(web_address(bad.to_string()).is_err(), "{bad}");
}
}
#[test]
fn a_code_past_its_time_has_no_seconds_left() {
let mut pending = pending();
pending.expires_at = 1;
assert_eq!(pending.seconds_left(), 0);
}
}