Skip to main content

Installation

pip install sss-auth

Django side setup

from a Django project run INSTALLED_APPSorAUTHENTICATION_BACKENDSadd to

INSTALLED_APPS = (
...
'sssauth.apps.sssAuthConfig',
...
)
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'sssauth.backend.Web3Backend'
]

Add the settings used by sssauth to settings.py.

SERVER_SECRET = '**************'
PUB = "************"
OWNER = "********"
NETWORK_TYPE = 152 # mainnet: 104, testnet: 152
EXPIRATION_DATE = 60 * 1 * 1 * 1000

Customize your custom model

# Using CustomUser
AUTH_USER_MODEL = 'sssauth.MyUser'

Add sssauth url to url template

urlpatterns = [
path(r'^', include('sssauth.urls', namespace='sssauth')),
]

Describe the login behavior and add it to the URL

from sssauth.forms import LoginForm, SignupForm
urlpatterns = [
path('signup/', CreateView.as_view(
template_name='accounts/signup.html',
form_class=SignupForm,
success_url='/',
), name='signup'),
path('login/', LoginView.as_view(
redirect_authenticated_user=True,
template_name='accounts/login.html',
), name='login'),
path('logout/', LogoutView.as_view(), name='logout'),
]

Web page settings

Enter

Importing sssauth javascript and you can request a signature by executing RequestToken

  <script src="{% static 'sssauth/js/sssAuth.js' %}"></script>
<script>
function requestToken() {
if (typeof window.SSS !== 'undefined') {
checkSSS(function (loggedIn) {
if (!loggedIn) {
alert("Please allow thin page in SSS")
} else {
var login_url = '{% url 'sssauth:sssauth_login_api' %}';
SSSLogin(login_url,console.log, console.log, console.log, console.log, console.log, function (resp) {
window.location.replace(resp.redirect_url);
})
};
});

} else {
alert('SSS missing');
}
}
</script>

Sign up

importing sssauth javascript and you can request a signature by executing RequestToken

{% load django_bootstrap5 %}

{% block main %}

<h2>register</h2>
<form method="post">
{% csrf_token %}
{% bootstrap_form form %}
{% bootstrap_button button_type="submit" content="signup" %}
</form>
{% endblock %}

docker configuration Example

Directory placement

Configure the directory as follows

.
├── docker
│ ├── Dockerfile
│ └── requirements.txt
├── docker-compose.yml
├── python
│ ├── accounts
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── migrations
│ │ ├── models.py
│ │ ├── tests.py
│ │ ├── urls.py
│ │ └── views.py
│ ├── config
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ ├── asgi.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ ├── views.py
│ │ └── wsgi.py
│ ├── db.sqlite3
│ ├── manage.py
│ └── templates
│ ├── accounts
│ ├── base.html
│ ├── home.html
│ └── index.html
└── script
└── start.sh

docker container

Dockerfile

Make changes to the python container as needed.

## docker/Dockerfile

FROM python:3.9.5

ENV PYTHONUNBUFFERED 1
RUN mkdir /workspace

WORKDIR /workspace

ADD requirements.txt /workspace/

RUN pip install --upgrade pip
RUN pip install -r requirements.txt

Also, install the required package for python as follows.

# docker/requirements.txt
Django==4
django-bootstrap5
symbol-hkdf-python

docker-compose

We will define the container in docker-compose. Change the port number as needed. This time we will define it as follows.

## docker-compose.yml

version: '3'

services:
server:
build: ./docker
ports:
- 8000:8000
volumes:
- ./python/:/workspace
- ./script:/script
working_dir: /workspace
command: bash /script/start.sh

Setting up the Startup Script

Describes the startup process.sssauth To use authentication, you need to create an index at the first startup.After confirmation, start the server.

## scripts/start.sh

# indexの有無を確認
if [ ! -e 'db.sqlite3' ]; then
# 初回起動
echo "### initialize start ####"
python manage.py makemigrations web3auth
python manage.py migrate
echo "### initialize end ####"
else
# 2回目以降
echo "### Already setup ###"
fi
# Djangoの起動
python manage.py runserver 0.0.0.0:8000

Start the Container

After you have configured these files, you can start the server with the following command

docker-compose up -d

After the launch、http://localhost:8000please visit the link url to see if you can view this page.