mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-25 13:39:22 +00:00
auth package
This commit is contained in:
parent
1a490a8e7a
commit
0d49377879
@ -1,4 +1,4 @@
|
|||||||
package server
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
@ -10,23 +10,24 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func userIsAuthenticated(req *http.Request, username, password string) bool {
|
func IsAuthenticated(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
|
||||||
}
|
}
|
||||||
parts := strings.Split(cookie.Value, ":")
|
parts := strings.Split(cookie.Value, ":")
|
||||||
if len(parts) != 2 || !stringsEqual(parts[0], username) {
|
if len(parts) != 2 || !StringsEqual(parts[0], username) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return stringsEqual(parts[1], secret(username, password))
|
return StringsEqual(parts[1], secret(username, password))
|
||||||
}
|
}
|
||||||
|
|
||||||
func userAuthenticate(rw http.ResponseWriter, username, password string) {
|
func Authenticate(rw http.ResponseWriter, username, password, basepath string) {
|
||||||
expires := time.Now().Add(time.Hour * 24 * 7) // 1 week
|
expires := time.Now().Add(time.Hour * 24 * 7) // 1 week
|
||||||
|
|
||||||
var cookiePath string
|
var cookiePath string
|
||||||
if BasePath != "" {
|
if basepath != "" {
|
||||||
cookiePath = BasePath
|
cookiePath = basepath
|
||||||
} else {
|
} else {
|
||||||
cookiePath = "/"
|
cookiePath = "/"
|
||||||
}
|
}
|
||||||
@ -39,7 +40,7 @@ func userAuthenticate(rw http.ResponseWriter, username, password string) {
|
|||||||
http.SetCookie(rw, &cookie)
|
http.SetCookie(rw, &cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
func userLogout(rw http.ResponseWriter) {
|
func Logout(rw http.ResponseWriter) {
|
||||||
cookie := http.Cookie{
|
cookie := http.Cookie{
|
||||||
Name: "auth",
|
Name: "auth",
|
||||||
Value: "",
|
Value: "",
|
||||||
@ -48,7 +49,7 @@ func userLogout(rw http.ResponseWriter) {
|
|||||||
http.SetCookie(rw, &cookie)
|
http.SetCookie(rw, &cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringsEqual(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
|
||||||
}
|
}
|
||||||
|
|
@ -3,9 +3,10 @@ package server
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/nkanaev/yarr/src/storage"
|
|
||||||
"github.com/nkanaev/yarr/src/assets"
|
"github.com/nkanaev/yarr/src/assets"
|
||||||
|
"github.com/nkanaev/yarr/src/auth"
|
||||||
"github.com/nkanaev/yarr/src/router"
|
"github.com/nkanaev/yarr/src/router"
|
||||||
|
"github.com/nkanaev/yarr/src/storage"
|
||||||
"html"
|
"html"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@ -43,12 +44,12 @@ func (s *Server) handler() http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleIndex(c *router.Context) {
|
func (s *Server) handleIndex(c *router.Context) {
|
||||||
if s.requiresAuth() && !userIsAuthenticated(c.Req, s.Username, s.Password) {
|
if s.requiresAuth() && !auth.IsAuthenticated(c.Req, s.Username, s.Password) {
|
||||||
if c.Req.Method == "POST" {
|
if c.Req.Method == "POST" {
|
||||||
username := c.Req.FormValue("username")
|
username := c.Req.FormValue("username")
|
||||||
password := c.Req.FormValue("password")
|
password := c.Req.FormValue("password")
|
||||||
if stringsEqual(username, s.Username) && stringsEqual(password, s.Password) {
|
if auth.StringsEqual(username, s.Username) && auth.StringsEqual(password, s.Password) {
|
||||||
userAuthenticate(c.Out, username, password)
|
auth.Authenticate(c.Out, username, password, BasePath)
|
||||||
http.Redirect(c.Out, c.Req, c.Req.URL.Path, http.StatusFound)
|
http.Redirect(c.Out, c.Req, c.Req.URL.Path, http.StatusFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -433,6 +434,6 @@ func (s *Server) handlePageCrawl(c *router.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleLogout(c *router.Context) {
|
func (s *Server) handleLogout(c *router.Context) {
|
||||||
userLogout(c.Out)
|
auth.Logout(c.Out)
|
||||||
c.Out.WriteHeader(http.StatusNoContent)
|
c.Out.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user