Browse Source

basic auth

Roberto Berto 5 years ago
parent
commit
62f7f42ea6

+ 3 - 1
README.md

@@ -15,7 +15,9 @@ It will work with a single VyoS server or to multiple VyOS servers, so datacente
 * Forum Post https://forum.vyos.io/t/vycenter-alpha-stage-announcement-vyos-web-interface/5221/4
 
 ## features
-in alpha stage we're going to provide just essential device config, interfaces and firewall, as proof of concencept, as well config module basic cruds (users, groups and vyOS Servers)
+# in alpha stage we're going to provide just essential device config, interfaces and firewall, as proof of concencept, as well config module basic cruds (users, groups and vyOS Servers)
+# basic authentication - use django admin to create superuser than http://127.0.0.1:8000/admin/login/?next=/admin/ to create a new user, after that you can use VyControl
+
 
 ### interfaces module
 * list interfaces - alpha

+ 0 - 0
vycenter/auth/__init__.py → vycenter/accounts/__init__.py


+ 0 - 0
vycenter/auth/admin.py → vycenter/accounts/admin.py


+ 5 - 0
vycenter/accounts/apps.py

@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class AccountsConfig(AppConfig):
+    name = 'accounts'

+ 0 - 0
vycenter/auth/migrations/__init__.py → vycenter/accounts/migrations/__init__.py


+ 0 - 0
vycenter/auth/models.py → vycenter/accounts/models.py


+ 38 - 0
vycenter/accounts/templates/registration/login.html

