use crate::claims::Claims;
use crate::error::Result;
use crate::signer::{Algorithm, JwtSigner};
pub struct GitHubAppJwt {
app_id: String,
pem_bytes: Vec<u8>,
}
impl GitHubAppJwt {
pub fn new(app_id: impl Into<String>, pem_bytes: impl Into<Vec<u8>>) -> Self {
Self {
app_id: app_id.into(),
pem_bytes: pem_bytes.into(),
}
}
pub async fn generate(&self) -> Result<String> {
let now = now_unix_secs();
let claims = Claims {
iss: Some(self.app_id.clone()),
iat: Some(now.saturating_sub(60)),
exp: Some(now + 600),
..Default::default()
};
let signer = JwtSigner::new(Algorithm::Rs256, &self.pem_bytes).await?;
signer.sign(&claims).await
}
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
fn now_unix_secs() -> u64 {
(js_sys::Date::now() / 1000.0) as u64
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
fn now_unix_secs() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("system clock before UNIX epoch")
.as_secs()
}