1
- //! Interact with the OpenSSH `allowed_signers` file.
1
+ //! Types and functions to interact with the OpenSSH `allowed_signers` file.
2
2
//!
3
3
//! # File format
4
4
//! https://man.openbsd.org/ssh-keygen.1#ALLOWED_SIGNERS
@@ -11,9 +11,6 @@ use std::{
11
11
path:: Path ,
12
12
} ;
13
13
14
- /// The format string for time fields.
15
- const TIME_FMT : & str = "%Y%m%d%H%M%S" ;
16
-
17
14
/// A single entry in the allowed signers file.
18
15
#[ derive( Debug ) ]
19
16
pub struct AllowedSigner {
@@ -23,15 +20,44 @@ pub struct AllowedSigner {
23
20
pub key : SshPublicKey ,
24
21
}
25
22
23
+ impl AllowedSigner {
24
+ /// The format string for timestamps.
25
+ const TIMESTAMP_FMT : & ' static str = "%Y%m%d%H%M%S" ;
26
+ }
27
+
26
28
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
+ /// ```
27
45
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
28
46
write ! ( f, "{}" , self . principal) ?;
29
47
30
48
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
+ ) ?;
32
54
} ;
33
55
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
+ ) ?;
35
61
} ;
36
62
37
63
write ! ( f, " {}" , self . key)
0 commit comments