Skip to content

Commit 9d51168

Browse files
committed
.
1 parent f9e09eb commit 9d51168

File tree

8 files changed

+255
-10
lines changed

8 files changed

+255
-10
lines changed

.gitignore

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Python template
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
env/
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
lib/
21+
lib64/
22+
parts/
23+
sdist/
24+
var/
25+
wheels/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*,cover
49+
.hypothesis/
50+
51+
# Translations
52+
*.mo
53+
*.pot
54+
55+
# Django stuff:
56+
*.log
57+
local_settings.py
58+
59+
# Flask stuff:
60+
instance/
61+
.webassets-cache
62+
63+
# Scrapy stuff:
64+
.scrapy
65+
66+
# Sphinx documentation
67+
docs/_build/
68+
69+
# PyBuilder
70+
target/
71+
72+
# Jupyter Notebook
73+
.ipynb_checkpoints
74+
75+
# pyenv
76+
.python-version
77+
78+
# celery beat schedule file
79+
celerybeat-schedule
80+
81+
# SageMath parsed files
82+
*.sage.py
83+
84+
# dotenv
85+
.env
86+
87+
# virtualenv
88+
.venv
89+
venv/
90+
ENV/
91+
92+
# Spyder project settings
93+
.spyderproject
94+
95+
# Rope project settings
96+
.ropeproject
97+
98+
.idea
99+
idea/
100+
101+
/db.sqlite3
102+
db.sqlite3
103+

config/settings.py

+8
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,11 @@
137137
# https://docs.djangoproject.com/en/1.11/howto/static-files/
138138

139139
STATIC_URL = '/static/'
140+
141+
#################################################
142+
# UNIFI CONFIGURATION #
143+
#################################################
144+
UNIFI_SERVER = "https://192.168.10.78:8443"; #Change to the IP/FQDN of your UniFi Server
145+
#It's important to note that if this server is offsite, you need to have port 8443 forwarded through to it
146+
UNIFI_USER = "administrator"; #Change to your UniFi Username
147+
UNIFI_PASSWORD = "xxxx!"; #Change to your UniFi Password

templates/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<title>Django Unifi Portal</title>
66
</head>
77
<body>
8-
8+
<h1>Welcome Django Unifi Portal</h1>
99
</body>
1010
</html>

templates/login.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ <h5 style="text-align:center">or</h5>
2323
{% block formbuttons %}
2424
{% include form.buttons %}
2525

26-
<div class="row">
26+
<div class="row left-align">
2727
<div class="checkbox-field col s12 required" id="id_terms_accepted_container">
2828
<label for="id_terms_accepted">If you click "Log in with Facebook" and are not a user, you will be registered and you agree to Terms & Conditions and Privacy Policy.</label>
2929
</div>

tests/__init__.py

Whitespace-only changes.

tests/test_socialLoginForm.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
3+
from django.contrib.auth.models import User
4+
from django.core.urlresolvers import reverse
5+
from django.test.client import Client
6+
7+
from unifi_portal.models import UnifiUser
8+
9+
class LoginTestCase(unittest.TestCase):
10+
def setUp(self):
11+
self.client = Client()
12+
13+
self.user = User();
14+
self.user.set_password('johnpassword');
15+
self.user.username = 'john';
16+
self.user.email = 'lennon@thebeatles.com';
17+
self.user.is_active = True;
18+
self.user.save();
19+
20+
fbuserprofile = UnifiUser();
21+
fbuserprofile.user = self.user;
22+
fbuserprofile.save();
23+
24+
def testLogin(self):
25+
print ">> testLogin"
26+
response = self.client.login(username='john', password='johnpassword')
27+
self.assertEqual(response, True)
28+
29+
class IndexTestCase(unittest.TestCase):
30+
def setUp(self):
31+
self.client = Client()
32+
33+
def testIndexView(self):
34+
print ">> testIndexView"
35+
unifi_url = '/guest/s/default/?id=88:ad:d2:e4:cf:f4&ap=f0:9f:c2:39:86:f2&t=1496848672&url=http://connectivitycheck.gstatic.com%2fgenerate_204&ssid=ClubLaVela'
36+
print unifi_url
37+
response = self.client.get(unifi_url)
38+
self.assertEqual(response.status_code, 200);

