zino_auth/authorization_provider.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
use super::ClientCredentials;
use zino_core::error::Error;
/// A server which provides authorization services.
///
/// # Examples
///
/// ```rust,ignore
/// use zino_core::{
/// auth::{AuthorizationProvider, ClientCredentials},
/// connector::HttpConnector,
/// error::Error,
/// extension::JsonObjectExt,
/// json,
/// state::State,
/// LazyLock, Map,
/// };
///
/// #[derive(Debug, Clone, Copy)]
/// pub struct DingtalkAuth;
///
/// impl DingtalkAuth {
/// pub async fn get_token() -> Result<String, Error> {
/// DINGTALK_CREDENTIALS.request().await
/// }
/// }
///
/// impl AuthorizationProvider for DingtalkAuth {
/// async fn grant_client_credentials(
/// client_credentials: &ClientCredentials<Self>,
/// ) -> Result<(), Error> {
/// let params = client_credentials.to_request_params();
/// let data: Map = DINGTALK_TOKEN_CONNECTOR.fetch_json(None, Some(¶ms)).await?;
/// if let Some(access_token) = data.get_str("access_token") {
/// client_credentials.set_access_token(access_token);
/// client_credentials.set_expires(std::time::Duration::from_secs(6600));
/// }
/// Ok(())
/// }
/// }
///
/// static DINGTALK_CREDENTIALS: LazyLock<ClientCredentials<DingtalkAuth>> = LazyLock::new(|| {
/// let config = State::shared()
/// .get_config("dingtalk")
/// .expect("the `dingtalk` field should be a table");
/// ClientCredentials::try_from_config(config)
/// .expect("fail to create the Dingtalk credentials")
/// });
///
/// static DINGTALK_TOKEN_CONNECTOR: LazyLock<HttpConnector> = LazyLock::new(|| {
/// let base_url = "https://oapi.dingtalk.com/gettoken";
/// connector = HttpConnector::try_new("GET", base_url)
/// .expect("fail to construct DingTalk token connector")
/// .query_param("appkey", Some("client_key"))
/// .query_param("appsecret", Some("client_secret"))
/// .build_query()
/// .expect("fail to build a query template for the connector")
/// });
/// ```
pub trait AuthorizationProvider {
/// Grants an access token for the client credentials.
async fn grant_client_credentials(
client_credentials: &ClientCredentials<Self>,
) -> Result<(), Error>;
}