use client::{Client, ClientOptions};
use config::get_config;
use errors::Error;
use license::{License, SchemeCode};
use serde::{Deserialize, Serialize};
use crate::config::KeygenConfig;
pub(crate) fn insert_optional<T: Serialize>(
map: &mut serde_json::Map<String, serde_json::Value>,
key: &str,
value: Option<T>,
) -> Result<(), Error> {
if let Some(v) = value {
map.insert(key.to_string(), serde_json::to_value(v)?);
}
Ok(())
}
pub(crate) mod certificate;
pub(crate) mod client;
pub(crate) mod decryptor;
pub(crate) mod verifier;
pub mod component;
pub mod config;
pub mod entitlement;
pub mod errors;
pub mod group;
pub mod license;
pub mod license_file;
pub mod machine;
pub mod machine_file;
pub mod service;
#[cfg(feature = "token")]
pub mod environment;
#[cfg(feature = "token")]
pub mod policy;
#[cfg(feature = "token")]
pub mod product;
#[cfg(feature = "token")]
pub mod release;
#[cfg(feature = "token")]
pub mod token;
#[cfg(feature = "token")]
pub mod user;
#[cfg(feature = "token")]
pub mod webhook;
#[cfg(feature = "token")]
pub mod arch;
#[cfg(feature = "token")]
pub mod artifact;
#[cfg(feature = "token")]
pub mod channel;
#[cfg(feature = "token")]
pub mod package;
#[cfg(feature = "token")]
pub mod platform;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct KeygenRelationshipData {
pub r#type: String,
pub id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct KeygenRelationship {
#[serde(default)]
pub data: Option<KeygenRelationshipData>,
#[serde(default)]
pub links: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub(crate) struct KeygenRelationships {
#[serde(default)]
pub policy: Option<KeygenRelationship>,
#[serde(default)]
pub account: Option<KeygenRelationship>,
#[serde(default)]
pub product: Option<KeygenRelationship>,
#[serde(default)]
pub group: Option<KeygenRelationship>,
#[serde(default)]
pub owner: Option<KeygenRelationship>,
#[serde(default)]
pub users: Option<KeygenRelationship>,
#[serde(default)]
pub machines: Option<KeygenRelationship>,
#[serde(default)]
pub environment: Option<KeygenRelationship>,
#[serde(default)]
pub license: Option<KeygenRelationship>,
#[serde(default)]
pub release: Option<KeygenRelationship>,
#[serde(flatten)]
pub other: std::collections::HashMap<String, serde_json::Value>,
}
impl KeygenRelationships {
pub(crate) fn extract_id(rel: &Option<KeygenRelationship>) -> Option<String> {
rel.as_ref()
.and_then(|r| r.data.as_ref().map(|d| d.id.clone()))
}
pub(crate) fn policy_id(&self) -> Option<String> {
Self::extract_id(&self.policy)
}
pub(crate) fn account_id(&self) -> Option<String> {
Self::extract_id(&self.account)
}
pub(crate) fn product_id(&self) -> Option<String> {
Self::extract_id(&self.product)
}
pub(crate) fn group_id(&self) -> Option<String> {
Self::extract_id(&self.group)
}
pub(crate) fn owner_id(&self) -> Option<String> {
Self::extract_id(&self.owner)
}
pub(crate) fn environment_id(&self) -> Option<String> {
Self::extract_id(&self.environment)
}
pub(crate) fn license_id(&self) -> Option<String> {
Self::extract_id(&self.license)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct KeygenResponseData<T> {
pub id: String,
pub r#type: String,
pub attributes: T,
pub relationships: KeygenRelationships,
}
pub async fn validate(fingerprints: &[String], entitlements: &[String]) -> Result<License, Error> {
let config = get_config()?;
validate_with_config(config, fingerprints, entitlements).await
}
pub async fn validate_with_config(
config: KeygenConfig,
fingerprints: &[String],
entitlements: &[String],
) -> Result<License, Error> {
let client = Client::new(ClientOptions::from(config.clone()))?;
let response = client.get("me", None::<&()>).await?;
let profile: license::LicenseResponse<()> = serde_json::from_value(response.body)?;
License::from(profile.data)
.with_config(config)
.validate_key(fingerprints, entitlements)
.await
}
#[must_use = "verification result should be checked"]
pub fn verify(scheme: SchemeCode, signed_key: &str) -> Result<Vec<u8>, Error> {
let config = get_config()?;
verify_with_config(&config, scheme, signed_key)
}
#[must_use = "verification result should be checked"]
pub fn verify_with_config(
config: &config::KeygenConfig,
scheme: SchemeCode,
signed_key: &str,
) -> Result<Vec<u8>, Error> {
License::from_signed_key(scheme, signed_key)
.with_config(config.clone())
.verify()
}