jeudi 11 juillet 2019

golang simple web authentication

I want to make golang simple web authentication. I found simple example with cookies, but it doesn't work. If I fill text fields and push the button there is no reaction. I f I print r.Header["Cookie"][0] it contains md5 and it constant even if i write something in text fields

go:

package main

import (
    "net/http"
    "html/template"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        url := "path/to/login.html"  //login page
        if len(r.Header["Cookie"]) != 0 && r.Header["Cookie"][0] == "auth=your_MD5_cookies" {
            url = "path/to/index.html" //page after login
        }
        t, _ := template.ParseFiles(url); t.Execute(w, "")
    })
    http.Handle("/js/", http.FileServer(http.Dir("path/to")))
    http.ListenAndServe(":8000", nil)

login.html

<html>
<head>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/js.cookie.min.js"></script>
    <script src="js/jquery.md5.min.js"></script>
</head>
<body>
    <p>Логин: <input id="login" type="text" /></p>
    <p>Пароль: <input id="pwd" type="password" /></p>
    <button id="button">Войти</button>
    <script>
        $('#button').on('click', function () {
            var auth = $.MD5($("#login").val()+$("#pwd").val());
            Cookies.set("auth", auth);
            location.reload();
        });
     </script>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire