Browse Source

improved authentication using our own templates

Roberto Berto 5 years ago
parent
commit
eb5dda50a8

+ 23 - 0
vycenter/accounts/templates/registration/logged_out.html

@@ -0,0 +1,23 @@
+{% extends "base.html" %}
+
+{% block header_title %}Logged out{% endblock %}
+{% block section_title %}Logged out{% endblock %}
+
+{% block debug %}
+{% endblock %}
+
+{% block content %}
+
+<p>
+You're logged out. 
+<p><a href="{% url 'registration-login' %}">Log-in again</a>.
+</p>
+
+{% endblock %}
+
+  
+
+
+
+
+

+ 10 - 2
vycenter/accounts/templates/registration/login.html

@@ -1,7 +1,15 @@
 {% extends "base.html" %}
 
+{% block header_title %}Login{% endblock %}
+{% block section_title %}Login{% endblock %}
+
+{% block debug %}
+{% endblock %}
+
+
 {% block content %}
 
+
 {% if form.errors %}
 <p>Your username and password didn't match. Please try again.</p>
 {% endif %}
@@ -15,7 +23,7 @@
     {% endif %}
 {% endif %}
 
-<form method="post" action="{% url 'login' %}">
+<form method="post" action="{% url 'registration-login' %}">
 {% csrf_token %}
 <table>
 <tr>
@@ -33,6 +41,6 @@
 </form>
 
 {# Assumes you setup the password_reset view in your URLconf #}
-<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
+<p><a href=" url 'vauth-reset' ">Lost password?</a></p>
 
 {% endblock %}

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

@@ -1,38 +0,0 @@
-{% 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 %}

+ 12 - 2
vycenter/accounts/urls.py

@@ -1,4 +1,14 @@
 from django.urls import path
+from django.urls import include
+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
+from django.contrib.auth import views as auth_views
+
+
+import vyos
 
 from . import views
 
@@ -6,8 +16,8 @@ app_name = 'accounts'
 
 
 urlpatterns = [
-    #path('', views.index, name='vauth-login'),
-   #     path('', include('django.contrib.auth.urls', name='vauth-login')
+   path('logout', views.logout, name='accounts-logout'),
+   # path('', include('django.contrib.auth.urls', name='vauth-login')
 ]
 
 

+ 1 - 23
vycenter/accounts/views.py

@@ -3,30 +3,8 @@ from django.http import HttpResponse
 from django.template import loader
 from django.shortcuts import redirect
 from django.contrib.auth import authenticate
+from django.views.generic.base import TemplateView
 
 
 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))
-

+ 26 - 25
vycenter/firewall/templates/firewall/list.html

@@ -34,33 +34,34 @@
 {% endif %}
 
 
-<h2>Create new rule</h2>
+<div class="separe-form">
+    <h2>Create new firewall</h2>
 
-<form action="{% url 'firewall:firewall-create' %}" method="post">
-    {% csrf_token %}
-    
-    <p>
-        <label for="alias">name</label><br>
-        <input type="text" name="name" id="name" value="{{ name }}" size="30">
-    </p>
-
-    <p>
-        <label for="alias">description</label><br>
-        <input type="text" name="description" id="description" value="{{ description }}" size="60">
-    </p>    
-    
-    <p>
-        <label for="hostname">default action</label><br>
-        <input type="radio" name="action" id="action" value="accept"> accept
-        <input type="radio" name="action" id="action" value="drop"> drop
-        <input type="radio" name="action" id="action" value="reject"> reject        
-    </p>
-
-    
-    
-    <input type="submit" value="Add Firewall">
-    </form>
+    <form action="{% url 'firewall:firewall-create' %}" method="post">
+        {% csrf_token %}
+        
+        <p>
+            <label for="alias">name</label><br>
+            <input type="text" name="name" id="name" value="{{ name }}" size="30">
+        </p>
+
+        <p>
+            <label for="alias">description</label><br>
+            <input type="text" name="description" id="description" value="{{ description }}" size="60">
+        </p>    
+        
+        <p>
+            <label for="hostname">default action</label><br>
+            <input type="radio" name="action" id="action" value="accept"> accept
+            <input type="radio" name="action" id="action" value="drop"> drop
+            <input type="radio" name="action" id="action" value="reject"> reject        
+        </p>
 
+        
+        
+        <input type="submit" value="Add Firewall">
+        </form>
+</div>
     
 
 

+ 1 - 1
vycenter/interface/templates/interface/index.html

