From a7bc22c76717960f51a00884d505107e7d4aec57 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Sun, 9 Jan 2022 17:47:07 +0100 Subject: [PATCH] Implement key record recreation --- authorizedkeys/parser.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/authorizedkeys/parser.py b/authorizedkeys/parser.py index 8111ef7..a88623b 100644 --- a/authorizedkeys/parser.py +++ b/authorizedkeys/parser.py @@ -48,6 +48,13 @@ class AuthorizedKey: self.type = split[0] self.key_b64 = split[1] self.coment = split[2] if len(split) >= 3 else None + + def to_string(self): + result = '' + if self.options is not None: result += self.options + ' ' + result += f'{self.type} {self.key_b64}' + if self.comment is not None: result += ' ' + self.comment + return result def parse_file(f: IO[str]) -> list[AuthorizedKey | str]: result = [] @@ -61,3 +68,10 @@ def parse_file(f: IO[str]) -> list[AuthorizedKey | str]: return result # TODO: Implement option parsing, key validation and decoding to bytes. + +def dump_file(keys: list[AuthorizedKey | str], f: IO[str]) -> None: + for rec in keys: + if isinstance(rec, AuthorizedKey): + rec = rec.to_string() + f.write(rec) + f.write('\n')