# JSM User JWT Service

Middleware to intercept JWT auth token and more utils functions

## Install

`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",
]
```

## 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

current_jwt_token()
"""
Response example:
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9. .oZvlK-XOBrgN9xZrChHHRdQ0rLMFTPp9jjuGvGM1U78"
"""

get_jsm_token()
"""
Response example:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6IjY0NzIyMzM4NjEyQGxveWFsdHlKU00uY29tIiwidW5pcXVlX25hbWUiOiJUZXN0IFVzZXIiLCJ3ZWJzaXRlIjoiQ2xpZW50QXBwbGljYXRpb24iLCJ1aWQiOiI4NzM1YmY4ZC01NDc5LTQ0NDgtODNjZi04OTk5N2Q3ZGY2MDgiLCJzZWxsZXJzIjoiMWUzYjgzMWYtOTVkMC00MWE3LWIzZTEtMWZlMDNmYWUyMDQ2Iiwicm9sZSI6IkN1c3RvbWVyIiwiY25wanMiOiI2OC41OTcuMjgwLzAwMDEtMDkiLCJuYmYiOjE1NjE1NTg2MzUsImV4cCI6MTU2MjE2MzQzNSwiaWF0IjoxNTYxNTU4NjM1LCJpc3MiOiJ0ZnA6Ly9pZGVudGl0eS5qc20vYXV0aG9yaXR5IiwiYXVkIjoiYmMwNTFkZTMtYWVhYS00YWM5LThmYmYtMDVhODZlMDgzZmNmIn0.4dT-6_PmJEbWGw5Q1F18mJBQcXcVmNugQwiLxp6e4Ew"
"""
get_ltm_token()
"""
Response example:
" "
"""

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:
{
    "pageIndex": 1,
    "pageSize": 20,
    "items": [
        "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",
            }
        ]
    ],
    "totalItems": 0,
    "totalPages": 1
}
"""
```