@@ -50,7 +50,7 @@
         </table>
 
         <div id="submit-firewall" class="text-right">
-            <input type="submit" value="Set interfaces firewall">
+            <input type="submit" value="Set Firewall">
         </div>
     </form>
 

+ 28 - 7
vycenter/interface/templates/interface/show.html

@@ -5,10 +5,14 @@
 
 {% block debug %}
 {{ interface }}
+{{ interface_type }}
+{{ interface_name }}
 {% endblock %}
 
 
 {% block content %}
+
+
 {% if interface %}
 
     <table border="1" width="100%">
@@ -26,23 +30,40 @@
 {% endif %}
 
 
-
-<form action="{% url 'interface:interface-firewall interface_type interface_name' %}" method="post">
+<div class="separe-form">
+<h2>Set Interface Firewall</h2>
+<form action="{% url 'interface:interface-show' interface_type interface_name %}" method="post">
     {% csrf_token %}
     
     <p>
-        <label for="alias">inbound</label><br>
-        
+        <label for="alias">IPv4 Inbound</label><br>
+        <select id="firewall-ipv4-in" name="firewall-ipv4-in"><option value="">None</option>
+            {% if firewall_all != False %}
+                {% for fkey, fvalue in firewall_all.items %}    
+                    {% for fitemkey, fitemvalue in fvalue.items %}
+                    <option value="{{ fitemkey }}">{{ fitemkey }}</option>
+                    {% endfor %}
+                {% endfor %}
+            {% endif %}
+        </select>
     </p>
 
     <p>
-        <label for="alias">outbound/label><br>
-        
+        <label for="alias">IPv4 Outbound</label><br>
+        <select id="firewall-ipv4-out" name="firewall-ipv4-out"><option value="">None</option>
+        {% if firewall_all != False %}
+            {% for fkey, fvalue in firewall_all.items %}    
+                {% for fitemkey, fitemvalue in fvalue.items %}
+                <option value="{{ fitemkey }}">{{ fitemkey }}</option>
+                {% endfor %}
+            {% endfor %}
+        {% endif %}
+    </select>
     </p>    
     
     <input type="submit" value="Set Firewall">
     </form>
-
+</div>
 
 
 {% endblock %}

+ 2 - 2
vycenter/interface/views.py

@@ -67,9 +67,8 @@ def interfaceshow(request, interface_type, interface_name):
         return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
         
     all_instances = vyos.instance_getall()
-
     hostname_default = vyos.get_hostname_prefered(request)
-    
+    firewall_all = vyos.get_firewall_all(hostname_default)   
     interface = vyos.get_interface(interface_type, interface_name, hostname=hostname_default)
     
     template = loader.get_template('interface/show.html')
@@ -79,6 +78,7 @@ def interfaceshow(request, interface_type, interface_name):
         'interface_type' : interface_type,
         'interface_name' : interface_name,
         'hostname_default': hostname_default,
+        'firewall_all' : firewall_all,
     }   
     return HttpResponse(template.render(context, request))
 

+ 3 - 2
vycenter/static/templates/static/list.html

@@ -1,9 +1,10 @@
 {% extends "base.html" %}
 
-{% block header_title %}Option{% endblock %}
-{% block section_title %}Option{% endblock %}
+{% block header_title %}Static Routes{% endblock %}
+{% block section_title %}Static Routes{% endblock %}
 
 {% block debug %}
+{{ static_list }}
 {% endblock %}
 
 {% block content %}

+ 2 - 0
vycenter/static/views.py

@@ -15,11 +15,13 @@ def index(request):
         
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
+    static_list = vyos.get_static(hostname_default)
 
     template = loader.get_template('static/list.html')
     context = { 
         'instances': all_instances,
         'hostname_default': hostname_default,
+        'static_list' : static_list
     }   
     return HttpResponse(template.render(context, request))
 

+ 0 - 14
vycenter/vauth/urls.py

@@ -1,14 +0,0 @@
-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')
-]
-
-
-

+ 9 - 3
vycenter/vycenter/settings.py

@@ -31,15 +31,18 @@ ALLOWED_HOSTS = []
 # Application definition
 
 INSTALLED_APPS = [
-    'django.contrib.admin',
-    'django.contrib.auth',
+ 
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
-    'interface.apps.InterfaceConfig',
+
+    #'vauth.apps.VauthConfig',
     'accounts.apps.AccountsConfig',
+
     'config.apps.ConfigConfig',
+
+    'interface.apps.InterfaceConfig',
     'firewall.apps.FirewallConfig',
     'static.apps.StaticConfig',
     'arp.apps.ArpConfig',
@@ -52,6 +55,9 @@ INSTALLED_APPS = [
     'ospf.apps.OspfConfig',
     'ssh.apps.SshConfig',
     'wanlb.apps.WanlbConfig',
+
+    'django.contrib.auth',
+    'django.contrib.admin',
 ]
 
 AUTHENTICATION_BACKENDS = [

+ 23 - 5
vycenter/vycenter/templates/base.html

@@ -15,7 +15,9 @@
         background-color: #333;
         color: white;
         
+        
     }
+
     .container {
         color: black;
     }
@@ -23,15 +25,16 @@
         background-color: #ccc;
         padding: 5px;
     }
+   
     .menu a, .menu h2 { 
       color: #000;
     }    
     .content { 
         background-color: rgb(120, 120, 120);
-        padding: 5px;
+        padding: 0 10px 0 10px;
     }
     .content a {
-      color: #3023AE
+      color: #640000;
     }
     #menu-logotop h1 {
       font-size: 10px;      
@@ -40,6 +43,11 @@
       background-color: #333;
       padding: 0 0px 4px 0px;
     }
+
+    #menu-logotop a {
+      color: #af1d1d;
+    }
+
     #menu-topline {
       padding-top: 3px;
       background-color: #e14342;
@@ -64,8 +72,9 @@
     }
     
     #debug {
-        background-color: #B161D0;
+        background-color: #AB425E;
         margin-top: 20px;
+        margin-bottom: 20px;
     }
     #debug h3 {
         font-size: 14px;
@@ -99,6 +108,15 @@
     #submit-firewall {
       margin-top: 20px;
     }
+
+    input[type=submit] {
+      background-color: #EACD65;
+      border: 0;
+    }
+
+    .separe-form {
+      margin-top: 30px;      
+    }
     </style>
   </head>
   <body >
@@ -124,8 +142,8 @@
               </select>
             
 
-              <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="{% url 'registration-login' %}">Login</a></span>
+              <span id="vycenter-config-menu"><a href="{% url 'registration-logout' %}">Logout</a></span>
 
               <span id="vycenter-config-menu"><a href="/admin">Admin</a></span>
 

+ 16 - 7
vycenter/vycenter/urls.py

@@ -15,20 +15,31 @@ Including another URLconf
 """
 from django.contrib import admin
 from django.urls import include, path
+from django.views.generic import RedirectView
 
 from . import views
 
+from django.contrib.auth import views as auth_views
+
 
-app_name = 'vycenter'
 
+app_name = 'vycenter'
 
 urlpatterns = [
-    path('interface/', include('interface.urls')),
-    path('config/', include('config.urls')),
+    path('', RedirectView.as_view(url='/login/')),
     #path('vauth/', include('vauth.urls')),
-    path('accounts/', include('django.contrib.auth.urls')),
-
+    path('config/', include('config.urls')),
     path('dashboard/', include('dashboard.urls')),
+    #path('', views.vycenter_login, name='vycenter-login'),
+    path('admin/', admin.site.urls, name="django-admin"),
+    #path('change-password/', auth_views.PasswordChangeView.as_view()),
+    path('login/', auth_views.LoginView.as_view(), name="registration-login"),
+    path('logout/', auth_views.LogoutView.as_view(), name="registration-logout"),
+
+
+
+    path('interface/', include('interface.urls')),
+
     path('firewall/', include('firewall.urls')),
     path('static/', include('static.urls')),
     path('arp/', include('arp.urls')),
@@ -41,6 +52,4 @@ urlpatterns = [
     path('qos/', include('qos.urls')),
     path('ssh/', include('ssh.urls')),
     path('wanlb/', include('wanlb.urls')),
-    path('', views.vycenter_login, name='vycenter-login'),
-    path('admin/', admin.site.urls, name="django-admin"),
 ]

+ 8 - 0
vycenter/vyos.py

@@ -171,4 +171,12 @@ def set_config(hostname, cmd):
 def insert_firewall_rules(hostname, cmd):
     pprint.pprint(cmd)
     result1 = api_set(hostname, cmd)
+    return result1
+
+
+
+def get_static(hostname):
+    cmd = {"op": "showConfig", "path": ["show","ip","route","static"]}
+
+    result1 = api_get(hostname, cmd)
     return result1