Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ GITHUB_TOKEN=
# 必須 (既定値なし。未設定なら起動時にエラーで止まる)
# トークンの確認を頼む、認証サービスの内部の URL。公開しない (トンネルに載せない) 口で、同じネットワークからだけ届くようにする
AUTH_INTERNAL_URL=http://auth.internal.example:8081
# 任意 (秘密)。auth の INTROSPECT_SECRET と同じ値。auth が共有秘密を求めるときに、確認 (/introspect) へ X-Toro-Token として付ける
# AUTH_INTROSPECT_SECRET=
# Minecraft サーバー (入り口のプロキシ) の稼働確認の接続先 (host:port)
MC_STATUS_ADDR=mc.internal.example:25565

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "toro-api"
version = "1.3.0"
version = "1.3.1"
edition = "2024"
description = "TORO Server の REST API (api.torosaba.net)"
publish = false
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct Config {
pub wiki_url: String,
/// トークンの確認を頼む、auth の内部の URL (末尾の / なし。公開しない)
pub auth_internal_url: String,
/// auth の `/introspect` へ付ける共有秘密 (`X-Toro-Token`)。auth が `INTROSPECT_SECRET` を設定したときに要る。無ければ付けない
pub auth_introspect_secret: Option<String>,
/// Cookie 付きで API を呼べる Origin (他のサブドメインは信じない)
pub cors_origins: Vec<String>,

Expand Down Expand Up @@ -136,6 +138,7 @@ impl Config {
mcp_url: trimmed("PUBLIC_MCP_URL", "https://mcp.torosaba.net"),
wiki_url: trimmed("PUBLIC_WIKI_URL", "https://wiki.torosaba.net"),
auth_internal_url,
auth_introspect_secret: optional("AUTH_INTROSPECT_SECRET"),
cors_origins: or_default("CORS_ORIGINS", "https://wiki.torosaba.net")
.split(',')
.map(|origin| origin.trim().to_string())
Expand Down
11 changes: 7 additions & 4 deletions src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ pub async fn identify(app: &App, ctx: &Ctx) -> Identity {

async fn introspect(app: &App, token: &str) -> Identity {
let url = format!("{}/introspect", app.cfg.auth_internal_url);
let response = app
let mut request = app
.http
.post(&url)
.timeout(INTROSPECT_TIMEOUT)
.json(&json!({ "token": token }))
.send()
.await;
.json(&json!({ "token": token }));
// auth が共有秘密を求めるとき (INTROSPECT_SECRET) の呼び出し側の証明
if let Some(secret) = &app.cfg.auth_introspect_secret {
request = request.header("X-Toro-Token", secret);
}
let response = request.send().await;
let body = match response {
Ok(res) if res.status().is_success() => res.json::<Value>().await.ok(),
Ok(res) => {
Expand Down
1 change: 1 addition & 0 deletions tests/common/fake_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub fn base_config() -> Config {
mcp_url: "https://mcp.torosaba.net".into(),
wiki_url: "https://wiki.torosaba.net".into(),
auth_internal_url: "http://127.0.0.1:9".into(),
auth_introspect_secret: None,
cors_origins: vec!["https://wiki.torosaba.net".into()],
github_token: "gh-token".into(),
github_owner: "TORO-Server".into(),
Expand Down
10 changes: 10 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub struct FakeAuth {
/// true の間は、何を聞かれても 500 を返す
pub broken: AtomicBool,
pub requests: Mutex<Vec<String>>,
/// 各リクエストの `X-Toro-Token` (無ければ None)
pub secrets: Mutex<Vec<Option<String>>>,
}

async fn start_fake_auth(state: Arc<FakeAuth>) -> String {
Expand All @@ -69,6 +71,13 @@ async fn start_fake_auth(state: Arc<FakeAuth>) -> String {
.and_then(|v| v["token"].as_str().map(str::to_string))
.unwrap_or_default();
state.requests.lock().unwrap().push(token.clone());
state.secrets.lock().unwrap().push(
parts
.headers
.get("x-toro-token")
.and_then(|v| v.to_str().ok())
.map(str::to_string),
);
match state.tokens.lock().unwrap().get(&token) {
Some(claims) => json_response(200, json!({ "active": true, "claims": claims })),
None => json_response(200, json!({ "active": false })),
Expand Down Expand Up @@ -245,6 +254,7 @@ pub async fn start_with(customize: impl FnOnce(&mut Config)) -> TestServer {
mcp_url: "https://mcp.torosaba.net".into(),
wiki_url: WIKI_ORIGIN.into(),
auth_internal_url: start_fake_auth(auth).await,
auth_introspect_secret: None,
cors_origins: vec![WIKI_ORIGIN.into()],
github_token: "gh-token".into(),
github_owner: "TORO-Server".into(),
Expand Down
31 changes: 31 additions & 0 deletions tests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,34 @@ async fn server_status_reports_a_running_server() {
let again: Value = server.get("/server/status").await.json().await.unwrap();
assert_eq!(again, body);
}

#[tokio::test]
async fn the_shared_secret_goes_to_auth_only_when_configured() {
// 設定が無いときは、ヘッダーを付けない (auth が秘密を求めない間の、これまでの動き)
let server = start().await;
let res = server
.client
.get(server.url("/me"))
.bearer_auth(MEMBER_TOKEN)
.send()
.await
.unwrap();
assert_eq!(res.status(), 200);
assert_eq!(*server.auth.secrets.lock().unwrap(), vec![None::<String>]);

// 設定したときは、auth への確認のたびに同じ値を付ける
let server =
start_with(|cfg| cfg.auth_introspect_secret = Some("shared-secret-value".into())).await;
let res = server
.client
.get(server.url("/me"))
.bearer_auth(MEMBER_TOKEN)
.send()
.await
.unwrap();
assert_eq!(res.status(), 200);
assert_eq!(
*server.auth.secrets.lock().unwrap(),
vec![Some("shared-secret-value".to_string())]
);
}