Skip to content

Commit 229a013

Browse files
committed
Merge branch 'feature/static-username-extractor'
2 parents b7b8a09 + b42f0bc commit 229a013

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ For instance, if a regular [pass] call would be `pass show dev/github.com/langui
284284

285285
No configuration options.
286286

287+
#### Strategy "static"
288+
289+
Allows defining the username in the mapping file using the `username` key.
290+
291+
Example:
292+
293+
```ini
294+
[DEFAULT]
295+
username_extractor=static
296+
username = myuser
297+
298+
[example.com]
299+
username = myexampleusername
300+
```
301+
287302
### File Encoding
288303

289304
By default, password store entries are assumed to use UTF-8 encoding.

passgithelper.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,31 @@ def get_value(
306306
return os.path.split(entry_name)[1]
307307

308308

309+
class StaticUsernameExtractor(DataExtractor):
310+
"""Extract username from a static field in the mapping configuration."""
311+
312+
def __init__(self) -> None:
313+
self._username: str | None = None
314+
315+
def configure(self, config: configparser.SectionProxy) -> None:
316+
"""Store the username from the mapping configuration."""
317+
self._username = config.get("username")
318+
319+
def get_value(
320+
self, entry_name: Text, entry_lines: Sequence[Text] # noqa: ARG002
321+
) -> Optional[Text]:
322+
"""Return the stored username."""
323+
return self._username
324+
325+
309326
_line_extractor_name = "specific_line"
310327
_username_extractors = {
311328
_line_extractor_name: SpecificLineExtractor(1, 0, option_suffix="_username"),
312329
"regex_search": RegexSearchExtractor(
313330
r"^username: +(.*)$", option_suffix="_username"
314331
),
315332
"entry_name": EntryNameExtractor(option_suffix="_username"),
333+
"static": StaticUsernameExtractor(),
316334
}
317335
_password_extractors = {
318336
_line_extractor_name: SpecificLineExtractor(0, 0, option_suffix="_password"),

test_passgithelper.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,73 @@ def test_smoke(self) -> None:
137137
assert passgithelper.EntryNameExtractor().get_value("foo/bar", []) == "bar"
138138

139139

140+
class TestStaticUsernameExtractor:
141+
def test_extracts_username_from_config(self) -> None:
142+
config = configparser.ConfigParser()
143+
config.read_string(
144+
"""[test]
145+
146+
"""
147+
)
148+
149+
extractor = passgithelper.StaticUsernameExtractor()
150+
extractor.configure(config["test"])
151+
152+
assert (
153+
extractor.get_value("any/entry", ["any", "lines"]) == "[email protected]"
154+
)
155+
156+
def test_returns_none_when_no_username_configured(self) -> None:
157+
config = configparser.ConfigParser()
158+
config.read_string(
159+
"""[test]
160+
target = some/target
161+
"""
162+
)
163+
164+
extractor = passgithelper.StaticUsernameExtractor()
165+
extractor.configure(config["test"])
166+
167+
assert extractor.get_value("any/entry", ["any", "lines"]) is None
168+
169+
def test_inherits_from_default_section(self) -> None:
170+
config = configparser.ConfigParser()
171+
config.read_string(
172+
"""[DEFAULT]
173+
174+
175+
[test]
176+
target = some/target
177+
"""
178+
)
179+
180+
extractor = passgithelper.StaticUsernameExtractor()
181+
extractor.configure(config["test"])
182+
183+
assert (
184+
extractor.get_value("any/entry", ["any", "lines"]) == "[email protected]"
185+
)
186+
187+
def test_section_overrides_default(self) -> None:
188+
config = configparser.ConfigParser()
189+
config.read_string(
190+
"""[DEFAULT]
191+
192+
193+
[test]
194+
target = some/target
195+
196+
"""
197+
)
198+
199+
extractor = passgithelper.StaticUsernameExtractor()
200+
extractor.configure(config["test"])
201+
202+
assert (
203+
extractor.get_value("any/entry", ["any", "lines"]) == "[email protected]"
204+
)
205+
206+
140207
@pytest.mark.parametrize(
141208
"helper_config",
142209
[

0 commit comments

Comments
 (0)