|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use derive_more::Deref; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +use super::{Client, HttpResponse}; |
| 7 | +use crate::errors::RvError; |
| 8 | + |
| 9 | +#[derive(Debug, Clone, Default, Serialize, Deserialize)] |
| 10 | +pub struct TokenInput { |
| 11 | + #[serde(default)] |
| 12 | + pub id: String, |
| 13 | + #[serde(default)] |
| 14 | + pub policies: Vec<String>, |
| 15 | + #[serde(default)] |
| 16 | + pub meta: HashMap<String, String>, |
| 17 | + #[serde(default)] |
| 18 | + pub lease: String, |
| 19 | + #[serde(default)] |
| 20 | + pub ttl: String, |
| 21 | + #[serde(default)] |
| 22 | + pub explicit_max_ttl: String, |
| 23 | + #[serde(default)] |
| 24 | + pub period: String, |
| 25 | + #[serde(default)] |
| 26 | + pub no_parent: bool, |
| 27 | + #[serde(default)] |
| 28 | + pub no_default_policy: bool, |
| 29 | + pub display_name: String, |
| 30 | + pub num_uses: u32, |
| 31 | + #[serde(default)] |
| 32 | + pub renewable: bool, |
| 33 | + #[serde(default, rename = "type")] |
| 34 | + pub logical_type: String, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Deref)] |
| 38 | +pub struct TokenAuth<'a> { |
| 39 | + #[deref] |
| 40 | + pub client: &'a Client, |
| 41 | +} |
| 42 | + |
| 43 | +impl Client { |
| 44 | + pub fn token(&self) -> TokenAuth { |
| 45 | + TokenAuth { client: self } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl TokenAuth<'_> { |
| 50 | + pub fn create(&self, input: &TokenInput) -> Result<HttpResponse, RvError> { |
| 51 | + let data = serde_json::to_value(input)?; |
| 52 | + self.request_write("/v1/auth/token/create", data.as_object().cloned()) |
| 53 | + } |
| 54 | + |
| 55 | + pub fn create_orphan(&self, input: &TokenInput) -> Result<HttpResponse, RvError> { |
| 56 | + let data = serde_json::to_value(input)?; |
| 57 | + self.request_write("/v1/auth/token/create-orphan", data.as_object().cloned()) |
| 58 | + } |
| 59 | + |
| 60 | + pub fn create_with_role(&self, input: &TokenInput, role_name: &str) -> Result<HttpResponse, RvError> { |
| 61 | + let data = serde_json::to_value(input)?; |
| 62 | + self.request_write(&format!("/v1/auth/token/create/{}", role_name), data.as_object().cloned()) |
| 63 | + } |
| 64 | + |
| 65 | + pub fn lookup(&self, token: &str) -> Result<HttpResponse, RvError> { |
| 66 | + let data = serde_json::json!({ |
| 67 | + "token": token, |
| 68 | + }); |
| 69 | + self.request_write("/v1/auth/token/lookup", data.as_object().cloned()) |
| 70 | + } |
| 71 | + |
| 72 | + pub fn lookup_accessor(&self, accessor: &str) -> Result<HttpResponse, RvError> { |
| 73 | + let data = serde_json::json!({ |
| 74 | + "accessor": accessor, |
| 75 | + }); |
| 76 | + self.request_write("/v1/auth/token/lookup-accessor", data.as_object().cloned()) |
| 77 | + } |
| 78 | + |
| 79 | + pub fn lookup_self(&self) -> Result<HttpResponse, RvError> { |
| 80 | + self.request_get("/v1/auth/token/lookup-self") |
| 81 | + } |
| 82 | + |
| 83 | + pub fn renew(&self, token: &str, increment: u32) -> Result<HttpResponse, RvError> { |
| 84 | + let data = serde_json::json!({ |
| 85 | + "token": token, |
| 86 | + "increment": increment, |
| 87 | + }); |
| 88 | + self.request_write("/v1/auth/token/renew", data.as_object().cloned()) |
| 89 | + } |
| 90 | + |
| 91 | + pub fn renew_accessor(&self, accessor: &str, increment: u32) -> Result<HttpResponse, RvError> { |
| 92 | + let data = serde_json::json!({ |
| 93 | + "accessor": accessor, |
| 94 | + "increment": increment, |
| 95 | + }); |
| 96 | + self.request_write("/v1/auth/token/renew-accessor", data.as_object().cloned()) |
| 97 | + } |
| 98 | + |
| 99 | + pub fn renew_self(&self, increment: u32) -> Result<HttpResponse, RvError> { |
| 100 | + let data = serde_json::json!({ |
| 101 | + "increment": increment, |
| 102 | + }); |
| 103 | + self.request_write("/v1/auth/token/renew-self", data.as_object().cloned()) |
| 104 | + } |
| 105 | + |
| 106 | + pub fn renew_token_as_self(&self, token: &str, increment: u32) -> Result<HttpResponse, RvError> { |
| 107 | + let mut client = self.client.clone(); |
| 108 | + client.token = token.to_string(); |
| 109 | + let data = serde_json::json!({ |
| 110 | + "increment": increment, |
| 111 | + }); |
| 112 | + client.request_write("/v1/auth/token/renew-self", data.as_object().cloned()) |
| 113 | + } |
| 114 | + |
| 115 | + pub fn revoke_accessor(&self, accessor: &str) -> Result<HttpResponse, RvError> { |
| 116 | + let data = serde_json::json!({ |
| 117 | + "accessor": accessor, |
| 118 | + }); |
| 119 | + self.request_write("/v1/auth/token/revoke-accessor", data.as_object().cloned()) |
| 120 | + } |
| 121 | + |
| 122 | + pub fn revoke_orphan(&self, token: &str) -> Result<HttpResponse, RvError> { |
| 123 | + let data = serde_json::json!({ |
| 124 | + "token": token, |
| 125 | + }); |
| 126 | + self.request_put("/v1/auth/token/revoke-orphan", data.as_object().cloned()) |
| 127 | + } |
| 128 | + |
| 129 | + pub fn revoke_self(&self) -> Result<HttpResponse, RvError> { |
| 130 | + self.request_put("/v1/auth/token/revoke-self", None) |
| 131 | + } |
| 132 | + |
| 133 | + pub fn revoke_tree(&self, token: &str) -> Result<HttpResponse, RvError> { |
| 134 | + let data = serde_json::json!({ |
| 135 | + "token": token, |
| 136 | + }); |
| 137 | + self.request_put("/v1/auth/token/revoke", data.as_object().cloned()) |
| 138 | + } |
| 139 | +} |
0 commit comments