jsm-user-services 2.5.1
Middleware to intercept JWT auth token and more utils functions
License: MIT
Requires Python: <4.0,>=3.10

# JSM User JWT Service

Middleware to intercept JWT auth token and more utils functions

## Local development

### Running tests

`docker-compose up integration-tests`

### Running lint and code formatter

`docker-compose up lint-formatter`

## Install - For Django users

`pip install jsm-user-services`

Add `jsm_user_services` to your `INSTALLED_APPS`:

```python
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "jsm_user_services",
    "app_test",
]
```

Add the Middleware:

```python
MIDDLEWARE = [
    ...
    "jsm_user_services.middleware.JsmJwtService",
]

# Dev url
USER_API_HOST = "http://ishtar-gate.dev.juntossomosmaisi.com.br/api/v1"
USER_API_TOKEN = "user_api_token"
```

## Use

```python
from jsm_user_services.services.user import current_jwt_token
from jsm_user_services.services.user import get_jsm_token
from jsm_user_services.services.user import get_jsm_user_data_from_jwt
from jsm_user_services.services.user import get_ltm_token
from jsm_user_services.services.user import get_user_email_from_jwt
from jsm_user_services.services.user import get_user_id_from_jwt
from jsm_user_services.services.user import get_user_data_from_server
from jsm_user_services.services.user import get_user_data_from_cpf
from jsm_user_services.services.user import get_cpf_from_jwt

current_jwt_token()
"""
Response example:
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTY1ODc4MjI4LCJqdGkiOiJiZDFmMzBiMGEzMTc0MmRmYjk3MTBiMzEzMTY0M2E2ZiIsInl1bnRpYW5kdSI6IjhyR0NjVHZGTE9VemR2LWRhTFk4SzdHZTF2MmVyQS01TWJOVkZ4TFBwTUNDTmRwTjQ2bDlJMHZFNG9aTmNNanRfZTh5SWJkTlczN0tKRUhWdzM3dmJjOEdLZmlfd0ZmZDJTRDlIYUJuNk80SVVabWVNUjlROVRqVTd0WFI1WDZPeFoyRDRiNXNHSWlKWGQwcGtP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VudGl0eS5qc20vYXV0aG9yaXR5IiwiYXVkIjoiYmMwNTFkZTMtYWVhYS00YWM5LThmYmYtMDVhODZlMDgzZmNmIn0.4dT-6_PmJEbWGw5Q1F18mJBQcXcVmNugQwiLxp6e4Ew"
"""
get_ltm_token()
"""
Response example:
"b51yreTQ3_4l2Z7zGsEb7UCosv-xulOtzkfflxsxTwrbOYva7loVGhxHmD6Qv5P0x5enm3LuykiAOGvf9fjDkr7qUETk5_OPONonkw1roSwHBpCCjS_IiGWdk2t2mpR4o-SFKMPfKyATzQQkBi886wMJ5g8Sg7GJNaQBHimbHDoWsvSF4zZhMin3IkD-3aVruS3IpBM6-72f4mabCB2kbtRQfTfjU5BvMYYKQzEp79_AWHbUm34gpgh5OJEt9VynHVb0Wzj-_M5sNl8uzEEQ0kixlWyueha2e5gZPHPA2TiSb0dt4WdBUqSL9BPXpsCB9TBtyRkU6JeH-DXo05Px02joOUh_MrjqhbXxYtJXWTkX4E6krHI1kS2r75DFU3dUTfQtELrvn1lQxpNCG_FAX0gB5B_XMWiX2Pn6hWE-QO9uglJgnkllxwhBBiwC1K57ony1tgpZLPY7kapAQveGmJAgWAkFXKtR2s4DK9Bkz_Xz-dUdZRONlBIH6yP4QotiE58QT0DFEluRMXhQX6huPSKuVnyaGLRrbFxdocUPuVgLTigg4rk9zgXX-GmOdHD5sxvC-cd8OWep0r7Pn5URwIdkWFjY_vly8dQDb3Cx2TXx2lXQeXBlj4XwQNlrAJTBGdmnL-Nu6Sk3oyh1_r-u53JyaF4frbt8Q6ZZsNAeZtRNLvM3l1JJ591XpApteouRRFakC6Iu8ED3IRmuzYvCgajDIDede7vtpsBvEp4yxl0"
"""

get_jsm_user_data_from_jwt()
"""
Response example:
{
  "email": "64722338612@loyaltyJSM.com",
  "unique_name": "Test User",
  "website": "ClientApplication",
  "uid": "8735bf8d-5479-4448-83cf-89997d7df608",
  "sellers": "1e3b831f-95d0-41a7-b3e1-1fe03fae2046",
  "role": "Customer",
  "cnpjs": "68.597.280/0001-09",
  "nbf": 1561558635,
  "exp": 1562163435,
  "iat": 1561558635,
  "iss": "tfp://identity.jsm/authority",
  "aud": "bc051de3-aeaa-4ac9-8fbf-05a86e083fcf"
}
"""

get_user_email_from_jwt()
"""
Response example:
"64722338612@loyaltyJSM.com"
"""

get_user_id_from_jwt()
"""
Response example:
"85071dde-b568-41af-a579-3f42c1df0998"
"""

get_user_data_from_server()
"""
Response example:
{
    "id": "85071dde-b568-41af-a579-3f42c1df0998",
    "username": "44444444444",
    "cpf": "44444444444",
    "occupation": "owner",
    "name": "ca1bd09f-b109-435f-b6d5-206d28a3e5e2",
    "birthday": "1990-06-15",
    "phones": [{"number": "5511111111111", "type": "MOBILE"}],
    "emails": [{"email": "fernando@gmail.com", "type": "PERSONAL"}],
    "roles": ["Proprietário"],
    "gender": "male",
    "allow_contact": True,
    "mediums": ["SMS", "Push"],
    "rules_agreement": True,
    "addresses": [
        {
            "postal_code": "06763180",
            "street": "Rua Adolfino Arruda Castanho",
            "number": "200",
            "complement": "Ap 133",
            "district": "Jardim Bom Tempo",
            "city": "Taboao da Serra",
            "state": "SP",
            "country": "Brazil",
            "type": "HOME",
        }
    ]
}
"""

get_user_data_from_cpf(cpf)
"""
Response example:
{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": "888707ab-9866-42a3-a67e-23a937cb3e7f",
            "created_at": "2019-10-18T20:06:44.552669Z",
            "updated_at": "2019-10-23T14:08:05.737326Z",
            "user_id_ref": "5cfd56c1-1bc1-4175-9fa9-00449ec448b8",
            "metadata": {
                "id": "5cfd56c1-1bc1-4175-9fa9-00449ec448b8",
                "cpf": "42879321964",
                "name": "Fernanda Gabrielly Duarte",
                "roles": [
                    "manager"
                ],
                "emails": [
                    {
                        "type": "personal",
                        "email": "fernandagabriellyduarte@ymail.com"
                    }
                ],
                "gender": "female",
                "phones": [
                    {
                        "type": "mobile",
                        "number": "5519991901717"
                    }
                ],
                "status": "active",
                "mediums": [
                    "sms"
                ],
                "birthday": "1982-06-04",
                "username": "42879321964",
                "addresses": [
                    {
                        "city": "Cascavel",
                        "type": "main",
                        "state": "PR",
                        "number": "217",
                        "street": "Rua Riachuelo",
                        "country": "Brazil",
                        "district": "Centro",
                        "postal_code": "85812110"
                    },
                    {
                        "city": "Cascavel",
                        "type": "shipping",
                        "state": "PR",
                        "number": "217",
                        "street": "Rua Riachuelo",
                        "country": "Brazil",
                        "district": "Centro",
                        "postal_code": "85812110"
                    }
                ],
                "occupation": "c85583ef-b602-4fea-b202-6fc5c58d8189",
                "allow_contact": true,
                "blocked_reason": null,
                "favorite_medium": "sms",
                "registered_date": "2019-10-18T20:06:31.705758+00:00",
                "rules_agreement": true,
                "rules_agreement_date": "2019-10-18T20:06:31.708640+00:00"
            },
            "emails": "fernandagabriellyduarte@ymail.com",
            "phones": "5519991901717"
        }
    ]
}
"""

get_cpf_from_jwt()
"""
Response example:
"09345699823"
"""

get_user_ip()
"""
Response example:
"192.168.0.1" or None
"""

```

