use crate::{Client, detail_from_data, model::GhItemDetail, queries, transport, validate};
use serde_json::{Value, json};
use std::sync::Arc;
use subspace_core::{
credentials::{Credential, Credentials},
http::{Error, status},
};
const BOUND_VIEWER_QUERY: &str = "query ViewerBound { viewer { id login } }";
const MAX_ACCOUNT_ID_BYTES: usize = 256;
mod packages;
pub use packages::{
Organization, PackageRelease, PackageRepository, ReleaseAsset, RepositoryRef,
ResolvedRepository, newest_package_release,
};
pub struct BoundRead {
pub(super) credential: Credential,
credentials: Arc<Credentials>,
endpoint: String,
account: String,
account_id: String,
}
impl BoundRead {
pub(crate) fn bind(client: &Client, login: &str) -> Result<Self, Error> {
validate::check_login(login)?;
let mut bound = BoundRead {
credential: client.credential(Some(login))?,
credentials: client.credentials.clone(),
endpoint: client.api_url.clone(),
account: login.to_owned(),
account_id: String::new(),
};
let data = bound.graphql(BOUND_VIEWER_QUERY, json!({}))?;
let actual = data["viewer"]["login"]
.as_str()
.ok_or_else(|| transport::unexpected("viewer"))?;
validate::check_login(actual)?;
if !actual.eq_ignore_ascii_case(login) {
return Err(transport::status(
status::UNAUTHORIZED,
"The GitHub credential belongs to another account.",
));
}
bound.account = actual.to_ascii_lowercase();
bound.account_id = verified_account_id(&data)?.to_owned();
Ok(bound)
}
}
impl BoundRead {
pub fn account(&self) -> &str {
&self.account
}
pub fn account_id(&self) -> &str {
&self.account_id
}
pub fn endpoint(&self) -> &str {
&self.endpoint
}
pub fn item(&self, key: &str) -> Result<GhItemDetail, Error> {
let item = validate::check_key(key)?;
let variables = json!({"owner":item.owner,"name":item.name,"number":item.number});
let data = self.graphql(&queries::detail_query(), variables)?;
let data = serde_json::from_value(data).map_err(|_| transport::unexpected("response"))?;
detail_from_data(data, &self.account)
}
pub(super) fn graphql(&self, query: &str, variables: Value) -> Result<Value, Error> {
let body = json!({"query":query,"variables":variables}).to_string();
let response = transport::post_bound_graphql(
&format!("{}/graphql", self.endpoint),
&self.credential,
&self.account,
&body,
);
if matches!(
&response,
Err(Error::Status {
status: status::UNAUTHORIZED,
..
})
) {
self.credentials.invalidate(&self.credential);
}
transport::graphql_data(&self.account, response?, false).map_err(safe_error)
}
}
pub(crate) fn verified_account_id(data: &Value) -> Result<&str, Error> {
data["viewer"]["id"]
.as_str()
.filter(|id| {
!id.trim().is_empty()
&& id.len() <= MAX_ACCOUNT_ID_BYTES
&& !id.chars().any(char::is_control)
})
.ok_or_else(|| transport::unexpected("viewer identity"))
}
fn safe_error(error: Error) -> Error {
match error {
Error::Status { status, .. } => {
transport::status(status, "GitHub rejected the bound read.")
}
_ => Error::Network("GitHub bound read failed.".into()),
}
}