-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add c8y.registration_mode enum instead of c8y.use_basic_auth bool
Signed-off-by: Rina Fujino <[email protected]>
- Loading branch information
Showing
7 changed files
with
111 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
crates/common/tedge_config/src/tedge_config_cli/models/registration_mode.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use camino::Utf8Path; | ||
use std::str::FromStr; | ||
use strum_macros::Display; | ||
|
||
#[derive( | ||
Debug, Display, Clone, Copy, Eq, PartialEq, doku::Document, serde::Serialize, serde::Deserialize, | ||
)] | ||
#[serde(rename_all = "kebab-case")] | ||
#[strum(serialize_all = "kebab-case")] | ||
pub enum RegistrationMode { | ||
Certificate, | ||
Basic, | ||
Auto, | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
#[error("Failed to parse flag: {input}. Supported values are: 'certificate', 'basic' or 'auto'")] | ||
pub struct InvalidRegistrationMode { | ||
input: String, | ||
} | ||
|
||
impl FromStr for RegistrationMode { | ||
type Err = InvalidRegistrationMode; | ||
|
||
fn from_str(input: &str) -> Result<Self, Self::Err> { | ||
match input { | ||
"certificate" => Ok(RegistrationMode::Certificate), | ||
"basic" => Ok(RegistrationMode::Basic), | ||
"auto" => Ok(RegistrationMode::Auto), | ||
_ => Err(InvalidRegistrationMode { | ||
input: input.to_string(), | ||
}), | ||
} | ||
} | ||
} | ||
|
||
pub enum AuthType { | ||
Certificate, | ||
Basic, | ||
} | ||
|
||
impl RegistrationMode { | ||
pub fn is_basic(self, credentials_path: &Utf8Path) -> bool { | ||
matches!(self.to_type(credentials_path), AuthType::Basic) | ||
} | ||
|
||
pub fn is_certificate(self, credentials_path: &Utf8Path) -> bool { | ||
matches!(self.to_type(credentials_path), AuthType::Certificate) | ||
} | ||
|
||
pub fn to_type(self, credentials_path: &Utf8Path) -> AuthType { | ||
match self { | ||
RegistrationMode::Certificate => AuthType::Certificate, | ||
RegistrationMode::Basic => AuthType::Basic, | ||
RegistrationMode::Auto if credentials_path.exists() => AuthType::Basic, | ||
RegistrationMode::Auto => AuthType::Certificate, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters