Skip to content

Commit 9135a34

Browse files
committed
rename some of the files to scatter-auth
Added dev csrf_exempt Added getCookie to base Some js (working) in signup.html
1 parent c766967 commit 9135a34

File tree

12 files changed

+150
-58
lines changed

12 files changed

+150
-58
lines changed

LICENSE

-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
MIT License
22

3-
Copyright (c) 2018 Alexander Tereshkin
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
22-
23-
MIT License
24-
253
Copyright (c) 2018 Bearle
264

275
Permission is hereby granted, free of charge, to any person obtaining a copy

README.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ Set your User model's field to use as ETH address provider:
6868

6969
.. code-block:: python
7070
71-
WEB3AUTH_USER_ADDRESS_FIELD = 'username'
71+
SCATTERAUTH_USER_ADDRESS_FIELD = 'username'
7272
7373
And if you have some other fields you want to be in the SignupForm, add them too:
7474

7575
.. code-block:: python
7676
77-
WEB3AUTH_USER_SIGNUP_FIELDS = ['email',]
77+
SCATTERAUTH_USER_SIGNUP_FIELDS = ['email',]
7878
7979
8080
Add Django-Web3-Auth's URL patterns:
@@ -128,7 +128,7 @@ If you have any questions left, head to the example app https://github.com/Bearl
128128
Important details and FAQ
129129
-------------------------
130130

131-
1. *If you set a custom address field (WEB3AUTH_USER_ADDRESS_FIELD), it MUST be unique (unique=True).*
131+
1. *If you set a custom address field (SCATTERAUTH_USER_ADDRESS_FIELD), it MUST be unique (unique=True).*
132132

133133
This is needed because if it's not, the user can register a new account with the same address as the other one,
134134
meaning that the user can now login as any of those accounts (sometimes being the wrong one).

