use std::path::PathBuf; use forge_core::lfs_token::{self, LfsOp}; use forge_core::time::now_unix; use forge_core::{Repo, User}; use forge_git::GitService; use forge_ssh::{GitBackend, LfsOperation, RepoPath}; use crate::access::{self, AccessError, Transport}; use crate::state::AppState; pub struct SshBackend { pub state: AppState, } const MSG_NOT_FOUND: &str = "repository not found"; const MSG_DENIED: &str = "permission denied"; const MSG_INTERNAL: &str = "internal error"; const LFS_TOKEN_TTL_SECS: i64 = 3600; impl SshBackend { async fn authorize( &self, user_id: &str, owner: &str, name: &str, service: GitService, ) -> Result<(User, Repo, PathBuf), String> { let user = match self.state.store.user_by_id(user_id).await { Ok(Some(user)) if !user.is_org() => user, Ok(_) => return Err(MSG_DENIED.to_string()), Err(e) => { tracing::error!(error = %e, "ssh: user lookup failed"); return Err(MSG_INTERNAL.to_string()); } }; match self.state.store.suspension(&user.id).await { Ok(None) => {} Ok(Some(_)) => return Err(MSG_DENIED.to_string()), Err(e) => { tracing::error!(error = %e, "ssh: suspension lookup failed"); return Err(MSG_INTERNAL.to_string()); } } match access::resolve_repo(&self.state, Some(&user), owner, name, service).await { Ok((repo, path)) => Ok((user, repo, path)), Err(AccessError::NotFound | AccessError::Unauthorized) => { Err(MSG_NOT_FOUND.to_string()) } Err(AccessError::Forbidden) => Err(MSG_DENIED.to_string()), Err(AccessError::Internal(detail)) => { tracing::error!(detail = %detail, "ssh: repo resolution failed"); Err(MSG_INTERNAL.to_string()) } } } } impl GitBackend for SshBackend { async fn authenticate_key(&self, _algorithm: &str, key_blob: &[u8]) -> Option { let key = match self.state.store.ssh_key_by_blob(key_blob).await { Ok(Some(key)) => key, Ok(None) => return None, Err(e) => { tracing::warn!(error = %e, "ssh key lookup failed"); return None; } }; if let Err(e) = self.state.store.touch_ssh_key(&key.id).await { tracing::warn!(error = %e, "could not record ssh key use"); } crate::live::bump_account(&self.state, &key.user_id); match self.state.store.user_by_id(&key.user_id).await { Ok(Some(owner)) if !owner.is_org() => Some(key.user_id), _ => None, } } async fn prepare( &self, user_id: &str, service: GitService, repo_path: &RepoPath, git_protocol: Option<&str>, ) -> Result { let (user, repo, path) = self .authorize(user_id, &repo_path.owner, &repo_path.name, service) .await?; Ok(access::git_process( &self.state, &repo, &path, Some(&user), service, Transport::Ssh, false, git_protocol, ) .into_tokio()) } async fn lfs_authenticate( &self, user_id: &str, repo_path: &RepoPath, operation: LfsOperation, ) -> Result { let (service, op) = match operation { LfsOperation::Download => (GitService::UploadPack, LfsOp::Download), LfsOperation::Upload => (GitService::ReceivePack, LfsOp::Upload), }; let (_user, repo, _path) = self .authorize(user_id, &repo_path.owner, &repo_path.name, service) .await?; if operation == LfsOperation::Upload { match self.state.store.pull_mirror(&repo.id).await { Ok(None) => {} Ok(Some(_)) => return Err(MSG_DENIED.to_string()), Err(e) => { tracing::error!(error = %e, "ssh: pull mirror lookup failed"); return Err(MSG_INTERNAL.to_string()); } } } let token = lfs_token::issue( &self.state.key, &repo.id, op, now_unix() + LFS_TOKEN_TTL_SECS, ); let base = self.state.config.base_url.trim_end_matches('/'); let reply = serde_json::json!({ "href": format!("{base}/{}/{}.git/info/lfs", repo.owner_name, repo.name), "header": { "Authorization": format!("RemoteAuth {token}") }, "expires_in": LFS_TOKEN_TTL_SECS, }); Ok(reply.to_string()) } } #[cfg(test)] mod org_tests { use super::*; #[tokio::test] async fn an_org_key_does_not_authenticate() { let dir = tempfile::tempdir().unwrap(); let state = crate::test_support::test_state(dir.path()).await; let tahinli = state .store .create_user("tahinli", None, "h", false) .await .unwrap(); let org = state.store.create_org("acme", &tahinli.id).await.unwrap(); let org_blob = b"org-key-blob"; let person_blob = b"person-key-blob"; state .store .add_ssh_key(&org.id, "ci", "ssh-ed25519", org_blob, "SHA256:org") .await .unwrap(); state .store .add_ssh_key( &tahinli.id, "laptop", "ssh-ed25519", person_blob, "SHA256:me", ) .await .unwrap(); let backend = SshBackend { state }; assert_eq!( backend.authenticate_key("ssh-ed25519", org_blob).await, None ); assert_eq!( backend.authenticate_key("ssh-ed25519", person_blob).await, Some(tahinli.id.clone()) ); } }