Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ env/
tmp/

db.sqlite3
source/app/conf/development/.env
source/app/conf/production/.env
3 changes: 3 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ name = "pypi"
[packages]
Django = "==4.1.4"
"django-bootstrap4" = "==22.3"

pangea-sdk = "*"
django-environ = "*"
264 changes: 263 additions & 1 deletion Pipfile.lock

Large diffs are not rendered by default.

104 changes: 56 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,79 +1,87 @@
# Simple Django Login and Registration
# Pangea Django Example

An example of Django project with basic user functionality.
This is an example of Python Django project integrating with Pangea's [Audit Log](https://pangea.cloud/services/audit-log/) and [File Intel Services](https://pangea.cloud/services/file-intel/).

## Screenshots
In this example, you will see: How to integrate Pangea’s python SDK, Secure Audit Log, and File Intel service and also a File Upload feature in Python’s Django web framework.

| Log In | Create an account | Authorized page |
| -------|--------------|-----------------|
| <img src="./screenshots/login.png" width="200"> | <img src="./screenshots/create_an_account.png" width="200"> | <img src="./screenshots/authorized_page.png" width="200"> |
Sample App is taken from [egorsmkv](https://github.com/egorsmkv/simple-django-login-and-register-dynamic-lang)'s Django Simple login and register example.

| Password reset | Set new password | Password change |
| ---------------|------------------|-----------------|
| <img src="./screenshots/password_reset.png" width="200"> | <img src="./screenshots/set_new_password.png" width="200"> | <img src="./screenshots/password_change.png" width="200"> |
## Prerequisites
* Python 3
* A Pangea account - [sign up here](https://console.pangea.cloud/)
* [Create an API token](https://console.pangea.cloud/project/tokens) in Pangea Console for Secure Audit Log and File Intel Services
* Clone [this repo](https://github.com/pangeacyber/pangea-django-example)

## Functionality
## Installing Pangea Python SDK

- Log in
- via username & password
- via email & password
- via email or username & password
- with a remember me checkbox (optional)
- Create an account
- Log out
- Profile activation via email
- Reset password
- Remind a username
- Resend an activation code
- Change password
- Change email
- Change profile
- Multilingual: English, French, Russian, Simplified Chinese and Spanish
The Pangea Python Django Sample contains separated logic for user accounts and app modules. source/accounts specifically contain login and registration flows, which this tutorial will focus on.

If you need dynamic URLs with the language code, check out https://github.com/egorsmkv/simple-django-login-and-register-dynamic-lang

## Installing

### Clone the project
First, add Pangea Python SDK as a dependency on the project. Open Pipfile in the project and add the lines below:

```bash
git clone https://github.com/egorsmkv/simple-django-login-and-register
cd simple-django-login-and-register
pangea-sdk = "*"
django-environ = "*"
```

### Install dependencies & activate virtualenv
You are adding the [pangea-python-sdk](https://pangea.cloud/docs/sdk/python/) and [django-environ](https://django-environ.readthedocs.io/en/latest/) latest packages to the project.

Run the commands below to install [pipenv](https://github.com/pypa/pipenv) (a python packaging & environment manager), install python dependencies, and activate the pipenv environment.

```bash
pip install pipenv

pipenv install
pipenv shell
```

### Configure the settings (connection to the database, connection to an SMTP server, and other options)

1. Edit `source/app/conf/development/settings.py` if you want to develop the project.

2. Edit `source/app/conf/production/settings.py` if you want to run the project in production.

### Apply migrations
Run the below commands to start the app and create a new user account.

```bash
python source/manage.py migrate

python source/manage.py runserver
```

### Collect static files (only on a production server)

> Note:

* Python 3 is the default version for this project.
* Instead of modifying the Pipfile, you can install the package by running pipenv install python-pangea

## Configuring Pangea’s API Keys

> Note: You generated API tokens to communicate with Pangea’s services in the previous section.Remember, API tokens are sensitive and should not be checked into your code. django-environ is being used to manage the environment variables through which the API tokens are accessed in the project.

Rename the `source/app/conf/development/.env.example` file to `.env` and fill in the details below by going to the Pangea Console.

```bash
python source/manage.py collectstatic
PANGEA_DOMAIN=
PANGEA_AUDIT_TOKEN=
PANGEA_FILE_INTEL_TOKEN=
```

### Running
Domain is the region where your application's services are hosted and consumed. The tokens give access to the respective services you enable in the Pangea Console.

#### A development server
Add the code below to `source/app/conf/development/settings`.py to import and configure the `django-environ` package.

Just run this command:
```python
import environ

env = environ.Env()
environ.Env.read_env()
```

```bash
python source/manage.py runserver
Below the constants in your code, add the Pangea API token and make it accessible in the entire application.

```python
#Pangea
PANGEA_DOMAIN = env('PANGEA_DOMAIN')
PANGEA_AUDIT_TOKEN = env('PANGEA_AUDIT_TOKEN')
PANGEA_FILE_INTEL_TOKEN = env('PANGEA_FILE_INTEL_TOKEN')
```


> Note: You can configure a different domain and API token configuration for a production environment. In this tutorial, you are configuring everything for development.

Adding [Secure Audit Log Integration ->](https://github.com/pangeacyber/pangea-django-example/tree/2-secure-audit-log)

3 changes: 3 additions & 0 deletions source/app/conf/development/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PANGEA_DOMAIN=
PANGEA_AUDIT_TOKEN=
PANGEA_FILE_INTEL_TOKEN=
15 changes: 12 additions & 3 deletions source/app/conf/development/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import warnings
from django.utils.translation import gettext_lazy as _
from os.path import dirname
import environ

env = environ.Env()
environ.Env.read_env()

warnings.simplefilter('error', DeprecationWarning)

Expand Down Expand Up @@ -92,14 +96,19 @@
},
]

ENABLE_USER_ACTIVATION = True
ENABLE_USER_ACTIVATION = False
DISABLE_USERNAME = False
LOGIN_VIA_EMAIL = True
LOGIN_VIA_EMAIL_OR_USERNAME = False
LOGIN_VIA_EMAIL = False
LOGIN_VIA_EMAIL_OR_USERNAME = True
LOGIN_REDIRECT_URL = 'index'
LOGIN_URL = 'accounts:log_in'
USE_REMEMBER_ME = True

#Pangea
PANGEA_DOMAIN = env('PANGEA_DOMAIN')
PANGEA_AUDIT_TOKEN = env('PANGEA_AUDIT_TOKEN')
PANGEA_FILE_INTEL_TOKEN = env('PANGEA_FILE_INTEL_TOKEN')

RESTORE_PASSWORD_VIA_EMAIL_OR_USERNAME = False
ENABLE_ACTIVATION_AFTER_EMAIL_CHANGE = True

Expand Down
3 changes: 3 additions & 0 deletions source/app/conf/production/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PANGEA_DOMAIN=
PANGEA_AUDIT_TOKEN=
PANGEA_FILE_INTEL_TOKEN=