docs/overview.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and 2 views for Signup (one with JSON responses, and the other - using Django Fo
77

88
It also has 2 forms, SignupForm (rendered) and LoginForm (uses hidden inputs, used to validate data only).
99

10-
Possible configuration includes customizable address field (``WEB3AUTH_USER_ADDRESS_FIELD``), additional fields for User model (``WEB3AUTH_USER_SIGNUP_FIELDS``) and on/off switch for registration (``WEB3AUTH_SIGNUP_ENABLED``).
10+
Possible configuration includes customizable address field (``SCATTERAUTH_USER_ADDRESS_FIELD``), additional fields for User model (``SCATTERAUTH_USER_SIGNUP_FIELDS``) and on/off switch for registration (``SCATTERAUTH_SIGNUP_ENABLED``).
1111
You can read more on that in the Configuration section.
1212

1313
Sign up
@@ -16,7 +16,7 @@ Sign up
1616
The signup process is as follows (signup_view example, signup_api is similar):
1717

1818
1. User heads to the signup URL (``{% url 'web3auth_signup' %}``)
19-
2. The signup view is rendered with a ``SignupForm`` which includes ``WEB3AUTH_USER_SIGNUP_FIELDS`` and ``WEB3AUTH_USER_ADDRESS_FIELD``
19+
2. The signup view is rendered with a ``SignupForm`` which includes ``SCATTERAUTH_USER_SIGNUP_FIELDS`` and ``SCATTERAUTH_USER_ADDRESS_FIELD``
2020
3. The user enters required data and clicks the submit button and the POST request fires to the same URL with ``signup_view``
2121
4. Signup view does the following:
2222
4.1. Creates an instance of a ``SignupForm``.

docs/settings.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Settings
44

55
You should specify settings in your settings.py like this::
66

7-
WEB3AUTH_USER_ADDRESS_FIELD = 'address'
8-
WEB3AUTH_USER_SIGNUP_FIELDS = ['email', 'username']
7+
SCATTERAUTH_USER_ADDRESS_FIELD = 'address'
8+
SCATTERAUTH_USER_SIGNUP_FIELDS = ['email', 'username']
99

1010

1111
In the above example the following User model is used:
@@ -29,9 +29,9 @@ Here's a list of available settings:
2929
+--------------------------------+------------+-------------------------------------------------------------------------+
3030
| Setting | Default | Description |
3131
+================================+============+=========================================================================+
32-
| WEB3AUTH_SIGNUP_ENABLED | True | If False, new users won't be able to sign up (used in ``signup_view``) |
32+
| SCATTERAUTH_SIGNUP_ENABLED | True | If False, new users won't be able to sign up (used in ``signup_view``) |
3333
+--------------------------------+------------+-------------------------------------------------------------------------+
34-
| WEB3AUTH_USER_SIGNUP_FIELDS | ['email'] | Specifies field to be used in signup form for a new User model |
34+
| SCATTERAUTH_USER_SIGNUP_FIELDS | ['email'] | Specifies field to be used in signup form for a new User model |
3535
+--------------------------------+------------+-------------------------------------------------------------------------+
36-
| WEB3AUTH_USER_ADDRESS_FIELD | 'username' | Field on the User model, which has ethereum address to check against. |
36+
| SCATTERAUTH_USER_ADDRESS_FIELD | 'username' | Field on the User model, which has ethereum address to check against. |
3737
+--------------------------------+------------+-------------------------------------------------------------------------+

example/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Example Project for web3auth
1+
Example Project for scatter-auth
22

33
This example is provided as a convenience feature to allow potential users to try the app straight from the app repo without having to create a django project.
44

example/templates/web3auth/base.html

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
{% load staticfiles i18n %}<!DOCTYPE html>
1+
{% load static i18n %}
2+
<!DOCTYPE html>
23
<html lang="en">
34
<head>
45
<meta charset="utf-8">
@@ -45,6 +46,11 @@
4546
<li class="nav-item">
4647
<a class="nav-link" href="{% url 'web3auth_signup' %}">Signup</a>
4748
</li>
49+
{% if request.user.is_authenticated %}
50+
<li class="nav-item">
51+
<a class="nav-link">Logged in as {{ request.user.username }}</a>
52+
</li>
53+
{% endif %}
4854
</ul>
4955
</div>
5056
</nav>
@@ -84,7 +90,24 @@
8490

8591
<!-- Latest compiled and minified JavaScript -->
8692
<script src="https://cdn.rawgit.com/twbs/bootstrap/v4-dev/dist/js/bootstrap.js"></script>
87-
93+
<script>
94+
function getCookie(name) {
95+
var cookieValue = null;
96+
if (document.cookie && document.cookie != '') {
97+
var cookies = document.cookie.split(';');
98+
for (var i = 0; i < cookies.length; i++) {
99+
var cookie = jQuery.trim(cookies[i]);
100+
// Does this cookie string begin with the name we want?
101+
if (cookie.substring(0, name.length + 1) == (name + '=')) {
102+
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
103+
break;
104+
}
105+
}
106+
}
107+
return cookieValue;
108+
}
109+
110+
</script>
88111
<!-- Your stuff: Third-party javascript libraries go here -->
89112
<script src="{% static 'web3auth/js/web3auth.js' %}"></script>
90113

example/templates/web3auth/signup.html

+91
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,94 @@
88
</form>
99
</div>
1010
{% endblock content %}
11+
{% block javascript %}
12+
{{ block.super }}
13+
<script>
14+
const signup = '{% url 'web3auth_signup_api' %}';
15+
const CHAIN_ID = '038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca';
16+
const CHAIN_PROTOCOL = 'http';
17+
const CHAIN_HOST = '193.93.219.219';
18+
const CHAIN_PORT = 8888;
19+
20+
const NETWORK = {
21+
protocol: CHAIN_PROTOCOL,
22+
blockchain: 'eos',
23+
host: CHAIN_HOST,
24+
port: CHAIN_PORT,
25+
chainId: CHAIN_ID
26+
};
27+
28+
function signupWithData(username, email, signup_url, onSignupRequestError, onSignupSuccess, onSignupFail) {
29+
var request = new XMLHttpRequest();
30+
request.open('POST', signup_url, true);
31+
request.onload = function () {
32+
if (request.status >= 200 && request.status < 400) {
33+
// Success!
34+
var resp = JSON.parse(request.responseText);
35+
if (resp.success) {
36+
if (typeof onSignupSuccess === 'function') {
37+
onSignupSuccess(resp);
38+
}
39+
} else {
40+
if (typeof onSignupFail === 'function') {
41+
onSignupFail(resp);
42+
}
43+
}
44+
} else {
45+
// We reached our target server, but it returned an error
46+
console.log("Signup failed - request status " + request.status);
47+
if (typeof onSignupRequestError === 'function') {
48+
onSignupRequestError(request);
49+
}
50+
}
51+
};
52+
53+
request.onerror = function () {
54+
console.log("Signup failed - there was an error");
55+
if (typeof onSignupRequestError === 'function') {
56+
onSignupRequestError(request);
57+
}
58+
// There was a connection error of some sort
59+
};
60+
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
61+
request.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
62+
var formData = 'username=' + username + '&email=' + email;
63+
request.send(formData);
64+
}
65+
66+
async function requestIdentity(onIdentityReject) {
67+
await scatter.suggestNetwork(NETWORK);
68+
69+
let required_fields = {
70+
personal: ['email'],
71+
accounts: [NETWORK]
72+
};
73+
74+
scatter.getIdentity(required_fields).then((identity) => {
75+
var account = identity.accounts.find(function (account) {
76+
return account.blockchain === 'eos'
77+
});
78+
79+
signupWithData(account.name, scatter.identity.personal.email, signup, console.log, console.log, console.log)
80+
81+
}).catch(error => {
82+
console.log("Identity or Network was rejected");
83+
if (typeof onIdentityReject === 'function') {
84+
onIdentityReject(error);
85+
}
86+
})
87+
}
88+
89+
document.addEventListener('scatterLoaded', scatterExtension => {
90+
console.log("Scatter installed!");
91+
if (scatter.identity) {
92+
// login the user with api
93+
console.log("Identity found");
94+
console.log(scatter.identity);
95+
console.log("Welcome, " + scatter.identity.name + '<br>Your public key: ' + scatter.identity.publicKey);
96+
} else {
97+
requestIdentity()
98+
}
99+
});
100+
</script>
101+
{% endblock javascript %}

setup.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ def get_version(*file_paths):
4545
history = open('HISTORY.rst').read().replace('.. :changelog:', '')
4646

4747
setup(
48-
name='django-web3-auth',
48+
name='django-scatter-auth',
4949
version=version,
5050
description="""django-web3-auth is a pluggable Django app that enables login/signup via an Ethereum wallet (a la CryptoKitties). The user authenticates themselves by digitally signing the session key with their wallet's private key.""",
5151
long_description=readme + '\n\n' + history,
5252
author='Bearle',
5353
author_email='tech@bearle.ru',
54-
url='https://github.com/Bearle/django-web3-auth',
54+
url='https://github.com/Bearle/django-scatter-auth',
5555
packages=[
56-
'web3auth',
56+
'scatter_auth',
5757
],
5858
include_package_data=True,
5959
install_requires=['ethereum>=2.3.1', 'rlp==0.6.0','eth_utils>=1.0.3'],
6060
license="MIT",
6161
zip_safe=False,
62-
keywords='django-web3-auth',
62+
keywords='django-scatter-auth',
6363
classifiers=[
6464
'Development Status :: 3 - Alpha',
6565
'Framework :: Django :: 1.11',

web3auth/backend.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def authenticate(self, request, address=None, token=None, signature=None):
1616
return None
1717
else:
1818
# get address field for the user model
19-
address_field = app_settings.WEB3AUTH_USER_ADDRESS_FIELD
19+
address_field = app_settings.SCATTERAUTH_USER_ADDRESS_FIELD
2020
kwargs = {
2121
address_field+"__iexact": address
2222
}

web3auth/forms.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def clean_signature(self):
2424

2525

2626
# list(set()) here is to eliminate the possibility of double including the address field
27-
signup_fields = list(set(app_settings.WEB3AUTH_USER_SIGNUP_FIELDS + [app_settings.WEB3AUTH_USER_ADDRESS_FIELD]))
27+
signup_fields = list(set(app_settings.SCATTERAUTH_USER_SIGNUP_FIELDS + [app_settings.SCATTERAUTH_USER_ADDRESS_FIELD]))
2828

2929

3030
class SignupForm(forms.ModelForm):
@@ -35,18 +35,18 @@ def __init__(self, *args, **kwargs):
3535

3636
# make sure to make email required, because password is not set
3737
# and if the user loses private key he can get 'reset' password link to email
38-
if 'email' in app_settings.WEB3AUTH_USER_SIGNUP_FIELDS:
38+
if 'email' in app_settings.SCATTERAUTH_USER_SIGNUP_FIELDS:
3939
self.fields['email'].required = True
40-
self.fields[app_settings.WEB3AUTH_USER_ADDRESS_FIELD].required = True
40+
self.fields[app_settings.SCATTERAUTH_USER_ADDRESS_FIELD].required = True
4141

4242
def clean_address_field(self):
43-
# validate_eth_address(self.cleaned_data[app_settings.WEB3AUTH_USER_ADDRESS_FIELD])
44-
return self.cleaned_data[app_settings.WEB3AUTH_USER_ADDRESS_FIELD].lower()
43+
# validate_eth_address(self.cleaned_data[app_settings.SCATTERAUTH_USER_ADDRESS_FIELD])
44+
return self.cleaned_data[app_settings.SCATTERAUTH_USER_ADDRESS_FIELD].lower()
4545

4646
class Meta:
4747
model = get_user_model()
4848
fields = signup_fields
4949

5050

5151
# hack to set the method for cleaning address field
52-
setattr(SignupForm, 'clean_' + app_settings.WEB3AUTH_USER_ADDRESS_FIELD, SignupForm.clean_address_field)
52+
setattr(SignupForm, 'clean_' + app_settings.SCATTERAUTH_USER_ADDRESS_FIELD, SignupForm.clean_address_field)

web3auth/settings.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33

44
class AppSettings(object):
55
@property
6-
def WEB3AUTH_USER_ADDRESS_FIELD(self):
6+
def SCATTERAUTH_USER_ADDRESS_FIELD(self):
77
"""
88
Field on the User model, which has ethereum address to check against.
99
This allows you to store it somewhere in arbitrary place other than just username.
1010
"""
11-
return getattr(django_settings, 'WEB3AUTH_USER_ADDRESS_FIELD', 'username')
11+
return getattr(django_settings, 'SCATTERAUTH_USER_ADDRESS_FIELD', 'username')
1212

1313
@property
14-
def WEB3AUTH_USER_SIGNUP_FIELDS(self):
14+
def SCATTERAUTH_USER_SIGNUP_FIELDS(self):
1515
"""
1616
Specifies field to be used in signup form for a new User model
1717
"""
18-
return getattr(django_settings, "WEB3AUTH_USER_SIGNUP_FIELDS", ['email'])
18+
return getattr(django_settings, "SCATTERAUTH_USER_SIGNUP_FIELDS", ['email'])
1919

2020
@property
21-
def WEB3AUTH_SIGNUP_ENABLED(self):
21+
def SCATTERAUTH_SIGNUP_ENABLED(self):
2222
"""
2323
Makes it possible to disable signups (similar to allauth)
2424
"""
25-
return getattr(django_settings, "WEB3AUTH_SIGNUP_ENABLED", True)
25+
return getattr(django_settings, "SCATTERAUTH_SIGNUP_ENABLED", True)
2626

2727

2828
app_settings = AppSettings()

web3auth/views.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from web3auth.utils import recover_to_addr
1212
from django.utils.translation import ugettext_lazy as _
1313
from web3auth.settings import app_settings
14-
14+
from django.views.decorators.csrf import csrf_exempt
1515
import json
1616

1717

@@ -56,15 +56,15 @@ def login_api(request):
5656
else:
5757
return JsonResponse({'success': False, 'error': json.loads(form.errors.as_json())})
5858

59-
59+
@csrf_exempt
6060
@require_http_methods(["POST"])
6161
def signup_api(request):
62-
if not app_settings.WEB3AUTH_SIGNUP_ENABLED:
62+
if not app_settings.SCATTERAUTH_SIGNUP_ENABLED:
6363
return JsonResponse({'success': False, 'error': _("Sorry, signup's are currently disabled")})
6464
form = SignupForm(request.POST)
6565
if form.is_valid():
6666
user = form.save(commit=False)
67-
addr_field = app_settings.WEB3AUTH_USER_ADDRESS_FIELD
67+
addr_field = app_settings.SCATTERAUTH_USER_ADDRESS_FIELD
6868
setattr(user, addr_field, form.cleaned_data[addr_field])
6969
user.save()
7070
login(request, user, 'web3auth.backend.Web3Backend')
@@ -88,14 +88,14 @@ def signup_view(request, template_name='web3auth/signup.html'):
8888
:return: rendered template with form
8989
"""
9090
form = SignupForm()
91-
if not app_settings.WEB3AUTH_SIGNUP_ENABLED:
91+
if not app_settings.SCATTERAUTH_SIGNUP_ENABLED:
9292
form.add_error(None, _("Sorry, signup's are currently disabled"))
9393
else:
9494
if request.method == 'POST':
9595
form = SignupForm(request.POST)
9696
if form.is_valid():
9797
user = form.save(commit=False)
98-
addr_field = app_settings.WEB3AUTH_USER_ADDRESS_FIELD
98+
addr_field = app_settings.SCATTERAUTH_USER_ADDRESS_FIELD
9999
setattr(user, addr_field, form.cleaned_data[addr_field])
100100
user.save()
101101
login(request, user, 'web3auth.backend.Web3Backend')

0 commit comments

Comments
 (0)