Answer the question
In order to leave comments, you need to log in
How to securely authorize?
There is a home server hosted by CloudFlare that I and a couple of my friends need to access occasionally. You need to put a password on it, because the information is personal there.
I did it like this:
1. The user enters the site, he checks the auth cookie in his cookies, if not, he throws it on the /login page.
The cookie is checked on all possible pages of the site, except for /login .
All handlers (again, except /login) are wrapped in an Auth function like this:
mux := http.NewServeMux()
mux.HandleFunc("/", handlers.Index)
mux.HandleFunc("/settings", handlers.Settings)
mux.HandleFunc("/ajax/fetchDataReset", ajax.FetchDataReset)
mux.HandleFunc("/ajax/fetchDataUpdate", ajax.FetchDataUpdate)
mux.HandleFunc("/ajax/fetchDataUpdateAll", ajax.FetchDataUpdateAll)
private := handlers.Auth(mux)
...
log.Fatalln(http.ListenAndServe(":8080", private))
func Auth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
*проверка куки, если нет/неверная - редирект на /login*
next.ServeHTTP(w, r)
}
}
randomBytes := make([]byte, 64)
_, err := rand.Read(randomBytes) // из пакета crypt/rand
if err != nil {
panic(err)
}
randStr := base64.URLEncoding.EncodeToString(randomBytes)[:64] // строка, возвращаемая сервером
var pass = prompt("Введите пароль для доступа к сайту");
const msgUint8 = new TextEncoder().encode(randStr+pass);
const hashBuffer = await crypto.subtle.digest('SHA-512', msgUint8);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex; // Далее отправляется POSTом серверу
http.Cookie{
Name: "__Host-auth",
Value: hash, // хэш от randStr+pass
Path: "/",
Secure: true,
SameSite: http.SameSiteStrictMode,
HttpOnly: true,
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question