Skip to content

Commit

Permalink
feat: moved library code from 2fas to lib2fas
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvandernoord committed Jan 29, 2024
0 parents commit e4be2b7
Show file tree
Hide file tree
Showing 18 changed files with 1,084 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

<!--next-version-placeholder-->

## v0.1.0 (2024-01-25)

### Feature

* Cli tweaks and more pytests ([`01f8574`](https://github.com/robinvandernoord/2fas-python/commit/01f8574e527a60025e4e7b7bf0416a4e344fde2e))
* Started basic cli functionality ([`f15bbbf`](https://github.com/robinvandernoord/2fas-python/commit/f15bbbfe1d4e79ba644775dd1e4eb638e394dc81))
* TwoFactorStorage is now a recursive data type when using .find() ([`1f4847f`](https://github.com/robinvandernoord/2fas-python/commit/1f4847fa07eecd9c85623e5cb799a34ab3a8714d))
* Added tests and more general functionality ([`be86df5`](https://github.com/robinvandernoord/2fas-python/commit/be86df54cc4616541c6e636e882a1fa444af9d3a))

### Fix

* Improved settings menu + mypy typing + refactor ([`5d08f62`](https://github.com/robinvandernoord/2fas-python/commit/5d08f62daba7873e84766562c07370fa22018868))
* Improved settings tui ([`c0275b5`](https://github.com/robinvandernoord/2fas-python/commit/c0275b5d5e1b77fba77f65f3efdb5d117d9f5715))
* Include rapidfuzz in dependencies (previously only collected via 'dev' extra) ([`d2016e0`](https://github.com/robinvandernoord/2fas-python/commit/d2016e033ff00392032492525a3c4eb14a4432b3))
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Robin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# lib2fas Python

Unofficial implementation of 2fas for Python (as a library).

## Installation

To install this project, use pip:

```bash
pip install lib2fas
# or to also install the cli tool:
pip install 2fas
```

## Usage

After installing the package, you can import it in your Python scripts as follows:

```python
import lib2fas

services = lib2fas.load_services("/path/to/file.2fas", passphrase="optional") # -> TwoFactorStorage

services.generate() # generate all TOTP keys
github = services.find("githbu") # fuzzy match should find GitHub, returns a new TwoFactorStorage.

for label, services in github.items():
# one label can have multiple services!
for service in services:
print(label, service.name, service.generate_int())

```

## License

This project is licensed under the MIT License.
184 changes: 184 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "lib2fas"
dynamic = ["version"]
description = 'Unofficial implementation of 2fas for Python (as a library)'
readme = "README.md"
requires-python = ">=3.10"
license = "MIT"
keywords = []
authors = [
{ name = "Robin van der Noord", email = "robinvandernoord@gmail.com" },
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"cryptography",
"configuraptor>=1.25",
"PyOTP",
"keyring",
"typer[all]",
"questionary",
"rapidfuzz",
]

[template.plugins.default]
src-layout = true

[tool.setuptools.package-data]
"lib2fas" = ["py.typed"]

[project.optional-dependencies]
dev = [
"hatch",
"python-semantic-release<8",
"su6[all]",
"pytest-mypy-testing",
"edwh",
]


[project.urls]
Documentation = "https://github.com/robinvandernoord/lib2fas-python#readme"
Issues = "https://github.com/robinvandernoord/lib2fas-python/issues"
Source = "https://github.com/robinvandernoord/lib2fas-python"

[tool.hatch.version]
path = "src/lib2fas/__about__.py"

[tool.hatch.build.targets.wheel]
packages = ["src/lib2fas"]

[tool.semantic_release]
branch = "master"
version_variable = "src/lib2fas/__about__.py:__version__"
change_log = "CHANGELOG.md"
upload_to_repository = false
upload_to_release = false
build_command = "hatch build"

### required in every su6 pyproject: ###
[tool.su6]
# every checker:
directory = "src"
# 'all' and 'fix':
include = []
exclude = []
# 'all':
stop-after-first-failure = true
# pytest:
coverage = 100
badge = true
# --format json indent
json-indent = 2


[tool.black]
target-version = ["py310"]
line-length = 120
# 'extend-exclude' excludes files or directories in addition to the defaults
extend-exclude = '''
# A regex preceded with ^/ will apply only to files and directories
# in the root of the project.
(
^.*\.bak\/.+ # ignore every .bak directory
^.*venv.+\/.+ # ignore every venv directory
venv.+|.+\.bak # idk why it suddenly works, let's not touch it
)
'''

[tool.mypy]
python_version = "3.11"

# `some: int = None` looks nicer than `some: int | None = None` and pycharm still understands it
no_implicit_optional = false # I guess 'strict_optional' should be true, but disable this one because it's double!
# same as above (thrown if no_implicit_optional = False)
# ACTUALLY: not the same! Setting strict_optional = false may miss some type errors like
# 'Item "None" of "Optional" has no attribute "lower"'
# 'strict_optional' complains more for class properties and 'no_implicit_optional' for function arguments
# strict_optional = false
# 3rd party packages may not be typed, that's not my fault!
ignore_missing_imports = true
# kinda hypocritical to disable Optional and still enable strict, but I do agree with some other strict rules.
strict = true
# fixes defs with clear return var (doesn't seem to work for __init__ which is the most obvious case)
# check_untyped_defs = True

exclude = ["venv", ".bak"]

[tool.ruff]
target-version = "py310"
line-length = 120

select = [
"F", # pyflake error
"E", # pycodestyle error
"W", # pycodestyle warning
"Q", # quotes
"A", # builtins
# "C4", # comprehensions - NO: doesn't allow dict()
# "RET", # return - NO: annoying
"SIM", # simplify
"ARG", # unused arguments
# "COM", # comma's - NO: annoying
# "PTH", # use pathlib - NO: annoying
"RUF", # ruff rules
]
unfixable = [
# Don't touch unused imports
"F401",
]
extend-exclude = ["*.bak/", "venv*/"]

ignore = [
"RUF013" # implicit Optional
]

[tool.bandit]
# bandit -c pyproject.toml -r .
exclude_dirs = [".bak", "venv"]
skips = [
"B108" # hard coded /tmp/... files are fine for me tbh
]

[tool.isort]
profile = "black"
extend_skip_glob = ["*.bak/*"]

[tool.pydocstyle]
convention = "google"
match-dir = '(?!venv)[^\.].*'
add_select = [
"D213", # = Multi-line docstring summary should start at the second line
"D416", # = Google-style section name checks.
"D417", # = Missing argument descriptions in the docstring
]
add_ignore = [
"D200", # = One-line docstring should fit on one line with quotes
"D212", # = Multi-line docstring summary should start at the first line
]

### and if it's a project and NOT a package, add this to make it not look for anything buildable: ###
# make this a meta package: not a library but simply allow me to run `pip install .[dev]`
#[build-system]
#build-backend = "setuptools.build_meta"
#requires = ["setuptools"]
#
#[tool.setuptools.packages.find]
## look nowhere for any code to 'build' since this is just used to manage (dev) dependencies
#where = []

[tool.pytest.ini_options]
pythonpath = [
"src",
]
5 changes: 5 additions & 0 deletions src/lib2fas/__about__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
This file stores the module version.
"""

__version__ = "0.0.0"
7 changes: 7 additions & 0 deletions src/lib2fas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
This file exposes the most important element to the global lib2fas namespace.
"""

from .core import load_services

__all__ = ["load_services"]
Loading

0 comments on commit e4be2b7

Please sign in to comment.