@@ -0,0 +1,38 @@
+{% extends "base.html" %}
+
+{% block content %}
+
+{% if form.errors %}
+<p>Your username and password didn't match. Please try again.</p>
+{% endif %}
+
+{% if next %}
+    {% if user.is_authenticated %}
+    <p>Your account doesn't have access to this page. To proceed,
+    please login with an account that has access.</p>
+    {% else %}
+    <p>Please login to see this page.</p>
+    {% endif %}
+{% endif %}
+
+<form method="post" action="{% url 'login' %}">
+{% csrf_token %}
+<table>
+<tr>
+    <td>{{ form.username.label_tag }}</td>
+    <td>{{ form.username }}</td>
+</tr>
+<tr>
+    <td>{{ form.password.label_tag }}</td>
+    <td>{{ form.password }}</td>
+</tr>
+</table>
+
+<input type="submit" value="login">
+<input type="hidden" name="next" value="{% if next %}{{ next }}{% else %}{% url 'interface:interface-list' %}{% endif %}">
+</form>
+
+{# Assumes you setup the password_reset view in your URLconf #}
+<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
+
+{% endblock %}

+ 38 - 0
vycenter/accounts/templates/vauth/login.html

@@ -0,0 +1,38 @@
+{% extends "base.html" %}
+
+{% block content %}
+
+{% if form.errors %}
+<p>Your username and password didn't match. Please try again.</p>
+{% endif %}
+
+{% if next %}
+    {% if user.is_authenticated %}
+    <p>Your account doesn't have access to this page. To proceed,
+    please login with an account that has access.</p>
+    {% else %}
+    <p>Please login to see this page.</p>
+    {% endif %}
+{% endif %}
+
+<form method="post" action="{% url 'login' %}">
+{% csrf_token %}
+<table>
+<tr>
+    <td>{{ form.username.label_tag }}</td>
+    <td>{{ form.username }}</td>
+</tr>
+<tr>
+    <td>{{ form.password.label_tag }}</td>
+    <td>{{ form.password }}</td>
+</tr>
+</table>
+
+<input type="submit" value="login">
+<input type="hidden" name="next" value="{{ next }}">
+</form>
+
+{# Assumes you setup the password_reset view in your URLconf #}
+<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
+
+{% endblock %}

+ 35 - 0
vycenter/accounts/templates/vauth/oldlogin.html

@@ -0,0 +1,35 @@
+{% extends "base.html" %}
+
+{% block header_title %}VyControl Login{% endblock %}
+{% block section_title %}VyControl Login{% endblock %}
+
+{% block debug %}
+{% endblock %}
+
+{% block content %}
+
+
+{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
+
+<form action="{% url 'vauth:vauth-login' %}" method="post">
+{% csrf_token %}
+
+<p>
+<label for="username">Username</label><br>
+<input type="text" name="username" id="username" size="20">
+</p>
+
+<p>
+<label for="password">password</label><br>
+<input type="password" name="password" id="password"  size="32">
+</p>
+
+<input type="submit" value="Login">
+</form>
+
+
+
+
+
+{% endblock %}
+

+ 0 - 0
vycenter/auth/tests.py → vycenter/accounts/tests.py


+ 14 - 0
vycenter/accounts/urls.py

@@ -0,0 +1,14 @@
+from django.urls import path
+
+from . import views
+
+app_name = 'accounts'
+
+
+urlpatterns = [
+    #path('', views.index, name='vauth-login'),
+   #     path('', include('django.contrib.auth.urls', name='vauth-login')
+]
+
+
+

+ 32 - 0
vycenter/accounts/views.py

@@ -0,0 +1,32 @@
+from django.shortcuts import render
+from django.http import HttpResponse
+from django.template import loader
+from django.shortcuts import redirect
+from django.contrib.auth import authenticate
+
+
+import vyos
+
+
+
+def index(request):
+    all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    if 'username' in request.POST and 'password' in request.POST:
+        user = authenticate(username=request.POST['username'], password=request.POST['password'])
+        if user is not None:
+            # A backend authenticated the credentials
+            return redirect('firewall:firewall-list')
+        else:
+            pass
+
+
+
+    template = loader.get_template('vauth/login.html')
+    context = { 
+        'instances': all_instances,
+        'hostname_default': hostname_default,
+    }   
+    return HttpResponse(template.render(context, request))
+

+ 2 - 2
vycenter/arp/templates/arp/list.html

@@ -1,7 +1,7 @@
 {% extends "base.html" %}
 
-{% block header_title %}Option{% endblock %}
-{% block section_title %}Option{% endblock %}
+{% block header_title %}ARP List{% endblock %}
+{% block section_title %}ARP List{% endblock %}
 
 {% block debug %}
 {% endblock %}

+ 4 - 0
vycenter/arp/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 0 - 5
vycenter/auth/apps.py

@@ -1,5 +0,0 @@
-from django.apps import AppConfig
-
-
-class AuthConfig(AppConfig):
-    name = 'auth'

+ 0 - 3
vycenter/auth/views.py

@@ -1,3 +0,0 @@
-from django.shortcuts import render
-
-# Create your views here.

+ 4 - 0
vycenter/bgp/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 23 - 0
vycenter/config/views.py

@@ -2,6 +2,8 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
+
 
 import pprint
 import vyos
@@ -13,6 +15,9 @@ from django.contrib.auth.models import User
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
@@ -28,6 +33,9 @@ def index(request):
 
 
 def users_list(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
@@ -47,6 +55,9 @@ def users_list(request):
 
 
 def instances(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 
@@ -70,6 +81,9 @@ def instances(request):
     return HttpResponse(template.render(context, request))
 
 def instance_add(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
@@ -98,6 +112,9 @@ def instance_add(request):
     return HttpResponse(template.render(context, request))
 
 def instance_conntry(request, hostname):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 
@@ -119,6 +136,9 @@ def instance_conntry(request, hostname):
 
 
 def instance_default(request, hostname):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
 
     # permcheck
@@ -136,6 +156,9 @@ def instance_default(request, hostname):
 
 
 def instance_remove(request, hostname):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
 
     # permcheck

+ 4 - 1
vycenter/dhcp/views.py

@@ -2,13 +2,16 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
-
+from django.conf import settings
 
 import vyos
 
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 13 - 0
vycenter/firewall/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
@@ -33,6 +37,9 @@ def index(request):
 
 
 def create(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
@@ -64,6 +71,9 @@ def create(request):
 
 
 def show(request, firewall_name):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
@@ -84,6 +94,9 @@ def show(request, firewall_name):
 
 
 def addrule(request, firewall_name):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)

+ 12 - 0
vycenter/interface/views.py

@@ -1,6 +1,9 @@
 from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
+from django.conf import settings
+from django.shortcuts import redirect
+
 
 import vyos
 
@@ -9,6 +12,9 @@ from config.models import Instance
 import pprint
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     hostname_default = vyos.get_hostname_prefered(request)
     all_instances = vyos.instance_getall()
     firewall_all = vyos.get_firewall_all(hostname_default)
@@ -57,6 +63,9 @@ def index(request):
     return HttpResponse(template.render(context, request))
 
 def interfaceshow(request, interface_type, interface_name):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
 
     hostname_default = vyos.get_hostname_prefered(request)
@@ -75,6 +84,9 @@ def interfaceshow(request, interface_type, interface_name):
 
 
 def interfacefirewall(request, interface_type, interface_name):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
 
     hostname_default = vyos.get_hostname_prefered(request)

+ 4 - 0
vycenter/ipsec/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 4 - 0
vycenter/nat/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 4 - 0
vycenter/openvpn/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 4 - 0
vycenter/ospf/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 2 - 2
vycenter/qos/templates/qos/list.html

@@ -1,7 +1,7 @@
 {% extends "base.html" %}
 
-{% block header_title %}Option{% endblock %}
-{% block section_title %}Option{% endblock %}
+{% block header_title %}QoS List{% endblock %}
+{% block section_title %}QoS List{% endblock %}
 
 {% block debug %}
 {% endblock %}

+ 7 - 0
vycenter/qos/views.py

@@ -2,13 +2,20 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
 
 
 
+
+
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+
+    
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 4 - 0
vycenter/ssh/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 4 - 0
vycenter/static/views.py

@@ -2,6 +2,7 @@ from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
+from django.conf import settings
 
 
 import vyos
@@ -9,6 +10,9 @@ import vyos
 
 
 def index(request):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
+        
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
 

+ 14 - 0
vycenter/vauth/urls.py

@@ -0,0 +1,14 @@
+from django.urls import path
+
+from . import views
+
+app_name = 'vauth'
+
+
+urlpatterns = [
+    #path('', views.index, name='vauth-login'),
+   #     path('', include('django.contrib.auth.urls', name='vauth-login')
+]
+
+
+

+ 5 - 0
vycenter/vycenter/settings.py

@@ -38,6 +38,7 @@ INSTALLED_APPS = [
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'interface.apps.InterfaceConfig',
+    'accounts.apps.AccountsConfig',
     'config.apps.ConfigConfig',
     'firewall.apps.FirewallConfig',
     'static.apps.StaticConfig',
@@ -53,6 +54,10 @@ INSTALLED_APPS = [
     'wanlb.apps.WanlbConfig',
 ]
 
+AUTHENTICATION_BACKENDS = [
+    'django.contrib.auth.backends.ModelBackend'
+]
+
 MIDDLEWARE = [
     'django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',

+ 11 - 9
vycenter/vycenter/templates/base.html

@@ -12,7 +12,7 @@
 
     <style>
     body {
-        background-color: black;
+        background-color: #333;
         color: white;
         
     }
@@ -20,11 +20,11 @@
         color: black;
     }
     .menu { 
-        background-color: #000;
+        background-color: #ccc;
         padding: 5px;
     }
     .menu a, .menu h2 { 
-      color: #fff;
+      color: #000;
     }    
     .content { 
         background-color: rgb(120, 120, 120);
@@ -37,12 +37,13 @@
       font-size: 10px;      
     }
     #menu-logotop {
-      background-color: #fff;
+      background-color: #333;
       padding: 0 0px 4px 0px;
     }
     #menu-topline {
       padding-top: 3px;
-      background-color: #3023AE;
+      background-color: #e14342;
+      margin-bottom: 2px;
     }
     #menu-logotop h2 {
       font-size: 12px;
@@ -123,9 +124,10 @@
               </select>
             
 
-            
-              <span id="vycenter-config-menu">Config</span>
-              <span id="vycenter-config-user">User</span>
+              <span id="vycenter-config-menu"><a href="/accounts/login/">Login</a></span>
+              <span id="vycenter-config-menu"><a href="/accounts/logout/">Logout</a></span>
+
+              <span id="vycenter-config-menu"><a href="/admin">Admin</a></span>
 
           </p>
         </form>
@@ -138,7 +140,7 @@
     <div class="col-3 menu">
 
       <p class="text-center">
-        <h1 align="center"><a href="/"><img src="https://storage.googleapis.com/imgvycontrol/logos/logo_transparent.png" height="160" alt="vycontrol"></a></h1>
+        <h1 align="center"><a href="/"><img src="https://storage.googleapis.com/vycontrol/logos/logo_minimal.png" width="100"  height="100" alt="vycontrol"></a></h1>
       </p>
 
 

+ 3 - 0
vycenter/vycenter/urls.py

@@ -25,6 +25,9 @@ app_name = 'vycenter'
 urlpatterns = [
     path('interface/', include('interface.urls')),
     path('config/', include('config.urls')),
+    #path('vauth/', include('vauth.urls')),
+    path('accounts/', include('django.contrib.auth.urls')),
+
     path('dashboard/', include('dashboard.urls')),
     path('firewall/', include('firewall.urls')),
     path('static/', include('static.urls')),