Macro diana::is_authed[][src]

macro_rules! is_authed {
    ($auth_state : expr, { $($key : expr => $value : expr), + }) => { ... };
}
Expand description

Checks to see if the given authentication state matches the series of given claims. This must be provided with the authentication state, a series of claims to check against. It will then return a boolean as to whether or not the user is authorized. This should be used instead of if_authed!.

Example

This is a simplified version of the internal logic that publishes data to the subscriptions server.

use diana::{
    errors::{Result, GQLResult, bail, ErrorKind},
    graphql_utils::get_auth_data_from_ctx,
    async_graphql::{Object as GQLObject},
    is_authed,
};

#[derive(Default, Clone)]
pub struct PublishMutation;
#[GQLObject]
impl PublishMutation {
    async fn publish(
        &self,
        raw_ctx: &async_graphql::Context<'_>,
        channel: String,
        data: String,
    ) -> Result<bool> {
        if is_authed!(
            get_auth_data_from_ctx(raw_ctx)?,
            {
                "role" => "graphql_server"
            }
        ) {
            // Your code here
            Ok(true)
        } else {
            // Your error handling code here
            bail!(ErrorKind::Unauthorised)
        }
    }
}