mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-24 21:19:19 +00:00
hmac-based auth
This commit is contained in:
parent
edc7d56219
commit
9fcaad6b2f
@ -1,30 +1,44 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
|
"encoding/hex"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func userIsAuthenticated(req *http.Request, username, password string) bool {
|
func userIsAuthenticated(req *http.Request, username, password string) bool {
|
||||||
cookie, _ := req.Cookie("auth")
|
cookie, _ := req.Cookie("auth")
|
||||||
if cookie == nil {
|
if cookie == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// TODO: change to something sane
|
parts := strings.Split(cookie.Value, ":")
|
||||||
if cookie.Value != username {
|
if len(parts) != 2 || !stringsEqual(parts[0], username) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return stringsEqual(parts[1], secret(username, password))
|
||||||
}
|
}
|
||||||
|
|
||||||
func userAuthenticate(rw http.ResponseWriter, username, password string) {
|
func userAuthenticate(rw http.ResponseWriter, username, password string) {
|
||||||
expires := time.Now().Add(time.Hour * 24 * 7) // 1 week
|
expires := time.Now().Add(time.Hour * 24 * 7) // 1 week
|
||||||
cookie := http.Cookie{Name: "auth", Value: username, Expires: expires}
|
cookie := http.Cookie{
|
||||||
|
Name: "auth",
|
||||||
|
Value: username + ":" + secret(username, password),
|
||||||
|
Expires: expires,
|
||||||
|
}
|
||||||
http.SetCookie(rw, &cookie)
|
http.SetCookie(rw, &cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
func safeCompare(p1, p2 string) bool {
|
func stringsEqual(p1, p2 string) bool {
|
||||||
return subtle.ConstantTimeCompare([]byte(p1), []byte(p2)) == 1
|
return subtle.ConstantTimeCompare([]byte(p1), []byte(p2)) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func secret(msg, key string) string {
|
||||||
|
mac := hmac.New(sha256.New, []byte(key))
|
||||||
|
mac.Write([]byte(msg))
|
||||||
|
src := mac.Sum(nil)
|
||||||
|
return hex.EncodeToString(src)
|
||||||
|
}
|
||||||
|
@ -95,7 +95,7 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
|
|||||||
if req.Method == "POST" {
|
if req.Method == "POST" {
|
||||||
username := req.FormValue("username")
|
username := req.FormValue("username")
|
||||||
password := req.FormValue("password")
|
password := req.FormValue("password")
|
||||||
if safeCompare(username, h.Username) && safeCompare(password, h.Password) {
|
if stringsEqual(username, h.Username) && stringsEqual(password, h.Password) {
|
||||||
userAuthenticate(rw, username, password)
|
userAuthenticate(rw, username, password)
|
||||||
http.Redirect(rw, req, req.URL.Path, http.StatusFound)
|
http.Redirect(rw, req, req.URL.Path, http.StatusFound)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user