Skip to main content

AuthClient

Struct AuthClient 

Source
pub struct AuthClient { /* private fields */ }

Implementations§

Source§

impl AuthClient

Source

pub async fn health(&self) -> Result<Value>

Examples found in repository?
examples/support/utils.rs (line 82)
60pub async fn create_client() -> Result<Client, Box<dyn Error>> {
61    let endpoint =
62        env::var("MUBIT_ENDPOINT").unwrap_or_else(|_| "http://127.0.0.1:3000".to_string());
63    let grpc_endpoint =
64        env::var("MUBIT_GRPC_ENDPOINT").unwrap_or_else(|_| "127.0.0.1:50051".to_string());
65    let timeout_ms = env::var("MUBIT_TIMEOUT_MS")
66        .ok()
67        .and_then(|raw| raw.parse::<u64>().ok())
68        .unwrap_or(30_000);
69
70    let api_key =
71        env::var("MUBIT_API_KEY").map_err(|_| boxed_error("MUBIT_API_KEY must be set"))?;
72
73    let mut cfg = ClientConfig::new(endpoint).transport("http");
74    cfg.grpc_endpoint = Some(grpc_endpoint);
75    cfg.timeout_ms = timeout_ms;
76    cfg.api_key = Some(api_key.clone());
77
78    let client = Client::new(cfg)?;
79    client.set_transport(TransportMode::Http);
80    client.set_api_key(Some(api_key));
81
82    let _ = client.auth.health().await?;
83
84    Ok(client)
85}
More examples
Hide additional examples
examples/internal/auth_lifecycle.rs (line 20)
10async fn main() -> Result<(), Box<dyn Error>> {
11    let name = "internal_auth_lifecycle";
12    let started = Instant::now();
13    let client = create_client().await?;
14    let username = format!("sdk_example_{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis());
15    let mut passed = true;
16    let mut detail = "validated raw auth lifecycle".to_string();
17    let mut metrics = json!({});
18
19    let scenario = async {
20        let health = client.auth.health().await?;
21        let created = client.auth.create_user(json!({"username": username, "role": "user"})).await?;
22        let listed = client.auth.list_users(json!({})).await?;
23        let rotated = client.auth.rotate_user_api_key(json!({"username": username})).await?;
24        let fetched = client.auth.get_user(json!({"username": username})).await?;
25        let revoked = client.auth.revoke_user_api_key(json!({"username": username})).await?;
26        let deleted = client.auth.delete_user(json!({"username": username})).await?;
27        let listed_users = listed
28            .as_array()
29            .cloned()
30            .or_else(|| listed.get("users").and_then(Value::as_array).cloned())
31            .unwrap_or_default();
32        let fetched_user = fetched.get("user").unwrap_or(&fetched);
33
34        require(!health.is_null(), format!("auth health failed: {health}"))?;
35        require(created.get("success").and_then(Value::as_bool).unwrap_or(true) || created.get("api_key").is_some(), format!("create_user failed: {created}"))?;
36        require(listed_users.iter().any(|item| item.get("username").and_then(Value::as_str) == Some(username.as_str())), format!("list_users missing created user: {listed}"))?;
37        require(rotated.get("success").and_then(Value::as_bool).unwrap_or(true) || rotated.get("api_key").is_some(), format!("rotate_user_api_key failed: {rotated}"))?;
38        require(fetched_user.get("username").and_then(Value::as_str) == Some(username.as_str()), format!("get_user failed: {fetched}"))?;
39        require(revoked.get("success").and_then(Value::as_bool).unwrap_or(true), format!("revoke_user_api_key failed: {revoked}"))?;
40        require(deleted.get("success").and_then(Value::as_bool).unwrap_or(true), format!("delete_user failed: {deleted}"))?;
41
42        metrics = json!({"username": username});
43        Ok::<(), Box<dyn Error>>(())
44    }.await;
45
46    if let Err(err) = scenario { passed = false; detail = err.to_string(); }
47    print_summary(name, passed, &detail, &metrics, started.elapsed().as_secs_f64(), true);
48    if passed { Ok(()) } else { Err(boxed_error(detail)) }
49}
Source

pub fn set_api_key(&self, api_key: Option<String>)

Source

pub fn set_token(&self, token: Option<String>)

Source

pub fn set_run_id(&self, run_id: Option<String>)

Source§

impl AuthClient

Source

pub async fn create_user<T: Serialize>(&self, payload: T) -> Result<Value>

Examples found in repository?
examples/internal/auth_lifecycle.rs (line 21)
10async fn main() -> Result<(), Box<dyn Error>> {
11    let name = "internal_auth_lifecycle";
12    let started = Instant::now();
13    let client = create_client().await?;
14    let username = format!("sdk_example_{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis());
15    let mut passed = true;
16    let mut detail = "validated raw auth lifecycle".to_string();
17    let mut metrics = json!({});
18
19    let scenario = async {
20        let health = client.auth.health().await?;
21        let created = client.auth.create_user(json!({"username": username, "role": "user"})).await?;
22        let listed = client.auth.list_users(json!({})).await?;
23        let rotated = client.auth.rotate_user_api_key(json!({"username": username})).await?;
24        let fetched = client.auth.get_user(json!({"username": username})).await?;
25        let revoked = client.auth.revoke_user_api_key(json!({"username": username})).await?;
26        let deleted = client.auth.delete_user(json!({"username": username})).await?;
27        let listed_users = listed
28            .as_array()
29            .cloned()
30            .or_else(|| listed.get("users").and_then(Value::as_array).cloned())
31            .unwrap_or_default();
32        let fetched_user = fetched.get("user").unwrap_or(&fetched);
33
34        require(!health.is_null(), format!("auth health failed: {health}"))?;
35        require(created.get("success").and_then(Value::as_bool).unwrap_or(true) || created.get("api_key").is_some(), format!("create_user failed: {created}"))?;
36        require(listed_users.iter().any(|item| item.get("username").and_then(Value::as_str) == Some(username.as_str())), format!("list_users missing created user: {listed}"))?;
37        require(rotated.get("success").and_then(Value::as_bool).unwrap_or(true) || rotated.get("api_key").is_some(), format!("rotate_user_api_key failed: {rotated}"))?;
38        require(fetched_user.get("username").and_then(Value::as_str) == Some(username.as_str()), format!("get_user failed: {fetched}"))?;
39        require(revoked.get("success").and_then(Value::as_bool).unwrap_or(true), format!("revoke_user_api_key failed: {revoked}"))?;
40        require(deleted.get("success").and_then(Value::as_bool).unwrap_or(true), format!("delete_user failed: {deleted}"))?;
41
42        metrics = json!({"username": username});
43        Ok::<(), Box<dyn Error>>(())
44    }.await;
45
46    if let Err(err) = scenario { passed = false; detail = err.to_string(); }
47    print_summary(name, passed, &detail, &metrics, started.elapsed().as_secs_f64(), true);
48    if passed { Ok(()) } else { Err(boxed_error(detail)) }
49}
Source

pub async fn rotate_user_api_key<T: Serialize>( &self, payload: T, ) -> Result<Value>

Examples found in repository?
examples/internal/auth_lifecycle.rs (line 23)
10async fn main() -> Result<(), Box<dyn Error>> {
11    let name = "internal_auth_lifecycle";
12    let started = Instant::now();
13    let client = create_client().await?;
14    let username = format!("sdk_example_{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis());
15    let mut passed = true;
16    let mut detail = "validated raw auth lifecycle".to_string();
17    let mut metrics = json!({});
18
19    let scenario = async {
20        let health = client.auth.health().await?;
21        let created = client.auth.create_user(json!({"username": username, "role": "user"})).await?;
22        let listed = client.auth.list_users(json!({})).await?;
23        let rotated = client.auth.rotate_user_api_key(json!({"username": username})).await?;
24        let fetched = client.auth.get_user(json!({"username": username})).await?;
25        let revoked = client.auth.revoke_user_api_key(json!({"username": username})).await?;
26        let deleted = client.auth.delete_user(json!({"username": username})).await?;
27        let listed_users = listed
28            .as_array()
29            .cloned()
30            .or_else(|| listed.get("users").and_then(Value::as_array).cloned())
31            .unwrap_or_default();
32        let fetched_user = fetched.get("user").unwrap_or(&fetched);
33
34        require(!health.is_null(), format!("auth health failed: {health}"))?;
35        require(created.get("success").and_then(Value::as_bool).unwrap_or(true) || created.get("api_key").is_some(), format!("create_user failed: {created}"))?;
36        require(listed_users.iter().any(|item| item.get("username").and_then(Value::as_str) == Some(username.as_str())), format!("list_users missing created user: {listed}"))?;
37        require(rotated.get("success").and_then(Value::as_bool).unwrap_or(true) || rotated.get("api_key").is_some(), format!("rotate_user_api_key failed: {rotated}"))?;
38        require(fetched_user.get("username").and_then(Value::as_str) == Some(username.as_str()), format!("get_user failed: {fetched}"))?;
39        require(revoked.get("success").and_then(Value::as_bool).unwrap_or(true), format!("revoke_user_api_key failed: {revoked}"))?;
40        require(deleted.get("success").and_then(Value::as_bool).unwrap_or(true), format!("delete_user failed: {deleted}"))?;
41
42        metrics = json!({"username": username});
43        Ok::<(), Box<dyn Error>>(())
44    }.await;
45
46    if let Err(err) = scenario { passed = false; detail = err.to_string(); }
47    print_summary(name, passed, &detail, &metrics, started.elapsed().as_secs_f64(), true);
48    if passed { Ok(()) } else { Err(boxed_error(detail)) }
49}
Source

pub async fn revoke_user_api_key<T: Serialize>( &self, payload: T, ) -> Result<Value>

Examples found in repository?
examples/internal/auth_lifecycle.rs (line 25)
10async fn main() -> Result<(), Box<dyn Error>> {
11    let name = "internal_auth_lifecycle";
12    let started = Instant::now();
13    let client = create_client().await?;
14    let username = format!("sdk_example_{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis());
15    let mut passed = true;
16    let mut detail = "validated raw auth lifecycle".to_string();
17    let mut metrics = json!({});
18
19    let scenario = async {
20        let health = client.auth.health().await?;
21        let created = client.auth.create_user(json!({"username": username, "role": "user"})).await?;
22        let listed = client.auth.list_users(json!({})).await?;
23        let rotated = client.auth.rotate_user_api_key(json!({"username": username})).await?;
24        let fetched = client.auth.get_user(json!({"username": username})).await?;
25        let revoked = client.auth.revoke_user_api_key(json!({"username": username})).await?;
26        let deleted = client.auth.delete_user(json!({"username": username})).await?;
27        let listed_users = listed
28            .as_array()
29            .cloned()
30            .or_else(|| listed.get("users").and_then(Value::as_array).cloned())
31            .unwrap_or_default();
32        let fetched_user = fetched.get("user").unwrap_or(&fetched);
33
34        require(!health.is_null(), format!("auth health failed: {health}"))?;
35        require(created.get("success").and_then(Value::as_bool).unwrap_or(true) || created.get("api_key").is_some(), format!("create_user failed: {created}"))?;
36        require(listed_users.iter().any(|item| item.get("username").and_then(Value::as_str) == Some(username.as_str())), format!("list_users missing created user: {listed}"))?;
37        require(rotated.get("success").and_then(Value::as_bool).unwrap_or(true) || rotated.get("api_key").is_some(), format!("rotate_user_api_key failed: {rotated}"))?;
38        require(fetched_user.get("username").and_then(Value::as_str) == Some(username.as_str()), format!("get_user failed: {fetched}"))?;
39        require(revoked.get("success").and_then(Value::as_bool).unwrap_or(true), format!("revoke_user_api_key failed: {revoked}"))?;
40        require(deleted.get("success").and_then(Value::as_bool).unwrap_or(true), format!("delete_user failed: {deleted}"))?;
41
42        metrics = json!({"username": username});
43        Ok::<(), Box<dyn Error>>(())
44    }.await;
45
46    if let Err(err) = scenario { passed = false; detail = err.to_string(); }
47    print_summary(name, passed, &detail, &metrics, started.elapsed().as_secs_f64(), true);
48    if passed { Ok(()) } else { Err(boxed_error(detail)) }
49}
Source

pub async fn list_users<T: Serialize>(&self, payload: T) -> Result<Value>

Examples found in repository?
examples/internal/auth_lifecycle.rs (line 22)
10async fn main() -> Result<(), Box<dyn Error>> {
11    let name = "internal_auth_lifecycle";
12    let started = Instant::now();
13    let client = create_client().await?;
14    let username = format!("sdk_example_{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis());
15    let mut passed = true;
16    let mut detail = "validated raw auth lifecycle".to_string();
17    let mut metrics = json!({});
18
19    let scenario = async {
20        let health = client.auth.health().await?;
21        let created = client.auth.create_user(json!({"username": username, "role": "user"})).await?;
22        let listed = client.auth.list_users(json!({})).await?;
23        let rotated = client.auth.rotate_user_api_key(json!({"username": username})).await?;
24        let fetched = client.auth.get_user(json!({"username": username})).await?;
25        let revoked = client.auth.revoke_user_api_key(json!({"username": username})).await?;
26        let deleted = client.auth.delete_user(json!({"username": username})).await?;
27        let listed_users = listed
28            .as_array()
29            .cloned()
30            .or_else(|| listed.get("users").and_then(Value::as_array).cloned())
31            .unwrap_or_default();
32        let fetched_user = fetched.get("user").unwrap_or(&fetched);
33
34        require(!health.is_null(), format!("auth health failed: {health}"))?;
35        require(created.get("success").and_then(Value::as_bool).unwrap_or(true) || created.get("api_key").is_some(), format!("create_user failed: {created}"))?;
36        require(listed_users.iter().any(|item| item.get("username").and_then(Value::as_str) == Some(username.as_str())), format!("list_users missing created user: {listed}"))?;
37        require(rotated.get("success").and_then(Value::as_bool).unwrap_or(true) || rotated.get("api_key").is_some(), format!("rotate_user_api_key failed: {rotated}"))?;
38        require(fetched_user.get("username").and_then(Value::as_str) == Some(username.as_str()), format!("get_user failed: {fetched}"))?;
39        require(revoked.get("success").and_then(Value::as_bool).unwrap_or(true), format!("revoke_user_api_key failed: {revoked}"))?;
40        require(deleted.get("success").and_then(Value::as_bool).unwrap_or(true), format!("delete_user failed: {deleted}"))?;
41
42        metrics = json!({"username": username});
43        Ok::<(), Box<dyn Error>>(())
44    }.await;
45
46    if let Err(err) = scenario { passed = false; detail = err.to_string(); }
47    print_summary(name, passed, &detail, &metrics, started.elapsed().as_secs_f64(), true);
48    if passed { Ok(()) } else { Err(boxed_error(detail)) }
49}
Source

pub async fn get_user<T: Serialize>(&self, payload: T) -> Result<Value>

Examples found in repository?
examples/internal/auth_lifecycle.rs (line 24)
10async fn main() -> Result<(), Box<dyn Error>> {
11    let name = "internal_auth_lifecycle";
12    let started = Instant::now();
13    let client = create_client().await?;
14    let username = format!("sdk_example_{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis());
15    let mut passed = true;
16    let mut detail = "validated raw auth lifecycle".to_string();
17    let mut metrics = json!({});
18
19    let scenario = async {
20        let health = client.auth.health().await?;
21        let created = client.auth.create_user(json!({"username": username, "role": "user"})).await?;
22        let listed = client.auth.list_users(json!({})).await?;
23        let rotated = client.auth.rotate_user_api_key(json!({"username": username})).await?;
24        let fetched = client.auth.get_user(json!({"username": username})).await?;
25        let revoked = client.auth.revoke_user_api_key(json!({"username": username})).await?;
26        let deleted = client.auth.delete_user(json!({"username": username})).await?;
27        let listed_users = listed
28            .as_array()
29            .cloned()
30            .or_else(|| listed.get("users").and_then(Value::as_array).cloned())
31            .unwrap_or_default();
32        let fetched_user = fetched.get("user").unwrap_or(&fetched);
33
34        require(!health.is_null(), format!("auth health failed: {health}"))?;
35        require(created.get("success").and_then(Value::as_bool).unwrap_or(true) || created.get("api_key").is_some(), format!("create_user failed: {created}"))?;
36        require(listed_users.iter().any(|item| item.get("username").and_then(Value::as_str) == Some(username.as_str())), format!("list_users missing created user: {listed}"))?;
37        require(rotated.get("success").and_then(Value::as_bool).unwrap_or(true) || rotated.get("api_key").is_some(), format!("rotate_user_api_key failed: {rotated}"))?;
38        require(fetched_user.get("username").and_then(Value::as_str) == Some(username.as_str()), format!("get_user failed: {fetched}"))?;
39        require(revoked.get("success").and_then(Value::as_bool).unwrap_or(true), format!("revoke_user_api_key failed: {revoked}"))?;
40        require(deleted.get("success").and_then(Value::as_bool).unwrap_or(true), format!("delete_user failed: {deleted}"))?;
41
42        metrics = json!({"username": username});
43        Ok::<(), Box<dyn Error>>(())
44    }.await;
45
46    if let Err(err) = scenario { passed = false; detail = err.to_string(); }
47    print_summary(name, passed, &detail, &metrics, started.elapsed().as_secs_f64(), true);
48    if passed { Ok(()) } else { Err(boxed_error(detail)) }
49}
Source

pub async fn delete_user<T: Serialize>(&self, payload: T) -> Result<Value>

Examples found in repository?
examples/internal/auth_lifecycle.rs (line 26)
10async fn main() -> Result<(), Box<dyn Error>> {
11    let name = "internal_auth_lifecycle";
12    let started = Instant::now();
13    let client = create_client().await?;
14    let username = format!("sdk_example_{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_millis());
15    let mut passed = true;
16    let mut detail = "validated raw auth lifecycle".to_string();
17    let mut metrics = json!({});
18
19    let scenario = async {
20        let health = client.auth.health().await?;
21        let created = client.auth.create_user(json!({"username": username, "role": "user"})).await?;
22        let listed = client.auth.list_users(json!({})).await?;
23        let rotated = client.auth.rotate_user_api_key(json!({"username": username})).await?;
24        let fetched = client.auth.get_user(json!({"username": username})).await?;
25        let revoked = client.auth.revoke_user_api_key(json!({"username": username})).await?;
26        let deleted = client.auth.delete_user(json!({"username": username})).await?;
27        let listed_users = listed
28            .as_array()
29            .cloned()
30            .or_else(|| listed.get("users").and_then(Value::as_array).cloned())
31            .unwrap_or_default();
32        let fetched_user = fetched.get("user").unwrap_or(&fetched);
33
34        require(!health.is_null(), format!("auth health failed: {health}"))?;
35        require(created.get("success").and_then(Value::as_bool).unwrap_or(true) || created.get("api_key").is_some(), format!("create_user failed: {created}"))?;
36        require(listed_users.iter().any(|item| item.get("username").and_then(Value::as_str) == Some(username.as_str())), format!("list_users missing created user: {listed}"))?;
37        require(rotated.get("success").and_then(Value::as_bool).unwrap_or(true) || rotated.get("api_key").is_some(), format!("rotate_user_api_key failed: {rotated}"))?;
38        require(fetched_user.get("username").and_then(Value::as_str) == Some(username.as_str()), format!("get_user failed: {fetched}"))?;
39        require(revoked.get("success").and_then(Value::as_bool).unwrap_or(true), format!("revoke_user_api_key failed: {revoked}"))?;
40        require(deleted.get("success").and_then(Value::as_bool).unwrap_or(true), format!("delete_user failed: {deleted}"))?;
41
42        metrics = json!({"username": username});
43        Ok::<(), Box<dyn Error>>(())
44    }.await;
45
46    if let Err(err) = scenario { passed = false; detail = err.to_string(); }
47    print_summary(name, passed, &detail, &metrics, started.elapsed().as_secs_f64(), true);
48    if passed { Ok(()) } else { Err(boxed_error(detail)) }
49}

Trait Implementations§

Source§

impl Clone for AuthClient

Source§

fn clone(&self) -> AuthClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more