wc-django-2factor


Namewc-django-2factor JSON
Version 0.1.9 PyPI version JSON
download
home_page
SummaryPackage to create general API for 2factor checkers.
upload_time2023-06-30 07:53:47
maintainer
docs_urlNone
authorWebCase
requires_python>=3.6
licenseMIT License
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # WebCase 2factor API

Package to create general API for 2factor checkers.

## Installation

```sh
pip install wc-django-2factor
```

In `settings.py`:

```python
INSTALLED_APPS += [
  'wcd_2factor',
]

WCD_2FACTOR = {
  # Available ways to send confirmation messages.
  'SENDERS': {
    'default': {
      'verbose_name': 'Phone sender',
      # Your own sender backend implementation.
      'backend': 'some.method.path.to.Backend',
      # Any options that that backend will receive(if it requires).
      'options': {
        'SOME: 'OPTION',
      },
    },
  },
  # Default sender key that will be used by default(if None specified).
  'DEFAULT_SENDER': 'default',
  # Generator function that will generate confirmation code.
  'CONFIRM_CODE_GENERATOR': 'wcd_2factor.services.confirmer.make_confirmation_code',
  # Since [0.1.1]. Show code, sended to backend straight in code request response. That done for faster debugging during development.
  'DEBUG_CODE_RESPONSE': False,
}

# All root options could also be provided as standalone ones(for overriding, etc.):
WCD_2FACTOR_DEFAULT_SENDER = 'default'
```

### Services

#### Confirmer

Service for confirmation state management.

```python
from wcd_2factor.services import confirmer

# Create new confirmation request.
state = confirmer.make_confirmation(meta={'any': 'json'})

print(state.is_confirmed)
# > False

# Check whether the confirmation request is confirmed.
state, confirmed = confirmer.check(state.id)

print(confirmed)
# > False

# Confirm confirmation request in two ways:
# Manually if you sure that all requirements had been accomplished.
state.confirm()
# or
# By running confirmation logic from service:
state, confirmed = confirmer.confirm(state.id, 'confirmation-code-provided-by-user')

# ...

# In some place in your application yop may "use" confirmation request.
# For example to prove that provided phone number is that one that user owns.
# It's one time usage, so it will not be accessible to use anymore elsewhere.
used = confirmer.use(state)

if not used:
  raise Exception('This state is not confirmed yet.')
```

#### Sender

Sender is a service that sends message with generated code.

```python
from wcd_2factor.services import sender

# It has only one method: `.send`.
sender.send(
  'sender-backend-key',
  'email.or.a.phone.number.etc@email.com',
  # Request confirmation state object.
  state,
  # Additional context if required.
  context={}
)
```

### Sender backend development

Sender backend is a callable that takes confirmation options and returns another callable that can handle data sending.

So it could look like that:

```python
def send(
  # Key for sender in configuration.
  name: str,
  options,
  verbose_name=None,
  **kwargs
):
  # Do something with confirmation state and confirmation options.
  # ...
  # Return callable that will handle data sending.
  def send(token, state, context):
    return send_somewhere(f'Here is yor code: {state.code}')

  return send
```

There are two helper classes for a little bit easier backend development:

```python
from wcd_2factor.sender import SenderBackend, FunctionalSenderBackend


# You may create a simple class as a backend.
class CustomBackend(SenderBackend):
  def send(self, token, state, context: dict = {}):
    return send_somewhere(f'Here is yor code: {state.code}')


# Or just made a function(it also will be resolved into a class-return wrapper):
@FunctionalSenderBackend.from_callable
def custom_callable_backend(
  token, state, name, context={}, options={}, **self.kwargs
):
  send_phone_confirmation_task.delay(token, state.code)
```

## Contrib

### DRF

There are ready for use frontend for django rest framework.

In `urls.py`:

```python
from wcd_2factor.contrib.drf.views import make_urlpatterns as twofactor_make_urlpatterns

urlpatters = [
  ...
  path(
    'api/v1/auth/2factor/',
    include((twofactor_make_urlpatterns(), 'wcd_2factor'),
    namespace='2factor')
  ),
]
```

There will be 2 views:
- `request-confirmation/` - To request confirmation code to your device.
- `confirm/` - To confirm that two factor request.
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.1.7]
### Added
- Default to confirmation states admin list.
- New django unified `JSONField` support.

## [0.1.6]
### Added
- Translation strings.

## [0.1.3]
### Added
- Admin search ui for confirmation state model.

## [0.1.1]
### Added
- `DEBUG_CODE_RESPONSE` setting. It adds generated 'code' field to a request confirmation response for easier debug.