### DRF Permissions

We have a set of permissions for Django REST Framework (DRF) that can be used to protect views. These permissions can be also protected by `jsm_user_services.drf.permissions.protected` decorator.

Example:

```python
from jsm_user_services.drf.authentications import LvJWTAuthentication
from jsm_user_services.drf.permissions import protected
from jsm_user_services.drf.permissions.lv import LVClientBuyerPermission

@authentication_classes((LvJWTAuthentication,))
@permission_classes((protected(LVClientBuyerPermission),))
def my_view(request: Request):
    pass
```

`protected` decorator can receive a couple of arguments:
- `keys`: set of keys to be protected, this will be injected to the request as `request.protected_keys` to the be used by the user. (Default: None)
- `should_return_forbidden`: if True, the permission will return a 403 Forbidden response if "fid" is present in the JWT token. (Default: False)

## Versioning
This lib follows the [pypi version format](https://www.python.org/dev/peps/pep-0440/) with the convention of using 
_major_._minor_._patch_ version.

### When to bump a patch version?
Bump the patch version if you are doing a quick fix, nothing that changes the library functionality.

### When to bump the minor version?
Bump the minor version if you are adding new functionality without breaking backwards compatibility. For example, 
adding support to new events.

### When to bump the major version?
Bump the major version if you are breaking backwards compatibility by adding new functionality or refactoring.