Skip to content

Commit 94b200e

Browse files
authored
Improve documentation of allowed_signers module (#13)
* Improve doc comment * Move `TIME_FMT` const into `AllowedSigner` impl * Add doc comment with example to `AllowedSigner`
1 parent 1340683 commit 94b200e

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/allowed_signers.rs

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Interact with the OpenSSH `allowed_signers` file.
1+
//! Types and functions to interact with the OpenSSH `allowed_signers` file.
22
//!
33
//! # File format
44
//! https://man.openbsd.org/ssh-keygen.1#ALLOWED_SIGNERS
@@ -11,9 +11,6 @@ use std::{
1111
path::Path,
1212
};
1313

14-
/// The format string for time fields.
15-
const TIME_FMT: &str = "%Y%m%d%H%M%S";
16-
1714
/// A single entry in the allowed signers file.
1815
#[derive(Debug)]
1916
pub struct AllowedSigner {
@@ -23,15 +20,44 @@ pub struct AllowedSigner {
2320
pub key: SshPublicKey,
2421
}
2522

23+
impl AllowedSigner {
24+
/// The format string for timestamps.
25+
const TIMESTAMP_FMT: &'static str = "%Y%m%d%H%M%S";
26+
}
27+
2628
impl fmt::Display for AllowedSigner {
29+
/// Display the allowed signer in the format expected by the `allowed_signers` file.
30+
///
31+
/// # Examples
32+
/// ```
33+
/// # use hanko::AllowedSigner;
34+
/// # use chrono::{TimeZone, Local};
35+
/// let signer = AllowedSigner {
36+
/// principal: "cwoods@universal.exports".to_string(),
37+
/// valid_after: None,
38+
/// valid_before: Some(Local.with_ymd_and_hms(2030, 1, 1, 0, 0, 0).unwrap()),
39+
/// key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHDGMF+tZQL3dcr1arPst+YP8v33Is0kAJVvyTKrxMw"
40+
/// .parse()
41+
/// .unwrap(),
42+
/// };
43+
/// assert_eq!(signer.to_string(), "cwoods@universal.exports valid-before=20300101000000 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHDGMF+tZQL3dcr1arPst+YP8v33Is0kAJVvyTKrxMw");
44+
/// ```
2745
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2846
write!(f, "{}", self.principal)?;
2947

3048
if let Some(valid_after) = self.valid_after {
31-
write!(f, " valid-after={}", valid_after.format(TIME_FMT))?;
49+
write!(
50+
f,
51+
" valid-after={}",
52+
valid_after.format(Self::TIMESTAMP_FMT)
53+
)?;
3254
};
3355
if let Some(valid_before) = self.valid_before {
34-
write!(f, " valid-before={}", valid_before.format(TIME_FMT))?;
56+
write!(
57+
f,
58+
" valid-before={}",
59+
valid_before.format(Self::TIMESTAMP_FMT)
60+
)?;
3561
};
3662

3763
write!(f, " {}", self.key)

0 commit comments

Comments
 (0)