## [0.1.0]
Initial version.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "wc-django-2factor",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "WebCase",
    "author_email": "info@webcase.studio",
    "download_url": "https://files.pythonhosted.org/packages/e3/9e/6798e117eac2f72fd4d397d038da79c2bbe3d54789953cb23760537ab25b/wc-django-2factor-0.1.9.tar.gz",
    "platform": null,
    "description": "# WebCase 2factor API\n\nPackage to create general API for 2factor checkers.\n\n## Installation\n\n```sh\npip install wc-django-2factor\n```\n\nIn `settings.py`:\n\n```python\nINSTALLED_APPS += [\n  'wcd_2factor',\n]\n\nWCD_2FACTOR = {\n  # Available ways to send confirmation messages.\n  'SENDERS': {\n    'default': {\n      'verbose_name': 'Phone sender',\n      # Your own sender backend implementation.\n      'backend': 'some.method.path.to.Backend',\n      # Any options that that backend will receive(if it requires).\n      'options': {\n        'SOME: 'OPTION',\n      },\n    },\n  },\n  # Default sender key that will be used by default(if None specified).\n  'DEFAULT_SENDER': 'default',\n  # Generator function that will generate confirmation code.\n  'CONFIRM_CODE_GENERATOR': 'wcd_2factor.services.confirmer.make_confirmation_code',\n  # Since [0.1.1]. Show code, sended to backend straight in code request response. That done for faster debugging during development.\n  'DEBUG_CODE_RESPONSE': False,\n}\n\n# All root options could also be provided as standalone ones(for overriding, etc.):\nWCD_2FACTOR_DEFAULT_SENDER = 'default'\n```\n\n### Services\n\n#### Confirmer\n\nService for confirmation state management.\n\n```python\nfrom wcd_2factor.services import confirmer\n\n# Create new confirmation request.\nstate = confirmer.make_confirmation(meta={'any': 'json'})\n\nprint(state.is_confirmed)\n# > False\n\n# Check whether the confirmation request is confirmed.\nstate, confirmed = confirmer.check(state.id)\n\nprint(confirmed)\n# > False\n\n# Confirm confirmation request in two ways:\n# Manually if you sure that all requirements had been accomplished.\nstate.confirm()\n# or\n# By running confirmation logic from service:\nstate, confirmed = confirmer.confirm(state.id, 'confirmation-code-provided-by-user')\n\n# ...\n\n# In some place in your application yop may \"use\" confirmation request.\n# For example to prove that provided phone number is that one that user owns.\n# It's one time usage, so it will not be accessible to use anymore elsewhere.\nused = confirmer.use(state)\n\nif not used:\n  raise Exception('This state is not confirmed yet.')\n```\n\n#### Sender\n\nSender is a service that sends message with generated code.\n\n```python\nfrom wcd_2factor.services import sender\n\n# It has only one method: `.send`.\nsender.send(\n  'sender-backend-key',\n  'email.or.a.phone.number.etc@email.com',\n  # Request confirmation state object.\n  state,\n  # Additional context if required.\n  context={}\n)\n```\n\n### Sender backend development\n\nSender backend is a callable that takes confirmation options and returns another callable that can handle data sending.\n\nSo it could look like that:\n\n```python\ndef send(\n  # Key for sender in configuration.\n  name: str,\n  options,\n  verbose_name=None,\n  **kwargs\n):\n  # Do something with confirmation state and confirmation options.\n  # ...\n  # Return callable that will handle data sending.\n  def send(token, state, context):\n    return send_somewhere(f'Here is yor code: {state.code}')\n\n  return send\n```\n\nThere are two helper classes for a little bit easier backend development:\n\n```python\nfrom wcd_2factor.sender import SenderBackend, FunctionalSenderBackend\n\n\n# You may create a simple class as a backend.\nclass CustomBackend(SenderBackend):\n  def send(self, token, state, context: dict = {}):\n    return send_somewhere(f'Here is yor code: {state.code}')\n\n\n# Or just made a function(it also will be resolved into a class-return wrapper):\n@FunctionalSenderBackend.from_callable\ndef custom_callable_backend(\n  token, state, name, context={}, options={}, **self.kwargs\n):\n  send_phone_confirmation_task.delay(token, state.code)\n```\n\n## Contrib\n\n### DRF\n\nThere are ready for use frontend for django rest framework.\n\nIn `urls.py`:\n\n```python\nfrom wcd_2factor.contrib.drf.views import make_urlpatterns as twofactor_make_urlpatterns\n\nurlpatters = [\n  ...\n  path(\n    'api/v1/auth/2factor/',\n    include((twofactor_make_urlpatterns(), 'wcd_2factor'),\n    namespace='2factor')\n  ),\n]\n```\n\nThere will be 2 views:\n- `request-confirmation/` - To request confirmation code to your device.\n- `confirm/` - To confirm that two factor request.\n# Changelog\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n## [Unreleased]\n\n## [0.1.7]\n### Added\n- Default to confirmation states admin list.\n- New django unified `JSONField` support.\n\n## [0.1.6]\n### Added\n- Translation strings.\n\n## [0.1.3]\n### Added\n- Admin search ui for confirmation state model.\n\n## [0.1.1]\n### Added\n- `DEBUG_CODE_RESPONSE` setting. It adds generated 'code' field to a request confirmation response for easier debug.\n\n## [0.1.0]\nInitial version.\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Package to create general API for 2factor checkers.",
    "version": "0.1.9",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e39e6798e117eac2f72fd4d397d038da79c2bbe3d54789953cb23760537ab25b",
                "md5": "99c1bf4f1c5e5a0222fdf04ecb6e5bce",
                "sha256": "74fd07d3bf70243d5fb3c82806bd9e89bd05676efc1aadc65a56cc3239384a11"
            },
            "downloads": -1,
            "filename": "wc-django-2factor-0.1.9.tar.gz",
            "has_sig": false,
            "md5_digest": "99c1bf4f1c5e5a0222fdf04ecb6e5bce",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 26699,
            "upload_time": "2023-06-30T07:53:47",
            "upload_time_iso_8601": "2023-06-30T07:53:47.987442Z",
            "url": "https://files.pythonhosted.org/packages/e3/9e/6798e117eac2f72fd4d397d038da79c2bbe3d54789953cb23760537ab25b/wc-django-2factor-0.1.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-30 07:53:47",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "wc-django-2factor"
}
        
Elapsed time: 0.32241s