unifi_portal/urls.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22
from django.conf.urls import include, url
33
from django.contrib.auth.decorators import login_required
44
from django.contrib import admin
5-
from django.shortcuts import render
65
from django.views import generic, static
76
from django.contrib.auth import views as auth_views
87

98
from unifi_portal import forms
10-
from unifi_portal.views import UnifiUserRegistration
9+
from unifi_portal.views import index_view, UnifiUserRegistration
1110

1211

13-
def index_view(request):
14-
context = {
15-
}
16-
return render(request, 'index.html', context)
17-
1812
urlpatterns = [
1913

20-
url(r'^$',index_view, name='index'),
14+
url(r'guest/s/default/$', index_view, name='index'),
2115
url(r'^login/$', auth_views.login,
2216
{'template_name': 'login.html',
2317
'authentication_form': forms.SocialLoginForm},

unifi_portal/views.py

+102
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,112 @@
55
from django.urls import reverse
66
from django.views.generic.edit import FormView
77
from django.core.urlresolvers import reverse_lazy
8+
from django.conf import settings
9+
from django.shortcuts import render
810

911
from forms import UnifiRegistrationForm
1012
from models import UnifiUser
1113

14+
def index_view(request, *args, **kwargs):
15+
_mac = request.GET.get('id', '')
16+
_ap = request.GET.get('ap', '')
17+
_url = request.GET.get('url', '')
18+
_t = request.GET.get('t', '')
19+
20+
print "mac:", _mac
21+
print "ap:", _ap
22+
print "t:", _t
23+
print "url:", _url
24+
25+
context = {
26+
}
27+
return render(request, 'index.html', context)
28+
29+
30+
def send_authorization(id, ap, minutes, url):
31+
unifiServer = settings.UNIFI_SERVER;
32+
unifiUser = settings.UNIFI_USER
33+
unifiPass = settings.UNIFI_USER
34+
35+
#
36+
# 1) Login to Unifi Server
37+
#
38+
'''
39+
//Start Curl for login
40+
$ch = curl_init();
41+
// We are posting data
42+
curl_setopt($ch, CURLOPT_POST, TRUE);
43+
// Set up cookies
44+
$cookie_file = "/tmp/unifi_cookie";
45+
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
46+
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
47+
// Allow Self Signed Certs
48+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
49+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
50+
// Force SSL3 only
51+
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
52+
// Login to the UniFi controller
53+
curl_setopt($ch, CURLOPT_URL, "$unifiServer/login");
54+
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=login&username=$unifiUser&password=$unifiPass");
55+
curl_exec ($ch);
56+
curl_close ($ch);
57+
'''
58+
59+
#
60+
# 2) Send user to authorize and the time allowed
61+
#
62+
data = {
63+
'cmd': 'authorize-guest',
64+
'mac': id,
65+
'ap': ap,
66+
'minutes': minutes
67+
}
68+
print "data->", data
69+
70+
'''
71+
$ch = curl_init();
72+
// We are posting data
73+
curl_setopt($ch, CURLOPT_POST, TRUE);
74+
// Set up cookies
75+
$cookie_file = "/tmp/unifi_cookie";
76+
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
77+
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
78+
// Allow Self Signed Certs
79+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
80+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
81+
// Force SSL3 only
82+
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
83+
// Make the API Call
84+
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/api/cmd/stamgr');
85+
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data);
86+
curl_exec ($ch);
87+
curl_close ($ch);
88+
'''
89+
90+
#
91+
# 3) Logout of the connection
92+
#
93+
'''
94+
$ch = curl_init();
95+
// We are posting data
96+
curl_setopt($ch, CURLOPT_POST, TRUE);
97+
// Set up cookies
98+
$cookie_file = "/tmp/unifi_cookie";
99+
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
100+
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
101+
// Allow Self Signed Certs
102+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
103+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
104+
// Force SSL3 only
105+
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
106+
// Make the API Call
107+
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/logout');
108+
curl_exec ($ch);
109+
curl_close ($ch);
110+
echo "Login successful, I should redirect to: ".$url; // $_SESSION['url'];
111+
sleep(8); // Small sleep to allow controller time to authorize
112+
header('Location: '.$url); // $_SESSION['url']);
113+
'''
12114

13115
class UnifiUserRegistration(FormView):
14116
template_name = 'registration.html'

0 commit comments

Comments
 (0)